reportlab/pdfgen/pycanvas.py
changeset 1740 71f51fea9301
parent 1739 9ee6c04932fa
child 1742 1dac293eabff
--- a/reportlab/pdfgen/pycanvas.py	Wed Oct 02 07:01:01 2002 +0000
+++ b/reportlab/pdfgen/pycanvas.py	Wed Oct 02 13:23:49 2002 +0000
@@ -1,4 +1,4 @@
-# a Pythonesque Canvas v0.4
+# a Pythonesque Canvas v0.5
 # Author : Jerome Alet - <alet@librelogiciel.com>
 # License : ReportLab's license
 #
@@ -18,8 +18,12 @@
 ad nauseam !
 
 the reportlab/test/test_pdfgen_pycanvas.py program is the test suite for
-pycanvas, you can do the following :
+pycanvas, you can do the following to run it :
 
+    First set verbose=1 in reportlab/rl_config.py
+    
+    then from the command interpreter :
+    
     $ cd reportlab/test
     $ python test_pdfgen_pycanvas.py >n1.py
     
@@ -46,14 +50,23 @@
     
     - To linearize a program : optimizing some complex parts for example.
     
-    - To debug : reading the generated Python source code may help you.
+    - To debug : reading the generated Python source code may help you or
+      the ReportLab team to diagnose problems. The generated code is now
+      clearly commented and shows nesting levels, page numbers, and so
+      on. You can use the generated script when asking for support : we
+      can see the results you obtain without needing your datas or complete
+      application.
     
     - To create standalone scripts : say your program uses a high level
       environment to generate its output (databases, RML, etc...), using 
       this class would give you an equivalent program but with complete 
       independance from the high level environment (e.g. if you don't 
-      have Oracle)
+      have Oracle).
     
+    - To contribute some nice looking PDF documents to the ReportLab website
+      without having to send a complete application you don't want to
+      distribute.
+      
     - ... Insert your own ideas here ...
     
     - For fun because you can do it !
@@ -64,7 +77,7 @@
 from reportlab.pdfgen import pathobject
 from reportlab.pdfgen import textobject
 
-PyHeader = """#! /usr/bin/env python
+PyHeader = '''#! /usr/bin/env python
 
 import sys
 from reportlab.pdfgen import pycanvas
@@ -72,18 +85,28 @@
 from reportlab.pdfgen import textobject
 from reportlab.lib.colors import Color
 
-def doIt(file) :"""
+def doIt(file) :
+    """Generates a PDF document, save it into file, and returns Python source code.
+    
+       file : either a filename or a file-like object.
+       
+       returns : the equivalent Python source code as a string of text.
+    """'''
 
-PyFooter = """    return str(c)
+PyFooter = '''
+    # sends back the equivalent Python source code
+    return str(c)
 
 if __name__ == "__main__" :
     if len(sys.argv) != 2 :
+        # second argument must be the name of the PDF file to create
         sys.stderr.write("%s needs one and only one argument\\n" % sys.argv[0])
         sys.exit(-1)
     else :
+        # we've got a filename, we can proceed.
         print doIt(sys.argv[1])    
         sys.exit(0)
-"""
+'''
     
 def buildargs(*args, **kwargs) :
     arguments = ""
@@ -116,6 +139,7 @@
         return retcode
         
 class PDFObject :
+    _number = 0
     def __init__(self, parent) :
         self._parent = parent
         self._initdone = 0
@@ -128,8 +152,9 @@
         
     def __call__(self, *args, **kwargs) :
         if not self._initdone :
+            self.__class__._number += 1
             methodname = apply(self._postinit, args, kwargs)
-            self._parent._PyWrite("    %s = %s.%s(%s)" % (self._name, self._parent._name, methodname, apply(buildargs, args, kwargs)))
+            self._parent._PyWrite("\n    # create PDF%sObject number %i\n    %s = %s.%s(%s)" % (methodname[5:], self.__class__._number, self._name, self._parent._name, methodname, apply(buildargs, args, kwargs)))
             self._initdone = 1
         return self
     
@@ -150,9 +175,34 @@
         def __call__(self, *args, **kwargs) :
             global _in
             try :
-                # print "canvas [%s] : %i" % (self._action, _in)
                 if (not _in) and (self._action != "__nonzero__") :
+                    if self._action == "showPage" :
+                        self._parent._PyWrite("\n    # Ends page %i" % self._parent._pagenumber)
+                    elif self._action == "saveState" :    
+                        state = {}
+                        d = self._parent._object.__dict__
+                        for name in self._parent._object.STATE_ATTRIBUTES:
+                            state[name] = d[name] 
+                        self._parent._PyWrite("\n    # Saves context level %i %s" % (self._parent._contextlevel, state))
+                        self._parent._contextlevel += 1
+                    elif self._action == "restoreState" :    
+                        self._parent._contextlevel -= 1
+                        self._parent._PyWrite("\n    # Restores context level %i %s" % (self._parent._contextlevel, self._parent._object.state_stack[-1]))
+                    elif self._action == "beginForm" :    
+                        self._parent._formnumber += 1
+                        self._parent._PyWrite("\n    # Begins form %i" % self._parent._formnumber)
+                    elif self._action == "endForm" :    
+                        self._parent._PyWrite("\n    # Ends form %i" % self._parent._formnumber)
+                    elif self._action == "save" :    
+                        self._parent._PyWrite("\n    # Saves the PDF document to disk")
+                        
                     self._parent._PyWrite("    %s.%s(%s)" % (self._parent._name, self._action, apply(buildargs, args, kwargs)))
+                    
+                    if self._action == "showPage" :
+                        self._parent._pagenumber += 1
+                        self._parent._PyWrite("\n    # Begins page %i" % self._parent._pagenumber)
+                    elif self._action == "endForm" :    
+                        self._parent._PyWrite("")
                 _in += 1    
                 retcode = apply(getattr(self._parent._object, self._action), args, kwargs)
                 _in -= 1    
@@ -163,6 +213,9 @@
 
     _name = "c"
     def __init__(self, *args, **kwargs) :
+        self._contextlevel = 0
+        self._pagenumber = 1
+        self._formnumber = 0
         self._footerpresent = 0
         self._object = apply(canvas.Canvas, args, kwargs)
         self._pyfile = cStringIO.StringIO()
@@ -171,7 +224,7 @@
             del kwargs["filename"]
         except KeyError :    
             pass
-        self._PyWrite("    %s = pycanvas.Canvas(file, %s)" % (self._name, apply(buildargs, args[1:], kwargs)))
+        self._PyWrite("    # create the PDF document\n    %s = pycanvas.Canvas(file, %s)\n\n    # Begins page 1" % (self._name, apply(buildargs, args[1:], kwargs)))
 
     def __str__(self) :
         if not self._footerpresent :