Debian Codegraph @

src/odf/odfmanifest.py

raw
1 #!/usr/bin/env python
2 # Copyright (C) 2006-2007 Søren Roug, European Environment Agency
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 #
18 # Contributor(s):
19 #
20
21 # This script lists the content of the manifest.xml file
22
23 import io
24 import zipfile
25 from xml.sax import handler, make_parser
26 from xml.sax.xmlreader import InputSource
27
28 MANIFESTNS = 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0'
29
30 # -----------------------------------------------------------------------------
31 #
32 # ODFMANIFESTHANDLER
33 #
34 # -----------------------------------------------------------------------------
35
36
37 class ODFManifestHandler(handler.ContentHandler):
38 """The ODFManifestHandler parses a manifest file and produces a list of
39 content"""
40
41 def __init__(self):
42 self.manifest = {}
43
44 # Tags
45 # FIXME: Also handle encryption data
46 self.elements = {
47 (MANIFESTNS, 'file-entry'): (self.s_file_entry, self.donothing),
48 }
49
50 def handle_starttag(self, tag, method, attrs):
51 method(tag, attrs)
52
53 def handle_endtag(self, tag, method):
54 method(tag)
55
56 def startElementNS(self, name, qname, attrs):
57 method = self.elements.get(name, (None, None))[0]
58 if method:
59 self.handle_starttag(name, method, attrs)
60 else:
61 self.unknown_starttag(name, attrs)
62
63 def endElementNS(self, name, qname):
64 method = self.elements.get(name, (None, None))[1]
65 if method:
66 self.handle_endtag(name, method)
67 else:
68 self.unknown_endtag(name)
69
70 def unknown_starttag(self, tag, attrs):
71 pass
72
73 def unknown_endtag(self, tag):
74 pass
75
76 def donothing(self, tag, attrs=None):
77 pass
78
79 def s_file_entry(self, tag, attrs):
80 m = attrs.get((MANIFESTNS, 'media-type'), 'application/octet-stream')
81 p = attrs.get((MANIFESTNS, 'full-path'))
82 self.manifest[p] = {'media-type': m, 'full-path': p}
83
84
85 # -----------------------------------------------------------------------------
86 #
87 # Reading the file
88 #
89 # -----------------------------------------------------------------------------
90
91
92 def manifestlist(manifestxml):
93 odhandler = ODFManifestHandler()
94 parser = make_parser()
95 parser.setFeature(handler.feature_namespaces, 1)
96 parser.setContentHandler(odhandler)
97 parser.setErrorHandler(handler.ErrorHandler())
98
99 inpsrc = InputSource()
100 inpsrc.setByteStream(io.BytesIO(manifestxml))
101 parser.setFeature(handler.feature_external_ges, False) # Changed by Kovid to ignore external DTDs
102 parser.parse(inpsrc)
103
104 return odhandler.manifest
105
106
107 def odfmanifest(odtfile):
108 z = zipfile.ZipFile(odtfile)
109 manifest = z.read('META-INF/manifest.xml')
110 z.close()
111 return manifestlist(manifest)
112
113
114 if __name__ == '__main__':
115 import sys
116
117 result = odfmanifest(sys.argv[1])
118 for file in result.values():
119 print('{:<40} {:<40}'.format(file['media-type'], file['full-path']))