Skip to content

Commit

Permalink
* Implemented creative solution to reference local images.
Browse files Browse the repository at this point in the history
* Added API for PDF Template integration.
* Fixed some bugs that surfaced when checking the PDF template samples. I 
  should really move them over.
  • Loading branch information
strichter committed Mar 14, 2007
1 parent 11867df commit 3d19ab3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
31 changes: 26 additions & 5 deletions src/z3c/rml/attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""
__docformat__ = "reStructuredText"
import cStringIO
import os
import re
import reportlab
import reportlab.lib.colors
Expand Down Expand Up @@ -164,17 +165,35 @@ def convert(self, value, context=None):

class Image(Text):

open = urllib.urlopen
open = staticmethod(urllib.urlopen)
packageExtract = re.compile('^\[([0-9A-z_.]*)\]/(.*)$')

def __init__(self, name=None, default=DEFAULT, onlyOpen=False):
super(Image, self).__init__(name, default)
self.onlyOpen = onlyOpen

def convert(self, value, context=None):
# Check whether the value is of the form:
# [<module.path>]/rel/path/image.gif"
if value.startswith('['):
result = self.packageExtract.match(value)
if result is None:
raise ValueError(
'The package-path-pair you specified was incorrect')
modulepath, path = result.groups()
module = __import__(modulepath, {}, {}, (modulepath))
value = os.path.join(os.path.dirname(module.__file__), path)
# Open/Download the file
fileObj = self.open(value)
if self.onlyOpen:
return fileObj
return reportlab.lib.utils.ImageReader(fileObj)
# ImageReader wants to be able to seek, but URL info objects can only
# be read, so we make a string IO object out of it
sio = cStringIO.StringIO()
sio.write(fileObj.read())
fileObj.close()
sio.seek(0)
return reportlab.lib.utils.ImageReader(sio)


class Color(Text):
Expand All @@ -184,12 +203,12 @@ def convert(self, value, context=None):
if value in ALL_COLORS:
return ALL_COLORS[value]
# Decimal triplet
rgb = value.split(',')
rgb = Sequence(valueType=Float(), length=3).convert(value)
if len(rgb) == 3:
return (float(num) for num in rgb)
return [float(num) for num in rgb]
# Hexdecimal triplet
if value.startswith('#'):
return (float(int(value[i:i+1], 16)) for i in range(1, 7, 2))
return [float(int(value[i:i+1], 16)) for i in range(1, 7, 2)]
raise ValueError('%r not a valid color.' %value)


Expand Down Expand Up @@ -267,6 +286,8 @@ def __init__(self):
super(TextNode, self).__init__('TEXT')

def get(self, element, default=DEFAULT, context=None):
if element.text is None:
return u''
return unicode(element.text).strip()

class FirstLevelTextNode(TextNode):
Expand Down
3 changes: 1 addition & 2 deletions src/z3c/rml/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,8 @@ def process(self):
if subElement.tail is not None:
self.processPoints(subElement.tail)

if kw.get('close', False):
if kw.pop('close', False):
self.path.close()
del kw['close']

self.context.drawPath(self.path, **kw)

Expand Down
2 changes: 1 addition & 1 deletion src/z3c/rml/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class ValueAxis(Axis):
attr.Bool('forceZero'), # TODO: Support 'near'
attr.Measurement('minimumTickSpacing'),
attr.Int('maximumTicks'),
attr.Text('labelTextFormat'),
attr.Attribute('labelTextFormat'),
attr.Text('labelTextPostFormat'),
attr.Float('labelTextScale'),
attr.Float('valueMin'),
Expand Down
10 changes: 10 additions & 0 deletions src/z3c/rml/rml2pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
$Id$
"""
__docformat__ = "reStructuredText"
import cStringIO
import os
import sys
import zope.interface
Expand All @@ -25,6 +26,15 @@
zope.interface.moduleProvides(interfaces.IRML2PDF)


def parseString(xml):
root = etree.fromstring(xml)
doc = document.Document(root)
output = cStringIO.StringIO()
doc.process(output)
output.seek(0)
return output


def go(xmlInputName, outputFileName=None, outDir=None, dtdDir=None):
if dtdDir is not None:
sys.stderr.write('The ``dtdDir`` option is not yet supported.')
Expand Down

0 comments on commit 3d19ab3

Please sign in to comment.