reportlab/lib/utils.py
author rgbecker
Wed, 19 Nov 2003 14:12:41 +0000
changeset 2139 0f27f8cb5586
parent 2133 f0b088e1fc1b
child 2156 e0398bc717f2
permissions -rw-r--r--
Make fp_str more like _rl_accel(C/java) versions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 474
diff changeset
     1
#copyright ReportLab Inc. 2000
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 474
diff changeset
     2
#see license.txt for license details
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 474
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/lib/utils.py?cvsroot=reportlab
2139
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/lib/utils.py,v 1.55 2003/11/19 14:12:41 rgbecker Exp $
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
     5
__version__=''' $Id: utils.py,v 1.55 2003/11/19 14:12:41 rgbecker Exp $ '''
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 519
diff changeset
     6
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
     7
import string, os, sys
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
     8
from types import *
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
     9
from reportlab.lib.logger import warnOnce
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    10
SeqTypes = (ListType,TupleType)
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 519
diff changeset
    11
1837
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    12
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    13
if os.name == 'mac':
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    14
    #with the Mac, we need to tag the file in a special
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    15
    #way so the system knows it is a PDF file.
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    16
    #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
    17
    import macfs, macostools
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    18
    _KNOWN_MAC_EXT = {
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    19
        'BMP' : ('ogle','BMP '),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    20
        'EPS' : ('ogle','EPSF'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    21
        'EPSF': ('ogle','EPSF'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    22
        'GIF' : ('ogle','GIFf'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    23
        'JPG' : ('ogle','JPEG'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    24
        'JPEG': ('ogle','JPEG'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    25
        'PCT' : ('ttxt','PICT'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    26
        'PICT': ('ttxt','PICT'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    27
        'PNG' : ('ogle','PNGf'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    28
        'PPM' : ('ogle','.PPM'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    29
        'TIF' : ('ogle','TIFF'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    30
        'TIFF': ('ogle','TIFF'),
2051
50350756e12c Added HTML to the Mac markfilename routine - HTML files should now 'know'
johnprecedo
parents: 2045
diff changeset
    31
        '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
    32
        'HTML': ('MSIE','TEXT'),
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    33
        }
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    34
    def markfilename(filename,creatorcode=None,filetype=None,ext='PDF'):
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    35
        try:
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    36
            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
    37
                try:
2057
22e7a13fa031 Fixed so that file creator and types are now correctly set on the Mac again
rptlab
parents: 2053
diff changeset
    38
                    creatorcode, filetype = _KNOWN_MAC_EXT[string.upper(ext)]
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    39
                except:
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    40
                    return
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    41
            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
    42
            macostools.touched(filename)
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    43
        except:
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    44
            pass
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    45
else:
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    46
    def markfilename(filename,creatorcode=None,filetype=None):
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    47
        pass
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    48
1837
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    49
def isFileSystemDistro():
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    50
    """Attempt to detect if this copy of reportlab is running in a
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    51
    file system (as opposed to mostly running in a zip or McMillan
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    52
    archive or Jar file).  This is used by test cases, so that
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    53
    we can write test cases that don't get activated in a compiled
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    54
    distribution."""
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    55
    # is this safe enough?
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    56
    import reportlab.pdfgen.canvas
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    57
    hypothetical_location = reportlab.pdfgen.canvas
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    58
    return os.path.isfile(hypothetical_location)
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    59
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    60
def isCompactDistro():
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    61
    return not isFileSystemDistro()
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    62
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    63
def isSourceDistro():
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    64
    import reportlab.pdfgen.canvas
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    65
    hypothetical_location = reportlab.pdfgen.canvas
1143
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    66
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    67
try:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    68
    #raise ImportError
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    69
    ### 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
    70
    ### 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
    71
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    72
        from _rl_accel import fp_str                # in case of builtin version
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
    73
    except ImportError:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    74
        from reportlab.lib._rl_accel import fp_str  # specific
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
    75
except ImportError:
2139
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    76
    from math import log
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    77
    _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
    78
    _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
    79
    import re
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    80
    _tz_re = re.compile('0+$')
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    81
    del re
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    82
    def fp_str(*a):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    83
        if len(a)==1 and type(a[0]) in SeqTypes: a = a[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    84
        s = []
2139
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    85
        A = s.append
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    86
        for i in a:
2139
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    87
            sa =abs(i)
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    88
            if sa<=1e-7: A('0')
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    89
            else:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    90
                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
    91
                n = _fp_fmts[l]%i
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    92
                if l:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    93
                    n = _tz_re.sub('',n)
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    94
                    try:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    95
                        if n[-1]=='.': n = n[:-1]
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    96
                    except:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    97
                        print i, n
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    98
                        raise
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
    99
                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
   100
        return string.join(s)
448
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   101
981
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
   102
#hack test for comma users
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
   103
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
   104
    _FP_STR = fp_str
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   105
    def fp_str(*a):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   106
        return string.replace(apply(_FP_STR,a),',','.')
981
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
   107
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   108
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
   109
    """Dynamically imports possible packagized module, or raises ImportError"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   110
    import imp
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   111
    parts = string.split(modulename, '.')
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   112
    name = parts[0]
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   113
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   114
    if baseDir is None:
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   115
        path = sys.path[:]
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   116
    else:
1834
c7ee3661d73a Fix typo
rgbecker
parents: 1833
diff changeset
   117
        if type(baseDir) not in SeqTypes:
1833
135322abc191 Fix recursivImport for case when baseDir is a sequence
rgbecker
parents: 1821
diff changeset
   118
            path = [baseDir]
135322abc191 Fix recursivImport for case when baseDir is a sequence
rgbecker
parents: 1821
diff changeset
   119
        else:
135322abc191 Fix recursivImport for case when baseDir is a sequence
rgbecker
parents: 1821
diff changeset
   120
            path = list(baseDir)
1835
ffea78bc6caa Fix major idiocy
rgbecker
parents: 1834
diff changeset
   121
    path = filter(None,path)
1396
40d1361f08b7 Enhanced the error message from recursiveImport
andy_robinson
parents: 1389
diff changeset
   122
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   123
    if noCWD:
1999
63668deb43d3 Added more debugs and allowed for multiple removes
rptlab
parents: 1902
diff changeset
   124
        while '.' in path:
63668deb43d3 Added more debugs and allowed for multiple removes
rptlab
parents: 1902
diff changeset
   125
            if debug: print 'removed . from path'
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   126
            path.remove('.')
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   127
        abspath = os.path.abspath('.')
1999
63668deb43d3 Added more debugs and allowed for multiple removes
rptlab
parents: 1902
diff changeset
   128
        while abspath in path:
63668deb43d3 Added more debugs and allowed for multiple removes
rptlab
parents: 1902
diff changeset
   129
            if debug: print 'removed "%s" from path' % abspath
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   130
            path.remove(abspath)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   131
    else:
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   132
        if '.' not in path:
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   133
            path.insert(0,'.')
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   134
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   135
    if debug:
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   136
        import pprint
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   137
        pp = pprint.pprint
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   138
        print 'path=',pp(path)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   139
        
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   140
    #make import errors a bit more informative
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   141
    fullName = name
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   142
    try:
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   143
        (file, pathname, description) = imp.find_module(name, path)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   144
        childModule = parentModule = imp.load_module(name, file, pathname, description)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   145
        if debug: print 'imported module = %s' % parentModule
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   146
        for name in parts[1:]:
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   147
            fullName = fullName + '.' + name
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   148
            if debug: print 'trying part %s' % name
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   149
            (file, pathname, description) = imp.find_module(name, [os.path.dirname(parentModule.__file__)])
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   150
            childModule = imp.load_module(fullName, file, pathname, description)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   151
            if debug: print 'imported module = %s' % childModule
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   152
            
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   153
            setattr(parentModule, name, childModule)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   154
            parentModule = childModule
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   155
    except ImportError:
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   156
        msg = "cannot import '%s' while attempting recursive import of '%s'" % (fullName, modulename)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   157
        if baseDir:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   158
            msg = msg + " under paths '%s'" % `path`
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   159
        raise ImportError, msg
1396
40d1361f08b7 Enhanced the error message from recursiveImport
andy_robinson
parents: 1389
diff changeset
   160
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   161
    return childModule
1389
6e5ba87ce34e added function recursiveImport('package1.package2.module') in r/lib/utils
andy_robinson
parents: 1387
diff changeset
   162
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   163
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   164
def recursiveGetAttr(obj, name):
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   165
    "Can call down into e.g. object1.object2[4].attr"
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   166
    return eval(name, obj.__dict__)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   167
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   168
def recursiveSetAttr(obj, name, value):
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   169
    "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
   170
    #get the thing above last.
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   171
    tokens = string.split(name, '.')
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   172
    #print 'name=%s, tokens=%s' % (name, tokens)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   173
    if len(tokens) == 1:
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   174
        setattr(obj, name, value)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   175
    else:        
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   176
        most = string.join(tokens[:-1], '.')
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   177
        last = tokens[-1]
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   178
        #print 'most=%s, last=%s' % (most, last)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   179
        parent = recursiveGetAttr(obj, most)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   180
        #print 'parent=%s' % parent
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   181
        setattr(parent, last, value)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   182
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
   183
def import_zlib():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   184
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   185
        import zlib
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   186
    except ImportError:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   187
        zlib = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   188
        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
   189
        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
   190
    return zlib
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
   191
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   192
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   193
# Image Capability Detection.  Set a flag haveImages
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   194
# 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
   195
# 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
   196
# module, as there are 2 ways to import it
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   197
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   198
if sys.platform[0:4] == 'java':
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   199
    try:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   200
        import javax.imageio
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   201
        import java.awt.image
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   202
        haveImages = 1
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   203
    except:
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   204
        haveImages = 0
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   205
else:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   206
    try:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   207
        from PIL import Image
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   208
    except ImportError:
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   209
        try:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   210
            import Image
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   211
        except ImportError:
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   212
            Image = None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   213
    haveImages = Image is not None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   214
    if haveImages: del Image
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   215
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   216
1586
d600d8ffaa21 This seems adequate if we avoid Dinuisms like writing to initialised StringIOs
rgbecker
parents: 1584
diff changeset
   217
__StringIO=None
1580
50792f072538 use generalised StringIO instance interface
rgbecker
parents: 1575
diff changeset
   218
def getStringIO(buf=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   219
    '''unified StringIO instance interface'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   220
    global __StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   221
    if not __StringIO:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   222
        try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   223
            from cStringIO import StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   224
        except ImportError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   225
            from StringIO import StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   226
        __StringIO = StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   227
    return buf is not None and __StringIO(buf) or __StringIO()
1580
50792f072538 use generalised StringIO instance interface
rgbecker
parents: 1575
diff changeset
   228
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   229
class ArgvDictValue:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   230
    '''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
   231
    def __init__(self,value,func):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   232
        self.value = value
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   233
        self.func = func
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   234
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   235
def getArgvDict(**kw):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   236
    ''' 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
   237
        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
   238
        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
   239
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   240
    def handleValue(v,av,func):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   241
        if func:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   242
            v = func(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   243
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   244
            t = type(v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   245
            if t is StringType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   246
                v = av
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   247
            elif t is FloatType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   248
                v = float(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   249
            elif t is IntType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   250
                v = int(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   251
            elif t is ListType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   252
                v = list(eval(av))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   253
            elif t is TupleType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   254
                v = tuple(eval(av))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   255
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   256
                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
   257
        return v
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   258
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   259
    A = sys.argv[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   260
    R = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   261
    for k, v in kw.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   262
        if isinstance(v,ArgvDictValue):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   263
            v, func = v.value, v.func
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   264
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   265
            func = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   266
        handled = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   267
        ke = k+'='
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   268
        for a in A:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   269
            if string.find(a,ke)==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   270
                av = a[len(ke):]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   271
                A.remove(a)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   272
                R[k] = handleValue(v,av,func)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   273
                handled = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   274
                break
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   275
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   276
        if not handled: R[k] = handleValue(v,v,func)
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   277
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   278
    return R
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   279
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   280
def getHyphenater(hDict=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   281
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   282
        from reportlab.lib.pyHnj import Hyphen
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   283
        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
   284
        return Hyphen(hDict)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   285
    except ImportError, errMsg:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   286
        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
   287
        return None
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   288
448
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   289
def _className(self):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   290
    '''Return a shortened class name'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   291
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   292
        name = self.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   293
        i=string.rfind(name,'.')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   294
        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
   295
        return name
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   296
    except AttributeError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   297
        return str(self)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   298
1575
7d2360bdfc4d Added open_for_read
rgbecker
parents: 1561
diff changeset
   299
def open_for_read(name,mode='b'):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   300
    '''attempt to open a file or URL for reading'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   301
    import urllib
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   302
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   303
        t, o = urllib.splittype(name)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   304
        if not t or t=='file': raise ValueError
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   305
        o = urllib.urlopen(name)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   306
        return getStringIO(o.read())
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   307
    except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   308
        return open(name,'r'+mode)
1575
7d2360bdfc4d Added open_for_read
rgbecker
parents: 1561
diff changeset
   309
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   310
class ImageReader:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   311
    "Wraps up either PIL or Java to get data from bitmaps"
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   312
    def __init__(self, fileName):
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   313
        if not haveImages:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   314
            warnOnce('Imaging Library not available, unable to import bitmaps')
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   315
            return
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   316
        #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
   317
        #the relevant engine.
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   318
        self.fileName = fileName
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   319
        self._image = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   320
        self._width = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   321
        self._height = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   322
        self._transparent = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   323
        self._data = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   324
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   325
        #detect which library we are using and open the image
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   326
        if sys.platform[0:4] == 'java':
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   327
            from javax.imageio import ImageIO
2085
3f4461bd280b fix mispelling
rgbecker
parents: 2059
diff changeset
   328
            if type(fileName) is type(''):
2133
f0b088e1fc1b Fix tabs reported by Steve Holden
rgbecker
parents: 2087
diff changeset
   329
                fp = open(fileName,'rb')
2059
e5eec1dd6f0c ImageReader can now take File objects as well as Strings
dragan1
parents: 2057
diff changeset
   330
            else:
2133
f0b088e1fc1b Fix tabs reported by Steve Holden
rgbecker
parents: 2087
diff changeset
   331
                fp = fileName
2086
ba38d238b928 Revert towards Andy's code
rgbecker
parents: 2085
diff changeset
   332
            self._image = ImageIO.read(fp)
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   333
        else:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   334
            import PIL.Image
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   335
            self._image = PIL.Image.open(fileName)
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   336
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   337
    def getSize(self):
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   338
        if (self._width is None or self._height is None):
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   339
            if sys.platform[0:4] == 'java':
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   340
                self._width = self._image.getWidth()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   341
                self._height = self._image.getHeight()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   342
            else:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   343
                self._width, self._height = self._image.size
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   344
        return (self._width, self._height)
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   345
            
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   346
    def getRGBData(self):
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   347
        "Return byte array of RGB data as string"
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   348
        if self._data is None:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   349
            if sys.platform[0:4] == 'java':
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   350
                import jarray
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   351
                from java.awt.image import PixelGrabber
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   352
                width, height = self.getSize()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   353
                buffer = jarray.zeros(width*height, 'i')
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   354
                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
   355
                pg.grabPixels()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   356
                # 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
   357
                # I just haven't found it yet...
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   358
                pixels = []
2059
e5eec1dd6f0c ImageReader can now take File objects as well as Strings
dragan1
parents: 2057
diff changeset
   359
                a = pixels.append
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   360
                for i in range(len(buffer)):
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   361
                    rgb = buffer[i]
2087
5c4b76884d9d fixed java file read bug
dragan1
parents: 2086
diff changeset
   362
                    a(chr((rgb>>16)&0xff))
5c4b76884d9d fixed java file read bug
dragan1
parents: 2086
diff changeset
   363
                    a(chr((rgb>>8)&0xff))
2059
e5eec1dd6f0c ImageReader can now take File objects as well as Strings
dragan1
parents: 2057
diff changeset
   364
                    a(chr(rgb&0xff))
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   365
                self._data = ''.join(pixels)
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   366
            else:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   367
                rgb = self._image.convert('RGB')
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   368
                self._data = rgb.tostring()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   369
        return self._data  
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   370
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   371
    def getImageData(self):
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   372
        width, height = self.getSize()
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   373
        return width, height, self.getRGBData()
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   374
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   375
    def getTransparent(self):
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   376
        if sys.platform[0:4] == 'java':
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   377
            return None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   378
        else:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   379
            if self._image.info.has_key("transparency"):
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   380
                transparency = self._image.info["transparency"] * 3
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   381
                palette = self._image.palette
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   382
                try:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   383
                    palette = palette.palette
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   384
                except:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   385
                    palette = palette.data
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   386
                return map(ord, palette[transparency:transparency+3])
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   387
            else:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   388
                return None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   389
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   390
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   391
def getImageData(imageFileName):
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   392
    "Get width, height and RGB pixels from image file.  Wraps Java/PIL"
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   393
    return ImageReader.getImageData(imageFileName)
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   394
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   395
class DebugMemo:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   396
    '''Intended as a simple report back encapsulator
1545
966ccacc4c7d Changes to DebugMemo.__doc__
rgbecker
parents: 1543
diff changeset
   397
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   398
    Typical usages
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   399
    1) To record error data
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   400
        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
   401
        dbg.add(anotherPayload='aaaa',andagain='bbb')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   402
        dbg.dump()
1543
3681c7d8898d Slight formatting improvements & added payload method
rgbecker
parents: 1538
diff changeset
   403
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   404
    2) To show the recorded info
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   405
        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
   406
        dbg.load()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   407
        dbg.show()
1543
3681c7d8898d Slight formatting improvements & added payload method
rgbecker
parents: 1538
diff changeset
   408
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   409
    3) To re-use recorded information
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   410
        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
   411
            dbg.load()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   412
        myTestFunc(dbg.payload('myVar'),dbg.payload('andagain'))
1545
966ccacc4c7d Changes to DebugMemo.__doc__
rgbecker
parents: 1543
diff changeset
   413
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   414
    in addition to the payload variables the dump records many useful bits
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   415
    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
   416
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   417
    def __init__(self,fn='rl_dbgmemo.dbg',mode='w',getScript=1,modules=(),**kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   418
        import time, socket
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   419
        self.fn = fn
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   420
        if mode!='w': return
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   421
        self.store = store = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   422
        if sys.exc_info() != (None,None,None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   423
            import traceback
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   424
            s = getStringIO()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   425
            traceback.print_exc(None,s)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   426
            store['__traceback'] = s.getvalue()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   427
        cwd=os.getcwd()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   428
        lcwd = os.listdir(cwd)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   429
        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
   430
        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
   431
                        'platform': sys.platform,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   432
                        'version': sys.version,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   433
                        'executable': sys.executable,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   434
                        'prefix': sys.prefix,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   435
                        'path': sys.path,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   436
                        'argv': sys.argv,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   437
                        'cwd': cwd,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   438
                        'hostname': socket.gethostname(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   439
                        'lcwd': lcwd,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   440
                        })
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   441
        if exed!=cwd: store.update({'exed': exed,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   442
                                    'lexed': os.listdir(exed),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   443
                                    })
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   444
        if hasattr(os,'uname'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   445
            store.update({
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   446
                'uname': os.uname(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   447
                'ctermid': os.ctermid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   448
                'getgid': os.getgid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   449
                'getuid': os.getuid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   450
                'getegid': os.getegid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   451
                'geteuid': os.geteuid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   452
                'getlogin': os.getlogin(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   453
                'getgroups': os.getgroups(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   454
                'getpgrp': os.getpgrp(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   455
                'getpid': os.getpid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   456
                'getppid': os.getppid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   457
                })
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   458
        if getScript:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   459
            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
   460
            if os.path.isfile(fn):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   461
                store['__script'] = open(fn,'r').read()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   462
        module_versions = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   463
        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
   464
            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
   465
                v = getattr(m,'__version__',None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   466
                if v: module_versions[n] = v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   467
        store['__module_versions'] = module_versions
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   468
        self.store['__payload'] = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   469
        self._add(kw)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   470
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   471
    def _add(self,D):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   472
        payload = self.store['__payload']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   473
        for k, v in D.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   474
            payload[k] = v
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   475
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   476
    def add(self,**kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   477
        self._add(kw)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   478
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   479
    def dump(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   480
        import pickle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   481
        pickle.dump(self.store,open(self.fn,'wb'))
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   482
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   483
    def load(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   484
        import pickle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   485
        self.store = pickle.load(open(self.fn,'rb'))
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   486
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   487
    def _show_module_versions(self,k,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   488
        print k[2:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   489
        K = v.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   490
        K.sort()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   491
        for k in K:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   492
            vk = v[k]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   493
            try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   494
                m = recursiveImport(k,sys.path[:],1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   495
                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
   496
            except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   497
                m = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   498
                d = '??????unknown??????'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   499
            print '  %s = %s (%s)' % (k,vk,d)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   500
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   501
    def _banner(self,k,what):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   502
        print '###################%s %s##################' % (what,k[2:])
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   503
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   504
    def _start(self,k):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   505
        self._banner(k,'Start  ')
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   506
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   507
    def _finish(self,k):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   508
        self._banner(k,'Finish ')
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   509
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   510
    def _show_lines(self,k,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   511
        self._start(k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   512
        print v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   513
        self._finish(k)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   514
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   515
    def _show_payload(self,k,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   516
        if v:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   517
            import pprint
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   518
            self._start(k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   519
            pprint.pprint(v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   520
            self._finish(k)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   521
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   522
    specials = {'__module_versions': _show_module_versions,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   523
                '__payload': _show_payload,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   524
                '__traceback': _show_lines,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   525
                '__script': _show_lines,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   526
                }
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   527
    def show(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   528
        K = self.store.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   529
        K.sort()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   530
        for k in K:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   531
            if k not in self.specials.keys(): print '%-15s = %s' % (k,self.store[k])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   532
        for k in K:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   533
            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
   534
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   535
    def payload(self,name):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   536
        return self.store['__payload'][name]
1561
1b10f25e76df Add DebugMemo __g/setitem__
rgbecker
parents: 1545
diff changeset
   537
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   538
    def __setitem__(self,name,value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   539
        self.store['__payload'][name] = value
1561
1b10f25e76df Add DebugMemo __g/setitem__
rgbecker
parents: 1545
diff changeset
   540
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   541
    def __getitem__(self,name):
1833
135322abc191 Fix recursivImport for case when baseDir is a sequence
rgbecker
parents: 1821
diff changeset
   542
        return self.store['__payload'][name]