reportlab/lib/utils.py
author andy_robinson
Wed, 17 Jul 2002 22:46:24 +0000
changeset 1677 1450177dd19e
parent 1650 c5a55092eecc
child 1683 7fa753e4420a
permissions -rw-r--r--
Exterminated all tab characters and added a test to make sure they never come back.
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
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/lib/utils.py,v 1.35 2002/07/17 22:46:23 andy_robinson Exp $
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
     5
__version__=''' $Id: utils.py,v 1.35 2002/07/17 22:46:23 andy_robinson Exp $ '''
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 519
diff changeset
     6
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
     7
import string, os, sys
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
     8
from types import *
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
     9
from reportlab.lib.logger import warnOnce
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    10
SeqTypes = (ListType,TupleType)
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 519
diff changeset
    11
1143
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    12
def _checkImportError(errMsg):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    13
    if string.lower(string.strip(str(errMsg)[0:16]))!='no module named': raise
1143
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    14
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    15
try:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    16
    #raise ImportError
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    17
    ### 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
    18
    ### 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
    19
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    20
        from _rl_accel import fp_str                # in case of builtin version
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    21
    except ImportError, errMsg:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    22
        _checkImportError(errMsg)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    23
        from reportlab.lib._rl_accel import fp_str  # specific
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    24
except ImportError, errMsg:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    25
    #_checkImportError(errMsg) # this effectively requires _rl_accel... should not be required
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    26
    def fp_str(*a):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    27
        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
    28
        s = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    29
        for i in a:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    30
            s.append('%0.6f' % i)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    31
        return string.join(s)
448
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
    32
981
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
    33
#hack test for comma users
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
    34
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
    35
    _FP_STR = fp_str
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    36
    def fp_str(*a):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    37
        return string.replace(apply(_FP_STR,a),',','.')
981
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
    38
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
    39
