relatorio
changeset 163:fa0cc3bd9a83
Update manifest.xml with added files in opendocument
| author | Cédric Krier <ced@b2ck.com> |
|---|---|
| date | Fri Mar 26 19:05:27 2010 +0100 (2 years ago) |
| parents | 770c1db086b0 |
| children | 8960712aea29 |
| files | CHANGES relatorio/templates/opendocument.py |
line diff
1.1 --- a/CHANGES Fri Mar 26 19:05:03 2010 +0100 1.2 +++ b/CHANGES Fri Mar 26 19:05:27 2010 +0100 1.3 @@ -1,3 +1,4 @@ 1.4 + * Update manifest.xml with added files in opendocument 1.5 * Do not write empty width or height attributes in opendocument 1.6 * Update py:attrs attributes in opendocument 1.7
2.1 --- a/relatorio/templates/opendocument.py Fri Mar 26 19:05:03 2010 +0100 2.2 +++ b/relatorio/templates/opendocument.py Fri Mar 26 19:05:27 2010 +0100 2.3 @@ -74,6 +74,7 @@ 2.4 2.5 RELATORIO_URI = 'http://relatorio.openhex.org/' 2.6 GENSHI_URI = 'http://genshi.edgewall.org/' 2.7 +MANIFEST = 'META-INF/manifest.xml' 2.8 output_encode = genshi.output.encode 2.9 EtreeElement = lxml.etree.Element 2.10 2.11 @@ -109,8 +110,9 @@ 2.12 class ImageHref: 2.13 "A class used to add images in the odf zipfile" 2.14 2.15 - def __init__(self, zfile, context): 2.16 + def __init__(self, zfile, manifest, context): 2.17 self.zip = zfile 2.18 + self.manifest = manifest 2.19 self.context = context.copy() 2.20 2.21 def __call__(self, expr): 2.22 @@ -125,6 +127,7 @@ 2.23 path = 'Pictures/%s.%s' % (name, EXTENSIONS[mimetype]) 2.24 if path not in self.zip.namelist(): 2.25 self.zip.writestr(path, file_content) 2.26 + self.manifest.add_file_entry(path, mimetype) 2.27 return {'{http://www.w3.org/1999/xlink}href': path} 2.28 2.29 2.30 @@ -662,7 +665,9 @@ 2.31 def generate(self, *args, **kwargs): 2.32 "creates the RelatorioStream." 2.33 serializer = OOSerializer(self.filepath) 2.34 - kwargs['__relatorio_make_href'] = ImageHref(serializer.outzip, kwargs) 2.35 + kwargs['__relatorio_make_href'] = ImageHref(serializer.outzip, 2.36 + serializer.manifest, 2.37 + kwargs) 2.38 kwargs['__relatorio_make_dimension'] = ImageDimension(self.namespaces) 2.39 kwargs['__relatorio_guess_type'] = guess_type 2.40 2.41 @@ -712,10 +717,33 @@ 2.42 yield mark, (kind, data, pos) 2.43 2.44 2.45 +class Manifest(object): 2.46 + 2.47 + def __init__(self, content): 2.48 + self.tree = lxml.etree.parse(StringIO(content)) 2.49 + self.root = self.tree.getroot() 2.50 + self.namespaces = self.root.nsmap 2.51 + 2.52 + def __str__(self): 2.53 + return lxml.etree.tostring(self.tree, encoding='UTF-8', 2.54 + xml_declaration=True) 2.55 + 2.56 + def add_file_entry(self, path, mimetype=None): 2.57 + manifest_namespace = self.namespaces['manifest'] 2.58 + attribs = {'media-type': mimetype or '', 2.59 + 'full-path': path} 2.60 + entry_node = EtreeElement('{%s}%s' % (manifest_namespace, 2.61 + 'file-entry'), 2.62 + attrib=attribs, 2.63 + nsmap={'manifest': manifest_namespace}) 2.64 + self.root.append(entry_node) 2.65 + 2.66 + 2.67 class OOSerializer: 2.68 2.69 def __init__(self, oo_path): 2.70 self.inzip = zipfile.ZipFile(oo_path) 2.71 + self.manifest = Manifest(self.inzip.read(MANIFEST)) 2.72 self.new_oo = StringIO() 2.73 self.outzip = zipfile.ZipFile(self.new_oo, 'w') 2.74 self.xml_serializer = genshi.output.XMLSerializer() 2.75 @@ -741,6 +769,8 @@ 2.76 setattr(new_info, attr, getattr(f_info, attr)) 2.77 serialized_stream = output_encode(self.xml_serializer(stream)) 2.78 self.outzip.writestr(new_info, serialized_stream) 2.79 + elif f_info.filename == MANIFEST: 2.80 + self.outzip.writestr(f_info, str(self.manifest)) 2.81 else: 2.82 self.outzip.writestr(f_info, self.inzip.read(f_info.filename)) 2.83 self.inzip.close()
