From 5053f7bacbb5d46cdc2691466a34aa8f7762b6c6 Mon Sep 17 00:00:00 2001 From: Kyle MacFarlane Date: Tue, 3 Feb 2015 23:11:37 +0000 Subject: [PATCH] Allow setting text attribute on para frags 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. --- src/z3c/rml/paraparser.py | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/z3c/rml/paraparser.py b/src/z3c/rml/paraparser.py index f2d273f..cddd6ad 100644 --- a/src/z3c/rml/paraparser.py +++ b/src/z3c/rml/paraparser.py @@ -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: @@ -48,7 +59,7 @@ 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): @@ -56,10 +67,9 @@ def __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: @@ -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): @@ -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: