ODFPY 1.2.0 |

Loading...
Searching...
No Matches
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
33MANIFESTNS="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"
34
35#-----------------------------------------------------------------------------
36#
37# ODFMANIFESTHANDLER
38#
39#-----------------------------------------------------------------------------
40
41
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 s_file_entry, self.donothing 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
The ODFManifestHandler parses a manifest file and produces a list of content.
Definition odfmanifest.py:43
odf.odfmanifest.ODFManifestHandler.donothing
donothing(self, tag, attrs=None)
Definition odfmanifest.py:80
odf.odfmanifest.ODFManifestHandler.unknown_endtag
unknown_endtag(self, tag)
Definition odfmanifest.py:77
odf.odfmanifest.ODFManifestHandler.handle_endtag
handle_endtag(self, tag, method)
Definition odfmanifest.py:57
odf.odfmanifest.ODFManifestHandler.manifest
manifest
Definition odfmanifest.py:46
odf.odfmanifest.ODFManifestHandler.endElementNS
endElementNS(self, tag, qname)
Definition odfmanifest.py:67
odf.odfmanifest.ODFManifestHandler.s_file_entry
s_file_entry(self, tag, attrs)
Definition odfmanifest.py:83
odf.odfmanifest.ODFManifestHandler.s_file_entry
s_file_entry
Definition odfmanifest.py:51
odf.odfmanifest.ODFManifestHandler.elements
elements
Definition odfmanifest.py:50
odf.odfmanifest.ODFManifestHandler.unknown_starttag
unknown_starttag(self, tag, attrs)
Definition odfmanifest.py:74
odf.odfmanifest.ODFManifestHandler.startElementNS
startElementNS(self, tag, qname, attrs)
Definition odfmanifest.py:60
odf.odfmanifest.ODFManifestHandler.handle_starttag
handle_starttag(self, tag, method, attrs)
Definition odfmanifest.py:54
odf.odfmanifest.ODFManifestHandler.donothing
donothing
Definition odfmanifest.py:51
odf.odfmanifest.ODFManifestHandler.__init__
__init__(self)
Definition odfmanifest.py:45