src/reportlab/pdfgen/pycanvas.py
branchpy33
changeset 3721 0c93dd8ff567
parent 3443 64d1fba94908
child 3723 99aa837b6703
equal deleted inserted replaced
3720:7a059dde5bf5 3721:0c93dd8ff567
    95     - ... Insert your own ideas here ...
    95     - ... Insert your own ideas here ...
    96 
    96 
    97     - For fun because you can do it !
    97     - For fun because you can do it !
    98 """
    98 """
    99 
    99 
   100 import cStringIO
   100 import io
   101 from reportlab.pdfgen import canvas
   101 from reportlab.pdfgen import canvas
   102 from reportlab.pdfgen import pathobject
   102 from reportlab.pdfgen import pathobject
   103 from reportlab.pdfgen import textobject
   103 from reportlab.pdfgen import textobject
   104 
   104 
   105 PyHeader = '''#! /usr/bin/env python
   105 PyHeader = '''#! /usr/bin/env python
   147 def buildargs(*args, **kwargs) :
   147 def buildargs(*args, **kwargs) :
   148     """Constructs a printable list of arguments suitable for use in source function calls."""
   148     """Constructs a printable list of arguments suitable for use in source function calls."""
   149     arguments = ""
   149     arguments = ""
   150     for arg in args :
   150     for arg in args :
   151         arguments = arguments + ("%s, " % repr(arg))
   151         arguments = arguments + ("%s, " % repr(arg))
   152     for (kw, val) in kwargs.items() :
   152     for (kw, val) in list(kwargs.items()) :
   153         arguments = arguments+ ("%s=%s, " % (kw, repr(val)))
   153         arguments = arguments+ ("%s=%s, " % (kw, repr(val)))
   154     if arguments[-2:] == ", " :
   154     if arguments[-2:] == ", " :
   155         arguments = arguments[:-2]
   155         arguments = arguments[:-2]
   156     return arguments
   156     return arguments
   157 
   157 
   272         self._pagenumber = 1
   272         self._pagenumber = 1
   273         self._formnumber = 0
   273         self._formnumber = 0
   274         self._footerpresent = 0
   274         self._footerpresent = 0
   275         self._object = canvas.Canvas(*args,**kwargs)
   275         self._object = canvas.Canvas(*args,**kwargs)
   276         self._enforceColorSpace = self._object._enforceColorSpace
   276         self._enforceColorSpace = self._object._enforceColorSpace
   277         self._pyfile = cStringIO.StringIO()
   277         self._pyfile = io.StringIO()
   278         self._PyWrite(PyHeader)
   278         self._PyWrite(PyHeader)
   279         try :
   279         try :
   280             del kwargs["filename"]
   280             del kwargs["filename"]
   281         except KeyError :
   281         except KeyError :
   282             pass
   282             pass
   283         self._PyWrite("    # create the PDF document\n    %s = Canvas(file, %s)\n\n    # Begins page 1" % (self._name, buildargs(*args[1:], **kwargs)))
   283         self._PyWrite("    # create the PDF document\n    %s = Canvas(file, %s)\n\n    # Begins page 1" % (self._name, buildargs(*args[1:], **kwargs)))
   284 
   284 
   285     def __nonzero__(self) :
   285     def __bool__(self) :
   286         """This is needed by platypus' tables."""
   286         """This is needed by platypus' tables."""
   287         return 1
   287         return 1
   288 
   288 
   289     def __str__(self) :
   289     def __str__(self) :
   290         """Returns the equivalent Python source code."""
   290         """Returns the equivalent Python source code."""
   305     def _PyWrite(self, pycode) :
   305     def _PyWrite(self, pycode) :
   306         """Outputs the source code with a trailing newline."""
   306         """Outputs the source code with a trailing newline."""
   307         self._pyfile.write("%s\n" % pycode)
   307         self._pyfile.write("%s\n" % pycode)
   308 
   308 
   309 if __name__ == '__main__':
   309 if __name__ == '__main__':
   310     print 'For test scripts, look in tests'
   310     print('For test scripts, look in tests')