def recursiveImport(modulename, baseDir=None, noCWD=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    40
    """Dynamically imports possible packagized module, or raises ImportError"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    41
    import imp
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    42
    parts = string.split(modulename, '.')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    43
    part = parts[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    44
    path = list(baseDir and (type(baseDir) not in SeqTypes and [baseDir] or filter(None,baseDir)) or None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    45
    if not noCWD and '.' not in path: path.insert(0,'.')
1396
40d1361f08b7 Enhanced the error message from recursiveImport
andy_robinson
parents: 1389
diff changeset
    46
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    47
    #make import errors a bit more informative
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    48
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    49
        (file, pathname, description) = imp.find_module(part, path)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    50
        childModule = parentModule = imp.load_module(part, file, pathname, description)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    51
        for name in parts[1:]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    52
            (file, pathname, description) = imp.find_module(name, parentModule.__file__)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    53
            childModule = imp.load_module(name, file, pathname, description)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    54
            setattr(parentModule, name, childModule)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    55
            parentModule = childModule
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    56
    except ImportError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    57
        msg = "cannot import '%s' while attempting recursive import of '%s'" % (part, modulename)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    58
        if baseDir:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    59
            msg = msg + " under paths '%s'" % `path`
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    60
        raise ImportError, msg
1396
40d1361f08b7 Enhanced the error message from recursiveImport
andy_robinson
parents: 1389
diff changeset
    61
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    62
    return childModule
1389
6e5ba87ce34e added function recursiveImport('package1.package2.module') in r/lib/utils
andy_robinson
parents: 1387
diff changeset
    63
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    64
def import_zlib():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    65
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    66
        import zlib
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    67
    except ImportError, errMsg:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    68
        _checkImportError(errMsg)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    69
        zlib = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    70
        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
    71
        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
    72
    return zlib
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    73
760
0e51a12a0e39 Changed to PIL_Image
rgbecker
parents: 677
diff changeset
    74
try:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    75
    from PIL import Image
760
0e51a12a0e39 Changed to PIL_Image
rgbecker
parents: 677
diff changeset
    76
except ImportError, errMsg:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    77
    _checkImportError(errMsg)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    78
    from reportlab.rl_config import PIL_WARNINGS
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    79
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    80
        import Image
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    81
        if PIL_WARNINGS: warnOnce('Python Imaging Library not available as package; upgrade your installation!')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    82
    except ImportError, errMsg:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    83
        _checkImportError(errMsg)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    84
        Image = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    85
        if PIL_WARNINGS: warnOnce('Python Imaging Library not available')
760
0e51a12a0e39 Changed to PIL_Image
rgbecker
parents: 677
diff changeset
    86
PIL_Image = Image
0e51a12a0e39 Changed to PIL_Image
rgbecker
parents: 677
diff changeset
    87
del Image
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    88
1586
d600d8ffaa21 This seems adequate if we avoid Dinuisms like writing to initialised StringIOs
rgbecker
parents: 1584
diff changeset
    89
__StringIO=None
1580
50792f072538 use generalised StringIO instance interface
rgbecker
parents: 1575
diff changeset
    90
def getStringIO(buf=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    91
    '''unified StringIO instance interface'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    92
    global __StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    93
    if not __StringIO:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    94
        try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    95
            from cStringIO import StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    96
        except ImportError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    97
            from StringIO import StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    98
        __StringIO = StringIO
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
    99
    return buf is not None and __StringIO(buf) or __StringIO()
1580
50792f072538 use generalised StringIO instance interface
rgbecker
parents: 1575
diff changeset
   100
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   101
class ArgvDictValue:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   102
    '''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
   103
    def __init__(self,value,func):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   104
        self.value = value
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   105
        self.func = func
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   106
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   107
def getArgvDict(**kw):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   108
    ''' 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
   109
        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
   110
        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
   111
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   112
    def handleValue(v,av,func):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   113
        if func:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   114
            v = func(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   115
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   116
            t = type(v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   117
            if t is StringType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   118
                v = av
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   119
            elif t is FloatType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   120
                v = float(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   121
            elif t is IntType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   122
                v = int(av)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   123
            elif t is ListType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   124
                v = list(eval(av))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   125
            elif t is TupleType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   126
                v = tuple(eval(av))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   127
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   128
                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
   129
        return v
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   130
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   131
    A = sys.argv[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   132
    R = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   133
    for k, v in kw.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   134
        if isinstance(v,ArgvDictValue):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   135
            v, func = v.value, v.func
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   136
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   137
            func = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   138
        handled = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   139
        ke = k+'='
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   140
        for a in A:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   141
            if string.find(a,ke)==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   142
                av = a[len(ke):]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   143
                A.remove(a)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   144
                R[k] = handleValue(v,av,func)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   145
                handled = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   146
                break
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   147
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   148
        if not handled: R[k] = handleValue(v,v,func)
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   149
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   150
    return R
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   151
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   152
def getHyphenater(hDict=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   153
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   154
        from reportlab.lib.pyHnj import Hyphen
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   155
        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
   156
        return Hyphen(hDict)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   157
    except ImportError, errMsg:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   158
        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
   159
        return None
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   160
448
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   161
def _className(self):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   162
    '''Return a shortened class name'''
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
        name = self.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   165
        i=string.rfind(name,'.')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   166
        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
   167
        return name
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   168
    except AttributeError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   169
        return str(self)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   170
1575
7d2360bdfc4d Added open_for_read
rgbecker
parents: 1561
diff changeset
   171
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
   172
    '''attempt to open a file or URL for reading'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   173
    import urllib
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   174
    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   175
        t, o = urllib.splittype(name)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   176
        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
   177
        o = urllib.urlopen(name)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   178
        return getStringIO(o.read())
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   179
    except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   180
        return open(name,'r'+mode)
1575
7d2360bdfc4d Added open_for_read
rgbecker
parents: 1561
diff changeset
   181
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   182
class DebugMemo:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   183
    '''Intended as a simple report back encapsulator
1545
966ccacc4c7d Changes to DebugMemo.__doc__
rgbecker
parents: 1543
diff changeset
   184
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   185
    Typical usages
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   186
    1) To record error data     
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   187
        dbg = DebugMemo(fn='dbgmemo.dbg',myVar=value)       
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   188
        dbg.add(anotherPayload='aaaa',andagain='bbb')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   189
        dbg.dump()
1543
3681c7d8898d Slight formatting improvements & added payload method
rgbecker
parents: 1538
diff changeset
   190
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   191
    2) To show the recorded info
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   192
        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
   193
        dbg.load()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   194
        dbg.show()
1543
3681c7d8898d Slight formatting improvements & added payload method
rgbecker
parents: 1538
diff changeset
   195
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   196
    3) To re-use recorded information
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   197
        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
   198
            dbg.load()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   199
        myTestFunc(dbg.payload('myVar'),dbg.payload('andagain'))
1545
966ccacc4c7d Changes to DebugMemo.__doc__
rgbecker
parents: 1543
diff changeset
   200
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   201
    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
   202
    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
   203
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   204
    def __init__(self,fn='rl_dbgmemo.dbg',mode='w',getScript=1,modules=(),**kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   205
        import time, socket
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   206
        self.fn = fn
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   207
        if mode!='w': return
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   208
        self.store = store = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   209
        if sys.exc_info() != (None,None,None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   210
            import traceback
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   211
            s = getStringIO()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   212
            traceback.print_exc(None,s)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   213
            store['__traceback'] = s.getvalue()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   214
        cwd=os.getcwd()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   215
        lcwd = os.listdir(cwd)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   216
        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
   217
        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
   218
                        'platform': sys.platform,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   219
                        'version': sys.version,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   220
                        'executable': sys.executable,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   221
                        'prefix': sys.prefix,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   222
                        'path': sys.path,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   223
                        'argv': sys.argv,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   224
                        'cwd': cwd,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   225
                        'hostname': socket.gethostname(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   226
                        'lcwd': lcwd,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   227
                        })
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   228
        if exed!=cwd: store.update({'exed': exed,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   229
                                    'lexed': os.listdir(exed),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   230
                                    })
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   231
        if hasattr(os,'uname'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   232
            store.update({
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   233
                'uname': os.uname(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   234
                'ctermid': os.ctermid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   235
                'getgid': os.getgid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   236
                'getuid': os.getuid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   237
                'getegid': os.getegid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   238
                'geteuid': os.geteuid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   239
                'getlogin': os.getlogin(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   240
                'getgroups': os.getgroups(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   241
                'getpgrp': os.getpgrp(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   242
                'getpid': os.getpid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   243
                'getppid': os.getppid(),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   244
                })
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   245
        if getScript:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   246
            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
   247
            if os.path.isfile(fn):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   248
                store['__script'] = open(fn,'r').read()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   249
        module_versions = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   250
        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
   251
            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
   252
                v = getattr(m,'__version__',None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   253
                if v: module_versions[n] = v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   254
        store['__module_versions'] = module_versions
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   255
        self.store['__payload'] = {}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   256
        self._add(kw)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   257
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   258
    def _add(self,D):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   259
        payload = self.store['__payload']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   260
        for k, v in D.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   261
            payload[k] = v
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   262
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   263
    def add(self,**kw):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   264
        self._add(kw)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   265
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   266
    def dump(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   267
        import pickle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   268
        pickle.dump(self.store,open(self.fn,'wb'))
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   269
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   270
    def load(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   271
        import pickle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   272
        self.store = pickle.load(open(self.fn,'rb'))
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   273
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   274
    def _show_module_versions(self,k,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   275
        print k[2:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   276
        K = v.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   277
        K.sort()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   278
        for k in K:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   279
            vk = v[k]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   280
            try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   281
                m = recursiveImport(k,sys.path[:],1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   282
                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
   283
            except:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   284
                m = None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   285
                d = '??????unknown??????'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   286
            print '  %s = %s (%s)' % (k,vk,d)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   287
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   288
    def _banner(self,k,what):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   289
        print '###################%s %s##################' % (what,k[2:])
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   290
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   291
    def _start(self,k):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   292
        self._banner(k,'Start  ')
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   293
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   294
    def _finish(self,k):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   295
        self._banner(k,'Finish ')
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   296
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   297
    def _show_lines(self,k,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   298
        self._start(k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   299
        print v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   300
        self._finish(k)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   301
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   302
    def _show_payload(self,k,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   303
        if v:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   304
            import pprint
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   305
            self._start(k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   306
            pprint.pprint(v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   307
            self._finish(k)
1538
faefcdc303a9 Added DebugMemo stuff
rgbecker
parents: 1537
diff changeset
   308
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   309
    specials = {'__module_versions': _show_module_versions,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   310
                '__payload': _show_payload,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   311
                '__traceback': _show_lines,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   312
                '__script': _show_lines,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   313
                }
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   314
    def show(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   315
        K = self.store.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   316
        K.sort()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   317
        for k in K:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   318
            if k not in self.specials.keys(): print '%-15s = %s' % (k,self.store[k])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   319
        for k in K:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   320
            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
   321
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   322
    def payload(self,name):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   323
        return self.store['__payload'][name]
1561
1b10f25e76df Add DebugMemo __g/setitem__
rgbecker
parents: 1545
diff changeset
   324
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   325
    def __setitem__(self,name,value):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   326
        self.store['__payload'][name] = value
1561
1b10f25e76df Add DebugMemo __g/setitem__
rgbecker
parents: 1545
diff changeset
   327
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   328
    def __getitem__(self,name):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1650
diff changeset
   329
        return self.store['__payload'][name]