"Fossies" - the Fresh Open Source Software Archive

Member "reportlab-3.5.32/src/reportlab/pdfbase/pdfpattern.py" (1 Oct 2019, 3799 Bytes) of package /linux/privat/reportlab-3.5.32.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "pdfpattern.py" see the Fossies "Dox" file reference documentation.

    1 __doc__="""helper for importing pdf structures into a ReportLab generated document
    2 """
    3 from reportlab.pdfbase.pdfdoc import format, PDFObject, pdfdocEnc
    4 from reportlab import ascii
    5 from reportlab.lib.utils import strTypes
    6 
    7 def _patternSequenceCheck(pattern_sequence):
    8     allowedTypes = strTypes if isinstance(strTypes, tuple) else (strTypes,)
    9     allowedTypes = allowedTypes + (PDFObject,PDFPatternIf)
   10     for x in pattern_sequence:
   11         if not isinstance(x,allowedTypes):
   12             if len(x)!=1:
   13                 raise ValueError("sequence elts must be strings/bytes/PDFPatternIfs or singletons containing strings: "+ascii(x))
   14             if not isinstance(x[0],strTypes):
   15                 raise ValueError("Singletons must contain strings/bytes or PDFObject instances only: "+ascii(x[0]))
   16 
   17 class PDFPattern(PDFObject):
   18     __RefOnly__ = 1
   19     def __init__(self, pattern_sequence, **keywordargs):
   20         """
   21         Description of a kind of PDF object using a pattern.
   22 
   23         Pattern sequence should contain strings, singletons of form [string] or
   24         PDFPatternIf objects.
   25         Strings are literal strings to be used in the object.
   26         Singletons are names of keyword arguments to include.
   27         PDFpatternIf objects allow some conditionality.
   28         Keyword arguments can be non-instances which are substituted directly in string conversion,
   29         or they can be object instances in which case they should be pdfdoc.* style
   30         objects with a x.format(doc) method.
   31         Keyword arguments may be set on initialization or subsequently using __setitem__, before format.
   32         "constant object" instances can also be inserted in the patterns.
   33         """
   34         _patternSequenceCheck(pattern_sequence)
   35         self.pattern = pattern_sequence
   36         self.arguments = keywordargs
   37 
   38     def __setitem__(self, item, value):
   39         self.arguments[item] = value
   40 
   41     def __getitem__(self, item):
   42         return self.arguments[item]
   43 
   44     def eval(self,L):
   45         arguments = self.arguments
   46         document = self.__document
   47         for x in L:
   48             if isinstance(x,strTypes):
   49                 yield pdfdocEnc(x)
   50             elif isinstance(x,PDFObject):
   51                 yield x.format(document)
   52             elif isinstance(x,PDFPatternIf):
   53                 result = list(self.eval(x.cond))
   54                 cond = result and result[0]
   55                 for z in self.eval(x.thenPart if cond else x.elsePart):
   56                     yield z
   57             else:
   58                 name = x[0]
   59                 value = arguments.get(name, None)
   60                 if value is None:
   61                     raise ValueError("%s value not defined" % ascii(name))
   62                 if isinstance(value,PDFObject):
   63                     yield format(value,document)
   64                 elif isinstance(value,strTypes):
   65                     yield pdfdocEnc(value)
   66                 else:
   67                     yield pdfdocEnc(str(value))
   68 
   69     def format(self, document):
   70         self.__document = document
   71         try:
   72             return b"".join(self.eval(self.pattern))
   73         finally:
   74             del self.__document
   75 
   76     def clone(self):
   77         c = object.__new__(self.__class__)
   78         c.pattern = self.pattern
   79         c.arguments = self.arguments
   80         return c
   81 
   82 class PDFPatternIf(object):
   83     '''cond will be evaluated as [cond] in PDFpattern eval.
   84     It should evaluate to a list with value 0/1 etc etc.
   85     thenPart is a list to be evaluated if the cond evaulates true,
   86     elsePart is the false sequence.
   87     '''
   88     def __init__(self,cond,thenPart=[],elsePart=[]):
   89         if not isinstance(cond,list): cond = [cond]
   90         for x in cond, thenPart, elsePart:
   91             _patternSequenceCheck(x)
   92         self.cond = cond
   93         self.thenPart = thenPart
   94         self.elsePart = elsePart