ODFPY  1.2.0
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 defusedxml.sax import make_parser
25 from xml.sax import handler
26 from xml.sax.xmlreader import InputSource
27 import xml.sax.saxutils
28 try:
29  from cStringIO import StringIO
30 except ImportError:
31  from io import StringIO
32 
33 MANIFESTNS="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"
34 
35 #-----------------------------------------------------------------------------
36 #
37 # ODFMANIFESTHANDLER
38 #
39 #-----------------------------------------------------------------------------
40 
41 
44 class ODFManifestHandler(handler.ContentHandler):
45 
46  def __init__(self):
47  self.manifest = {}
48 
49  # Tags
50  # FIXME: Also handle encryption data
51  self.elements = {
52  (MANIFESTNS, 'file-entry'): (self.s_file_entry, self.donothing),
53  }
54 
55  def handle_starttag(self, tag, method, attrs):
56  method(tag,attrs)
57 
58  def handle_endtag(self, tag, method):
59  method(tag)
60 
61  def startElementNS(self, tag, qname, attrs):
62  method = self.elements.get(tag, (None, None))[0]
63  if method:
64  self.handle_starttag(tag, method, attrs)
65  else:
66  self.unknown_starttag(tag,attrs)
67 
68  def endElementNS(self, tag, qname):
69  method = self.elements.get(tag, (None, None))[1]
70  if method:
71  self.handle_endtag(tag, method)
72  else:
73  self.unknown_endtag(tag)
74 
75  def unknown_starttag(self, tag, attrs):
76  pass
77 
78  def unknown_endtag(self, tag):
79  pass
80 
81  def donothing(self, tag, attrs=None):
82  pass
83 
84  def s_file_entry(self, tag, attrs):
85  m = attrs.get((MANIFESTNS, 'media-type'),"application/octet-stream")
86  p = attrs.get((MANIFESTNS, 'full-path'))
87  self.manifest[p] = { 'media-type':m, 'full-path':p }
88 
89 
90 #-----------------------------------------------------------------------------
91 #
92 # Reading the file
93 #
94 #-----------------------------------------------------------------------------
95 
96 def manifestlist(manifestxml):
97  odhandler = ODFManifestHandler()
98  parser = make_parser()
99  parser.setFeature(handler.feature_namespaces, 1)
100  parser.setContentHandler(odhandler)
101  parser.setErrorHandler(handler.ErrorHandler())
102 
103  inpsrc = InputSource()
104  if not isinstance(manifestxml, str):
105  manifestxml=manifestxml.decode("utf-8")
106  inpsrc.setByteStream(StringIO(manifestxml))
107  parser.parse(inpsrc)
108 
109  return odhandler.manifest
110 
111 def odfmanifest(odtfile):
112  z = zipfile.ZipFile(odtfile)
113  manifest = z.read('META-INF/manifest.xml')
114  z.close()
115  return manifestlist(manifest)
116 
117 if __name__ == "__main__":
118  import sys
119  result = odfmanifest(sys.argv[1])
120  for file in result.values():
121  print ("%-40s %-40s" % (file['media-type'], file['full-path']))
122 
def unknown_starttag(self, tag, attrs)
Definition: odfmanifest.py:75
def handle_starttag(self, tag, method, attrs)
Definition: odfmanifest.py:55
def odfmanifest(odtfile)
Definition: odfmanifest.py:111
def donothing(self, tag, attrs=None)
Definition: odfmanifest.py:81
def endElementNS(self, tag, qname)
Definition: odfmanifest.py:68
def startElementNS(self, tag, qname, attrs)
Definition: odfmanifest.py:61
def s_file_entry(self, tag, attrs)
Definition: odfmanifest.py:84
def handle_endtag(self, tag, method)
Definition: odfmanifest.py:58
def manifestlist(manifestxml)
Definition: odfmanifest.py:96
The ODFManifestHandler parses a manifest file and produces a list of content.
Definition: odfmanifest.py:44