reportlab/pdfgen/pycanvas.py
author andy_robinson
Wed, 02 Oct 2002 07:01:01 +0000
changeset 1739 9ee6c04932fa
parent 1738 64f2ccdb91b9
child 1740 71f51fea9301
permissions -rw-r--r--
Jerome's latest version, refactored he says
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
     1
# a Pythonesque Canvas v0.4
1738
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
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
    98
# global nesting level
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    99
_in = 0
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   100
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   101
class PDFAction :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   102
    def __init__(self, parent, action) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   103
        self._parent = parent
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   104
        self._action = action
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   105
    
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   106
    def __getattr__(self, name) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   107
        return getattr(getattr(self._parent._object, self._action), name)
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   108
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   109
    def __call__(self, *args, **kwargs) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   110
        global _in
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   111
        if not _in :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   112
            self._parent._parent._PyWrite("    %s.%s(%s)" % (self._parent._name, self._action, apply(buildargs, args, kwargs)))
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   113
        _in += 1
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   114
        retcode = apply(getattr(self._parent._object, self._action), args, kwargs)
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   115
        _in -= 1
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   116
        return retcode
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   117
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   118
class PDFObject :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   119
    def __init__(self, parent) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   120
        self._parent = parent
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   121
        self._initdone = 0
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   122
    
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   123
    def __getattr__(self, name) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   124
        return PDFAction(self, name)
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   125
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   126
    def __repr__(self) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   127
        return self._name
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   128
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   129
    def __call__(self, *args, **kwargs) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   130
        if not self._initdone :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   131
            methodname = apply(self._postinit, args, kwargs)
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   132
            self._parent._PyWrite("    %s = %s.%s(%s)" % (self._name, self._parent._name, methodname, apply(buildargs, args, kwargs)))
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   133
            self._initdone = 1
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   134
        return self
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   135
    
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   136
class Canvas :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   137
    class TextObject(PDFObject) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   138
        _name = "t"
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   139
        def _postinit(self, *args, **kwargs) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   140
            self._object = apply(textobject.PDFTextObject, (self._parent, ) + args, kwargs)
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   141
            return "beginText"
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   142
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   143
    class PathObject(PDFObject) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   144
        _name = "p"
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   145
        def _postinit(self, *args, **kwargs) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   146
            self._object = apply(pathobject.PDFPathObject, args, kwargs)
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   147
            return "beginPath"
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   148
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   149
    class Action(PDFAction) :
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   150
        def __call__(self, *args, **kwargs) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   151
            global _in
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   152
            try :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   153
                # print "canvas [%s] : %i" % (self._action, _in)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   154
                if (not _in) and (self._action != "__nonzero__") :
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   155
                    self._parent._PyWrite("    %s.%s(%s)" % (self._parent._name, self._action, apply(buildargs, args, kwargs)))
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   156
                _in += 1    
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   157
                retcode = apply(getattr(self._parent._object, self._action), args, kwargs)
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   158
                _in -= 1    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   159
                return retcode
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   160
            except AttributeError :    # __nonzero__, but I don't know why
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   161
                _in -= 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   162
                return 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   163
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   164
    _name = "c"
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   165
    def __init__(self, *args, **kwargs) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   166
        self._footerpresent = 0
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   167
        self._object = apply(canvas.Canvas, args, kwargs)
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   168
        self._pyfile = cStringIO.StringIO()
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   169
        self._PyWrite(PyHeader)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   170
        try :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   171
            del kwargs["filename"]
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   172
        except KeyError :    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   173
            pass
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   174
        self._PyWrite("    %s = pycanvas.Canvas(file, %s)" % (self._name, apply(buildargs, args[1:], kwargs)))
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   175
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   176
    def __str__(self) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   177
        if not self._footerpresent :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   178
            self._PyWrite(PyFooter)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   179
            self._footerpresent = 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   180
        return self._pyfile.getvalue()
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   181
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   182
    def __getattr__(self, name) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   183
        if name == "beginPath" :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   184
            return self.PathObject(self)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   185
        elif name == "beginText" :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   186
            return self.TextObject(self)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   187
        else :    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   188
            return self.Action(self, name)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   189
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   190
    def _PyWrite(self, pycode) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   191
        self._pyfile.write("%s\n" % pycode)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   192
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   193
if __name__ == '__main__':
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   194
    print 'For test scripts, look in reportlab/test'