src/reportlab/pdfbase/pdfpattern.py
author rptlab
Tue, 30 Apr 2013 14:20:22 +0100
branchpy33
changeset 3721 0c93dd8ff567
parent 3317 7cc62d990f32
child 3794 398ea04239b5
permissions -rw-r--r--
initial changes from 2to3-3.3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3032
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 2964
diff changeset
     1
__doc__="""helper for importing pdf structures into a ReportLab generated document
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     2
"""
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     3
from reportlab.pdfbase.pdfdoc import format
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     4
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     5
import string
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     6
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     7
class PDFPattern:
3317
7cc62d990f32 pdfform/pdfpattern.py: add __PDFObject__ to classes (patch from Arnaud Fontaine <arnaud@andesi.org>
rgbecker
parents: 3032
diff changeset
     8
    __PDFObject__ = True
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     9
    __RefOnly__ = 1
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    10
    def __init__(self, pattern_sequence, **keywordargs):
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    11
        """
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    12
        Description of a kind of PDF object using a pattern.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1660
diff changeset
    13
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    14
        Pattern sequence should contain strings or singletons of form [string].
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    15
        Strings are literal strings to be used in the object.
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    16
        Singletons are names of keyword arguments to include.
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    17
        Keyword arguments can be non-instances which are substituted directly in string conversion,
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    18
        or they can be object instances in which case they should be pdfdoc.* style
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    19
        objects with a x.format(doc) method.
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    20
        Keyword arguments may be set on initialization or subsequently using __setitem__, before format.
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    21
        "constant object" instances can also be inserted in the patterns.
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    22
        """
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    23
        self.pattern = pattern_sequence
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    24
        self.arguments = keywordargs
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    25
        from types import StringType, InstanceType
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    26
        toptypes = (StringType, InstanceType)
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    27
        for x in pattern_sequence:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    28
            if type(x) not in toptypes:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    29
                if len(x)!=1:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3317
diff changeset
    30
                    raise ValueError("sequence elts must be strings or singletons containing strings: "+repr(x))
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    31
                if type(x[0]) is not StringType:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3317
diff changeset
    32
                    raise ValueError("Singletons must contain strings or instances only: "+repr(x[0]))
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    33
    def __setitem__(self, item, value):
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    34
        self.arguments[item] = value
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    35
    def __getitem__(self, item):
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    36
        return self.arguments[item]
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    37
    def format(self, document):
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    38
        L = []
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    39
        arguments = self.arguments
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    40
        from types import StringType, InstanceType
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    41
        for x in self.pattern:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    42
            tx = type(x)
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    43
            if tx is StringType:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    44
                L.append(x)
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    45
            elif tx is InstanceType:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    46
                L.append( x.format(document) )
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    47
            else:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    48
                name = x[0]
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    49
                value = arguments.get(name, None)
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    50
                if value is None:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3317
diff changeset
    51
                    raise ValueError("%s value not defined" % repr(name))
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    52
                if type(value) is InstanceType:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    53
                    #L.append( value.format(document) )
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    54
                    L.append(format(value, document))
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    55
                else:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    56
                    L.append( str(value) )
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    57
        return string.join(L, "")
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    58
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1660
diff changeset
    59