src/reportlab/lib/utils.py
author rgbecker
Tue, 17 Feb 2009 17:32:00 +0000
branchjython25
changeset 3119 b9e540d1267a
parent 3083 95f7752b0284
child 3123 e53425121cc4
permissions -rw-r--r--
rl-jython25: jython support for isSourceDistro
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2625
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents: 2565
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2006
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 474
diff changeset
     2
#see license.txt for license details
2625
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents: 2565
diff changeset
     3
# $URI:$
2327
c40a1b477f3f Forgot to check the exactly equal case for _startswith_rl
rgbecker
parents: 2313
diff changeset
     4
__version__=''' $Id$ '''
3029
eded59f94021 adding docstrings to lib
andy
parents: 3028
diff changeset
     5
__doc__='''Gazillions of miscellaneous internal utility functions'''
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 519
diff changeset
     6
2952
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
     7
import string, os, sys, imp, time
3119
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
     8
jython = sys.platform[0:4] == 'java'
2952
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
     9
try:
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
    10
    from hashlib import md5
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
    11
except:
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
    12
    from md5 import md5
2499
11bc8441ec51 legends.py: fix 2.1 compatibility problem
rgbecker
parents: 2497
diff changeset
    13
from reportlab.lib.logger import warnOnce
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    14
from types import *
2625
71abbda1f58c reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents: 2565
diff changeset
    15
from rltempfile import get_rl_tempfile, get_rl_tempdir, _rl_getuid
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    16
SeqTypes = (ListType,TupleType)
2499
11bc8441ec51 legends.py: fix 2.1 compatibility problem
rgbecker
parents: 2497
diff changeset
    17
if sys.hexversion<0x2020000:
11bc8441ec51 legends.py: fix 2.1 compatibility problem
rgbecker
parents: 2497
diff changeset
    18
    def isSeqType(v):
2502
ac49318db2d9 utils.py: fix argument reference
rgbecker
parents: 2499
diff changeset
    19
        return type(v) in SeqTypes
2499
11bc8441ec51 legends.py: fix 2.1 compatibility problem
rgbecker
parents: 2497
diff changeset
    20
else:
11bc8441ec51 legends.py: fix 2.1 compatibility problem
rgbecker
parents: 2497
diff changeset
    21
    def isSeqType(v):
11bc8441ec51 legends.py: fix 2.1 compatibility problem
rgbecker
parents: 2497
diff changeset
    22
        return isinstance(v,(tuple,list))
11bc8441ec51 legends.py: fix 2.1 compatibility problem
rgbecker
parents: 2497
diff changeset
    23
2259
d2738e86e6f6 True/False aren't defined for Python < 2.3
rgbecker
parents: 2258
diff changeset
    24
if sys.hexversion<0x2030000:
d2738e86e6f6 True/False aren't defined for Python < 2.3
rgbecker
parents: 2258
diff changeset
    25
    True = 1
d2738e86e6f6 True/False aren't defined for Python < 2.3
rgbecker
parents: 2258
diff changeset
    26
    False = 0
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 519
diff changeset
    27
2902
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
    28
if sys.hexversion >= 0x02000000:
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
    29
    def _digester(s):
2952
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
    30
        return md5(s).hexdigest()
2902
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
    31
else:
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
    32
    # hexdigest not available in 1.5
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
    33
    def _digester(s):
2952
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
    34
        return join(map(lambda x : "%02x" % ord(x), md5(s).digest()), '')
2902
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
    35
2215
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    36
def _findFiles(dirList,ext='.ttf'):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    37
    from os.path import isfile, isdir, join as path_join
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    38
    from os import listdir
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    39
    ext = ext.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    40
    R = []
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    41
    A = R.append
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    42
    for D in dirList:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    43
        if not isdir(D): continue
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    44
        for fn in listdir(D):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    45
            fn = path_join(D,fn)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    46
            if isfile(fn) and (not ext or fn.lower().endswith(ext)): A(fn)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    47
    return R
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    48
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    49
try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    50
    _UserDict = dict
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    51
except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    52
    from UserDict import UserDict as _UserDict
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    53
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    54
class CIDict(_UserDict):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    55
    def __init__(self,*a,**kw):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    56
        map(self.update, a)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    57
        self.update(kw)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    58
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    59
    def update(self,D):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    60
        for k,v in D.items(): self[k] = v
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    61
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    62
    def __setitem__(self,k,v):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    63
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    64
            k = k.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    65
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    66
            pass
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    67
        _UserDict.__setitem__(self,k,v)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    68
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    69
    def __getitem__(self,k):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    70
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    71
            k = k.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    72
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    73
            pass
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    74
        return _UserDict.__getitem__(self,k)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    75
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    76
    def __delitem__(self,k):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    77
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    78
            k = k.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    79
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    80
            pass
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    81
        return _UserDict.__delitem__(self,k)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    82
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    83
    def get(self,k,dv=None):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    84
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    85
            return self[k]
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    86
        except KeyError:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    87
            return dv
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    88
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    89
    def has_key(self,k):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    90
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    91
            self[k]
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    92
            return True
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    93
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    94
            return False
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    95
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    96
    def pop(self,k,*a):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    97
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    98
            k = k.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    99
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
   100
            pass
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
   101
        return _UserDict.pop(*((self,k)+a))
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
   102
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
   103
    def setdefault(self,k,*a):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
   104
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
   105
            k = k.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
   106
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
   107
            pass
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
   108
        return _UserDict.setdefault(*((self,k)+a))
1837
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
   109
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   110
if os.name == 'mac':
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   111
    #with the Mac, we need to tag the file in a special
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   112
    #way so the system knows it is a PDF file.
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   113
    #This supplied by Joe Strout
