reportlab/pdfgen/pycanvas.py
author andy_robinson
Tue, 01 Oct 2002 12:55:02 +0000
changeset 1738 64f2ccdb91b9
child 1739 9ee6c04932fa
permissions -rw-r--r--
Jerome Alet's Macro-recording canvas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     1
# a Pythonesque Canvas v0.3
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     2
# Author : Jerome Alet - <alet@librelogiciel.com>
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     3
# License : ReportLab's license
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     4
#
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     5
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     6
__doc__ = """pycanvas.Canvas : a Canvas class which can also output Python source code.
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     7
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     8
pycanvas.Canvas class works exactly like canvas.Canvas, but you can call str() on
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     9
pycanvas.Canvas instances. Doing so will return the Python source code equivalent
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    10
to your own program, which would, when run, produce the same PDF document as
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    11
your original program.
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    12
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    13
Generated Python source code defines a doIt() function which accepts a filename
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    14
or file-like object as its single parameter. The doIt() function will generate
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    15
a PDF document and save it in the file you specified in this argument, and will
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    16
also return you the Generated Python source code, which you can run again
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    17
to produce the very same PDF document and the Python source code, which...
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    18
ad nauseam !
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    19
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    20
the reportlab/test/test_pdfgen_pycanvas.py program is the test suite for
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    21
pycanvas, you can do the following :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    22
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    23
    $ cd reportlab/test
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    24
    $ python test_pdfgen_pycanvas.py >n1.py
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    25
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    26
    this will produce both n1.py and test_pdfgen_pycanvas.pdf
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    27
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    28
    then :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    29
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    30
    $ python n1.py n1.pdf >n2.py
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    31
    $ python n2.py n2.pdf >n3.py
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    32
    $ ...
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    33
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    34
    n1.py, n2.py, n3.py and so on will be identical files.
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    35
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    36
    n1.pdf, n2.pdf, n3.pdf and so on will be PDF files
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    37
    similar to test_pdfgen_pycanvas.pdf.
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    38
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    39
Alternatively you can import n1.py (or n3.py, or n16384.py if you prefer)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    40
in your own program, and then call its doIt function :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    41
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    42
    import n1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    43
    pythonsource = n1.doIt("myfile.pdf")
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    44
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    45
Why would you want to use such a beast ?
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    46
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    47
    - To linearize a program : optimizing some complex parts for example.
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    48
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    49
    - To debug : reading the generated Python source code may help you.
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    50
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    51
    - To create standalone scripts : say your program uses a high level
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    52
      environment to generate its output (databases, RML, etc...), using 
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    53
      this class would give you an equivalent program but with complete 
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    54
      independance from the high level environment (e.g. if you don't 
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    55
      have Oracle)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    56
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    57
    - ... Insert your own ideas here ...
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    58
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    59
    - For fun because you can do it !
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    60
"""
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    61
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    62
import cStringIO
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    63
from reportlab.pdfgen import canvas
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    64
from reportlab.pdfgen import pathobject
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    65
from reportlab.pdfgen import textobject
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    66
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    67
PyHeader = """#! /usr/bin/env python
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    68
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    69
import sys
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    70
from reportlab.pdfgen import pycanvas
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    71
from reportlab.pdfgen import pathobject
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    72
from reportlab.pdfgen import textobject
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    73
from reportlab.lib.colors import Color
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    74
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    75
def doIt(file) :"""
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    76
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    77
PyFooter = """    return str(c)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    78
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    79
if __name__ == "__main__" :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    80
    if len(sys.argv) != 2 :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    81
        sys.stderr.write("%s needs one and only one argument\\n" % sys.argv[0])
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    82
        sys.exit(-1)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    83
    else :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    84
        print doIt(sys.argv[1])    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    85
        sys.exit(0)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    86
"""
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    87
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    88
def buildargs(*args, **kwargs) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    89
    arguments = ""
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    90
    for arg in args :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    91
        arguments += "%s, " % repr(arg)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    92
    for (kw, val) in kwargs.items() :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    93
        arguments += "%s=%s, " % (kw, repr(val))
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    94
    if arguments[-2:] == ", " :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    95
        arguments = arguments[:-2]
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    96
    return arguments    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    97
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    98
_in = 0
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    99
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   100
class Canvas :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   101
    class TextObject :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   102
        class Action :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   103
            def __init__(self, parent, action) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   104
                self._parent = parent
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   105
                self._action = action
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   106
        
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   107
            def __getattr__(self, name) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   108
                return getattr(getattr(self._parent._text, self._action), name)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   109
                
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   110
            def __call__(self, *args, **kwargs) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   111
                global _in
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   112
                # print "text [%s] : %i" % (self._action, _in)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   113
                if not _in :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   114
                    self._parent._parent._PyWrite("    t.%s(%s)" % (self._action, apply(buildargs, args, kwargs)))
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   115
                _in += 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   116
                retcode = apply(getattr(self._parent._text, self._action), args, kwargs)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   117
                _in -= 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   118
                return retcode
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   119
        
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   120
        def __init__(self, parent) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   121
            self._parent = parent
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   122
            self._initdone = 0
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   123
        
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   124
        def __repr__(self) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   125
            return "t"
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   126
            
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   127
        def __getattr__(self, name) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   128
            return self.Action(self, name)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   129
            
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   130
        def __call__(self, *args, **kwargs) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   131
            if not self._initdone :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   132
                self._text = apply(textobject.PDFTextObject, (self._parent, ) + args, kwargs)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   133
                self._parent._PyWrite("    t = c.beginText(%s)" % apply(buildargs, args, kwargs))
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   134
                self._initdone = 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   135
            return self
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   136
        
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   137
    class PathObject :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   138
        class Action :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   139
            def __init__(self, parent, action) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   140
                self._parent = parent
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   141
                self._action = action
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   142
                
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   143
            def __getattr__(self, name) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   144
                return getattr(getattr(self._parent._path, self._action), name)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   145
        
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   146
            def __call__(self, *args, **kwargs) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   147
                global _in
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   148
                # print "path [%s] : %i" % (self._action, _in)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   149
                if not _in :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   150
                    self._parent._parent._PyWrite("    p.%s(%s)" % (self._action, apply(buildargs, args, kwargs)))
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   151
                _in += 1    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   152
                retcode = apply(getattr(self._parent._path, self._action), args, kwargs)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   153
                _in -= 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   154
                return retcode
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   155
        
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   156
        def __init__(self, parent) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   157
            self._parent = parent
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   158
            self._initdone = 0
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   159
        
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   160
        def __repr__(self) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   161
            return "p"
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   162
            
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   163
        def __getattr__(self, name) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   164
            return self.Action(self, name)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   165
            
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   166
        def __call__(self, *args, **kwargs) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   167
            if not self._initdone :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   168
                self._path = apply(pathobject.PDFPathObject, args, kwargs)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   169
                self._parent._PyWrite("    p = c.beginPath(%s)" % apply(buildargs, args, kwargs))
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   170
                self._initdone = 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   171
            return self
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   172
        
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   173
    class Action :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   174
        def __init__(self, parent, action) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   175
            self._parent = parent
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   176
            self._action = action
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   177
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   178
        def __getattr__(self, name) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   179
            return getattr(getattr(self._parent._canvas, self._action), name)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   180
        
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   181
        def __call__(self, *args, **kwargs) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   182
            global _in
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   183
            try :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   184
                # print "canvas [%s] : %i" % (self._action, _in)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   185
                if (not _in) and (self._action != "__nonzero__") :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   186
                    self._parent._PyWrite("    c.%s(%s)" % (self._action, apply(buildargs, args, kwargs)))
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   187
                _in += 1    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   188
                retcode = apply(getattr(self._parent._canvas, self._action), args, kwargs)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   189
                _in -= 1    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   190
                return retcode
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   191
            except AttributeError :    # __nonzero__, but I don't know why
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   192
                _in -= 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   193
                return 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   194
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   195
    def __init__(self, *args, **kwargs) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   196
        self._footerpresent = 0
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   197
        self._canvas = apply(canvas.Canvas, args, kwargs)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   198
        self._pyfile = cStringIO.StringIO()
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   199
        self._PyWrite(PyHeader)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   200
        try :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   201
            del kwargs["filename"]
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   202
        except KeyError :    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   203
            pass
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   204
        self._PyWrite("    c = pycanvas.Canvas(file, %s)" % apply(buildargs, args[1:], kwargs))
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   205
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   206
    def __str__(self) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   207
        if not self._footerpresent :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   208
            self._PyWrite(PyFooter)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   209
            self._footerpresent = 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   210
        return self._pyfile.getvalue()
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   211
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   212
    def __getattr__(self, name) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   213
        if name == "beginPath" :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   214
            return self.PathObject(self)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   215
        elif name == "beginText" :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   216
            return self.TextObject(self)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   217
        else :    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   218
            return self.Action(self, name)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   219
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   220
    def _PyWrite(self, pycode) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   221
        self._pyfile.write("%s\n" % pycode)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   222
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   223
if __name__ == '__main__':
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   224
    print 'For test scripts, look in reportlab/test'