reportlab/lib/utils.py
author rgbecker
Sun, 23 May 2004 10:39:30 +0000
changeset 2302 240a26f9ee8f
parent 2301 3df8c1c14c0e
child 2305 3dbfbe55e9ff
permissions -rw-r--r--
rl_get_module now seems to work as intended
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
2302
240a26f9ee8f rl_get_module now seems to work as intended
rgbecker
parents: 2301
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/lib/utils.py,v 1.77 2004/05/23 10:39:30 rgbecker Exp $
240a26f9ee8f rl_get_module now seems to work as intended
rgbecker
parents: 2301
diff changeset
     5
__version__=''' $Id: utils.py,v 1.77 2004/05/23 10:39:30 rgbecker Exp $ '''
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 519
diff changeset
     6
2302
240a26f9ee8f rl_get_module now seems to work as intended
rgbecker
parents: 2301
diff changeset
     7
import string, os, sys, imp
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)
2259
d2738e86e6f6 True/False aren't defined for Python < 2.3
rgbecker
parents: 2258
diff changeset
    11
if sys.hexversion<0x2030000:
d2738e86e6f6 True/False aren't defined for Python < 2.3
rgbecker
parents: 2258
diff changeset
    12
    True = 1
d2738e86e6f6 True/False aren't defined for Python < 2.3
rgbecker
parents: 2258
diff changeset
    13
    False = 0
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 519
diff changeset
    14
2215
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    15
def _findFiles(dirList,ext='.ttf'):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    16
    from os.path import isfile, isdir, join as path_join
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    17
    from os import listdir
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    18
    ext = ext.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    19
    R = []
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    20
    A = R.append
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    21
    for D in dirList:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    22
        if not isdir(D): continue
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    23
        for fn in listdir(D):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    24
            fn = path_join(D,fn)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    25
            if isfile(fn) and (not ext or fn.lower().endswith(ext)): A(fn)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    26
    return R
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    27
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    28
try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    29
    _UserDict = dict
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    30
except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    31
    from UserDict import UserDict as _UserDict
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    32
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    33
class CIDict(_UserDict):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    34
    def __init__(self,*a,**kw):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    35
        map(self.update, a)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    36
        self.update(kw)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    37
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    38
    def update(self,D):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    39
        for k,v in D.items(): self[k] = v
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    40
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    41
    def __setitem__(self,k,v):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    42
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    43
            k = k.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    44
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    45
            pass
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    46
        _UserDict.__setitem__(self,k,v)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    47
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    48
    def __getitem__(self,k):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    49
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    50
            k = k.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    51
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    52
            pass
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    53
        return _UserDict.__getitem__(self,k)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    54
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    55
    def __delitem__(self,k):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    56
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    57
            k = k.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    58
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    59
            pass
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    60
        return _UserDict.__delitem__(self,k)
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    61
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    62
    def get(self,k,dv=None):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    63
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    64
            return self[k]
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    65
        except KeyError:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    66
            return dv
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    67
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    68
    def has_key(self,k):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    69
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    70
            self[k]
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    71
            return True
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    72
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    73
            return False
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    74
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    75
    def pop(self,k,*a):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    76
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    77
            k = k.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    78
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    79
            pass
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    80
        return _UserDict.pop(*((self,k)+a))
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    81
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    82
    def setdefault(self,k,*a):
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    83
        try:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    84
            k = k.lower()
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    85
        except:
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    86
            pass
e86aad9766c7 Added _findFiles & CIDict
rgbecker
parents: 2200
diff changeset
    87
        return _UserDict.setdefault(*((self,k)+a))
