ODFPY: odf/odfmanifest.py Source File

ODFPY 1.2.0 |

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