src/reportlab/pdfbase/pdfpattern.py
author robin
Tue, 19 Nov 2013 16:30:03 +0000
branchpy33
changeset 3800 e8547b00eb59
parent 3794 398ea04239b5
child 4001 504e9ffb1e3c
permissions -rw-r--r--
attempt to remove old type testing
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
class PDFPattern:
3317
7cc62d990f32 pdfform/pdfpattern.py: add __PDFObject__ to classes (patch from Arnaud Fontaine <arnaud@andesi.org>
rgbecker
parents: 3032
diff changeset
     5
    __PDFObject__ = True
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     6
    __RefOnly__ = 1
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     7
    def __init__(self, pattern_sequence, **keywordargs):
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     8
        """
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     9
        Description of a kind of PDF object using a pattern.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1660
diff changeset
    10
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    11
        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
    12
        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
    13
        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
    14
        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
    15
        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
    16
        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
    17
        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
    18
        "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
    19
        """
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    20
        self.pattern = pattern_sequence
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    21
        self.arguments = keywordargs
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    22
        for x in pattern_sequence:
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    23
            if not isinstance(x,str) and not hasattr(x,'format'):
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    24
                if len(x)!=1:
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    25
                    raise ValueError("sequence elts must be strings or singletons containing strings: "+ascii(x))
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    26
                if not isinstance(x[0],str):
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    27
                    raise ValueError("Singletons must contain strings or instances only: "+ascii(x[0]))
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    28
    def __setitem__(self, item, value):
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    29
        self.arguments[item] = value
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    30
    def __getitem__(self, item):
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    31
        return self.arguments[item]
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    32
    def format(self, document):
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    33
        L = []
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    34
        arguments = self.arguments
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    35
        for x in self.pattern:
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    36
            if isinstance(x,str):
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    37
                L.append(x)
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    38
            elif hasattr(x,'format'):
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    39
                L.append(x.format(document) )
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    40
            else:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    41
                name = x[0]
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    42
                value = arguments.get(name, None)
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    43
                if value is None:
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    44
                    raise ValueError("%s value not defined" % ascii(name))
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    45
                if isinstance(value,str):
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    46
                    L.append(value)
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    47
                else:
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    48
                    L.append(value.formatformat(document))
3794
398ea04239b5 string module usage minimization
robin
parents: 3721
diff changeset
    49
        return "".join(L)
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    50
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1660
diff changeset
    51