1837
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
    88
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    89
if os.name == 'mac':
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    90
    #with the Mac, we need to tag the file in a special
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    91
    #way so the system knows it is a PDF file.
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
    92
    #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
    93
    import macfs, macostools
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    94
    _KNOWN_MAC_EXT = {
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    95
        'BMP' : ('ogle','BMP '),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    96
        'EPS' : ('ogle','EPSF'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    97
        'EPSF': ('ogle','EPSF'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    98
        'GIF' : ('ogle','GIFf'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
    99
        'JPG' : ('ogle','JPEG'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   100
        'JPEG': ('ogle','JPEG'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   101
        'PCT' : ('ttxt','PICT'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   102
        'PICT': ('ttxt','PICT'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   103
        'PNG' : ('ogle','PNGf'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   104
        'PPM' : ('ogle','.PPM'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   105
        'TIF' : ('ogle','TIFF'),
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   106
        'TIFF': ('ogle','TIFF'),
2051
50350756e12c Added HTML to the Mac markfilename routine - HTML files should now 'know'
johnprecedo
parents: 2045
diff changeset
   107
        '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
   108
        'HTML': ('MSIE','TEXT'),
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   109
        }
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   110
    def markfilename(filename,creatorcode=None,filetype=None,ext='PDF'):
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   111
        try:
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   112
            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
   113
                try:
2057
22e7a13fa031 Fixed so that file creator and types are now correctly set on the Mac again
rptlab
parents: 2053
diff changeset
   114
                    creatorcode, filetype = _KNOWN_MAC_EXT[string.upper(ext)]
2007
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   115
                except:
2759dd179707 Added John's mac type dict to markfilename
rgbecker
parents: 1999
diff changeset
   116
                    return
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   117
            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
   118
            macostools.touched(filename)
1902
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   119
        except:
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   120
            pass
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   121
else:
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   122
    def markfilename(filename,creatorcode=None,filetype=None):
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   123
        pass
3dc6869b1b47 Moved markfilename from pdfdoc
rgbecker
parents: 1837
diff changeset
   124
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   125
import reportlab
2251
afa4604d1a15 We prefer absolute RL DIR
rgbecker
parents: 2247
diff changeset
   126
__RL_DIR=os.path.dirname(reportlab.__file__)    #possibly relative
afa4604d1a15 We prefer absolute RL DIR
rgbecker
parents: 2247
diff changeset
   127
_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
   128
del reportlab
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   129
2225
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   130
#Attempt to detect if this copy of reportlab is running in a
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   131
#file system (as opposed to mostly running in a zip or McMillan
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   132
#archive or Jar file).  This is used by test cases, so that
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   133
#we can write test cases that don't get activated in a compiled
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   134
try:
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   135
    __file__
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   136
except:
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   137
    __file__ = sys.argv[0]
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   138
try:
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   139
    _isFSD = not __loader__
2251
afa4604d1a15 We prefer absolute RL DIR
rgbecker
parents: 2247
diff changeset
   140
    _loaderpfxlenR = len(__loader__.archive+os.sep)
afa4604d1a15 We prefer absolute RL DIR
rgbecker
parents: 2247
diff changeset
   141
    _loaderpfxlen = _loaderpfxlenR+len(_RL_DIR)-len(__RL_DIR)
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   142
except:
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   143
    _isFSD = os.path.isfile(__file__)   #slight risk of wrong path
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   144
    __loader__ = None
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   145
_isFSSD = _isFSD and os.path.isfile(os.path.splitext(__file__)[0] +'.py')
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   146
1837
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
   147
def isFileSystemDistro():
2225
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   148
    '''return truth if a file system distribution'''
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   149
    return _isFSD
1837
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
   150
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
   151
def isCompactDistro():
2225
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   152
    '''return truth if not a file system distribution'''
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   153
    return not _isFSD
1837
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
   154
a3920893b1b8 Back in synch, diagnostic function for distro type added
andy_robinson
parents: 1835
diff changeset
   155
def isSourceDistro():
2225
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   156
    '''return truth if a source file system distribution'''
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   157
    return _isFSSD
1143
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
   158
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
   159
try:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   160
    #raise ImportError
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   161
    ### 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
   162
    ### 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
   163
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   164
        from _rl_accel import fp_str                # in case of builtin version
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   165
    except ImportError:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   166
        from reportlab.lib._rl_accel import fp_str  # specific
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   167
except ImportError:
2139
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   168
    from math import log
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   169
    _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
   170
    _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
   171
    import re
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   172
    _tz_re = re.compile('0+$')
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   173
    del re
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   174
    def fp_str(*a):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   175
        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
   176
        s = []
2139
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   177
        A = s.append
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   178
        for i in a:
2139
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   179
            sa =abs(i)
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   180
            if sa<=1e-7: A('0')
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   181
            else:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   182
                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
   183
                n = _fp_fmts[l]%i
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   184
                if l:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   185
                    n = _tz_re.sub('',n)
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   186
                    try:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   187
                        if n[-1]=='.': n = n[:-1]
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   188
                    except:
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   189
                        print i, n
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   190
                        raise
0f27f8cb5586 Make fp_str more like _rl_accel(C/java) versions
rgbecker
parents: 2133
diff changeset
   191
                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
   192
        return string.join(s)
448
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   193
981
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
   194
#hack test for comma users
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
   195
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
   196
    _FP_STR = fp_str
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   197
    def fp_str(*a):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   198
        return string.replace(apply(_FP_STR,a),',','.')
981
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
   199
2260
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   200
_rl_tempdir=None
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   201
def get_rl_tempdir(*subdirs):
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   202
    global _rl_tempdir
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   203
    if _rl_tempdir is None:
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   204
        import tempfile
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   205
        _rl_tempdir = os.path.join(tempfile.gettempdir(),'ReportLab_tmp%s' % (sys.platform=='unix' and `os.getuid()` or ''))
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   206
    d = _rl_tempdir
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   207
    if subdirs: d = os.path.join(*((d,)+subdirs))
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   208
    try:
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   209
        os.makedirs(d)
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   210
    except:
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   211
        pass
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   212
    return d
8afb75cdbcdc Added get_rl_tempdir
rgbecker
parents: 2259
diff changeset
   213
2279
e91cd2959c5c Added get_rl_tempfile
rgbecker
parents: 2260
diff changeset
   214
def get_rl_tempfile(fn=None):
e91cd2959c5c Added get_rl_tempfile
rgbecker
parents: 2260
diff changeset
   215
    if not fn:
e91cd2959c5c Added get_rl_tempfile
rgbecker
parents: 2260
diff changeset
   216
        import tempfile
e91cd2959c5c Added get_rl_tempfile
rgbecker
parents: 2260
diff changeset
   217
        return tempfile.mktemp('','',get_rl_tempdir())
e91cd2959c5c Added get_rl_tempfile
rgbecker
parents: 2260
diff changeset
   218
    else:
e91cd2959c5c Added get_rl_tempfile
rgbecker
parents: 2260
diff changeset
   219
        return os.path.join(get_rl_tempdir(),fn)
e91cd2959c5c Added get_rl_tempfile
rgbecker
parents: 2260
diff changeset
   220
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   221
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
   222
    """Dynamically imports possible packagized module, or raises ImportError"""
2160
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   223
    normalize = lambda x: os.path.normcase(os.path.abspath(os.path.normpath(x)))
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   224
    path = map(normalize,sys.path)
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   225
    if baseDir:
1834
c7ee3661d73a Fix typo
rgbecker
parents: 1833
diff changeset
   226
        if type(baseDir) not in SeqTypes:
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   227
            tp = [baseDir]
1833
135322abc191 Fix recursivImport for case when baseDir is a sequence
rgbecker
parents: 1821
diff changeset
   228
        else:
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   229
            tp = filter(None,list(baseDir))
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   230
        for p in tp:
2160
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   231
            p = normalize(p)
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   232
            if p not in path: path.insert(0,p)
1396
40d1361f08b7 Enhanced the error message from recursiveImport
andy_robinson
parents: 1389
diff changeset
   233
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   234
    if noCWD:
2160
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   235
        for p in ('','.',normalize('.')):
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   236
            while p in path:
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   237
                if debug: print 'removed "%s" from path' % p
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   238
                path.remove(p)
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   239
    elif '.' not in path:
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   240
            path.insert(0,'.')
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   241
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   242
    if debug:
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   243
        import pprint
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   244
        pp = pprint.pprint
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   245
        print 'path=',
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   246
        pp(path)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2160
diff changeset
   247
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   248
    #make import errors a bit more informative
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   249
    opath = sys.path
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   250
    try:
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   251
        sys.path = path
2160
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   252
        exec 'import %s\nm = %s\n' % (modulename,modulename) in locals()
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   253
        sys.path = opath
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   254
        return m
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   255
    except ImportError:
2160
0ac717b8d778 Minor changes to recursiveimport
rgbecker
parents: 2156
diff changeset
   256
        sys.path = opath
2156
e0398bc717f2 First attempt at using import rather than imp
rgbecker
parents: 2139
diff changeset
   257
        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
   258
        if baseDir:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   259
            msg = msg + " under paths '%s'" % `path`
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   260
        raise ImportError, msg
1396
40d1361f08b7 Enhanced the error message from recursiveImport
andy_robinson
parents: 1389
diff changeset
   261
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   262
def recursiveGetAttr(obj, name):
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   263
    "Can call down into e.g. object1.object2[4].attr"
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   264
    return eval(name, obj.__dict__)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   265
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   266
def recursiveSetAttr(obj, name, value):
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   267
    "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
   268
    #get the thing above last.
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   269
    tokens = string.split(name, '.')
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   270
    #print 'name=%s, tokens=%s' % (name, tokens)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   271
    if len(tokens) == 1:
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   272
        setattr(obj, name, value)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2160
diff changeset
   273
    else:
1821
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   274
        most = string.join(tokens[:-1], '.')
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   275
        last = tokens[-1]
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   276
        #print 'most=%s, last=%s' % (most, last)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   277
        parent = recursiveGetAttr(obj, most)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   278
        #print 'parent=%s' % parent
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   279
        setattr(parent, last, value)
7854ddd0fc30 Fixed recursive import, setting and gettign attributes
andy_robinson
parents: 1683
diff changeset
   280
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
   281
def import_zlib():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   282
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   283
        import zlib
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   284
    except ImportError:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   285
        zlib = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   286
        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
   287
        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
   288
    return zlib
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
   289
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   290
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   291
# Image Capability Detection.  Set a flag haveImages
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   292
# 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
   293
# 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
   294
# 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
   295
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   296
if sys.platform[0:4] == 'java':
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   297
    try:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   298
        import javax.imageio
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   299
        import java.awt.image
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   300
        haveImages = 1
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   301
    except:
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   302
        haveImages = 0
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   303
else:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   304
    try:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   305
        from PIL import Image
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   306
    except ImportError:
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   307
        try:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   308
            import Image
2053
2a962a30dc46 Removed checkImportError
rgbecker
parents: 2051
diff changeset
   309
        except ImportError:
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   310
            Image = None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   311
    haveImages = Image is not None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   312
    if haveImages: del Image
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   313
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   314
1586
d600d8ffaa21 This seems adequate if we avoid Dinuisms like writing to initialised StringIOs
rgbecker
parents: 1584
diff changeset
   315
__StringIO=None
1580
50792f072538 use generalised StringIO instance interface
rgbecker
parents: 1575
diff changeset
   316
def getStringIO(buf=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   317
    '''unified StringIO instance interface'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   318
    global __StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   319
    if not __StringIO:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   320
        try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   321
            from cStringIO import StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   322
        except ImportError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   323
            from StringIO import StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   324
        __StringIO = StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   325
    return buf is not None and __StringIO(buf) or __StringIO()
1580
50792f072538 use generalised StringIO instance interface
rgbecker
parents: 1575
diff changeset
   326
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   327
class ArgvDictValue:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   328
    '''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
   329
    def __init__(self,value,func):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   330
        self.value = value
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   331
        self.func = func
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   332
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   333
def getArgvDict(**kw):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   334
    ''' 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
   335
        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
   336
        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
   337
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   338
    def handleValue(v,av,func):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   339
        if func:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   340
            v = func(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   341
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   342
            t = type(v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   343
            if t is StringType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   344
                v = av
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   345
            elif t is FloatType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   346
                v = float(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   347
            elif t is IntType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   348
                v = int(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   349
            elif t is ListType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   350
                v = list(eval(av))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   351
            elif t is TupleType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   352
                v = tuple(eval(av))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   353
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   354
                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
   355
        return v
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   356
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   357
    A = sys.argv[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   358
    R = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   359
    for k, v in kw.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   360
        if isinstance(v,ArgvDictValue):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   361
            v, func = v.value, v.func
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   362
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   363
            func = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   364
        handled = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   365
        ke = k+'='
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   366
        for a in A:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   367
            if string.find(a,ke)==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   368
                av = a[len(ke):]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   369
                A.remove(a)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   370
                R[k] = handleValue(v,av,func)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   371
                handled = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   372
                break
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   373
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   374
        if not handled: R[k] = handleValue(v,v,func)
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   375
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   376
    return R
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   377
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   378
def getHyphenater(hDict=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   379
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   380
        from reportlab.lib.pyHnj import Hyphen
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   381
        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
   382
        return Hyphen(hDict)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   383
    except ImportError, errMsg:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   384
        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
   385
        return None
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   386
448
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   387
def _className(self):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   388
    '''Return a shortened class name'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   389
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   390
        name = self.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   391
        i=string.rfind(name,'.')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   392
        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
   393
        return name
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   394
    except AttributeError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   395
        return str(self)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   396
2256
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   397
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   398
import glob, fnmatch
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   399
if sys.platform=='win32':
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   400
    def __startswith_rl(fn,_RL_DIR=_RL_DIR.upper(),__RL_DIR=__RL_DIR.upper()):
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   401
        '''if the name starts with a known prefix strip it off'''
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   402
        fn = fn.replace('/',os.sep)
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   403
        if fn.upper().startswith(_RL_DIR): return 1,fn[_loaderpfxlen:]
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   404
        if fn.upper().startswith(__RL_DIR): return 1,fn[_loaderpfxlenR:]
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   405
        return 0,fn
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   406
else:
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   407
    def __startswith_rl(fn):
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   408
        '''if the name starts with a known prefix strip it off'''
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   409
        fn = fn.replace('/',os.sep)
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   410
        if fn.startswith(_RL_DIR): return 1,fn[_loaderpfxlen:]
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   411
        if fn.startswith(__RL_DIR): return 1,fn[_loaderpfxlenR:]
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   412
        return 0,fn
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   413
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   414
def _startswith_rl(fn):
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   415
    return __startswith_rl(fn)[1]
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   416
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   417
def rl_glob(pattern,glob=glob.glob,fnmatch=fnmatch.fnmatch, _RL_DIR=_RL_DIR,pjoin=os.path.join):
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   418
    c, pfn = __startswith_rl(pattern)
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   419
    r = glob(pattern)
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   420
    if c or r==[]:
2258
08e55f5390cc Remove things that python 2.1 fails on
rgbecker
parents: 2256
diff changeset
   421
        r += map(lambda x,_RL_DIR=_RL_DIR,pjoin=pjoin: pjoin(_RL_DIR,x),filter(lambda x,pattern=pattern,fnmatch=fnmatch: fnmatch(x,pattern),__loader__._files.keys()))
2256
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   422
    return r
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   423
del glob, fnmatch
be9ddb36ce9a Added rl_glob
rgbecker
parents: 2251
diff changeset
   424
1575
7d2360bdfc4d Added open_for_read
rgbecker
parents: 1561
diff changeset
   425
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
   426
    '''attempt to open a file or URL for reading'''
2229
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2227
diff changeset
   427
    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
   428
    import urllib
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
        t, o = urllib.splittype(name)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   431
        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
   432
        o = urllib.urlopen(name)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   433
        return getStringIO(o.read())
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   434
    except:
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   435
        if 'r' not in mode: mode = 'r'+mode
2231
19537d9c99f1 First attempt at reading from the zip archive
rgbecker
parents: 2229
diff changeset
   436
        try:
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   437
            return open(name,mode)
2231
19537d9c99f1 First attempt at reading from the zip archive
rgbecker
parents: 2229
diff changeset
   438
        except IOError:
19537d9c99f1 First attempt at reading from the zip archive
rgbecker
parents: 2229
diff changeset
   439
            t, v = sys.exc_info()[:2]
19537d9c99f1 First attempt at reading from the zip archive
rgbecker
parents: 2229
diff changeset
   440
            if _isFSD or __loader__ is None: raise
19537d9c99f1 First attempt at reading from the zip archive
rgbecker
parents: 2229
diff changeset
   441
            try:
19537d9c99f1 First attempt at reading from the zip archive
rgbecker
parents: 2229
diff changeset
   442
                #we have a __loader__, perhaps the filename starts with
19537d9c99f1 First attempt at reading from the zip archive
rgbecker
parents: 2229
diff changeset
   443
                #the dirname(reportlab.__file__) or is relative
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   444
                name = _startswith_rl(name)
2231
19537d9c99f1 First attempt at reading from the zip archive
rgbecker
parents: 2229
diff changeset
   445
                s = __loader__.get_data(name)
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   446
                if 'b' not in mode and os.linesep!='\n': s = s.replace(os.linesep,'\n')
2231
19537d9c99f1 First attempt at reading from the zip archive
rgbecker
parents: 2229
diff changeset
   447
                return getStringIO(s)
19537d9c99f1 First attempt at reading from the zip archive
rgbecker
parents: 2229
diff changeset
   448
            except:
19537d9c99f1 First attempt at reading from the zip archive
rgbecker
parents: 2229
diff changeset
   449
                raise t, v
1575
7d2360bdfc4d Added open_for_read
rgbecker
parents: 1561
diff changeset
   450
2225
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   451
def open_and_read(name,mode='b'):
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   452
    return open_for_read(name,mode).read()
783cbac215e5 Improve the fs distro check
rgbecker
parents: 2215
diff changeset
   453
2247
256cea0756a9 Added open_and_readlines
rgbecker
parents: 2244
diff changeset
   454
def open_and_readlines(name,mode='t'):
256cea0756a9 Added open_and_readlines
rgbecker
parents: 2244
diff changeset
   455
    return open_and_read(name,mode).split('\n')
256cea0756a9 Added open_and_readlines
rgbecker
parents: 2244
diff changeset
   456
2240
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   457
def rl_isfile(fn,os_path_isfile=os.path.isfile):
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   458
    if hasattr(fn,'read'): return True
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   459
    if os_path_isfile(fn): return True
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   460
    if _isFSD or __loader__ is None: return False
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   461
    fn = _startswith_rl(fn)
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   462
    return fn in __loader__._files.keys()
2240
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   463
2244
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   464
def rl_isdir(pn,os_path_isdir=os.path.isdir):
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   465
    if os_path_isdir(pn): return True
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   466
    if _isFSD or __loader__ is None: return False
58148991bc4d Improvements for compact running
rgbecker
parents: 2240
diff changeset
   467
    pn = _startswith_rl(pn)
2258
08e55f5390cc Remove things that python 2.1 fails on
rgbecker
parents: 2256
diff changeset
   468
    return len(filter(lambda x,pn=pn: x.startswith(pn),__loader__._files.keys()))>0
2240
068e2487e780 Added rl_isfile
rgbecker
parents: 2231
diff changeset
   469
2301
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   470
def rl_get_module(name,dir):
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   471
    if sys.modules.has_key(name):
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   472
        om = sys.modules[name]
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   473
        del sys.modules[name]
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   474
    else:
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   475
        om = None
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   476
    try:
2302
240a26f9ee8f rl_get_module now seems to work as intended
rgbecker
parents: 2301
diff changeset
   477
        f = None
2301
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   478
        try:
2302
240a26f9ee8f rl_get_module now seems to work as intended
rgbecker
parents: 2301
diff changeset
   479
            f, p, desc= imp.find_module(name,[dir])
2301
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   480
            return imp.load_module(name,f,p,desc)
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   481
        except:
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   482
            if isCompactDistro() and not os.path.isabs(dir):
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   483
                #attempt a load from inside the zip archive
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   484
                import zipimport
2302
240a26f9ee8f rl_get_module now seems to work as intended
rgbecker
parents: 2301
diff changeset
   485
                zi = zipimport.zipimporter(os.path.join(__loader__.archive,dir.replace('/',os.sep)))
2301
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   486
                return zi.load_module(name)
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   487
            raise ImportError('%s[%s]' % (name,dir))
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   488
    finally:
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   489
        if om: sys.modules[name] = om
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   490
        del om
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   491
        if f: f.close()
3df8c1c14c0e rl_get_module added
rgbecker
parents: 2300
diff changeset
   492
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   493
class ImageReader:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   494
    "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
   495
    def __init__(self, fileName):
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   496
        if not haveImages:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   497
            warnOnce('Imaging Library not available, unable to import bitmaps')
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   498
            return
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   499
        #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
   500
        #the relevant engine.
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   501
        self.fileName = fileName
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   502
        self._image = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   503
        self._width = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   504
        self._height = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   505
        self._transparent = None
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   506
        self._data = None
2229
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2227
diff changeset
   507
        self.fp = open_for_read(fileName,'b')
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   508
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   509
        #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
   510
        if sys.platform[0:4] == 'java':
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   511
            from javax.imageio import ImageIO
2229
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2227
diff changeset
   512
            self._image = ImageIO.read(self.fp)
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   513
        else:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   514
            import PIL.Image
2229
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2227
diff changeset
   515
            self._image = PIL.Image.open(self.fp)
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   516
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   517
    def getSize(self):
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   518
        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
   519
            if sys.platform[0:4] == 'java':
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   520
                self._width = self._image.getWidth()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   521
                self._height = self._image.getHeight()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   522
            else:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   523
                self._width, self._height = self._image.size
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   524
        return (self._width, self._height)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2160
diff changeset
   525
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   526
    def getRGBData(self):
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   527
        "Return byte array of RGB data as string"
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   528
        if self._data is None:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   529
            if sys.platform[0:4] == 'java':
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   530
                import jarray
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   531
                from java.awt.image import PixelGrabber
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   532
                width, height = self.getSize()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   533
                buffer = jarray.zeros(width*height, 'i')
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   534
                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
   535
                pg.grabPixels()
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   536
                # 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
   537
                # I just haven't found it yet...
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   538
                pixels = []
2059
e5eec1dd6f0c ImageReader can now take File objects as well as Strings
dragan1
parents: 2057
diff changeset
   539
                a = pixels.append
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   540
                for i in range(len(buffer)):
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   541
                    rgb = buffer[i]
2087
5c4b76884d9d fixed java file read bug
dragan1
parents: 2086
diff changeset
   542
                    a(chr((rgb>>16)&0xff))
5c4b76884d9d fixed java file read bug
dragan1
parents: 2086
diff changeset
   543
                    a(chr((rgb>>8)&0xff))
2059
e5eec1dd6f0c ImageReader can now take File objects as well as Strings
dragan1
parents: 2057
diff changeset
   544
                    a(chr(rgb&0xff))
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   545
                self._data = ''.join(pixels)
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   546
            else:
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   547
                rgb = self._image.convert('RGB')
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   548
                self._data = rgb.tostring()
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2160
diff changeset
   549
        return self._data
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   550
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   551
    def getImageData(self):
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   552
        width, height = self.getSize()
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   553
        return width, height, self.getRGBData()
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   554
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   555
    def getTransparent(self):
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   556
        if sys.platform[0:4] == 'java':
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   557
            return None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   558
        else:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   559
            if self._image.info.has_key("transparency"):
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   560
                transparency = self._image.info["transparency"] * 3
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   561
                palette = self._image.palette
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   562
                try:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   563
                    palette = palette.palette
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   564
                except:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   565
                    palette = palette.data
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   566
                return map(ord, palette[transparency:transparency+3])
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   567
            else:
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   568
                return None
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   569
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   570
def getImageData(imageFileName):
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   571
    "Get width, height and RGB pixels from image file.  Wraps Java/PIL"
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2044
diff changeset
   572
    return ImageReader.getImageData(imageFileName)
2044
3be472f4a6dd Various work on abstracting out images, plus outstanding patches
andy_robinson
parents: 2007
diff changeset
   573
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   574
class DebugMemo:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   575
    '''Intended as a simple report back encapsulator
1545
966ccacc4c7d Changes to DebugMemo.__doc__
rgbecker
parents: 1543
diff changeset
   576
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   577
    Typical usages
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   578
    1) To record error data
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   579
        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
   580
        dbg.add(anotherPayload='aaaa',andagain='bbb')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   581
        dbg.dump()
1543
3681c7d8898d Slight formatting improvements & added payload method
rgbecker
parents: 1538
diff changeset
   582
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   583
    2) To show the recorded info
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   584
        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
   585
        dbg.load()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   586
        dbg.show()
1543
3681c7d8898d Slight formatting improvements & added payload method
rgbecker
parents: 1538
diff changeset
   587
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   588
    3) To re-use recorded information
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   589
        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
   590
            dbg.load()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   591
        myTestFunc(dbg.payload('myVar'),dbg.payload('andagain'))
1545
966ccacc4c7d Changes to DebugMemo.__doc__
rgbecker
parents: 1543
diff changeset
   592
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   593
    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
   594
    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
   595
    '''
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   596
    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
   597
        import time, socket
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   598
        self.fn = fn
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   599
        if mode!='w': return
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   600
        if not stdout: 
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   601
            self.stdout = sys.stdout
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   602
        else:
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   603
            if hasattr(stdout,'write'):
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   604
                self.stdout = stdout
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   605
            else:
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   606
                self.stdout = open(stdout,'w')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   607
        self.store = store = {}
2296
8f5419265559 Added capture_traceback=1 to DebugMemo init
rgbecker
parents: 2279
diff changeset
   608
        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
   609
            import traceback
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   610
            s = getStringIO()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   611
            traceback.print_exc(None,s)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   612
            store['__traceback'] = s.getvalue()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   613
        cwd=os.getcwd()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   614
        lcwd = os.listdir(cwd)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   615
        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
   616
        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
   617
                        'platform': sys.platform,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   618
                        'version': sys.version,
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   619
                        'hexversion': hex(sys.hexversion),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   620
                        'executable': sys.executable,
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   621
                        'exec_prefix': sys.exec_prefix,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   622
                        'prefix': sys.prefix,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   623
                        'path': sys.path,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   624
                        'argv': sys.argv,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   625
                        'cwd': cwd,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   626
                        'hostname': socket.gethostname(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   627
                        'lcwd': lcwd,
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   628
                        'byteorder': sys.byteorder,
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   629
                        'maxint': sys.maxint,
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   630
                        'maxint': getattr(sys,'maxunicode','????'),
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   631
                        'api_version': getattr(sys,'api_version','????'),
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   632
                        'version_info': getattr(sys,'version_info','????'),
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   633
                        'winver': getattr(sys,'winver','????'),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   634
                        })
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   635
        for M,A in (
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   636
                (sys,('getwindowsversion','getfilesystemencoding')),
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   637
                (os,('uname', 'ctermid', 'getgid', 'getuid', 'getegid',
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   638
                    'geteuid', 'getlogin', 'getgroups', 'getpgrp', 'getpid', 'getppid',
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   639
                    )),
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   640
                ):
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   641
            for a in A:
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   642
                if hasattr(M,a):
2300
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   643
                    try:
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   644
                        store[a] = getattr(M,a)()
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   645
                    except:
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   646
                        pass
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   647
        if exed!=cwd:
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   648
            try:
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   649
                store.update({'exed': exed, 'lexed': os.listdir(exed),})
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   650
            except:
3896af138a75 Attempt to robustify the memo
rgbecker
parents: 2297
diff changeset
   651
                pass
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   652
        if getScript:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   653
            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
   654
            if os.path.isfile(fn):
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   655
                try:
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   656
                    store['__script'] = (fn,open(fn,'r').read())
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   657
                except:
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   658
                    pass
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   659
        module_versions = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   660
        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
   661
            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
   662
                v = getattr(m,'__version__',None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   663
                if v: module_versions[n] = v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   664
        store['__module_versions'] = module_versions
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   665
        self.store['__payload'] = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   666
        self._add(kw)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   667
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   668
    def _add(self,D):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   669
        payload = self.store['__payload']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   670
        for k, v in D.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   671
            payload[k] = v
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   672
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   673
    def add(self,**kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   674
        self._add(kw)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   675
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   676
    def dump(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   677
        import pickle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   678
        pickle.dump(self.store,open(self.fn,'wb'))
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   679
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   680
    def load(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   681
        import pickle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   682
        self.store = pickle.load(open(self.fn,'rb'))
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   683
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   684
    def _show_module_versions(self,k,v):
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   685
        self._writeln(k[2:])
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   686
        K = v.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   687
        K.sort()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   688
        for k in K:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   689
            vk = v[k]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   690
            try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   691
                m = recursiveImport(k,sys.path[:],1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   692
                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
   693
            except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   694
                m = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   695
                d = '??????unknown??????'
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   696
            self._writeln('  %s = %s (%s)' % (k,vk,d))
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   697
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   698
    def _banner(self,k,what):
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   699
        self._writeln('###################%s %s##################' % (what,k[2:]))
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   700
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   701
    def _start(self,k):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   702
        self._banner(k,'Start  ')
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   703
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   704
    def _finish(self,k):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   705
        self._banner(k,'Finish ')
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   706
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   707
    def _show_lines(self,k,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   708
        self._start(k)
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   709
        self._writeln(v)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   710
        self._finish(k)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   711
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   712
    def _show_file(self,k,v):
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   713
        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
   714
        self._show_lines(k,v[1])
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   715
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   716
    def _show_payload(self,k,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   717
        if v:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   718
            import pprint
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   719
            self._start(k)
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   720
            pprint.pprint(v,self.stdout)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   721
            self._finish(k)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   722
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   723
    specials = {'__module_versions': _show_module_versions,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   724
                '__payload': _show_payload,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   725
                '__traceback': _show_lines,
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   726
                '__script': _show_file,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   727
                }
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   728
    def show(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   729
        K = self.store.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   730
        K.sort()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   731
        for k in K:
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   732
            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
   733
        for k in K:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   734
            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
   735
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   736
    def payload(self,name):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   737
        return self.store['__payload'][name]
1561
1b10f25e76df Add DebugMemo __g/setitem__
rgbecker
parents: 1545
diff changeset
   738
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   739
    def __setitem__(self,name,value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   740
        self.store['__payload'][name] = value
1561
1b10f25e76df Add DebugMemo __g/setitem__
rgbecker
parents: 1545
diff changeset
   741
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   742
    def __getitem__(self,name):
1833
135322abc191 Fix recursivImport for case when baseDir is a sequence
rgbecker
parents: 1821
diff changeset
   743
        return self.store['__payload'][name]
2297
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   744
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   745
    def _writeln(self,msg):
7c706ac8a6b7 Added some more stuff and script name to DebugMemo output
rgbecker
parents: 2296
diff changeset
   746
        self.stdout.write(msg+'\n')