Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/odf/odfmanifest.py: 37%
61 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
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#
21from __future__ import print_function
22# This script lists the content of the manifest.xml file
23import zipfile
24from defusedxml.sax import make_parser
25from xml.sax import handler
26from xml.sax.xmlreader import InputSource
27import xml.sax.saxutils
28try:
29 from cStringIO import StringIO
30except ImportError:
31 from io import StringIO
33MANIFESTNS="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"
35#-----------------------------------------------------------------------------
36#
37# ODFMANIFESTHANDLER
38#
39#-----------------------------------------------------------------------------
41class ODFManifestHandler(handler.ContentHandler):
42 """ The ODFManifestHandler parses a manifest file and produces a list of
43 content """
45 def __init__(self):
46 self.manifest = {}
48 # Tags
49 # FIXME: Also handle encryption data
50 self.elements = {
51 (MANIFESTNS, 'file-entry'): (self.s_file_entry, self.donothing),
52 }
54 def handle_starttag(self, tag, method, attrs):
55 method(tag,attrs)
57 def handle_endtag(self, tag, method):
58 method(tag)
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)
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)
74 def unknown_starttag(self, tag, attrs):
75 pass
77 def unknown_endtag(self, tag):
78 pass
80 def donothing(self, tag, attrs=None):
81 pass
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 }
89#-----------------------------------------------------------------------------
90#
91# Reading the file
92#
93#-----------------------------------------------------------------------------
95def 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())
102 inpsrc = InputSource()
103 if not isinstance(manifestxml, str):
104 manifestxml=manifestxml.decode("utf-8")
105 inpsrc.setByteStream(StringIO(manifestxml))
106 parser.parse(inpsrc)
108 return odhandler.manifest
110def odfmanifest(odtfile):
111 z = zipfile.ZipFile(odtfile)
112 manifest = z.read('META-INF/manifest.xml')
113 z.close()
114 return manifestlist(manifest)
116if __name__ == "__main__": 116 ↛ 117line 116 didn't jump to line 117, because the condition on line 116 was never true
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']))