Changeset 8:983c94386bdf
- Timestamp:
- 07/08/08 19:23:12 (5 years ago)
- Branch:
- default
- Files:
-
- 1 added
- 6 modified
-
examples/basic.odt (modified) (previous)
-
examples/bouteille.png (added)
-
examples/demo.py (modified) (2 diffs)
-
examples/invoice.odt (modified) (previous)
-
relatorio/templates/odt.py (modified) (4 diffs)
-
setup.cfg (modified) (1 diff)
-
setup.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
examples/demo.py
r5 r8 1 1 import operator 2 from cStringIO import StringIO 2 3 import relatorio 3 4 … … 47 48 ], 48 49 id='MZY-20080703', 49 status='late') 50 status='late', 51 trombine=(file('bouteille.png', 'r'), 'image/png')) 50 52 51 53 -
relatorio/templates/odt.py
r3 r8 19 19 ############################################################################### 20 20 21 __revision__ = "$Id: odt.py 4 2008-07-04 18:13:58Z nicoe $"21 __revision__ = "$Id: odt.py 9 2008-07-08 19:23:12Z nicoe $" 22 22 __metaclass__ = type 23 23 24 24 import os 25 25 import re 26 import md5 26 27 import zipfile 27 28 from cStringIO import StringIO … … 32 33 NS = {'text': 'urn:oasis:names:tc:opendocument:xmlns:text:1.0', 33 34 'table': 'urn:oasis:names:tc:opendocument:xmlns:table:1.0', 35 'draw': 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0', 36 'xlink': 'http://www.w3.org/1999/xlink', 34 37 'py': 'http://genshi.edgewall.org/', 35 38 } 36 39 GENSHI_TAGS = re.compile(r'<(/)?(for|choose|otherwise|when|if|with)( (\w+)="(.*)"|)>') 40 EXTENSIONS = {'image/png': 'png', 41 'image/jpeg': 'jpg', 42 'image/bmp': 'bmp', 43 'image/gif': 'gif', 44 'image/tiff': 'tif', 45 'image/xbm': 'xbm', 46 } 47 48 def _make_href(zip): 49 def attr_maker(expr, name): 50 bitstream, mimetype = expr 51 name = md5.new(name).hexdigest() 52 path = 'Pictures/%s.%s' % (name, EXTENSIONS[mimetype]) 53 bitstream.seek(0) 54 zip.writestr(path, bitstream.read()) 55 return {'{%s}href' % NS['xlink']: path} 56 return attr_maker 37 57 38 58 … … 58 78 root = tree.getroot() 59 79 80 self._handle_placeholders(tree) 81 self._handle_images(tree) 82 return lxml.etree.tostring(tree) 83 84 def _handle_placeholders(self, tree): 85 """ 86 Will treat all placeholders tag (py:if/for/choose/when/otherwise) 87 tags 88 """ 60 89 # First we create the list of all the placeholders nodes. 61 90 # If this is node matches a genshi directive it is put apart for … … 130 159 else: 131 160 p.attrib['{%s}replace' % NS['py']] = directive 132 return lxml.etree.tostring(tree) 161 162 def _handle_images(self, tree): 163 for draw in tree.xpath('//draw:frame', namespaces=NS): 164 d_name = draw.attrib['{%s}name' % NS['draw']] 165 if d_name.startswith('image: '): 166 attr_expr = "make_href(%s, '%s')" % (d_name[7:], d_name[7:]) 167 attributes = {} 168 attributes['{%s}attrs' % NS['py']] = attr_expr 169 image_node = lxml.etree.Element('{%s}image' % NS['draw'], 170 attrib=attributes, 171 nsmap=NS) 172 draw.replace(draw[0], image_node) 173 133 174 134 175 def generate(self, *args, **kwargs): 135 content = str(self.content_template.generate(*args, **kwargs))136 176 new_oo = StringIO() 137 177 inzip = zipfile.ZipFile(self.filepath) 138 178 outzip = zipfile.ZipFile(new_oo, 'w') 179 180 kwargs['make_href'] = _make_href(outzip) 181 content = str(self.content_template.generate(*args, **kwargs)) 139 182 140 183 for f in inzip.infolist(): -
setup.cfg
r6 r8 1 1 [egg_info] 2 tag_build = dev 3 tag_svn_revision = true 2 tag_svn_revision = false 4 3 -
setup.py
r6 r8 6 6 name="relatorio", 7 7 url="http://relatorio.openhex.org", 8 author="Nicolas Évrard",8 author="Nicolas Evrard", 9 9 author_email="nicoe@openhex.org", 10 10 description="A templating library able to output odt and pdf files", … … 21 21 version=relatorio.__version__, 22 22 packages=find_packages(exclude=['tests', 'examples']), 23 install_requires=[ 24 "Genshi >= 0.4" 25 ], 23 26 classifiers=[ 24 27 "Development Status :: 3 - Alpha", … … 27 30 "Operating System :: OS Independent", 28 31 "Programming Language :: Python", 29 "Topic :: Software Development :: Libraries :: Python Modules" 32 "Topic :: Software Development :: Libraries :: Python Modules", 30 33 "Topic :: Text Processing", 31 34 ],