2057
22e7a13fa031 Fixed so that file creator and types are now correctly set on the Mac again
rptlab
parents: 2053
diff changeset
   114
    import macfs, macostools
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   115
    _KNOWN_MAC_EXT = {
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   116
        'BMP' : ('ogle','BMP '),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   117
        'EPS' : ('ogle','EPSF'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   118
        'EPSF': ('ogle','EPSF'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   119
        'GIF' : ('ogle','GIFf'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   120
        'JPG' : ('ogle','JPEG'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   121
        'JPEG': ('ogle','JPEG'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   122
        'PCT' : ('ttxt','PICT'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   123
        'PICT': ('ttxt','PICT'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   124
        'PNG' : ('ogle','PNGf'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   125
        'PPM' : ('ogle','.PPM'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   126
        'TIF' : ('ogle','TIFF'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   127
        'TIFF': ('ogle','TIFF'),
2051
50350756e12c Added HTML to the Mac markfilename routine - HTML files should now 'know'
johnprecedo
parents: 2045
diff changeset
   128
        'PDF' : ('CARO','PDF '),
2057
22e7a13fa031 Fixed so that file creator and types are now correctly set on the Mac again
rptlab
parents: 2053
diff changeset
   129
        'HTML': ('MSIE','TEXT'),
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   130
        }
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   131
    def markfilename(filename,creatorcode=None,filetype=None,ext='PDF'):
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   132
        try:
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   133
            if creatorcode is None or filetype is None and ext is not None:
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   134
                try:
2057
22e7a13fa031 Fixed so that file creator and types are now correctly set on the Mac again
rptlab
parents: 2053
diff changeset
   135
                    creatorcode, filetype = _KNOWN_MAC_EXT[string.upper(ext)]
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   136
                except:
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   137
                    return
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   138
            macfs.FSSpec(filename).SetCreatorType(creatorcode,filetype)
2051
50350756e12c Added HTML to the Mac markfilename routine - HTML files should now 'know'
johnprecedo
parents: 2045
diff changeset
   139
            macostools.touched(filename)
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   140
        except:
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   141
            pass
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   142
else:
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   143
    def markfilename(filename,creatorcode=None,filetype=None):
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   144
        pass
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   145
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   146
import reportlab
2251
afa4604d1a15 We prefer absolute RL DIR
rgbecker
parents: 2247
diff changeset
   147
__RL_DIR=os.path.dirname(reportlab.__file__)    #possibly relative
afa4604d1a15 We prefer absolute RL DIR
rgbecker
parents: 2247
diff changeset
   148
_RL_DIR=os.path.isabs(__RL_DIR) and __RL_DIR or os.path.abspath(__RL_DIR)
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   149
del reportlab
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   150
2225
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   151
#Attempt to detect if this copy of reportlab is running in a
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   152
#file system (as opposed to mostly running in a zip or McMillan
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   153
#archive or Jar file).  This is used by test cases, so that
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   154
#we can write test cases that don't get activated in a compiled
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   155
try:
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   156
    __file__
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   157
except:
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   158
    __file__ = sys.argv[0]
2305
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   159
import glob, fnmatch
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   160
try:
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   161
    _isFSD = not __loader__
2484
590938600027 utils.py: use normpath in _archive etc etc
rgbecker
parents: 2482
diff changeset
   162
    _archive = os.path.normcase(os.path.normpath(__loader__.archive))
2327
c40a1b477f3f Forgot to check the exactly equal case for _startswith_rl
rgbecker
parents: 2313
diff changeset
   163
    _archivepfx = _archive + os.sep
2484
590938600027 utils.py: use normpath in _archive etc etc
rgbecker
parents: 2482
diff changeset
   164
    _archivedir = os.path.dirname(_archive)
2327
c40a1b477f3f Forgot to check the exactly equal case for _startswith_rl
rgbecker
parents: 2313
diff changeset
   165
    _archivedirpfx = _archivedir + os.sep
2305
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   166
    _archivepfxlen = len(_archivepfx)
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   167
    _archivedirpfxlen = len(_archivedirpfx)
2352
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   168
    def __startswith_rl(fn,
2484
590938600027 utils.py: use normpath in _archive etc etc
rgbecker
parents: 2482
diff changeset
   169
                    _archivepfx=_archivepfx,
590938600027 utils.py: use normpath in _archive etc etc
rgbecker
parents: 2482
diff changeset
   170
                    _archivedirpfx=_archivedirpfx,
590938600027 utils.py: use normpath in _archive etc etc
rgbecker
parents: 2482
diff changeset
   171
                    _archive=_archive,
590938600027 utils.py: use normpath in _archive etc etc
rgbecker
parents: 2482
diff changeset
   172
                    _archivedir=_archivedir,
2352
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   173
                    os_path_normpath=os.path.normpath,
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   174
                    os_path_normcase=os.path.normcase,
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   175
                    os_getcwd=os.getcwd,
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   176
                    os_sep=os.sep,
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   177
                    os_sep_len = len(os.sep)):
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   178
        '''if the name starts with a known prefix strip it off'''
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   179
        fn = os_path_normpath(fn.replace('/',os_sep))
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   180
        nfn = os_path_normcase(fn)
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   181
        if nfn in (_archivedir,_archive): return 1,''
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   182
        if nfn.startswith(_archivepfx): return 1,fn[_archivepfxlen:]
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   183
        if nfn.startswith(_archivedirpfx): return 1,fn[_archivedirpfxlen:]
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   184
        cwd = os_path_normcase(os_getcwd())
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   185
        n = len(cwd)
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   186
        if nfn.startswith(cwd):
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   187
            if fn[n:].startswith(os_sep): return 1, fn[n+os_sep_len:]
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   188
            if n==len(fn): return 1,''
c8bf2490acc9 Another know location
rgbecker
parents: 2340
diff changeset
   189
        return not os.path.isabs(fn),fn
2305
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   190
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   191
    def _startswith_rl(fn):
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   192
        return __startswith_rl(fn)[1]
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   193
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   194
    def rl_glob(pattern,glob=glob.glob,fnmatch=fnmatch.fnmatch, _RL_DIR=_RL_DIR,pjoin=os.path.join):
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   195
        c, pfn = __startswith_rl(pattern)
2307
a1f106a7bed8 More compact distro changes
rgbecker
parents: 2305
diff changeset
   196
        r = glob(pfn)
2305
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   197
        if c or r==[]:
2307
a1f106a7bed8 More compact distro changes
rgbecker
parents: 2305
diff changeset
   198
            r += map(lambda x,D=_archivepfx,pjoin=pjoin: pjoin(_archivepfx,x),filter(lambda x,pfn=pfn,fnmatch=fnmatch: fnmatch(x,pfn),__loader__._files.keys()))
2305
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   199
        return r
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   200
except:
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   201
    _isFSD = os.path.isfile(__file__)   #slight risk of wrong path
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   202
    __loader__ = None
2305
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   203
    def _startswith_rl(fn):
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   204
        return fn
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   205
    def rl_glob(pattern,glob=glob.glob):
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   206
        return glob(pattern)
3dbfbe55e9ff Use more advanced __loader__ friendly code
rgbecker
parents: 2302
diff changeset
   207
del glob, fnmatch
3119
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   208
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   209
def _getPyFn(fn):
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   210
    if jython and fn.endswith('$py.class'):
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   211
        fn = fn[:-9]
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   212
    else:
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   213
        fn = os.path.splitext(__file__)[0]
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   214
    return fn+'.py'
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   215
_isFSSD = _isFSD and os.path.isfile(_getPyFn(__file__))
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   216
1837
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
   217
def isFileSystemDistro():
2225
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   218
    '''return truth if a file system distribution'''
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   219
    return _isFSD
1837
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
   220
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
   221
def isCompactDistro():
2225
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   222
    '''return truth if not a file system distribution'''
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   223
    return not _isFSD
1837
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
   224
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
   225
def isSourceDistro():
2225
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   226
    '''return truth if a source file system distribution'''
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   227
    return _isFSSD
1143
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
   228
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
   229
try:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   230
    #raise ImportError
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   231
    ### NOTE!  FP_STR SHOULD PROBABLY ALWAYS DO A PYTHON STR() CONVERSION ON ARGS
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   232
    ### IN CASE THEY ARE "LAZY OBJECTS".  ACCELLERATOR DOESN'T DO THIS (YET)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   233
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   234
        from _rl_accel import fp_str                # in case of builtin version
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   235
    except ImportError:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   236
        from reportlab.lib._rl_accel import fp_str  # specific
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   237
except ImportError:
2139
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   238
    from math import log
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   239
    _log_10 = lambda x,log=log,_log_e_10=log(10.0): log(x)/_log_e_10
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   240
    _fp_fmts = "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f", "%.6f"
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   241
    import re
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   242
    _tz_re = re.compile('0+$')
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   243
    del re
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   244
    def fp_str(*a):
2499
11bc8441ec51 legends.py: fix 2.1 compatibility problem
rgbecker
parents: 2497
diff changeset
   245
        if len(a)==1 and isSeqType(a[0]): a = a[0]
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   246
        s = []
2139
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   247
        A = s.append
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   248
        for i in a:
2139
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   249
            sa =abs(i)
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   250
            if sa<=1e-7: A('0')
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   251
            else:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   252
                l = sa<=1 and 6 or min(max(0,(6-int(_log_10(sa)))),6)
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   253
                n = _fp_fmts[l]%i
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   254
                if l:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   255
                    n = _tz_re.sub('',n)
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   256
                    try:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   257
                        if n[-1]=='.': n = n[:-1]
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   258
                    except:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   259
                        print i, n
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   260
                        raise
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   261
                A((n[0]!='0' or len(n)==1) and n or n[1:])
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   262
        return string.join(s)
448
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   263
981
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
   264
#hack test for comma users
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
   265
if ',' in fp_str(0.25):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   266
    _FP_STR = fp_str
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   267
    def fp_str(*a):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   268
        return string.replace(apply(_FP_STR,a),',','.')
981
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
   269
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   270
def recursiveImport(modulename, baseDir=None, noCWD=0, debug=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   271
    """Dynamically imports possible packagized module, or raises ImportError"""
2160
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   272
    normalize = lambda x: os.path.normcase(os.path.abspath(os.path.normpath(x)))
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   273
    path = map(normalize,sys.path)
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   274
    if baseDir:
2499
11bc8441ec51 legends.py: fix 2.1 compatibility problem
rgbecker
parents: 2497
diff changeset
   275
        if not isSeqType(baseDir):
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   276
            tp = [baseDir]
1833
135322abc191 Fix recursivImport for case when baseDir is a sequence
rgbecker
parents: 1821
diff changeset
   277
        else:
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   278
            tp = filter(None,list(baseDir))
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   279
        for p in tp:
2160
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   280
            p = normalize(p)
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   281
            if p not in path: path.insert(0,p)
1396
40d1361f08b7 Enhanced the error message from recursiveImport
andy_robinson
parents: 1389
diff changeset
   282
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   283
    if noCWD:
2160
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   284
        for p in ('','.',normalize('.')):
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   285
            while p in path:
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   286
                if debug: print 'removed "%s" from path' % p
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   287
                path.remove(p)
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   288
    elif '.' not in path:
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   289
            path.insert(0,'.')
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   290
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   291
    if debug:
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   292
        import pprint
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   293
        pp = pprint.pprint
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   294
        print 'path=',
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   295
        pp(path)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2160
diff changeset
   296
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   297
    #make import errors a bit more informative
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   298
    opath = sys.path
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   299
    try:
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   300
        sys.path = path
2160
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   301
        exec 'import %s\nm = %s\n' % (modulename,modulename) in locals()
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   302
        sys.path = opath
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   303
        return m
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   304
    except ImportError:
2160
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   305
        sys.path = opath
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   306
        msg = "recursiveimport(%s,baseDir=%s) failed" % (modulename,baseDir)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   307
        if baseDir:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   308
            msg = msg + " under paths '%s'" % `path`
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   309
        raise ImportError, msg
1396
40d1361f08b7 Enhanced the error message from recursiveImport
andy_robinson
parents: 1389
diff changeset
   310
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   311
def recursiveGetAttr(obj, name):
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   312
    "Can call down into e.g. object1.object2[4].attr"
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   313
    return eval(name, obj.__dict__)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   314
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   315
def recursiveSetAttr(obj, name, value):
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   316
    "Can call down into e.g. object1.object2[4].attr = value"
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   317
    #get the thing above last.
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   318
    tokens = string.split(name, '.')
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   319
    if len(tokens) == 1:
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   320
        setattr(obj, name, value)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2160
diff changeset
   321
    else:
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   322
        most = string.join(tokens[:-1], '.')
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   323
        last = tokens[-1]
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   324
        parent = recursiveGetAttr(obj, most)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   325
        setattr(parent, last, value)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   326
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
   327
def import_zlib():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   328
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   329
        import zlib
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   330
    except ImportError:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   331
        zlib = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   332
        from reportlab.rl_config import ZLIB_WARNINGS
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   333
        if ZLIB_WARNINGS: warnOnce('zlib not available')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   334
    return zlib
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
   335
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   336
# Image Capability Detection.  Set a flag haveImages
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   337
# to tell us if either PIL or Java imaging libraries present.
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   338
# define PIL_Image as either None, or an alias for the PIL.Image
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   339
# module, as there are 2 ways to import it
3119
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   340
if jython:
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   341
    try:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   342
        import javax.imageio
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   343
        import java.awt.image
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   344
        haveImages = 1
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   345
    except:
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   346
        haveImages = 0
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   347
else:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   348
    try:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   349
        from PIL import Image
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   350
    except ImportError:
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   351
        try:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   352
            import Image
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   353
        except ImportError:
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   354
            Image = None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   355
    haveImages = Image is not None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   356
    if haveImages: del Image
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   357
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   358
try:
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   359
    from cStringIO import StringIO as __StringIO
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   360
except ImportError:
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   361
    from StringIO import StringIO as __StringIO
1580
50792f072538 use generalised StringIO instance interface
rgbecker
parents: 1575
diff changeset
   362
def getStringIO(buf=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   363
    '''unified StringIO instance interface'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   364
    return buf is not None and __StringIO(buf) or __StringIO()
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   365
_StringIOKlass=__StringIO().__class__
1580
50792f072538 use generalised StringIO instance interface
rgbecker
parents: 1575
diff changeset
   366
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   367
class ArgvDictValue:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   368
    '''A type to allow clients of getArgvDict to specify a conversion function'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   369
    def __init__(self,value,func):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   370
        self.value = value
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   371
        self.func = func
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   372
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   373
def getArgvDict(**kw):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   374
    ''' Builds a dictionary from its keyword arguments with overrides from sys.argv.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   375
        Attempts to be smart about conversions, but the value can be an instance
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   376
        of ArgDictValue to allow specifying a conversion function.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   377
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   378
    def handleValue(v,av,func):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   379
        if func:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   380
            v = func(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   381
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   382
            t = type(v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   383
            if t is StringType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   384
                v = av
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   385
            elif t is FloatType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   386
                v = float(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   387
            elif t is IntType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   388
                v = int(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   389
            elif t is ListType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   390
                v = list(eval(av))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   391
            elif t is TupleType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   392
                v = tuple(eval(av))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   393
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   394
                raise TypeError, "Can't convert string '%s' to %s" % (av,str(t))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   395
        return v
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   396
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   397
    A = sys.argv[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   398
    R = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   399
    for k, v in kw.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   400
        if isinstance(v,ArgvDictValue):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   401
            v, func = v.value, v.func
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   402
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   403
            func = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   404
        handled = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   405
        ke = k+'='
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   406
        for a in A:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   407
            if string.find(a,ke)==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   408
                av = a[len(ke):]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   409
                A.remove(a)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   410
                R[k] = handleValue(v,av,func)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   411
                handled = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   412
                break
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   413
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   414
        if not handled: R[k] = handleValue(v,v,func)
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   415
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   416
    return R
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   417
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   418
def getHyphenater(hDict=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   419
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   420
        from reportlab.lib.pyHnj import Hyphen
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   421
        if hDict is None: hDict=os.path.join(os.path.dirname(__file__),'hyphen.mashed')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   422
        return Hyphen(hDict)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   423
    except ImportError, errMsg:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   424
        if str(errMsg)!='No module named pyHnj': raise
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   425
        return None
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   426
448
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   427
def _className(self):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   428
    '''Return a shortened class name'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   429
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   430
        name = self.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   431
        i=string.rfind(name,'.')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   432
        if i>=0: return name[i+1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   433
        return name
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   434
    except AttributeError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   435
        return str(self)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   436
2309
47243845ef50 Attempt to fix open_for_read again
rgbecker
parents: 2307
diff changeset
   437
def open_for_read_by_name(name,mode='b'):
47243845ef50 Attempt to fix open_for_read again
rgbecker
parents: 2307
diff changeset
   438
    if 'r' not in mode: mode = 'r'+mode
47243845ef50 Attempt to fix open_for_read again
rgbecker
parents: 2307
diff changeset
   439
    try:
47243845ef50 Attempt to fix open_for_read again
rgbecker
parents: 2307
diff changeset
   440
        return open(name,mode)
47243845ef50 Attempt to fix open_for_read again
rgbecker
parents: 2307
diff changeset
   441
    except IOError:
47243845ef50 Attempt to fix open_for_read again
rgbecker
parents: 2307
diff changeset
   442
        if _isFSD or __loader__ is None: raise
2313
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   443
        #we have a __loader__, perhaps the filename starts with
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   444
        #the dirname(reportlab.__file__) or is relative
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   445
        name = _startswith_rl(name)
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   446
        s = __loader__.get_data(name)
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   447
        if 'b' not in mode and os.linesep!='\n': s = s.replace(os.linesep,'\n')
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   448
        return getStringIO(s)
2309
47243845ef50 Attempt to fix open_for_read again
rgbecker
parents: 2307
diff changeset
   449
2313
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   450
import urllib
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   451
def open_for_read(name,mode='b', urlopen=urllib.urlopen):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   452
    '''attempt to open a file or URL for reading'''
2229
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2227
diff changeset
   453
    if hasattr(name,'read'): return name
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   454
    try:
2313
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   455
        return open_for_read_by_name(name,mode)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   456
    except:
2313
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   457
        try:
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   458
            return getStringIO(urlopen(name).read())
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   459
        except:
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   460
            raise IOError('Cannot open resource "%s"' % name)
e823a147f0b5 Another rework of the open for read code
rgbecker
parents: 2309
diff changeset
   461
del urllib
1575
7d2360bdfc4d Added open_for_read
rgbecker
parents: 1561
diff changeset
   462
2225
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   463
def open_and_read(name,mode='b'):
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   464
    return open_for_read(name,mode).read()
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   465
2247
256cea0756a9 Added open_and_readlines
rgbecker
parents: 2244
diff changeset
   466
def open_and_readlines(name,mode='t'):
256cea0756a9 Added open_and_readlines
rgbecker
parents: 2244
diff changeset
   467
    return open_and_read(name,mode).split('\n')
256cea0756a9 Added open_and_readlines
rgbecker
parents: 2244
diff changeset
   468
2240
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   469
def rl_isfile(fn,os_path_isfile=os.path.isfile):
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   470
    if hasattr(fn,'read'): return True
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   471
    if os_path_isfile(fn): return True
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   472
    if _isFSD or __loader__ is None: return False
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   473
    fn = _startswith_rl(fn)
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   474
    return fn in __loader__._files.keys()
2240
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   475
2339
4b1dae4bd1a8 More changes related to compact distro path recognition
rgbecker
parents: 2332
diff changeset
   476
def rl_isdir(pn,os_path_isdir=os.path.isdir,os_path_normpath=os.path.normpath):
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   477
    if os_path_isdir(pn): return True
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   478
    if _isFSD or __loader__ is None: return False
2354
c20a2fe18b0e reportlab.lib.utils fix rl_isdir
rgbecker
parents: 2352
diff changeset
   479
    pn = _startswith_rl(os_path_normpath(pn))
c20a2fe18b0e reportlab.lib.utils fix rl_isdir
rgbecker
parents: 2352
diff changeset
   480
    if not pn.endswith(os.sep): pn += os.sep
2258
08e55f5390cc Remove things that python 2.1 fails on
rgbecker
parents: 2256
diff changeset
   481
    return len(filter(lambda x,pn=pn: x.startswith(pn),__loader__._files.keys()))>0
2240
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   482
2794
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   483
def rl_listdir(pn,os_path_isdir=os.path.isdir,os_path_normpath=os.path.normpath,os_listdir=os.listdir):
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   484
    if os_path_isdir(pn) or _isFSD or __loader__ is None: return os_listdir(pn)
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   485
    pn = _startswith_rl(os_path_normpath(pn))
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   486
    if not pn.endswith(os.sep): pn += os.sep
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   487
    return [x[len(pn):] for x in __loader__._files.keys() if x.startswith(pn)]
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   488
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   489
def rl_getmtime(pn,os_path_isfile=os.path.isfile,os_path_normpath=os.path.normpath,os_path_getmtime=os.path.getmtime,time_mktime=time.mktime):
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   490
    if os_path_isfile(pn) or _isFSD or __loader__ is None: return os_path_getmtime(pn)
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   491
    p = _startswith_rl(os_path_normpath(pn))
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   492
    try:
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   493
        e = __loader__._files[p]
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   494
    except KeyError:
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   495
        return os_path_getmtime(pn)
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   496
    s = e[5]
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   497
    d = e[6]
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   498
    y = ((d>>9)&0x7f)+1980
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   499
    m = (d>>5)&0xf
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   500
    d &= 0x1f
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   501
    h = (s>>11)&0xf
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   502
    m = (s>>5)&0x3f
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   503
    s &= 0x1f
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   504
    s <<= 1
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   505
    return time_mktime((y,m,d,h,m,s,0,0,0))
6539e5dc77b8 add support for fontfinder in zip distribution
rgbecker
parents: 2764
diff changeset
   506
2301
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   507
def rl_get_module(name,dir):
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   508
    if sys.modules.has_key(name):
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   509
        om = sys.modules[name]
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   510
        del sys.modules[name]
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   511
    else:
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   512
        om = None
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   513
    try:
2302
240a26f9ee8f rl_get_module now seems to work as intended
rgbecker
parents: 2301
diff changeset
   514
        f = None
2301
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   515
        try:
2302
240a26f9ee8f rl_get_module now seems to work as intended
rgbecker
parents: 2301
diff changeset
   516
            f, p, desc= imp.find_module(name,[dir])
2301
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   517
            return imp.load_module(name,f,p,desc)
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   518
        except:
2307
a1f106a7bed8 More compact distro changes
rgbecker
parents: 2305
diff changeset
   519
            if isCompactDistro():
2301
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   520
                #attempt a load from inside the zip archive
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   521
                import zipimport
2307
a1f106a7bed8 More compact distro changes
rgbecker
parents: 2305
diff changeset
   522
                dir = _startswith_rl(dir)
2356
4a71278dd82d Minor fixes to lib.utils.rl_get_module
rgbecker
parents: 2354
diff changeset
   523
                dir = (dir=='.' or not dir) and _archive or os.path.join(_archive,dir.replace('/',os.sep))
2327
c40a1b477f3f Forgot to check the exactly equal case for _startswith_rl
rgbecker
parents: 2313
diff changeset
   524
                zi = zipimport.zipimporter(dir)
2301
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   525
                return zi.load_module(name)
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   526
            raise ImportError('%s[%s]' % (name,dir))
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   527
    finally:
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   528
        if om: sys.modules[name] = om
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   529
        del om
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   530
        if f: f.close()
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   531
2487
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   532
def _isPILImage(im):
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   533
    try:
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   534
        from PIL.Image import Image
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   535
        return isinstance(im,Image)
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   536
    except ImportError:
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   537
        return 0
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   538
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   539
class ImageReader(object):
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   540
    "Wraps up either PIL or Java to get data from bitmaps"
2952
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
   541
    _cache={}
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   542
    def __init__(self, fileName):
2487
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   543
        if isinstance(fileName,ImageReader):
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   544
            self.__dict__ = fileName.__dict__   #borgize
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   545
            return
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   546
        #start wih lots of null private fields, to be populated by
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   547
        #the relevant engine.
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   548
        self.fileName = fileName
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   549
        self._image = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   550
        self._width = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   551
        self._height = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   552
        self._transparent = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   553
        self._data = None
2487
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   554
        if _isPILImage(fileName):
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   555
            self._image = fileName
2902
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
   556
            self.fp = getattr(fileName,'fp',None)
2487
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   557
            try:
2720
2c04d204766d utils.py: attempt to fix fileName from PIL images
rgbecker
parents: 2716
diff changeset
   558
                self.fileName = self._image.fileName
2487
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   559
            except AttributeError:
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   560
                self.fileName = 'PILIMAGE_%d' % id(self)
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   561
        else:
2556
d7c3c6b74053 utils.py: annotate ImageReader read errors
rgbecker
parents: 2502
diff changeset
   562
            try:
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   563
                from reportlab.rl_config import imageReaderFlags
2556
d7c3c6b74053 utils.py: annotate ImageReader read errors
rgbecker
parents: 2502
diff changeset
   564
                self.fp = open_for_read(fileName,'b')
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   565
                if isinstance(self.fp,_StringIOKlass):  imageReaderFlags=0 #avoid messing with already internal files
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   566
                if imageReaderFlags>0:  #interning
2952
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
   567
                    data = self.fp.read()
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   568
                    if imageReaderFlags&2:  #autoclose
2952
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
   569
                        try:
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
   570
                            self.fp.close()
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
   571
                        except:
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
   572
                            pass
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   573
                    if imageReaderFlags&4:  #cache the data
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   574
                        if not self._cache:
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   575
                            from rl_config import register_reset
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   576
                            register_reset(self._cache.clear)
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   577
                        data=self._cache.setdefault(md5(data).digest(),data)
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   578
                    self.fp=getStringIO(data)
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   579
                elif imageReaderFlags==-1 and isinstance(fileName,(str,unicode)):
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   580
                    #try Ralf Schmitt's re-opening technique of avoiding too many open files
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   581
                    self.fp.close()
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   582
                    del self.fp #will become a property in the next statement
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   583
                    self.__class__=LazyImageReader
2953
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   584
                if haveImages:
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   585
                    #detect which library we are using and open the image
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   586
                    if not self._image:
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   587
                        self._image = self._read_image(self.fp)
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   588
                    if getattr(self._image,'format',None)=='JPEG': self.jpeg_fh = self._jpeg_fh
2953
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   589
                else:
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   590
                    from reportlab.pdfbase.pdfutils import readJPEGInfo
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   591
                    try:
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   592
                        self._width,self._height,c=readJPEGInfo(self.fp)
2952
8a51cf037687 reportlab: add rl_config.internImageFiles and extra support in ImageReader
rgbecker
parents: 2942
diff changeset
   593
                    except:
2953
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   594
                        raise RuntimeError('Imaging Library not available, unable to import bitmaps only jpegs')
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   595
                    self.jpeg_fh = self._jpeg_fh
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   596
                    self._data = self.fp.read()
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   597
                    self._dataA=None
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   598
                    self.fp.seek(0)
2556
d7c3c6b74053 utils.py: annotate ImageReader read errors
rgbecker
parents: 2502
diff changeset
   599
            except:
d7c3c6b74053 utils.py: annotate ImageReader read errors
rgbecker
parents: 2502
diff changeset
   600
                et,ev,tb = sys.exc_info()
d7c3c6b74053 utils.py: annotate ImageReader read errors
rgbecker
parents: 2502
diff changeset
   601
                if hasattr(ev,'args'):
2953
59f221d6007c reportlab: improve ImageReader so it works with Image flowables in the absence of PIL
rgbecker
parents: 2952
diff changeset
   602
                    a = str(ev.args[-1])+(' fileName=%r'%fileName)
2556
d7c3c6b74053 utils.py: annotate ImageReader read errors
rgbecker
parents: 2502
diff changeset
   603
                    ev.args= ev.args[:-1]+(a,)
d7c3c6b74053 utils.py: annotate ImageReader read errors
rgbecker
parents: 2502
diff changeset
   604
                    raise et,ev,tb
d7c3c6b74053 utils.py: annotate ImageReader read errors
rgbecker
parents: 2502
diff changeset
   605
                else:
d7c3c6b74053 utils.py: annotate ImageReader read errors
rgbecker
parents: 2502
diff changeset
   606
                    raise
d7c3c6b74053 utils.py: annotate ImageReader read errors
rgbecker
parents: 2502
diff changeset
   607
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   608
    def _read_image(self,fp):
3119
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   609
        if jython:
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   610
            from javax.imageio import ImageIO
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   611
            return ImageIO.read(fp)
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   612
        else:
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   613
            import PIL.Image
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   614
            return PIL.Image.open(fp)
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   615
2487
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   616
    def _jpeg_fh(self):
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   617
        fp = self.fp
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   618
        fp.seek(0)
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   619
        return fp
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   620
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   621
    def jpeg_fh(self):
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   622
        return None
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   623
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   624
    def getSize(self):
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   625
        if (self._width is None or self._height is None):
3119
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   626
            if jython:
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   627
                self._width = self._image.getWidth()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   628
                self._height = self._image.getHeight()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   629
            else:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   630
                self._width, self._height = self._image.size
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   631
        return (self._width, self._height)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2160
diff changeset
   632
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   633
    def getRGBData(self):
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   634
        "Return byte array of RGB data as string"
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   635
        if self._data is None:
2902
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
   636
            self._dataA = None
3119
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   637
            if jython:
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   638
                import jarray
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   639
                from java.awt.image import PixelGrabber
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   640
                width, height = self.getSize()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   641
                buffer = jarray.zeros(width*height, 'i')
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   642
                pg = PixelGrabber(self._image, 0,0,width,height,buffer,0,width)
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   643
                pg.grabPixels()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   644
                # there must be a way to do this with a cast not a byte-level loop,
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   645
                # I just haven't found it yet...
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   646
                pixels = []
2059
e5eec1dd6f0c ImageReader can now take File objects as well as Strings
dragan1
parents: 2057
diff changeset
   647
                a = pixels.append
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   648
                for i in range(len(buffer)):
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   649
                    rgb = buffer[i]
2087
5c4b76884d9d fixed java file read bug
dragan1
parents: 2086
diff changeset
   650
                    a(chr((rgb>>16)&0xff))
5c4b76884d9d fixed java file read bug
dragan1
parents: 2086
diff changeset
   651
                    a(chr((rgb>>8)&0xff))
2059
e5eec1dd6f0c ImageReader can now take File objects as well as Strings
dragan1
parents: 2057
diff changeset
   652
                    a(chr(rgb&0xff))
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   653
                self._data = ''.join(pixels)
2482
4ae04d1800af CMYK image improvements
rgbecker
parents: 2479
diff changeset
   654
                self.mode = 'RGB'
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   655
            else:
2482
4ae04d1800af CMYK image improvements
rgbecker
parents: 2479
diff changeset
   656
                im = self._image
4ae04d1800af CMYK image improvements
rgbecker
parents: 2479
diff changeset
   657
                mode = self.mode = im.mode
2902
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
   658
                if mode=='RGBA':
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
   659
                    self._dataA = ImageReader(im.split()[3])
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
   660
                    im = im.convert('RGB')
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
   661
                    self.mode = 'RGB'
f15ac27dc73f reportlab: add in RGBA functionality for sensible images
rgbecker
parents: 2838
diff changeset
   662
                elif mode not in ('L','RGB','CMYK'):
2482
4ae04d1800af CMYK image improvements
rgbecker
parents: 2479
diff changeset
   663
                    im = im.convert('RGB')
4ae04d1800af CMYK image improvements
rgbecker
parents: 2479
diff changeset
   664
                    self.mode = 'RGB'
4ae04d1800af CMYK image improvements
rgbecker
parents: 2479
diff changeset
   665
                self._data = im.tostring()
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2160
diff changeset
   666
        return self._data
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   667
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   668
    def getImageData(self):
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   669
        width, height = self.getSize()
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   670
        return width, height, self.getRGBData()
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   671
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   672
    def getTransparent(self):
3119
b9e540d1267a rl-jython25: jython support for isSourceDistro
rgbecker
parents: 3083
diff changeset
   673
        if jython:
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   674
            return None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   675
        else:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   676
            if self._image.info.has_key("transparency"):
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   677
                transparency = self._image.info["transparency"] * 3
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   678
                palette = self._image.palette
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   679
                try:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   680
                    palette = palette.palette
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   681
                except:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   682
                    palette = palette.data
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   683
                return map(ord, palette[transparency:transparency+3])
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   684
            else:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   685
                return None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   686
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   687
class LazyImageReader(ImageReader): 
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   688
    def fp(self): 
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   689
        return open_for_read(self.fileName, 'b') 
3083
95f7752b0284 reportlab: remove 2.3/2.4 blockers
rgbecker
parents: 3029
diff changeset
   690
    fp=property(fp) 
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   691
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   692
    def _image(self):
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   693
        return self._read_image(self.fp)
3083
95f7752b0284 reportlab: remove 2.3/2.4 blockers
rgbecker
parents: 3029
diff changeset
   694
    _image=property(_image) 
2954
5ec6485e810a reportlab: implement Ralf Schmitt's open files limiter
rgbecker
parents: 2953
diff changeset
   695
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   696
def getImageData(imageFileName):
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   697
    "Get width, height and RGB pixels from image file.  Wraps Java/PIL"
2487
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   698
    try:
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   699
        return imageFileName.getImageData()
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   700
    except AttributeError:
a51bf871b709 reportlab: better support for PIL image
rgbecker
parents: 2484
diff changeset
   701
        return ImageReader(imageFileName).getImageData()
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   702
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   703
class DebugMemo:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   704
    '''Intended as a simple report back encapsulator
1545
966ccacc4c7d Changes to DebugMemo.__doc__
rgbecker
parents: 1543
diff changeset
   705
3028
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   706
    Typical usages:
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   707
        
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   708
    1. To record error data::
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   709
        
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   710
        dbg = DebugMemo(fn='dbgmemo.dbg',myVar=value)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   711
        dbg.add(anotherPayload='aaaa',andagain='bbb')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   712
        dbg.dump()
1543
3681c7d8898d Slight formatting improvements & added payload method
rgbecker
parents: 1538
diff changeset
   713
3028
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   714
    2. To show the recorded info::
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   715
        
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   716
        dbg = DebugMemo(fn='dbgmemo.dbg',mode='r')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   717
        dbg.load()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   718
        dbg.show()
1543
3681c7d8898d Slight formatting improvements & added payload method
rgbecker
parents: 1538
diff changeset
   719
3028
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   720
    3. To re-use recorded information::
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   721
        
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   722
        dbg = DebugMemo(fn='dbgmemo.dbg',mode='r')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   723
            dbg.load()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   724
        myTestFunc(dbg.payload('myVar'),dbg.payload('andagain'))
1545
966ccacc4c7d Changes to DebugMemo.__doc__
rgbecker
parents: 1543
diff changeset
   725
3028
082f5208644e docstring modifications to adhere to restructuredtext
damian
parents: 2964
diff changeset
   726
    In addition to the payload variables the dump records many useful bits
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   727
    of information which are also printed in the show() method.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   728
    '''
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   729
    def __init__(self,fn='rl_dbgmemo.dbg',mode='w',getScript=1,modules=(),capture_traceback=1, stdout=None, **kw):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   730
        import time, socket
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   731
        self.fn = fn
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   732
        if mode!='w': return
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   733
        if not stdout: 
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   734
            self.stdout = sys.stdout
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   735
        else:
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   736
            if hasattr(stdout,'write'):
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   737
                self.stdout = stdout
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   738
            else:
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   739
                self.stdout = open(stdout,'w')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   740
        self.store = store = {}
2296
8f5419265559 Added capture_traceback=1 to DebugMemo init
rgbecker
parents: 2279
diff changeset
   741
        if capture_traceback and sys.exc_info() != (None,None,None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   742
            import traceback
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   743
            s = getStringIO()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   744
            traceback.print_exc(None,s)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   745
            store['__traceback'] = s.getvalue()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   746
        cwd=os.getcwd()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   747
        lcwd = os.listdir(cwd)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   748
        exed = os.path.abspath(os.path.dirname(sys.argv[0]))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   749
        store.update({  'gmt': time.asctime(time.gmtime(time.time())),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   750
                        'platform': sys.platform,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   751
                        'version': sys.version,
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   752
                        'hexversion': hex(sys.hexversion),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   753
                        'executable': sys.executable,
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   754
                        'exec_prefix': sys.exec_prefix,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   755
                        'prefix': sys.prefix,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   756
                        'path': sys.path,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   757
                        'argv': sys.argv,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   758
                        'cwd': cwd,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   759
                        'hostname': socket.gethostname(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   760
                        'lcwd': lcwd,
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   761
                        'byteorder': sys.byteorder,
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   762
                        'maxint': sys.maxint,
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   763
                        'maxint': getattr(sys,'maxunicode','????'),
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   764
                        'api_version': getattr(sys,'api_version','????'),
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   765
                        'version_info': getattr(sys,'version_info','????'),
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   766
                        'winver': getattr(sys,'winver','????'),
2459
a1be912dcbd4 Add process environment to logged traceback
jjlee
parents: 2454
diff changeset
   767
                        'environment': os.environ,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   768
                        })
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   769
        for M,A in (
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   770
                (sys,('getwindowsversion','getfilesystemencoding')),
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   771
                (os,('uname', 'ctermid', 'getgid', 'getuid', 'getegid',
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   772
                    'geteuid', 'getlogin', 'getgroups', 'getpgrp', 'getpid', 'getppid',
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   773
                    )),
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   774
                ):
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   775
            for a in A:
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   776
                if hasattr(M,a):
2300
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   777
                    try:
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   778
                        store[a] = getattr(M,a)()
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   779
                    except:
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   780
                        pass
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   781
        if exed!=cwd:
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   782
            try:
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   783
                store.update({'exed': exed, 'lexed': os.listdir(exed),})
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   784
            except:
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   785
                pass
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   786
        if getScript:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   787
            fn = os.path.abspath(sys.argv[0])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   788
            if os.path.isfile(fn):
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   789
                try:
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   790
                    store['__script'] = (fn,open(fn,'r').read())
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   791
                except:
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   792
                    pass
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   793
        module_versions = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   794
        for n,m in sys.modules.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   795
            if n=='reportlab' or n=='rlextra' or n[:10]=='reportlab.' or n[:8]=='rlextra.':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   796
                v = getattr(m,'__version__',None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   797
                if v: module_versions[n] = v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   798
        store['__module_versions'] = module_versions
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   799
        self.store['__payload'] = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   800
        self._add(kw)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   801
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   802
    def _add(self,D):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   803
        payload = self.store['__payload']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   804
        for k, v in D.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   805
            payload[k] = v
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   806
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   807
    def add(self,**kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   808
        self._add(kw)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   809
2942
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   810
    def _dump(self,f):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   811
        import pickle
2942
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   812
        pickle.dump(self.store,f)
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   813
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   814
    def dump(self):
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   815
        f = open(self.fn,'wb')
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   816
        try:
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   817
            self._dump(f)
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   818
        finally:
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   819
            f.close()
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   820
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   821
    def dumps(self):
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   822
        f = getStringIO()
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   823
        self._dump(f)
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   824
        return f.getvalue()
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   825
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   826
    def _load(self,f):
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   827
        import pickle
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   828
        self.store = pickle.load(f)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   829
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   830
    def load(self):
2942
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   831
        f = open(self.fn,'rb')
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   832
        try:
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   833
            self._load(f)
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   834
        finally:
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   835
            f.close()
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   836
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   837
    def loads(self,s):
e6e20484c315 reportlab/lib/utils.py: slight extensions to DebugMemo
rgbecker
parents: 2929
diff changeset
   838
        self._load(getStringIO(s))
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   839
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   840
    def _show_module_versions(self,k,v):
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   841
        self._writeln(k[2:])
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   842
        K = v.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   843
        K.sort()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   844
        for k in K:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   845
            vk = v[k]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   846
            try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   847
                m = recursiveImport(k,sys.path[:],1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   848
                d = getattr(m,'__version__',None)==vk and 'SAME' or 'DIFFERENT'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   849
            except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   850
                m = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   851
                d = '??????unknown??????'
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   852
            self._writeln('  %s = %s (%s)' % (k,vk,d))
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   853
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   854
    def _banner(self,k,what):
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   855
        self._writeln('###################%s %s##################' % (what,k[2:]))
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   856
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   857
    def _start(self,k):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   858
        self._banner(k,'Start  ')
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   859
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   860
    def _finish(self,k):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   861
        self._banner(k,'Finish ')
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   862
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   863
    def _show_lines(self,k,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   864
        self._start(k)
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   865
        self._writeln(v)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   866
        self._finish(k)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   867
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   868
    def _show_file(self,k,v):
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   869
        k = '%s %s' % (k,os.path.basename(v[0]))
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   870
        self._show_lines(k,v[1])
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   871
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   872
    def _show_payload(self,k,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   873
        if v:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   874
            import pprint
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   875
            self._start(k)
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   876
            pprint.pprint(v,self.stdout)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   877
            self._finish(k)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   878
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   879
    specials = {'__module_versions': _show_module_versions,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   880
                '__payload': _show_payload,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   881
                '__traceback': _show_lines,
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   882
                '__script': _show_file,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   883
                }
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   884
    def show(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   885
        K = self.store.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   886
        K.sort()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   887
        for k in K:
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   888
            if k not in self.specials.keys(): self._writeln('%-15s = %s' % (k,self.store[k]))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   889
        for k in K:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   890
            if k in self.specials.keys(): apply(self.specials[k],(self,k,self.store[k]))
1543
3681c7d8898d Slight formatting improvements & added payload method
rgbecker
parents: 1538
diff changeset
   891
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   892
    def payload(self,name):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   893
        return self.store['__payload'][name]
1561
1b10f25e76df Add DebugMemo __g/setitem__
rgbecker
parents: 1545
diff changeset
   894
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   895
    def __setitem__(self,name,value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   896
        self.store['__payload'][name] = value
1561
1b10f25e76df Add DebugMemo __g/setitem__
rgbecker
parents: 1545
diff changeset
   897
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   898
    def __getitem__(self,name):
1833
135322abc191 Fix recursivImport for case when baseDir is a sequence
rgbecker
parents: 1821
diff changeset
   899
        return self.store['__payload'][name]
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   900
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   901
    def _writeln(self,msg):
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   902
        self.stdout.write(msg+'\n')
2365
cda528e430aa reportlab/lib/utils.py: added flatten utility function
rgbecker
parents: 2356
diff changeset
   903
cda528e430aa reportlab/lib/utils.py: added flatten utility function
rgbecker
parents: 2356
diff changeset
   904
def _flatten(L,a):
cda528e430aa reportlab/lib/utils.py: added flatten utility function
rgbecker
parents: 2356
diff changeset
   905
    for x in L:
2499
11bc8441ec51 legends.py: fix 2.1 compatibility problem
rgbecker
parents: 2497
diff changeset
   906
        if isSeqType(x): _flatten(x,a)
2365
cda528e430aa reportlab/lib/utils.py: added flatten utility function
rgbecker
parents: 2356
diff changeset
   907
        else: a(x)
cda528e430aa reportlab/lib/utils.py: added flatten utility function
rgbecker
parents: 2356
diff changeset
   908
cda528e430aa reportlab/lib/utils.py: added flatten utility function
rgbecker
parents: 2356
diff changeset
   909
def flatten(L):
cda528e430aa reportlab/lib/utils.py: added flatten utility function
rgbecker
parents: 2356
diff changeset
   910
    '''recursively flatten the list or tuple L'''
cda528e430aa reportlab/lib/utils.py: added flatten utility function
rgbecker
parents: 2356
diff changeset
   911
    R = []
cda528e430aa reportlab/lib/utils.py: added flatten utility function
rgbecker
parents: 2356
diff changeset
   912
    _flatten(L,R.append)
cda528e430aa reportlab/lib/utils.py: added flatten utility function
rgbecker
parents: 2356
diff changeset
   913
    return R
2406
311191cddb33 reportlab lib utils.py: add find_locals
rgbecker
parents: 2365
diff changeset
   914
311191cddb33 reportlab lib utils.py: add find_locals
rgbecker
parents: 2365
diff changeset
   915
def find_locals(func,depth=0):
311191cddb33 reportlab lib utils.py: add find_locals
rgbecker
parents: 2365
diff changeset
   916
    '''apply func to the locals at each stack frame till func returns a non false value'''
311191cddb33 reportlab lib utils.py: add find_locals
rgbecker
parents: 2365
diff changeset
   917
    while 1:
311191cddb33 reportlab lib utils.py: add find_locals
rgbecker
parents: 2365
diff changeset
   918
        _ = func(sys._getframe(depth).f_locals)
311191cddb33 reportlab lib utils.py: add find_locals
rgbecker
parents: 2365
diff changeset
   919
        if _: return _
311191cddb33 reportlab lib utils.py: add find_locals
rgbecker
parents: 2365
diff changeset
   920
        depth += 1
2454
8cc3d50b0703 utils.py: added FmtSelfDict class
rgbecker
parents: 2448
diff changeset
   921
8cc3d50b0703 utils.py: added FmtSelfDict class
rgbecker
parents: 2448
diff changeset
   922
class _FmtSelfDict:
2479
ea93388b723d Allow overriding parameters in _fmt utility method
jjlee
parents: 2459
diff changeset
   923
    def __init__(self,obj,overrideArgs):
2454
8cc3d50b0703 utils.py: added FmtSelfDict class
rgbecker
parents: 2448
diff changeset
   924
        self.obj = obj
2479
ea93388b723d Allow overriding parameters in _fmt utility method
jjlee
parents: 2459
diff changeset
   925
        self._overrideArgs = overrideArgs
2454
8cc3d50b0703 utils.py: added FmtSelfDict class
rgbecker
parents: 2448
diff changeset
   926
    def __getitem__(self,k):
8cc3d50b0703 utils.py: added FmtSelfDict class
rgbecker
parents: 2448
diff changeset
   927
        try:
2479
ea93388b723d Allow overriding parameters in _fmt utility method
jjlee
parents: 2459
diff changeset
   928
            return self._overrideArgs[k]
2454
8cc3d50b0703 utils.py: added FmtSelfDict class
rgbecker
parents: 2448
diff changeset
   929
        except KeyError:
2479
ea93388b723d Allow overriding parameters in _fmt utility method
jjlee
parents: 2459
diff changeset
   930
            try:
ea93388b723d Allow overriding parameters in _fmt utility method
jjlee
parents: 2459
diff changeset
   931
                return self.obj.__dict__[k]
ea93388b723d Allow overriding parameters in _fmt utility method
jjlee
parents: 2459
diff changeset
   932
            except KeyError:
ea93388b723d Allow overriding parameters in _fmt utility method
jjlee
parents: 2459
diff changeset
   933
                return getattr(self.obj,k)
2454
8cc3d50b0703 utils.py: added FmtSelfDict class
rgbecker
parents: 2448
diff changeset
   934
8cc3d50b0703 utils.py: added FmtSelfDict class
rgbecker
parents: 2448
diff changeset
   935
class FmtSelfDict:
8cc3d50b0703 utils.py: added FmtSelfDict class
rgbecker
parents: 2448
diff changeset
   936
    '''mixin to provide the _fmt method'''
2479
ea93388b723d Allow overriding parameters in _fmt utility method
jjlee
parents: 2459
diff changeset
   937
    def _fmt(self,fmt,**overrideArgs):
ea93388b723d Allow overriding parameters in _fmt utility method
jjlee
parents: 2459
diff changeset
   938
        D = _FmtSelfDict(self, overrideArgs)
2454
8cc3d50b0703 utils.py: added FmtSelfDict class
rgbecker
parents: 2448
diff changeset
   939
        return fmt % D
2715
5cdfb604dff2 reportlab/lib/utils.py: added simplSplit func
rgbecker
parents: 2706
diff changeset
   940
2716
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   941
def _simpleSplit(txt,mW,SW):
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   942
    L = []
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   943
    ws = SW(' ')
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   944
    O = []
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   945
    w = -ws
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   946
    for t in txt.split():
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   947
        lt = SW(t)
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   948
        if w+ws+lt<=mW or O==[]:
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   949
            O.append(t)
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   950
            w = w + ws + lt
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   951
        else:
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   952
            L.append(string.join(O,' '))
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   953
            O = [t]
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   954
            w = lt
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   955
    if O!=[]: L.append(string.join(O,' '))
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   956
    return L
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   957
2715
5cdfb604dff2 reportlab/lib/utils.py: added simplSplit func
rgbecker
parents: 2706
diff changeset
   958
def simpleSplit(text,fontName,fontSize,maxWidth):
2716
6f35525eaffb reportlab/lib/utils.py: fix circular import
rgbecker
parents: 2715
diff changeset
   959
    from reportlab.pdfbase.pdfmetrics import stringWidth
2715
5cdfb604dff2 reportlab/lib/utils.py: added simplSplit func
rgbecker
parents: 2706
diff changeset
   960
    lines = text.split('\n')
5cdfb604dff2 reportlab/lib/utils.py: added simplSplit func
rgbecker
parents: 2706
diff changeset
   961
    SW = lambda text, fN=fontName, fS=fontSize: stringWidth(text, fN, fS)
5cdfb604dff2 reportlab/lib/utils.py: added simplSplit func
rgbecker
parents: 2706
diff changeset
   962
    if maxWidth:
5cdfb604dff2 reportlab/lib/utils.py: added simplSplit func
rgbecker
parents: 2706
diff changeset
   963
        L = []
5cdfb604dff2 reportlab/lib/utils.py: added simplSplit func
rgbecker
parents: 2706
diff changeset
   964
        for l in lines:
5cdfb604dff2 reportlab/lib/utils.py: added simplSplit func
rgbecker
parents: 2706
diff changeset
   965
            L[-1:-1] = _simpleSplit(l,maxWidth,SW)
5cdfb604dff2 reportlab/lib/utils.py: added simplSplit func
rgbecker
parents: 2706
diff changeset
   966
        lines = L
5cdfb604dff2 reportlab/lib/utils.py: added simplSplit func
rgbecker
parents: 2706
diff changeset
   967
    return lines
2764
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   968
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   969
def escapeTextOnce(text):
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   970
    "Escapes once only"
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   971
    from xml.sax.saxutils import escape
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   972
    if text is None:
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   973
        return text
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   974
    text = escape(text)
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   975
    text = text.replace('&amp;amp;', '&amp;')
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   976
    text = text.replace('&amp;gt;', '&gt;')
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   977
    text = text.replace('&amp;lt;', '&lt;')
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   978
    return text
2858dec9bd27 added quote-once
andy
parents: 2720
diff changeset
   979
2813
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2794
diff changeset
   980
def fileName2Utf8(fn):
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2794
diff changeset
   981
    '''attempt to convert a filename to utf8'''
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2794
diff changeset
   982
    from reportlab.rl_config import fsEncodings
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2794
diff changeset
   983
    for enc in fsEncodings:
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2794
diff changeset
   984
        try:
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2794
diff changeset
   985
            return fn.decode(enc).encode('utf8')
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2794
diff changeset
   986
        except:
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2794
diff changeset
   987
            pass
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2794
diff changeset
   988
    raise ValueError('cannot convert %r to utf8' % fn)
2929
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
   989
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
   990
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
   991
import itertools
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
   992
def prev_this_next(items):
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
   993
    """
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
   994
    Loop over a collection with look-ahead and look-back.
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
   995
    
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
   996
    From Thomas Guest, 
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
   997
    http://wordaligned.org/articles/zippy-triples-served-with-python
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
   998
    
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
   999
    Seriously useful looping tool (Google "zippy triples")
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1000
    lets you loop a collection and see the previous and next items,
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1001
    which get set to None at the ends.
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1002
    
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1003
    To be used in layout algorithms where one wants a peek at the
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1004
    next item coming down the pipe.
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1005
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1006
    """
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1007
    
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1008
    extend = itertools.chain([None], items, [None])
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1009
    prev, this, next = itertools.tee(extend, 3)
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1010
    try:
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1011
        this.next()
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1012
        next.next()
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1013
        next.next()
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1014
    except StopIteration:
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1015
        pass
02a5623f4f26 added nifty triple iterator
andy
parents: 2902
diff changeset
  1016
    return itertools.izip(prev, this, next)