Changeset 24:a4d4dbf7c524

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

- Better namespace support

  • Fix width/height problem image in for loop
  • Added tests for draw:image stuffs
Location:
relatorio
Files:
2 added
3 modified

Legend:

Unmodified
Added
Removed
  • relatorio/templates/odt.py

    r21 r24  
    1919############################################################################### 
    2020 
    21 __revision__ = "$Id: odt.py 22 2008-07-17 18:21:49Z nicoe $" 
     21__revision__ = "$Id: odt.py 25 2008-07-23 10:38:25Z nicoe $" 
    2222__metaclass__ = type 
    2323 
     
    3333from genshi.template import MarkupTemplate 
    3434 
    35 NS = {'text': 'urn:oasis:names:tc:opendocument:xmlns:text:1.0', 
    36       'table': 'urn:oasis:names:tc:opendocument:xmlns:table:1.0', 
    37       'draw': 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0', 
    38       'xlink': 'http://www.w3.org/1999/xlink', 
    39       'py': 'http://genshi.edgewall.org/', 
    40       } 
    4135GENSHI_TAGS = re.compile(r'''<(/)?(for|choose|otherwise|when|if|with)( (\w+)=["'](.*)["']|)>''') 
    4236EXTENSIONS = {'image/png': 'png', 
     
    6458        bitstream.seek(0) 
    6559        self.zip.writestr(path, bitstream.read()) 
    66         return {'{%s}href' % NS['xlink']: path} 
     60        return {'{http://www.w3.org/1999/xlink}href': path} 
    6761 
    6862 
    6963class Template(MarkupTemplate): 
     64 
     65    def __init__(self, source, filepath=None, filename=None, loader=None, 
     66                 encoding=None, lookup='strict', allow_exec=True): 
     67        self.namespaces = {} 
     68        super(Template, self).__init__(source, filepath, filename, loader, 
     69                                       encoding, lookup, allow_exec) 
    7070 
    7171    def _parse(self, source, encoding): 
     
    8787        tree = lxml.etree.parse(StringIO(content)) 
    8888        root = tree.getroot() 
     89        self.namespaces = root.nsmap.copy() 
     90        self.namespaces['py'] = 'http://genshi.edgewall.org/' 
    8991 
    9092        self._handle_placeholders(tree) 
     
    101103        # further processing. 
    102104        genshi_directives, placeholders = [], [] 
    103         for statement in tree.xpath('//text:placeholder', namespaces=NS): 
     105        for statement in tree.xpath('//text:placeholder', 
     106                                    namespaces=self.namespaces): 
    104107            match_obj = GENSHI_TAGS.match(statement.text) 
    105108            if match_obj is not None: 
     
    150153                        break 
    151154 
    152                 genshi_node = lxml.etree.Element('{%s}%s' % (NS['py'], 
     155                genshi_node = lxml.etree.Element('{%s}%s' % (self.namespaces['py'], 
    153156                                                             directive), 
    154157                                                 attrib={attr: a_val}, 
    155                                                  nsmap=NS) 
     158                                                 nsmap=self.namespaces) 
    156159                can_append = False 
    157160                for node in ancestor.iterchildren(): 
     
    168171                ancestor.remove(outermost_c_ancestor) 
    169172            else: 
    170                 p.attrib['{%s}replace' % NS['py']] = directive 
     173                p.attrib['{%s}replace' % self.namespaces['py']] = directive 
    171174 
    172175    def _handle_images(self, tree): 
    173         for draw in tree.xpath('//draw:frame', namespaces=NS): 
    174             d_name = draw.attrib['{%s}name' % NS['draw']] 
     176        for draw in tree.xpath('//draw:frame', namespaces=self.namespaces): 
     177            d_name = draw.attrib['{%s}name' % self.namespaces['draw']] 
    175178            if d_name.startswith('image: '): 
    176179                attr_expr = "make_href(%s, '%s')" % (d_name[7:], d_name[7:]) 
    177180                attributes = {} 
    178                 attributes['{%s}attrs' % NS['py']] = attr_expr 
    179                 image_node = lxml.etree.Element('{%s}image' % NS['draw'], 
     181                attributes['{%s}attrs' % self.namespaces['py']] = attr_expr 
     182                image_node = lxml.etree.Element('{%s}image' % self.namespaces['draw'], 
    180183                                                attrib=attributes, 
    181                                                 nsmap=NS) 
     184                                                nsmap=self.namespaces) 
    182185                draw.replace(draw[0], image_node) 
    183186 
  • relatorio/tests/test_odt.py

    r20 r24  
    2020############################################################################### 
    2121 
    22 __revision__ = "$Id: test_odt.py 21 2008-07-17 16:46:24Z nicoe $" 
     22__revision__ = "$Id: test_odt.py 25 2008-07-23 10:38:25Z nicoe $" 
    2323 
    2424import os 
     
    3232from genshi.template.eval import UndefinedError 
    3333 
    34 from templates.odt import Template, NS 
     34from templates.odt import Template 
    3535 
    3636def pseudo_gettext(string): 
     
    6161                     'hobbies': [u'Music', u'Dancing', u'DJing'], 
    6262                     'animals': [u'Felix da housecat', u'Dog eat Dog'], 
     63                     'images': [(file(os.path.join(thisdir, 'one.jpg')), 
     64                                 'image/jpeg'), 
     65                                (file(os.path.join(thisdir, 'two.png')), 
     66                                 'image/png')], 
    6367                     'footer': u'We sell stuffs'} 
    6468 
     
    7175    def test_directives(self): 
    7276        "Testing the directives interpolation" 
    73         xml = '''<a xmlns="urn:a" xmlns:text="%s"> 
     77        xml = '''<b:a xmlns:b="urn:b" xmlns:text="%s" xmlns:draw="urn:draw"> 
    7478        <text:placeholder>&lt;foo&gt;</text:placeholder> 
    75         </a>''' % NS['text'] 
     79        </b:a>''' % 'urn:text' 
    7680        parsed = self.oot.add_directives(xml)  
    7781        root = lxml.etree.parse(StringIO(xml)).getroot() 
     
    112116        ok_('We sell stuffs' not in translated_xml) 
    113117        ok_('On vend des brols' in translated_xml) 
     118 
     119    def test_images(self): 
     120        "Testing the image replacement directive" 
     121        stream = self.oot.generate(**self.data) 
     122        rendered = stream.events.render() 
     123        content_idx = rendered.find('<?relatorio content.xml?>') 
     124        tree = lxml.etree.parse(StringIO(rendered[content_idx + 25:])) 
     125        root = tree.getroot() 
     126        images = root.xpath('//draw:frame', namespaces=self.oot.namespaces) 
     127        eq_(len(images), 2) 
     128        eq_(images[0].attrib['{%s}name' % self.oot.namespaces['draw']], 
     129            'image: img') 
     130        eq_(images[0].attrib['{%s}width' % self.oot.namespaces['svg']], 
     131            '1.732cm') 
     132        eq_(images[0].attrib['{%s}height' % self.oot.namespaces['svg']], 
     133            '1.513cm') 
     134        eq_(images[1].attrib['{%s}width' % self.oot.namespaces['svg']], 
     135            '1.732cm') 
     136        eq_(images[1].attrib['{%s}height' % self.oot.namespaces['svg']], 
     137            '1.513cm')