Changeset 8:983c94386bdf

Show
Ignore:
Timestamp:
07/08/08 19:23:12 (5 years ago)
Author:
Nicolas Evrard <nicoe@…>
Branch:
default
Message:

- Added the featrue to add images

  • Added a dependancy on Genshi
  • Added an image example file from wikipedia
Files:
1 added
6 modified

Legend:

Unmodified
Added
Removed
  • examples/demo.py

    r5 r8  
    11import operator 
     2from cStringIO import StringIO 
    23import relatorio 
    34 
     
    4748                    ], 
    4849              id='MZY-20080703', 
    49               status='late') 
     50              status='late', 
     51              trombine=(file('bouteille.png', 'r'), 'image/png')) 
    5052 
    5153 
  • relatorio/templates/odt.py

    r3 r8  
    1919############################################################################### 
    2020 
    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 $" 
    2222__metaclass__ = type 
    2323 
    2424import os 
    2525import re 
     26import md5 
    2627import zipfile 
    2728from cStringIO import StringIO 
     
    3233NS = {'text': 'urn:oasis:names:tc:opendocument:xmlns:text:1.0', 
    3334      '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', 
    3437      'py': 'http://genshi.edgewall.org/', 
    3538      } 
    3639GENSHI_TAGS = re.compile(r'<(/)?(for|choose|otherwise|when|if|with)( (\w+)="(.*)"|)>') 
     40EXTENSIONS = {'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 
     48def _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 
    3757 
    3858 
     
    5878        root = tree.getroot() 
    5979 
     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        """ 
    6089        # First we create the list of all the placeholders nodes. 
    6190        # If this is node matches a genshi directive it is put apart for 
     
    130159            else: 
    131160                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 
    133174 
    134175    def generate(self, *args, **kwargs): 
    135         content = str(self.content_template.generate(*args, **kwargs)) 
    136176        new_oo = StringIO() 
    137177        inzip = zipfile.ZipFile(self.filepath) 
    138178        outzip = zipfile.ZipFile(new_oo, 'w') 
     179 
     180        kwargs['make_href'] = _make_href(outzip) 
     181        content = str(self.content_template.generate(*args, **kwargs)) 
    139182 
    140183        for f in inzip.infolist(): 
  • setup.cfg

    r6 r8  
    11[egg_info] 
    2 tag_build = dev 
    3 tag_svn_revision = true 
     2tag_svn_revision = false 
    43 
  • setup.py

    r6 r8  
    66    name="relatorio", 
    77    url="http://relatorio.openhex.org", 
    8     author="Nicolas Évrard", 
     8    author="Nicolas Evrard", 
    99    author_email="nicoe@openhex.org", 
    1010    description="A templating library able to output odt and pdf files", 
     
    2121    version=relatorio.__version__, 
    2222    packages=find_packages(exclude=['tests', 'examples']), 
     23    install_requires=[ 
     24        "Genshi >= 0.4" 
     25    ], 
    2326    classifiers=[ 
    2427        "Development Status :: 3 - Alpha", 
     
    2730        "Operating System :: OS Independent", 
    2831        "Programming Language :: Python", 
    29         "Topic :: Software Development :: Libraries :: Python Modules" 
     32        "Topic :: Software Development :: Libraries :: Python Modules", 
    3033        "Topic :: Text Processing", 
    3134    ],