src/reportlab/pdfbase/pdfpattern.py
author rgbecker
Wed, 03 Sep 2008 16:10:51 +0000
changeset 2964 32352db0d71e
parent 1683 reportlab/pdfbase/pdfpattern.py@7fa753e4420a
child 3032 22224b1b4d24
permissions -rw-r--r--
reportlab-2.2: second stage of major re-org
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1660
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     1
"""
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     2
helper for importing pdf structures into a ReportLab generated document
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     3
"""
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     4
from reportlab.pdfbase.pdfdoc import format
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     5
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     6
import string
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     7
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
     8
class PDFPattern:
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:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    30
                    raise ValueError, "sequence elts must be strings or singletons containing strings: "+repr(x)
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:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    32
                    raise ValueError, "Singletons must contain strings or instances only: "+repr(x[0])
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:
c93476060b9d simple support for acrobat forms in reportlab documents. Minor optimizations to metrics
aaron_watters
parents:
diff changeset
    51
                    raise ValueError, "%s value not defined" % repr(name)
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