Skip to content

Commit

Permalink
Allow setting text attribute on para frags
Browse files Browse the repository at this point in the history
In complicated documents ReportLab 3.1.44 sometimes tries to set the
text attribute of fragments. Unfortunately I couldn't recreate the issue
in a simple test.
  • Loading branch information
kylemacfarlane committed Feb 3, 2015
1 parent 90ace6f commit 5053f7b
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/z3c/rml/paraparser.py
Expand Up @@ -21,17 +21,28 @@
import reportlab.platypus.paraparser


class PageNumberFragment(reportlab.platypus.paraparser.ParaFrag):
class ParaFragWrapper(reportlab.platypus.paraparser.ParaFrag):
@property
def text(self):
if not hasattr(self, '_text'):
self._text = self._get_text()
return self._text

@text.setter
def text(self, value):
self._text = value


class PageNumberFragment(ParaFragWrapper):
"""A fragment whose `text` is computed at access time."""

def __init__(self, **attributes):
reportlab.platypus.paraparser.ParaFrag.__init__(self, **attributes)
self.counting_from = attributes.get('countingFrom', 1)

@property
def text(self):
def _get_text(self):
# Guess 1: We're in a paragraph in a story.
frame = sys._getframe(4)
frame = sys._getframe(5)
canvas = frame.f_locals.get('canvas', None)

if canvas is None:
Expand All @@ -48,18 +59,17 @@ def text(self):
return str(canvas.getPageNumber() + int(self.counting_from) - 1)


class GetNameFragment(reportlab.platypus.paraparser.ParaFrag):
class GetNameFragment(ParaFragWrapper):
"""A fragment whose `text` is computed at access time."""

def __init__(self, **attributes):
reportlab.platypus.paraparser.ParaFrag.__init__(self, **attributes)
self.id = attributes['id']
self.default = attributes.get('default')

@property
def text(self):
def _get_text(self):
# Guess 1: We're in a paragraph in a story.
frame = sys._getframe(4)
frame = sys._getframe(5)
canvas = frame.f_locals.get('canvas', None)

if canvas is None:
Expand All @@ -76,15 +86,14 @@ def text(self):
return canvas.manager.get_name(self.id, self.default)


class EvalStringFragment(reportlab.platypus.paraparser.ParaFrag):
class EvalStringFragment(ParaFragWrapper):
"""A fragment whose `text` is evaluated at access time."""

def __init__(self, **attributes):
reportlab.platypus.paraparser.ParaFrag.__init__(self, **attributes)
self.frags = []

@property
def text(self):
def _get_text(self):
text = u''
for frag in self.frags:
if isinstance(frag, six.string_types):
Expand All @@ -95,16 +104,15 @@ def text(self):
return do_eval(text)


class NameFragment(reportlab.platypus.paraparser.ParaFrag):
class NameFragment(ParaFragWrapper):
"""A fragment whose attribute `value` is set to a variable."""

def __init__(self, **attributes):
reportlab.platypus.paraparser.ParaFrag.__init__(self, **attributes)

@property
def text(self):
def _get_text(self):
# Guess 1: We're in a paragraph in a story.
frame = sys._getframe(4)
frame = sys._getframe(5)
canvas = frame.f_locals.get('canvas', None)

if canvas is None:
Expand Down

0 comments on commit 5053f7b

Please sign in to comment.