platypus: changes to make identity more useful
authorrgbecker
Thu, 29 Sep 2005 11:49:23 +0000
changeset 2531 40b6f4b41f7c
parent 2530 aa87633edde3
child 2532 130a873a98f6
platypus: changes to make identity more useful
reportlab/platypus/doctemplate.py
reportlab/platypus/flowables.py
reportlab/platypus/frames.py
--- a/reportlab/platypus/doctemplate.py	Wed Sep 28 17:22:53 2005 +0000
+++ b/reportlab/platypus/doctemplate.py	Thu Sep 29 11:49:23 2005 +0000
@@ -109,7 +109,7 @@
         return self
 
     def identity(self, maxLen=None):
-        return "ActionFlowable: %s" % str(self.action)
+        return "ActionFlowable: %s%s" % (str(self.action),self._frameName())
 
 class LCActionFlowable(ActionFlowable):
     locChanger = 1                  #we cause a frame or page change
@@ -299,8 +299,10 @@
                     'title':None,
                     'author':None,
                     'invariant':None,
+                    'pageCompression':None,
                     '_pageBreakQuick':1,
-                    'rotation':0,}
+                    'rotation':0,
+                    '_debug':0}
     _invalidInitArgs = ()
     _firstPageTemplateIndex = 0
 
@@ -316,7 +318,6 @@
                     raise ValueError, "Invalid argument %s" % k
                 v = kw[k]
             setattr(self,k,v)
-        #print "pagesize is", self.pagesize
 
         p = self.pageTemplates
         self.pageTemplates = []
@@ -387,7 +388,7 @@
         '''Perform actions required at beginning of page.
         shouldn't normally be called directly'''
         self.page = self.page + 1
-        logger.debug("beginning page %d" % self.page)
+        if self._debug: logger.debug("beginning page %d" % self.page)
         self.pageTemplate.beforeDrawPage(self.canv,self)
         self.pageTemplate.checkPageSize(self.canv,self)
         self.pageTemplate.onPage(self.canv,self)
@@ -398,6 +399,7 @@
         if hasattr(self,'_nextFrameIndex'):
             del self._nextFrameIndex
         self.frame = self.pageTemplate.frames[0]
+        self.frame._debug = self._debug
         self.handle_frameBegin()
 
     def handle_pageEnd(self):
@@ -423,7 +425,7 @@
             self.pageTemplate.afterDrawPage(self.canv, self)
             self.pageTemplate.onPageEnd(self.canv, self)
             self.afterPage()
-            logger.debug("ending page %d" % self.page)
+            if self._debug: logger.debug("ending page %d" % self.page)
             self.canv.setPageRotation(getattr(self.pageTemplate,'rotation',self.rotation))
             self.canv.showPage()
 
@@ -469,6 +471,7 @@
 
         if hasattr(self,'_nextFrameIndex'):
             self.frame = self.pageTemplate.frames[self._nextFrameIndex]
+            self.frame._debug = self._debug
             del self._nextFrameIndex
             self.handle_frameBegin(resume)
         elif hasattr(self.frame,'lastFrame') or self.frame is self.pageTemplate.frames[-1]:
@@ -477,6 +480,7 @@
         else:
             f = self.frame
             self.frame = self.pageTemplate.frames[self.pageTemplate.frames.index(f) + 1]
+            self.frame._debug = self._debug
             self.handle_frameBegin()
 
     def handle_nextPageTemplate(self,pt):
@@ -571,6 +575,14 @@
             del flowables[:i]
             flowables.insert(0,K)
 
+    def _fIdent(self,f,maxLen=None,frame=None):
+        if frame: f._frame = frame
+        try:
+            return f.identity(maxLen)
+        finally:
+            if frame: del f._frame
+
+
     def handle_flowable(self,flowables):
         '''try to handle one flowable from the front of list flowables.'''
 
@@ -581,7 +593,6 @@
         self.handle_breakBefore(flowables)
         self.handle_keepWithNext(flowables)
         f = flowables[0]
-        #print 'handling flowable %s' % f.identity()
         del flowables[0]
         if f is None:
             return
@@ -601,21 +612,18 @@
                 self._curPageFlowableCount = self._curPageFlowableCount + 1
                 self.afterFlowable(f)
             else:
-                #if isinstance(f, KeepTogether): print 'could not add it to frame'
                 if self.allowSplitting:
                     # see if this is a splittable thing
                     S = self.frame.split(f,self.canv)
-                    #print '%d parts to sequence on page %d' % (len(S), self.page)
                     n = len(S)
                 else:
                     n = 0
-                #if isinstance(f, KeepTogether): print 'n=%d' % n
                 if n:
                     if self.frame.add(S[0], self.canv, trySplit=0):
                         self._curPageFlowableCount = self._curPageFlowableCount + 1
                         self.afterFlowable(S[0])
                     else:
-                        ident = "Splitting error(n==%d) on page %d in\n%s" % (n,self.page,f.identity(30))
+                        ident = "Splitting error(n==%d) on page %d in\n%s" % (n,self.page,self._fIdent(f,30,self.frame))
                         #leave to keep apart from the raise
                         raise LayoutError(ident)
                     del S[0]
