Skip to content

Commit

Permalink
Got 3 more RML testsuite tests to pass. Yipee!
Browse files Browse the repository at this point in the history
  • Loading branch information
strichter committed Mar 16, 2007
1 parent 346b3d9 commit 09eb2e3
Show file tree
Hide file tree
Showing 15 changed files with 956 additions and 101 deletions.
25 changes: 16 additions & 9 deletions src/z3c/rml/attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
DEFAULT = object()


def getManager(context, interface):
while (not interface.providedBy(context) and context is not None):
context = context.parent
if context is None:
raise ValueError(
'Manager for %s could not be found.' %interface.getName())
return context


class Attribute(object):

def __init__(self, name=None, default=DEFAULT):
Expand Down Expand Up @@ -236,22 +245,20 @@ class Color(Text):
def convert(self, value, context=None):
if value == 'None':
return None
manager = getManager(context, interfaces.IColorsManager)
if value in manager.colors:
return manager.colors[value]
return reportlab.lib.colors.toColor(value)


class Style(Text):

def __init__(self, name=None, type='para', default='Normal'):
def __init__(self, name=None, default='Normal'):
super(Style, self).__init__(name, default)
self.type = type

def convert(self, value, context=None):
# First, get the custom styles
proc = context
while (not interfaces.IStylesManager.providedBy(proc) and
proc is not None):
proc = proc.parent
for styles in (proc.styles.get(self.type, {}),
manager = getManager(context, interfaces.IStylesManager)
for styles in (manager.styles,
reportlab.lib.styles.getSampleStyleSheet().byName):
if value in styles:
return styles[value]
Expand Down Expand Up @@ -385,4 +392,4 @@ class XMLContent(RawXMLContent):

def get(self, element, default=DEFAULT, context=None):
result = super(XMLContent, self).get(element, default, context)
return result.strip()
return result.strip().replace('\t', ' ')
8 changes: 3 additions & 5 deletions src/z3c/rml/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,18 +394,16 @@ def process(self):


class Canvas(element.ContainerElement):
zope.interface.implements(
interfaces.IStylesManager, interfaces.IPostProcessorManager)
zope.interface.implements(interfaces.IPostProcessorManager)

subElements = {
'stylesheet': stylesheet.Stylesheet,
'pageDrawing': PageDrawing,
'pageInfo': PageInfo,
}

def __init__(self, element):
self.element = element
self.styles = {}
def __init__(self, element, parent, context):
super(Canvas, self).__init__(element, parent, context)
self.postProcessors = []

def process(self, outputFile):
Expand Down
21 changes: 11 additions & 10 deletions src/z3c/rml/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PropertyItem(element.Element):
attrs = None

def process(self):
attrs = element.extractAttributes(self.attrs, self.element)
attrs = element.extractAttributes(self.attrs, self.element, self)
self.context.append(attrs)


Expand All @@ -50,7 +50,7 @@ class PropertyCollection(element.ContainerElement):
def processAttributes(self):
prop = getattr(self.context, self.propertyName)
# Get global properties
attrs = element.extractAttributes(self.attrs, self.element)
attrs = element.extractAttributes(self.attrs, self.element, self)
for name, value in attrs.items():
setattr(prop, name, value)

Expand Down Expand Up @@ -80,7 +80,7 @@ class Text(element.Element):
)

def process(self):
attrs = element.extractAttributes(self.attrs, self.element)
attrs = element.extractAttributes(self.attrs, self.element, self)
string = shapes.String(
attrs.pop('x'), attrs.pop('y'), attrs.pop('TEXT'))
angle = attrs.pop('angle')
Expand All @@ -100,7 +100,8 @@ class Series(element.Element):
attrList = None

def process(self):
attrs = element.extractPositionalArguments(self.attrList, self.element)
attrs = element.extractPositionalArguments(
self.attrList, self.element, self)
self.context.append(attrs[0])

class Data(element.ContainerElement):
Expand Down Expand Up @@ -219,7 +220,7 @@ class Name(element.Element):
attrs = (attr.TextNode(),)

def process(self):
attrs = element.extractAttributes(self.attrs, self.element)
attrs = element.extractAttributes(self.attrs, self.element, self)
self.context.append(attrs['TEXT'])


Expand Down Expand Up @@ -320,7 +321,7 @@ class SliceLabel(Label):
attrs = Label.attrs[2:]

def process(self):
attrs = element.extractAttributes(self.attrs, self.element)
attrs = element.extractAttributes(self.attrs, self.element, self)
for name, value in attrs.items():
self.context['label_'+name] = value
# Now we do not have simple labels anymore
Expand All @@ -336,7 +337,7 @@ class SlicePointer(element.Element):
)

