xref: /trunk/test/odf-validate/fixtures/doctypes/derive-chart.py (revision 0e16a31d78a652c56c5c0fc79c752f48b9048df9) !
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20# usage: derive-chart.py <package-with-chart.ods> <out.odc>
21# Not AOO output as a standalone file: repackages the embedded chart object of a saved package as a chart document.
22import sys, zipfile
23import xml.etree.ElementTree as ET
24
25src, out = sys.argv[1:3]
26MIME = "application/vnd.oasis.opendocument.chart"
27z = zipfile.ZipFile(src)
28M = "{urn:oasis:names:tc:opendocument:xmlns:manifest:1.0}"
29dirs = [e.get(M + "full-path") for e in ET.fromstring(z.read("META-INF/manifest.xml")) if e.get(M + "media-type") == MIME]
30dirs = [d for d in dirs if d.endswith("/") and d != "/"]
31assert len(dirs) == 1, dirs
32obj = dirs[0][:-1]
33mime = MIME
34entries = [n for n in z.namelist() if n.startswith(obj + "/") and not n.endswith("/")]
35rows = ['<manifest:file-entry manifest:media-type="%s" manifest:full-path="/"/>' % mime]
36rows += ['<manifest:file-entry manifest:media-type="text/xml" manifest:full-path="%s"/>' % n[len(obj) + 1:]
37         for n in entries if n[len(obj) + 1:] in ("content.xml", "styles.xml", "meta.xml", "settings.xml")]
38manifest = ('<?xml version="1.0" encoding="UTF-8"?>\n<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" '
39            'manifest:version="1.2">\n %s\n</manifest:manifest>\n' % "\n ".join(rows))
40with zipfile.ZipFile(out, "w") as o:
41    o.writestr("mimetype", mime, zipfile.ZIP_STORED)
42    for n in entries:
43        if n[len(obj) + 1:] in ("content.xml", "styles.xml", "meta.xml", "settings.xml"):
44            o.writestr(n[len(obj) + 1:], z.read(n), zipfile.ZIP_DEFLATED)
45    o.writestr("META-INF/manifest.xml", manifest, zipfile.ZIP_DEFLATED)
46