@@ -623,7 +631,7 @@
                         flowables.insert(f,S[f])    # put split flowables back on the list
                 else:
                     if hasattr(f,'_postponed'):
-                        ident = "Flowable %s too large on page %d" % (f.identity(30), self.page)
+                        ident = "Flowable %s too large on page %d" % (self._fIdent(f,30,self.frame), self.page)
                         #leave to keep apart from the raise
                         raise LayoutError(ident)
                     # this ought to be cleared when they are finally drawn!
@@ -646,7 +654,8 @@
         self._calc()
         self.canv = canvasmaker(filename or self.filename,
                                 pagesize=self.pagesize,
-                                invariant=self.invariant)
+                                invariant=self.invariant,
+                                pageCompression=self.pageCompression)
         if self._onPage:
             self.canv.setPageCallBack(self._onPage)
         self.handle_documentBegin()
@@ -737,8 +746,6 @@
         for thing in story:
             if thing.isIndexing():
                 self._indexingFlowables.append(thing)
-        #print 'scanned story, found these indexing flowables:\n'
-        #print self._indexingFlowables
 
         #better fix for filename is a 'file' problem
         self._doSave = 0
--- a/reportlab/platypus/flowables.py	Wed Sep 28 17:22:53 2005 +0000
+++ b/reportlab/platypus/flowables.py	Thu Sep 29 11:49:23 2005 +0000
@@ -170,7 +170,12 @@
             r = '...'
         if r and maxLen:
             r = r[:maxLen]
-        return "<%s at %d>%s" % (self.__class__.__name__, id(self), r)
+        return "<%s at %s%s>%s" % (self.__class__.__name__, hex(id(self)), self._frameName(), r)
+
+    def _frameName(self):
+        f = getattr(self,'_frame',None)
+        if f and f.id: return ' frame=%s' % f.id
+        return ''
 
 class XBox(Flowable):
     """Example flowable - a box with an x through it and a caption.
@@ -479,7 +484,7 @@
         return S
 
     def identity(self):
-        return "<KeepTogether at %d> containing :" + "\n".join([f.identity() for f in self._flowables])
+        return "<KeepTogether at %s%s> containing :%s" % (hex(id(self)),self._frameName(),"\n".join([f.identity() for f in self._flowables]))
 
 class Macro(Flowable):
     """This is not actually drawn (i.e. it has zero height)
@@ -744,14 +749,14 @@
     return max(1./s1, 1./s2)
 
 class KeepInFrame(_Container,Flowable):
-    def __init__(self, maxWidth, maxHeight, content=[], mergeSpace=1, mode='shrink', name=None):
+    def __init__(self, maxWidth, maxHeight, content=[], mergeSpace=1, mode='shrink', name=''):
         '''mode describes the action to take when overflowing
             error       raise an error in the normal way
             continue    ignore ie just draw it and report maxWidth, maxHeight
             shrink      shrinkToFit
             truncate    fit as much as possible
         '''
-        self.name = name or str(id(self))
+        self.name = name
         self.maxWidth = maxWidth
         self.maxHeight = maxHeight
         self.mode = mode
@@ -765,7 +770,7 @@
         return self.maxWidth - self._leftExtraIndent - self._rightExtraIndent
 
     def identity(self, maxLen=None):
-        return "<%s at %d%s> size=%sx%s" % (self.__class__.__name__, id(self), self.name and ' name="%s"'%self.name, fp_str(self.maxWidth),fp_str(self.maxHeight))
+        return "<%s at %s%s%s> size=%sx%s" % (self.__class__.__name__, hex(id(self)), self._frameName(), self.name and ' name="%s"'%self.name or '', fp_str(self.maxWidth),fp_str(self.maxHeight))
 
     def wrap(self,availWidth,availHeight):
         mode = self.mode
--- a/reportlab/platypus/frames.py	Wed Sep 28 17:22:53 2005 +0000
+++ b/reportlab/platypus/frames.py	Thu Sep 29 11:49:23 2005 +0000
@@ -48,8 +48,9 @@
     '''
     def __init__(self, x1, y1, width,height, leftPadding=6, bottomPadding=6,
             rightPadding=6, topPadding=6, id=None, showBoundary=0,
-            overlapAttachedSpace=None):
+            overlapAttachedSpace=None,_debug=None):
         self.id = id
+        self._debug = _debug
 
         #these say where it goes on the page
         self.__dict__['_x1'] = x1
@@ -127,9 +128,10 @@
                 s = max(s-self._prevASpace,0)
         h = y - p - s
         if h>0:
+            flowable._frame = self
             flowable.canv = canv #so they can use stringWidth etc
             w, h = flowable.wrap(aW, h)
-            del flowable.canv
+            del flowable.canv, flowable._frame
         else:
             return 0
 
@@ -143,8 +145,10 @@
             return 0
         else:
             #now we can draw it, and update the current point.
+            flowable._frame = self
             flowable.drawOn(canv, self._x + self._leftExtraIndent, y, _sW=aW-w)
-            logger.debug('drew %s: \n    %s' % (flowable.__class__.__name__, flowable.identity()))
+            if self._debug: logger.debug('drew %s' % flowable.identity())
+            del flowable._frame
             s = flowable.getSpaceAfter()
             y -= s
             if self._oASpace: self._prevASpace = s