src/reportlab/platypus/doctemplate.py
branchpy33
changeset 3731 b233dd0577ff
parent 3723 99aa837b6703
child 3760 674899ebab37
equal deleted inserted replaced
3730:690aaa390f45 3731:b233dd0577ff
    33 from reportlab.platypus.paragraph import Paragraph
    33 from reportlab.platypus.paragraph import Paragraph
    34 from reportlab.platypus.frames import Frame
    34 from reportlab.platypus.frames import Frame
    35 from reportlab.rl_config import defaultPageSize, verbose
    35 from reportlab.rl_config import defaultPageSize, verbose
    36 import reportlab.lib.sequencer
    36 import reportlab.lib.sequencer
    37 from reportlab.pdfgen import canvas
    37 from reportlab.pdfgen import canvas
       
    38 from reportlab.lib.utils import isSeq
    38 try:
    39 try:
    39     set
    40     set
    40 except NameError:
    41 except NameError:
    41     from sets import Set as set
    42     from sets import Set as set
    42 
    43 
    46 except ImportError:
    47 except ImportError:
    47     import pickle
    48     import pickle
    48 dumps = pickle.dumps
    49 dumps = pickle.dumps
    49 loads = pickle.loads
    50 loads = pickle.loads
    50 
    51 
    51 from types import *
       
    52 import sys
    52 import sys
    53 import logging
    53 import logging
    54 logger = logging.getLogger("reportlab.platypus")
    54 logger = logging.getLogger("reportlab.platypus")
    55 
    55 
    56 class LayoutError(Exception):
    56 class LayoutError(Exception):
   126     '''
   126     '''
   127     def __init__(self,action=()):
   127     def __init__(self,action=()):
   128         #must call super init to ensure it has a width and height (of zero),
   128         #must call super init to ensure it has a width and height (of zero),
   129         #as in some cases the packer might get called on it...
   129         #as in some cases the packer might get called on it...
   130         Flowable.__init__(self)
   130         Flowable.__init__(self)
   131         if type(action) not in (ListType, TupleType):
   131         if not isSeq(action):
   132             action = (action,)
   132             action = (action,)
   133         self.action = tuple(action)
   133         self.action = tuple(action)
   134 
   134 
   135     def apply(self,doc):
   135     def apply(self,doc):
   136         '''
   136         '''
   200 
   200 
   201 FrameBreak = _FrameBreak('frameEnd')
   201 FrameBreak = _FrameBreak('frameEnd')
   202 PageBegin = LCActionFlowable('pageBegin')
   202 PageBegin = LCActionFlowable('pageBegin')
   203 
   203 
   204 def _evalMeasurement(n):
   204 def _evalMeasurement(n):
   205     if type(n) is type(''):
   205     if isinstance(n,str):
   206         from .paraparser import _num
   206         from .paraparser import _num
   207         n = _num(n)
   207         n = _num(n)
   208         if type(n) is type(()): n = n[1]
   208         if isSeq(n): n = n[1]
   209     return n
   209     return n
   210 
   210 
   211 class FrameActionFlowable(Flowable):
   211 class FrameActionFlowable(Flowable):
   212     _fixedWidth = _fixedHeight = 1
   212     _fixedWidth = _fixedHeight = 1
   213     def __init__(self,*arg,**kw):
   213     def __init__(self,*arg,**kw):
   253     derived classes can also implement beforeDrawPage and afterDrawPage if they want
   253     derived classes can also implement beforeDrawPage and afterDrawPage if they want
   254     """
   254     """
   255     def __init__(self,id=None,frames=[],onPage=_doNothing, onPageEnd=_doNothing,
   255     def __init__(self,id=None,frames=[],onPage=_doNothing, onPageEnd=_doNothing,
   256                  pagesize=None, autoNextPageTemplate=None):
   256                  pagesize=None, autoNextPageTemplate=None):
   257         frames = frames or []
   257         frames = frames or []
   258         if type(frames) not in (ListType,TupleType): frames = [frames]
   258         if not isSeq(frames): frames = [frames]
   259         assert [x for x in frames if not isinstance(x,Frame)]==[], "frames argument error"
   259         assert [x for x in frames if not isinstance(x,Frame)]==[], "frames argument error"
   260         self.id = id
   260         self.id = id
   261         self.frames = frames
   261         self.frames = frames
   262         self.onPage = onPage
   262         self.onPage = onPage
   263         self.onPageEnd = onPageEnd
   263         self.onPageEnd = onPageEnd
   519         while len(self._hanging):
   519         while len(self._hanging):
   520             self.handle_flowable(self._hanging)
   520             self.handle_flowable(self._hanging)
   521 
   521 
   522     def addPageTemplates(self,pageTemplates):
   522     def addPageTemplates(self,pageTemplates):
   523         'add one or a sequence of pageTemplates'
   523         'add one or a sequence of pageTemplates'
   524         if type(pageTemplates) not in (ListType,TupleType):
   524         if not isSeq(pageTemplates):
   525             pageTemplates = [pageTemplates]
   525             pageTemplates = [pageTemplates]
   526         #this test below fails due to inconsistent imports!
   526         #this test below fails due to inconsistent imports!
   527         #assert filter(lambda x: not isinstance(x,PageTemplate), pageTemplates)==[], "pageTemplates argument error"
   527         #assert filter(lambda x: not isinstance(x,PageTemplate), pageTemplates)==[], "pageTemplates argument error"
   528         for t in pageTemplates:
   528         for t in pageTemplates:
   529             self.pageTemplates.append(t)
   529             self.pageTemplates.append(t)
   636             self.frame._debug = self._debug
   636             self.frame._debug = self._debug
   637             self.handle_frameBegin()
   637             self.handle_frameBegin()
   638 
   638 
   639     def handle_nextPageTemplate(self,pt):
   639     def handle_nextPageTemplate(self,pt):
   640         '''On endPage change to the page template with name or index pt'''
   640         '''On endPage change to the page template with name or index pt'''
   641         if type(pt) is StringType:
   641         if isinstance(pt,''):
   642             if hasattr(self, '_nextPageTemplateCycle'): del self._nextPageTemplateCycle
   642             if hasattr(self, '_nextPageTemplateCycle'): del self._nextPageTemplateCycle
   643             for t in self.pageTemplates:
   643             for t in self.pageTemplates:
   644                 if t.id == pt:
   644                 if t.id == pt:
   645                     self._nextPageTemplateIndex = self.pageTemplates.index(t)
   645                     self._nextPageTemplateIndex = self.pageTemplates.index(t)
   646                     return
   646                     return
   647             raise ValueError("can't find template('%s')"%pt)
   647             raise ValueError("can't find template('%s')"%pt)
   648         elif type(pt) is IntType:
   648         elif isinstance(pt,int):
   649             if hasattr(self, '_nextPageTemplateCycle'): del self._nextPageTemplateCycle
   649             if hasattr(self, '_nextPageTemplateCycle'): del self._nextPageTemplateCycle
   650             self._nextPageTemplateIndex = pt
   650             self._nextPageTemplateIndex = pt
   651         elif type(pt) in (ListType, TupleType):
   651         elif isSeq(pt):
   652             #used for alternating left/right pages
   652             #used for alternating left/right pages
   653             #collect the refs to the template objects, complain if any are bad
   653             #collect the refs to the template objects, complain if any are bad
   654             c = PTCycle()
   654             c = PTCycle()
   655             for ptn in pt:
   655             for ptn in pt:
   656                 found = 0
   656                 found = 0
   673         else:
   673         else:
   674             raise TypeError("argument pt should be string or integer or list")
   674             raise TypeError("argument pt should be string or integer or list")
   675 
   675 
   676     def handle_nextFrame(self,fx,resume=0):
   676     def handle_nextFrame(self,fx,resume=0):
   677         '''On endFrame change to the frame with name or index fx'''
   677         '''On endFrame change to the frame with name or index fx'''
   678         if type(fx) is StringType:
   678         if isinstance(fx,str):
   679             for f in self.pageTemplate.frames:
   679             for f in self.pageTemplate.frames:
   680                 if f.id == fx:
   680                 if f.id == fx:
   681                     self._nextFrameIndex = self.pageTemplate.frames.index(f)
   681                     self._nextFrameIndex = self.pageTemplate.frames.index(f)
   682                     return
   682                     return
   683             raise ValueError("can't find frame('%s') in %r(%s) which has frames %r"%(fx,self.pageTemplate,self.pageTemplate.id,[(f,f.id) for f in self.pageTemplate.frames]))
   683             raise ValueError("can't find frame('%s') in %r(%s) which has frames %r"%(fx,self.pageTemplate,self.pageTemplate.id,[(f,f.id) for f in self.pageTemplate.frames]))
   684         elif type(fx) is IntType:
   684         elif isinstance(fx,int):
   685             self._nextFrameIndex = fx
   685             self._nextFrameIndex = fx
   686         else:
   686         else:
   687             raise TypeError("argument fx should be string or integer")
   687             raise TypeError("argument fx should be string or integer")
   688 
   688 
   689     def handle_currentFrame(self,fx,resume=0):
   689     def handle_currentFrame(self,fx,resume=0):