def process(self):
attrs = element.extractAttributes(self.attrs, self.element)
attrs = element.extractAttributes(self.attrs, self.element, self)
for name, value in attrs.items():
self.context['label_pointer_'+name] = value

Expand All @@ -358,7 +359,7 @@ class Slice(element.ContainerElement):
'pointer': SlicePointer}

def process(self):
attrs = element.extractAttributes(self.attrs, self.element)
attrs = element.extractAttributes(self.attrs, self.element, self)
self.processSubElements(attrs)
self.context.append(attrs)

Expand All @@ -377,7 +378,7 @@ class Slices(element.ContainerElement):

def process(self):
# Get global slice properties
attrs = element.extractAttributes(self.attrs, self.element)
attrs = element.extractAttributes(self.attrs, self.element, self)
for name, value in attrs.items():
setattr(self.context.slices, name, value)
# Get slice specific properties
Expand Down Expand Up @@ -498,7 +499,7 @@ class Chart(element.ContainerElement):

def getAttributes(self):
attrs = [(attr.name, attr) for attr in self.attrs]
return element.extractKeywordArguments(attrs, self.element)
return element.extractKeywordArguments(attrs, self.element, self)

def createChart(self, attributes):
raise NotImplementedError
Expand Down
44 changes: 36 additions & 8 deletions src/z3c/rml/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
"""
__docformat__ = "reStructuredText"
import sys

from reportlab.pdfbase import pdfmetrics, ttfonts
from z3c.rml import attr, element, error
import zope.interface
from reportlab.pdfbase import pdfmetrics, ttfonts, cidfonts
from z3c.rml import attr, element, error, interfaces
from z3c.rml import canvas, stylesheet, template


class RegisterType1Face(element.Element):
args = ( attr.Attribute('afmFile'), attr.Attribute('pfbFile') )

def process(self):
args = element.extractPositionalArguments(self.args, self.element)
args = element.extractPositionalArguments(self.args, self.element, self)
face = pdfmetrics.EmbeddedType1Face(*args)
pdfmetrics.registerTypeFace(face)

Expand All @@ -39,7 +39,7 @@ class RegisterFont(element.Element):
attr.Attribute('encName') )

def process(self):
args = element.extractPositionalArguments(self.args, self.element)
args = element.extractPositionalArguments(self.args, self.element, self)
font = pdfmetrics.Font(*args)
pdfmetrics.registerFont(font)

Expand All @@ -50,28 +50,56 @@ class RegisterTTFont(element.Element):
attr.Attribute('fileName') )

def process(self):
args = element.extractPositionalArguments(self.args, self.element)
args = element.extractPositionalArguments(self.args, self.element, self)
font = ttfonts.TTFont(*args)
pdfmetrics.registerFont(font)


class RegisterCidFont(element.Element):
args = ( attr.Attribute('faceName'), )

def process(self):
args = element.extractPositionalArguments(self.args, self.element, self)
pdfmetrics.registerFont(cidfonts.UnicodeCIDFont(*args))


class ColorDefinition(element.FunctionElement):
args = (
attr.Text('id'),
attr.Color('RGB'), )

def process(self):
id, value = self.getPositionalArguments()
manager = attr.getManager(self, interfaces.IColorsManager)
manager.colors[id] = value


class DocInit(element.ContainerElement):

subElements = {
'registerType1Face': RegisterType1Face,
'registerFont': RegisterFont,
'registerTTFont': RegisterTTFont,
'registerCidFont': RegisterCidFont,
'color': ColorDefinition,
}


class Document(element.ContainerElement):
zope.interface.implements(
interfaces.INamesManager,
interfaces.IStylesManager,
interfaces.IColorsManager)

subElements = {
'docinit': DocInit
}

def __init__(self, element):
self.element = element
self.names = {}
self.styles = {}
self.colors = {}

def process(self, outputFile=None):
"""Process document"""
Expand All @@ -82,7 +110,7 @@ def process(self, outputFile=None):
self.processSubElements(None)

if self.element.find('pageDrawing') is not None:
canvas.Canvas(self.element).process(outputFile)
canvas.Canvas(self.element, self, None).process(outputFile)

if self.element.find('template') is not None:
template.Template(self.element).process(outputFile)
template.Template(self.element, self, None).process(outputFile)
58 changes: 43 additions & 15 deletions src/z3c/rml/flowable.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ class BarCodeFlowable(Flowable):

class Preformatted(Flowable):
klass = reportlab.platypus.Preformatted
args = ( attr.RawXMLContent(u''), attr.Style('style', 'para', 'Normal') )
args = ( attr.RawXMLContent(u''), attr.Style('style', 'Normal') )

class XPreformatted(Flowable):
klass = reportlab.platypus.XPreformatted
args = ( attr.RawXMLContent(u''), attr.Style('style', 'para', 'Normal') )
args = ( attr.RawXMLContent(u''), attr.Style('style', 'Normal') )

class PluginFlowable(Flowable):
args = ( attr.Text('module'), attr.Text('function'), attr.TextNode())
Expand All @@ -82,20 +82,36 @@ def process(self):

class Paragraph(Flowable):
klass = reportlab.platypus.Paragraph
args = ( attr.XMLContent(u''), attr.Style('style', 'para', 'Normal') )
args = ( attr.XMLContent(u''), attr.Style('style', 'Normal') )
kw = ( ('bulletText', attr.Attribute('bulletText')), )

styleAttrs = stylesheet.ParagraphStyle.attrs[3:]

def processStyle(self, style):
attrs = element.extractAttributes(self.styleAttrs, self.element, self)
if attrs:
style = copy.deepcopy(style)
for name, value in attrs.items():
setattr(style, name, value)
return style

def process(self):
args = self.getPositionalArguments()
kw = self.getKeywordArguments()
args[1] = self.processStyle(args[1])
self.parent.flow.append(self.klass(*args, **kw))

class Title(Paragraph):
args = ( attr.XMLContent(u''), attr.Style('style', 'para', 'Title'), )
args = ( attr.XMLContent(u''), attr.Style('style', 'Title'), )

class Heading1(Paragraph):
args = ( attr.XMLContent(u''), attr.Style('style', 'para', 'Heading1'), )
args = ( attr.XMLContent(u''), attr.Style('style', 'Heading1'), )

class Heading2(Paragraph):
args = ( attr.XMLContent(u''), attr.Style('style', 'para', 'Heading2'), )
args = ( attr.XMLContent(u''), attr.Style('style', 'Heading2'), )

class Heading3(Paragraph):
args = ( attr.XMLContent(u''), attr.Style('style', 'para', 'Heading3'), )
args = ( attr.XMLContent(u''), attr.Style('style', 'Heading3'), )

class TableCell(element.Element):

Expand Down Expand Up @@ -147,12 +163,13 @@ def processStyle(self):
for styleName, attrs in self.styleAttrs:
args = []
for attribute in attrs:
value = attribute.get(self.element)
value = attribute.get(self.element, context=self)
if value is not attr.DEFAULT:
args.append(value)
if args or len(attrs) == 0:
self.parent.parent.style.add(
styleName, [col, row], [col, row], *args)

def process(self):
# Produce style
self.processStyle()
Expand Down Expand Up @@ -184,10 +201,13 @@ def process(self):
))
self.parent.rows = attribute.get(self.element)


class BlockTableStyle(stylesheet.BlockTableStyle):

def setStyle(self, id, style):
self.parent.style = style
def process(self):
self.parent.style = copy.deepcopy(self.parent.style)
self.processSubElements(self.parent.style)


class BlockTable(element.ContainerElement, Flowable):
klass = reportlab.platypus.Table
Expand Down Expand Up @@ -250,20 +270,28 @@ class ConditionalPageBreak(Flowable):
class KeepInFrame(Flowable):
klass = reportlab.platypus.flowables.KeepInFrame
args = (
attr.Measurement('maxWidth'),
attr.Measurement('maxHeight'), )
attr.Measurement('maxWidth', None),
attr.Measurement('maxHeight', None), )
kw = (
('mergeSpace', attr.Bool('mergeSpace')),
('mode', attr.Choice('onOverflow',
('error', 'overflow', 'shrink', 'truncate'))),
('name', attr.Text('id')) )
('name', attr.Text('id')),
('frame', attr.StringOrInt('frame')), )

def process(self):
flow = Flow(self.element, self.parent, self.context)
flow.process()
args = self.getPositionalArguments()
kw = self.getKeywordArguments()
# If the frame was specifed, get us there
frame = kw.pop('frame', None)
if frame:
self.parent.flow.append(
reportlab.platypus.doctemplate.FrameBreak(frame))
# Create the content of the container
flow = Flow(self.element, self.parent, self.context)
flow.process()
kw['content'] = flow.flow
# Create the keep in frame container
frame = self.klass(*args, **kw)
self.parent.flow.append(frame)

Expand Down
Loading

0 comments on commit 09eb2e3

Please sign in to comment.