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