author | andy |
Wed, 21 May 2008 10:24:47 +0000 | |
changeset 2929 | 02a5623f4f26 |
parent 2902 | f15ac27dc73f |
child 2942 | e6e20484c315 |
permissions | -rw-r--r-- |
2625
71abbda1f58c
reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
2565
diff
changeset
|
1 |
#Copyright ReportLab Europe Ltd. 2000-2006 |
494 | 2 |
#see license.txt for license details |
2625
71abbda1f58c
reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
2565
diff
changeset
|
3 |
# $URI:$ |
2327
c40a1b477f3f
Forgot to check the exactly equal case for _startswith_rl
rgbecker
parents:
2313
diff
changeset
|
4 |
__version__=''' $Id$ ''' |
562 | 5 |
|
2902
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
6 |
import string, os, sys, imp, time, md5 |
2499 | 7 |
from reportlab.lib.logger import warnOnce |
413 | 8 |
from types import * |
2625
71abbda1f58c
reportlab.lib: break out rltempfile.py to avoid rl_accel imports
rgbecker
parents:
2565
diff
changeset
|
9 |
from rltempfile import get_rl_tempfile, get_rl_tempdir, _rl_getuid |
413 | 10 |
SeqTypes = (ListType,TupleType) |
2499 | 11 |
if sys.hexversion<0x2020000: |
12 |
def isSeqType(v): |
|
2502 | 13 |
return type(v) in SeqTypes |
2499 | 14 |
else: |
15 |
def isSeqType(v): |
|
16 |
return isinstance(v,(tuple,list)) |
|
17 |
||
2259 | 18 |
if sys.hexversion<0x2030000: |
19 |
True = 1 |
|
20 |
False = 0 |
|
562 | 21 |
|
2902
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
22 |
if sys.hexversion >= 0x02000000: |
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
23 |
def _digester(s): |
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
24 |
return md5.md5(s).hexdigest() |
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
25 |
else: |
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
26 |
# hexdigest not available in 1.5 |
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
27 |
def _digester(s): |
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
28 |
return join(map(lambda x : "%02x" % ord(x), md5.md5(s).digest()), '') |
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
29 |
|
2215 | 30 |
def _findFiles(dirList,ext='.ttf'): |
31 |
from os.path import isfile, isdir, join as path_join |
|
32 |
from os import listdir |
|
33 |
ext = ext.lower() |
|
34 |
R = [] |
|
35 |
A = R.append |
|
36 |
for D in dirList: |
|
37 |
if not isdir(D): continue |
|
38 |
for fn in listdir(D): |
|
39 |
fn = path_join(D,fn) |
|
40 |
if isfile(fn) and (not ext or fn.lower().endswith(ext)): A(fn) |
|
41 |
return R |
|
42 |
||
43 |
try: |
|
44 |
_UserDict = dict |
|
45 |
except: |
|
46 |
from UserDict import UserDict as _UserDict |
|
47 |
||
48 |
class CIDict(_UserDict): |
|
49 |
def __init__(self,*a,**kw): |
|
50 |
map(self.update, a) |
|
51 |
self.update(kw) |
|
52 |
||
53 |
def update(self,D): |
|
54 |
for k,v in D.items(): self[k] = v |
|
55 |
||
56 |
def __setitem__(self,k,v): |
|
57 |
try: |
|
58 |
k = k.lower() |
|
59 |
except: |
|
60 |
pass |
|
61 |
_UserDict.__setitem__(self,k,v) |
|
62 |
||
63 |
def __getitem__(self,k): |
|
64 |
try: |
|
65 |
k = k.lower() |
|
66 |
except: |
|
67 |
pass |
|
68 |
return _UserDict.__getitem__(self,k) |
|
69 |
||
70 |
def __delitem__(self,k): |
|
71 |
try: |
|
72 |
k = k.lower() |
|
73 |
except: |
|
74 |
pass |
|
75 |
return _UserDict.__delitem__(self,k) |
|
76 |
||
77 |
def get(self,k,dv=None): |
|
78 |
try: |
|
79 |
return self[k] |
|
80 |
except KeyError: |
|
81 |
return dv |
|
82 |
||
83 |
def has_key(self,k): |
|
84 |
try: |
|
85 |
self[k] |
|
86 |
return True |
|
87 |
except: |
|
88 |
return False |
|
89 |
||
90 |
def pop(self,k,*a): |
|
91 |
try: |
|
92 |
k = k.lower() |
|
93 |
except: |
|
94 |
pass |
|
95 |
return _UserDict.pop(*((self,k)+a)) |
|
96 |
||
97 |
def setdefault(self,k,*a): |
|
98 |
try: |
|
99 |
k = k.lower() |
|
100 |
except: |
|
101 |
pass |
|
102 |
return _UserDict.setdefault(*((self,k)+a)) |
|
1837
a3920893b1b8
Back in synch, diagnostic function for distro type added
andy_robinson
parents:
1835
diff
changeset
|
103 |
|
1902 | 104 |
if os.name == 'mac': |
105 |
#with the Mac, we need to tag the file in a special |
|
106 |
#way so the system knows it is a PDF file. |
|
107 |
#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
|
108 |
import macfs, macostools |
2007 | 109 |
_KNOWN_MAC_EXT = { |
110 |
'BMP' : ('ogle','BMP '), |
|
111 |
'EPS' : ('ogle','EPSF'), |
|
112 |
'EPSF': ('ogle','EPSF'), |
|
113 |
'GIF' : ('ogle','GIFf'), |
|
114 |
'JPG' : ('ogle','JPEG'), |
|
115 |
'JPEG': ('ogle','JPEG'), |
|
116 |
'PCT' : ('ttxt','PICT'), |
|
117 |
'PICT': ('ttxt','PICT'), |
|
118 |
'PNG' : ('ogle','PNGf'), |
|
119 |
'PPM' : ('ogle','.PPM'), |
|
120 |
'TIF' : ('ogle','TIFF'), |
|
121 |
'TIFF': ('ogle','TIFF'), |
|
2051
50350756e12c
Added HTML to the Mac markfilename routine - HTML files should now 'know'
johnprecedo
parents:
2045
diff
changeset
|
122 |
'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
|
123 |
'HTML': ('MSIE','TEXT'), |
2007 | 124 |
} |
125 |
def markfilename(filename,creatorcode=None,filetype=None,ext='PDF'): |
|
1902 | 126 |
try: |
2007 | 127 |
if creatorcode is None or filetype is None and ext is not None: |
128 |
try: |
|
2057
22e7a13fa031
Fixed so that file creator and types are now correctly set on the Mac again
rptlab
parents:
2053
diff
changeset
|
129 |
creatorcode, filetype = _KNOWN_MAC_EXT[string.upper(ext)] |
2007 | 130 |
except: |
131 |
return |
|
1902 | 132 |
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
|
133 |
macostools.touched(filename) |
1902 | 134 |
except: |
135 |
pass |
|
136 |
else: |
|
137 |
def markfilename(filename,creatorcode=None,filetype=None): |
|
138 |
pass |
|
139 |
||
2244 | 140 |
import reportlab |
2251 | 141 |
__RL_DIR=os.path.dirname(reportlab.__file__) #possibly relative |
142 |
_RL_DIR=os.path.isabs(__RL_DIR) and __RL_DIR or os.path.abspath(__RL_DIR) |
|
2244 | 143 |
del reportlab |
144 |
||
2225 | 145 |
#Attempt to detect if this copy of reportlab is running in a |
146 |
#file system (as opposed to mostly running in a zip or McMillan |
|
147 |
#archive or Jar file). This is used by test cases, so that |
|
148 |
#we can write test cases that don't get activated in a compiled |
|
2244 | 149 |
try: |
150 |
__file__ |
|
151 |
except: |
|
152 |
__file__ = sys.argv[0] |
|
2305 | 153 |
import glob, fnmatch |
2244 | 154 |
try: |
155 |
_isFSD = not __loader__ |
|
2484 | 156 |
_archive = os.path.normcase(os.path.normpath(__loader__.archive)) |
2327
c40a1b477f3f
Forgot to check the exactly equal case for _startswith_rl
rgbecker
parents:
2313
diff
changeset
|
157 |
_archivepfx = _archive + os.sep |
2484 | 158 |
_archivedir = os.path.dirname(_archive) |
2327
c40a1b477f3f
Forgot to check the exactly equal case for _startswith_rl
rgbecker
parents:
2313
diff
changeset
|
159 |
_archivedirpfx = _archivedir + os.sep |
2305 | 160 |
_archivepfxlen = len(_archivepfx) |
161 |
_archivedirpfxlen = len(_archivedirpfx) |
|
2352 | 162 |
def __startswith_rl(fn, |
2484 | 163 |
_archivepfx=_archivepfx, |
164 |
_archivedirpfx=_archivedirpfx, |
|
165 |
_archive=_archive, |
|
166 |
_archivedir=_archivedir, |
|
2352 | 167 |
os_path_normpath=os.path.normpath, |
168 |
os_path_normcase=os.path.normcase, |
|
169 |
os_getcwd=os.getcwd, |
|
170 |
os_sep=os.sep, |
|
171 |
os_sep_len = len(os.sep)): |
|
172 |
'''if the name starts with a known prefix strip it off''' |
|
173 |
fn = os_path_normpath(fn.replace('/',os_sep)) |
|
174 |
nfn = os_path_normcase(fn) |
|
175 |
if nfn in (_archivedir,_archive): return 1,'' |
|
176 |
if nfn.startswith(_archivepfx): return 1,fn[_archivepfxlen:] |
|
177 |
if nfn.startswith(_archivedirpfx): return 1,fn[_archivedirpfxlen:] |
|
178 |
cwd = os_path_normcase(os_getcwd()) |
|
179 |
n = len(cwd) |
|
180 |
if nfn.startswith(cwd): |
|
181 |
if fn[n:].startswith(os_sep): return 1, fn[n+os_sep_len:] |
|
182 |
if n==len(fn): return 1,'' |
|
183 |
return not os.path.isabs(fn),fn |
|
2305 | 184 |
|
185 |
def _startswith_rl(fn): |
|
186 |
return __startswith_rl(fn)[1] |
|
187 |
||
188 |
def rl_glob(pattern,glob=glob.glob,fnmatch=fnmatch.fnmatch, _RL_DIR=_RL_DIR,pjoin=os.path.join): |
|
189 |
c, pfn = __startswith_rl(pattern) |
|
2307 | 190 |
r = glob(pfn) |
2305 | 191 |
if c or r==[]: |
2307 | 192 |
r += map(lambda x,D=_archivepfx,pjoin=pjoin: pjoin(_archivepfx,x),filter(lambda x,pfn=pfn,fnmatch=fnmatch: fnmatch(x,pfn),__loader__._files.keys())) |
2305 | 193 |
return r |
2244 | 194 |
except: |
195 |
_isFSD = os.path.isfile(__file__) #slight risk of wrong path |
|
196 |
__loader__ = None |
|
2305 | 197 |
def _startswith_rl(fn): |
198 |
return fn |
|
199 |
def rl_glob(pattern,glob=glob.glob): |
|
200 |
return glob(pattern) |
|
201 |
del glob, fnmatch |
|
2244 | 202 |
_isFSSD = _isFSD and os.path.isfile(os.path.splitext(__file__)[0] +'.py') |
203 |
||
1837
a3920893b1b8
Back in synch, diagnostic function for distro type added
andy_robinson
parents:
1835
diff
changeset
|
204 |
def isFileSystemDistro(): |
2225 | 205 |
'''return truth if a file system distribution''' |
206 |
return _isFSD |
|
1837
a3920893b1b8
Back in synch, diagnostic function for distro type added
andy_robinson
parents:
1835
diff
changeset
|
207 |
|
a3920893b1b8
Back in synch, diagnostic function for distro type added
andy_robinson
parents:
1835
diff
changeset
|
208 |
def isCompactDistro(): |
2225 | 209 |
'''return truth if not a file system distribution''' |
210 |
return not _isFSD |
|
1837
a3920893b1b8
Back in synch, diagnostic function for distro type added
andy_robinson
parents:
1835
diff
changeset
|
211 |
|
a3920893b1b8
Back in synch, diagnostic function for distro type added
andy_robinson
parents:
1835
diff
changeset
|
212 |
def isSourceDistro(): |
2225 | 213 |
'''return truth if a source file system distribution''' |
214 |
return _isFSSD |
|
1143 | 215 |
|
413 | 216 |
try: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
217 |
#raise ImportError |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
218 |
### 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
|
219 |
### 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
|
220 |
try: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
221 |
from _rl_accel import fp_str # in case of builtin version |
2053 | 222 |
except ImportError: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
223 |
from reportlab.lib._rl_accel import fp_str # specific |
2053 | 224 |
except ImportError: |
2139 | 225 |
from math import log |
226 |
_log_10 = lambda x,log=log,_log_e_10=log(10.0): log(x)/_log_e_10 |
|
227 |
_fp_fmts = "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f", "%.6f" |
|
228 |
import re |
|
229 |
_tz_re = re.compile('0+$') |
|
230 |
del re |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
231 |
def fp_str(*a): |
2499 | 232 |
if len(a)==1 and isSeqType(a[0]): a = a[0] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
233 |
s = [] |
2139 | 234 |
A = s.append |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
235 |
for i in a: |
2139 | 236 |
sa =abs(i) |
237 |
if sa<=1e-7: A('0') |
|
238 |
else: |
|
239 |
l = sa<=1 and 6 or min(max(0,(6-int(_log_10(sa)))),6) |
|
240 |
n = _fp_fmts[l]%i |
|
241 |
if l: |
|
242 |
n = _tz_re.sub('',n) |
|
243 |
try: |
|
244 |
if n[-1]=='.': n = n[:-1] |
|
245 |
except: |
|
246 |
print i, n |
|
247 |
raise |
|
248 |
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
|
249 |
return string.join(s) |
448 | 250 |
|
981 | 251 |
#hack test for comma users |
252 |
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
|
253 |
_FP_STR = fp_str |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
254 |
def fp_str(*a): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
255 |
return string.replace(apply(_FP_STR,a),',','.') |
981 | 256 |
|
1821
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
257 |
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
|
258 |
"""Dynamically imports possible packagized module, or raises ImportError""" |
2160 | 259 |
normalize = lambda x: os.path.normcase(os.path.abspath(os.path.normpath(x))) |
260 |
path = map(normalize,sys.path) |
|
2156 | 261 |
if baseDir: |
2499 | 262 |
if not isSeqType(baseDir): |
2156 | 263 |
tp = [baseDir] |
1833
135322abc191
Fix recursivImport for case when baseDir is a sequence
rgbecker
parents:
1821
diff
changeset
|
264 |
else: |
2156 | 265 |
tp = filter(None,list(baseDir)) |
266 |
for p in tp: |
|
2160 | 267 |
p = normalize(p) |
2156 | 268 |
if p not in path: path.insert(0,p) |
1396
40d1361f08b7
Enhanced the error message from recursiveImport
andy_robinson
parents:
1389
diff
changeset
|
269 |
|
1821
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
270 |
if noCWD: |
2160 | 271 |
for p in ('','.',normalize('.')): |
272 |
while p in path: |
|
273 |
if debug: print 'removed "%s" from path' % p |
|
274 |
path.remove(p) |
|
275 |
elif '.' not in path: |
|
1821
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
276 |
path.insert(0,'.') |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
277 |
|
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
278 |
if debug: |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
279 |
import pprint |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
280 |
pp = pprint.pprint |
2244 | 281 |
print 'path=', |
282 |
pp(path) |
|
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2160
diff
changeset
|
283 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
284 |
#make import errors a bit more informative |
2156 | 285 |
opath = sys.path |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
286 |
try: |
2156 | 287 |
sys.path = path |
2160 | 288 |
exec 'import %s\nm = %s\n' % (modulename,modulename) in locals() |
289 |
sys.path = opath |
|
2156 | 290 |
return m |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
291 |
except ImportError: |
2160 | 292 |
sys.path = opath |
2156 | 293 |
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
|
294 |
if baseDir: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
295 |
msg = msg + " under paths '%s'" % `path` |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
296 |
raise ImportError, msg |
1396
40d1361f08b7
Enhanced the error message from recursiveImport
andy_robinson
parents:
1389
diff
changeset
|
297 |
|
1821
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
298 |
def recursiveGetAttr(obj, name): |
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" |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
300 |
return eval(name, obj.__dict__) |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
301 |
|
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
302 |
def recursiveSetAttr(obj, name, value): |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
303 |
"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
|
304 |
#get the thing above last. |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
305 |
tokens = string.split(name, '.') |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
306 |
if len(tokens) == 1: |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
307 |
setattr(obj, name, value) |
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2160
diff
changeset
|
308 |
else: |
1821
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
309 |
most = string.join(tokens[:-1], '.') |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
310 |
last = tokens[-1] |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
311 |
parent = recursiveGetAttr(obj, most) |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
312 |
setattr(parent, last, value) |
7854ddd0fc30
Fixed recursive import, setting and gettign attributes
andy_robinson
parents:
1683
diff
changeset
|
313 |
|
674 | 314 |
def import_zlib(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
315 |
try: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
316 |
import zlib |
2053 | 317 |
except ImportError: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
318 |
zlib = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
319 |
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
|
320 |
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
|
321 |
return zlib |
674 | 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 |
2045 | 327 |
if sys.platform[0:4] == 'java': |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
328 |
try: |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
329 |
import javax.imageio |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
330 |
import java.awt.image |
2045 | 331 |
haveImages = 1 |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
332 |
except: |
2045 | 333 |
haveImages = 0 |
334 |
else: |
|
335 |
try: |
|
336 |
from PIL import Image |
|
2053 | 337 |
except ImportError: |
2045 | 338 |
try: |
339 |
import Image |
|
2053 | 340 |
except ImportError: |
2045 | 341 |
Image = None |
342 |
haveImages = Image is not None |
|
343 |
if haveImages: del Image |
|
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
344 |
|
1586
d600d8ffaa21
This seems adequate if we avoid Dinuisms like writing to initialised StringIOs
rgbecker
parents:
1584
diff
changeset
|
345 |
__StringIO=None |
1580 | 346 |
def getStringIO(buf=None): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
347 |
'''unified StringIO instance interface''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
348 |
global __StringIO |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
349 |
if not __StringIO: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
350 |
try: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
351 |
from cStringIO import StringIO |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
352 |
except ImportError: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
353 |
from StringIO import StringIO |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
354 |
__StringIO = StringIO |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
355 |
return buf is not None and __StringIO(buf) or __StringIO() |
1580 | 356 |
|
1375 | 357 |
class ArgvDictValue: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
358 |
'''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
|
359 |
def __init__(self,value,func): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
360 |
self.value = value |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
361 |
self.func = func |
1375 | 362 |
|
363 |
def getArgvDict(**kw): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
364 |
''' 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
|
365 |
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
|
366 |
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
|
367 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
368 |
def handleValue(v,av,func): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
369 |
if func: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
370 |
v = func(av) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
371 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
372 |
t = type(v) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
373 |
if t is StringType: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
374 |
v = av |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
375 |
elif t is FloatType: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
376 |
v = float(av) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
377 |
elif t is IntType: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
378 |
v = int(av) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
379 |
elif t is ListType: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
380 |
v = list(eval(av)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
381 |
elif t is TupleType: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
382 |
v = tuple(eval(av)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
383 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
384 |
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
|
385 |
return v |
1387 | 386 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
387 |
A = sys.argv[1:] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
388 |
R = {} |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
389 |
for k, v in kw.items(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
390 |
if isinstance(v,ArgvDictValue): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
391 |
v, func = v.value, v.func |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
392 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
393 |
func = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
394 |
handled = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
395 |
ke = k+'=' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
396 |
for a in A: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
397 |
if string.find(a,ke)==0: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
398 |
av = a[len(ke):] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
399 |
A.remove(a) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
400 |
R[k] = handleValue(v,av,func) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
401 |
handled = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
402 |
break |
1387 | 403 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
404 |
if not handled: R[k] = handleValue(v,v,func) |
1387 | 405 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
406 |
return R |
1375 | 407 |
|
452 | 408 |
def getHyphenater(hDict=None): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
409 |
try: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
410 |
from reportlab.lib.pyHnj import Hyphen |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
411 |
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
|
412 |
return Hyphen(hDict) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
413 |
except ImportError, errMsg: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
414 |
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
|
415 |
return None |
452 | 416 |
|
448 | 417 |
def _className(self): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
418 |
'''Return a shortened class name''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
419 |
try: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
420 |
name = self.__class__.__name__ |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
421 |
i=string.rfind(name,'.') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
422 |
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
|
423 |
return name |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
424 |
except AttributeError: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
425 |
return str(self) |
1538 | 426 |
|
2309 | 427 |
def open_for_read_by_name(name,mode='b'): |
428 |
if 'r' not in mode: mode = 'r'+mode |
|
429 |
try: |
|
430 |
return open(name,mode) |
|
431 |
except IOError: |
|
432 |
if _isFSD or __loader__ is None: raise |
|
2313 | 433 |
#we have a __loader__, perhaps the filename starts with |
434 |
#the dirname(reportlab.__file__) or is relative |
|
435 |
name = _startswith_rl(name) |
|
436 |
s = __loader__.get_data(name) |
|
437 |
if 'b' not in mode and os.linesep!='\n': s = s.replace(os.linesep,'\n') |
|
438 |
return getStringIO(s) |
|
2309 | 439 |
|
2313 | 440 |
import urllib |
441 |
def open_for_read(name,mode='b', urlopen=urllib.urlopen): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
442 |
'''attempt to open a file or URL for reading''' |
2229 | 443 |
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
|
444 |
try: |
2313 | 445 |
return open_for_read_by_name(name,mode) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
446 |
except: |
2313 | 447 |
try: |
448 |
return getStringIO(urlopen(name).read()) |
|
449 |
except: |
|
450 |
raise IOError('Cannot open resource "%s"' % name) |
|
451 |
del urllib |
|
1575 | 452 |
|
2225 | 453 |
def open_and_read(name,mode='b'): |
454 |
return open_for_read(name,mode).read() |
|
455 |
||
2247 | 456 |
def open_and_readlines(name,mode='t'): |
457 |
return open_and_read(name,mode).split('\n') |
|
458 |
||
2240 | 459 |
def rl_isfile(fn,os_path_isfile=os.path.isfile): |
460 |
if hasattr(fn,'read'): return True |
|
461 |
if os_path_isfile(fn): return True |
|
462 |
if _isFSD or __loader__ is None: return False |
|
2244 | 463 |
fn = _startswith_rl(fn) |
464 |
return fn in __loader__._files.keys() |
|
2240 | 465 |
|
2339
4b1dae4bd1a8
More changes related to compact distro path recognition
rgbecker
parents:
2332
diff
changeset
|
466 |
def rl_isdir(pn,os_path_isdir=os.path.isdir,os_path_normpath=os.path.normpath): |
2244 | 467 |
if os_path_isdir(pn): return True |
468 |
if _isFSD or __loader__ is None: return False |
|
2354 | 469 |
pn = _startswith_rl(os_path_normpath(pn)) |
470 |
if not pn.endswith(os.sep): pn += os.sep |
|
2258 | 471 |
return len(filter(lambda x,pn=pn: x.startswith(pn),__loader__._files.keys()))>0 |
2240 | 472 |
|
2794 | 473 |
def rl_listdir(pn,os_path_isdir=os.path.isdir,os_path_normpath=os.path.normpath,os_listdir=os.listdir): |
474 |
if os_path_isdir(pn) or _isFSD or __loader__ is None: return os_listdir(pn) |
|
475 |
pn = _startswith_rl(os_path_normpath(pn)) |
|
476 |
if not pn.endswith(os.sep): pn += os.sep |
|
477 |
return [x[len(pn):] for x in __loader__._files.keys() if x.startswith(pn)] |
|
478 |
||
479 |
def rl_getmtime(pn,os_path_isfile=os.path.isfile,os_path_normpath=os.path.normpath,os_path_getmtime=os.path.getmtime,time_mktime=time.mktime): |
|
480 |
if os_path_isfile(pn) or _isFSD or __loader__ is None: return os_path_getmtime(pn) |
|
481 |
p = _startswith_rl(os_path_normpath(pn)) |
|
482 |
try: |
|
483 |
e = __loader__._files[p] |
|
484 |
except KeyError: |
|
485 |
return os_path_getmtime(pn) |
|
486 |
s = e[5] |
|
487 |
d = e[6] |
|
488 |
y = ((d>>9)&0x7f)+1980 |
|
489 |
m = (d>>5)&0xf |
|
490 |
d &= 0x1f |
|
491 |
h = (s>>11)&0xf |
|
492 |
m = (s>>5)&0x3f |
|
493 |
s &= 0x1f |
|
494 |
s <<= 1 |
|
495 |
return time_mktime((y,m,d,h,m,s,0,0,0)) |
|
496 |
||
2301 | 497 |
def rl_get_module(name,dir): |
498 |
if sys.modules.has_key(name): |
|
499 |
om = sys.modules[name] |
|
500 |
del sys.modules[name] |
|
501 |
else: |
|
502 |
om = None |
|
503 |
try: |
|
2302 | 504 |
f = None |
2301 | 505 |
try: |
2302 | 506 |
f, p, desc= imp.find_module(name,[dir]) |
2301 | 507 |
return imp.load_module(name,f,p,desc) |
508 |
except: |
|
2307 | 509 |
if isCompactDistro(): |
2301 | 510 |
#attempt a load from inside the zip archive |
511 |
import zipimport |
|
2307 | 512 |
dir = _startswith_rl(dir) |
2356 | 513 |
dir = (dir=='.' or not dir) and _archive or os.path.join(_archive,dir.replace('/',os.sep)) |
2327
c40a1b477f3f
Forgot to check the exactly equal case for _startswith_rl
rgbecker
parents:
2313
diff
changeset
|
514 |
zi = zipimport.zipimporter(dir) |
2301 | 515 |
return zi.load_module(name) |
516 |
raise ImportError('%s[%s]' % (name,dir)) |
|
517 |
finally: |
|
518 |
if om: sys.modules[name] = om |
|
519 |
del om |
|
520 |
if f: f.close() |
|
521 |
||
2487 | 522 |
def _isPILImage(im): |
523 |
try: |
|
524 |
from PIL.Image import Image |
|
525 |
return isinstance(im,Image) |
|
526 |
except ImportError: |
|
527 |
return 0 |
|
528 |
||
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
529 |
class ImageReader: |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
530 |
"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
|
531 |
def __init__(self, fileName): |
2487 | 532 |
if isinstance(fileName,ImageReader): |
533 |
self.__dict__ = fileName.__dict__ #borgize |
|
534 |
return |
|
2045 | 535 |
if not haveImages: |
2497
c9cb49d639ef
utils.py: raise RuntimeError if no image handling available in ImageReader
rgbecker
parents:
2487
diff
changeset
|
536 |
raise RuntimeError('Imaging Library not available, unable to import bitmaps') |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
537 |
#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
|
538 |
#the relevant engine. |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
539 |
self.fileName = fileName |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
540 |
self._image = None |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
541 |
self._width = None |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
542 |
self._height = None |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
543 |
self._transparent = None |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
544 |
self._data = None |
2487 | 545 |
if _isPILImage(fileName): |
546 |
self._image = fileName |
|
2902
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
547 |
self.fp = getattr(fileName,'fp',None) |
2487 | 548 |
try: |
2720
2c04d204766d
utils.py: attempt to fix fileName from PIL images
rgbecker
parents:
2716
diff
changeset
|
549 |
self.fileName = self._image.fileName |
2487 | 550 |
except AttributeError: |
551 |
self.fileName = 'PILIMAGE_%d' % id(self) |
|
552 |
else: |
|
2556 | 553 |
try: |
554 |
self.fp = open_for_read(fileName,'b') |
|
555 |
#detect which library we are using and open the image |
|
556 |
if sys.platform[0:4] == 'java': |
|
557 |
from javax.imageio import ImageIO |
|
558 |
self._image = ImageIO.read(self.fp) |
|
559 |
else: |
|
560 |
import PIL.Image |
|
561 |
self._image = PIL.Image.open(self.fp) |
|
562 |
if self._image=='JPEG': self.jpeg_fh = self._jpeg_fp |
|
563 |
except: |
|
564 |
et,ev,tb = sys.exc_info() |
|
565 |
if hasattr(ev,'args'): |
|
566 |
a = str(ev.args[-1])+(' fileName='+fileName) |
|
567 |
ev.args= ev.args[:-1]+(a,) |
|
568 |
raise et,ev,tb |
|
569 |
else: |
|
570 |
raise |
|
571 |
||
2487 | 572 |
def _jpeg_fh(self): |
573 |
fp = self.fp |
|
574 |
fp.seek(0) |
|
575 |
return fp |
|
576 |
||
577 |
def jpeg_fh(self): |
|
578 |
return None |
|
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
579 |
|
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
580 |
def getSize(self): |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
581 |
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
|
582 |
if sys.platform[0:4] == 'java': |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
583 |
self._width = self._image.getWidth() |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
584 |
self._height = self._image.getHeight() |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
585 |
else: |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
586 |
self._width, self._height = self._image.size |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
587 |
return (self._width, self._height) |
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2160
diff
changeset
|
588 |
|
2045 | 589 |
def getRGBData(self): |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
590 |
"Return byte array of RGB data as string" |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
591 |
if self._data is None: |
2902
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
592 |
self._dataA = None |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
593 |
if sys.platform[0:4] == 'java': |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
594 |
import jarray |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
595 |
from java.awt.image import PixelGrabber |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
596 |
width, height = self.getSize() |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
597 |
buffer = jarray.zeros(width*height, 'i') |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
598 |
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
|
599 |
pg.grabPixels() |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
600 |
# 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
|
601 |
# I just haven't found it yet... |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
602 |
pixels = [] |
2059
e5eec1dd6f0c
ImageReader can now take File objects as well as Strings
dragan1
parents:
2057
diff
changeset
|
603 |
a = pixels.append |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
604 |
for i in range(len(buffer)): |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
605 |
rgb = buffer[i] |
2087 | 606 |
a(chr((rgb>>16)&0xff)) |
607 |
a(chr((rgb>>8)&0xff)) |
|
2059
e5eec1dd6f0c
ImageReader can now take File objects as well as Strings
dragan1
parents:
2057
diff
changeset
|
608 |
a(chr(rgb&0xff)) |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
609 |
self._data = ''.join(pixels) |
2482 | 610 |
self.mode = 'RGB' |
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
611 |
else: |
2482 | 612 |
im = self._image |
613 |
mode = self.mode = im.mode |
|
2902
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
614 |
if mode=='RGBA': |
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
615 |
self._dataA = ImageReader(im.split()[3]) |
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
616 |
im = im.convert('RGB') |
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
617 |
self.mode = 'RGB' |
f15ac27dc73f
reportlab: add in RGBA functionality for sensible images
rgbecker
parents:
2838
diff
changeset
|
618 |
elif mode not in ('L','RGB','CMYK'): |
2482 | 619 |
im = im.convert('RGB') |
620 |
self.mode = 'RGB' |
|
621 |
self._data = im.tostring() |
|
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2160
diff
changeset
|
622 |
return self._data |
2045 | 623 |
|
624 |
def getImageData(self): |
|
625 |
width, height = self.getSize() |
|
626 |
return width, height, self.getRGBData() |
|
627 |
||
628 |
def getTransparent(self): |
|
629 |
if sys.platform[0:4] == 'java': |
|
630 |
return None |
|
631 |
else: |
|
632 |
if self._image.info.has_key("transparency"): |
|
633 |
transparency = self._image.info["transparency"] * 3 |
|
634 |
palette = self._image.palette |
|
635 |
try: |
|
636 |
palette = palette.palette |
|
637 |
except: |
|
638 |
palette = palette.data |
|
639 |
return map(ord, palette[transparency:transparency+3]) |
|
640 |
else: |
|
641 |
return None |
|
642 |
||
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
643 |
def getImageData(imageFileName): |
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
644 |
"Get width, height and RGB pixels from image file. Wraps Java/PIL" |
2487 | 645 |
try: |
646 |
return imageFileName.getImageData() |
|
647 |
except AttributeError: |
|
648 |
return ImageReader(imageFileName).getImageData() |
|
2044
3be472f4a6dd
Various work on abstracting out images, plus outstanding patches
andy_robinson
parents:
2007
diff
changeset
|
649 |
|
1538 | 650 |
class DebugMemo: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
651 |
'''Intended as a simple report back encapsulator |
1545 | 652 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
653 |
Typical usages |
1683 | 654 |
1) To record error data |
655 |
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
|
656 |
dbg.add(anotherPayload='aaaa',andagain='bbb') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
657 |
dbg.dump() |
1543
3681c7d8898d
Slight formatting improvements & added payload method
rgbecker
parents:
1538
diff
changeset
|
658 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
659 |
2) To show the recorded info |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
660 |
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
|
661 |
dbg.load() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
662 |
dbg.show() |
1543
3681c7d8898d
Slight formatting improvements & added payload method
rgbecker
parents:
1538
diff
changeset
|
663 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
664 |
3) To re-use recorded information |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
665 |
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
|
666 |
dbg.load() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
667 |
myTestFunc(dbg.payload('myVar'),dbg.payload('andagain')) |
1545 | 668 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
669 |
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
|
670 |
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
|
671 |
''' |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
672 |
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
|
673 |
import time, socket |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
674 |
self.fn = fn |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
675 |
if mode!='w': return |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
676 |
if not stdout: |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
677 |
self.stdout = sys.stdout |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
678 |
else: |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
679 |
if hasattr(stdout,'write'): |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
680 |
self.stdout = stdout |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
681 |
else: |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
682 |
self.stdout = open(stdout,'w') |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
683 |
self.store = store = {} |
2296 | 684 |
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
|
685 |
import traceback |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
686 |
s = getStringIO() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
687 |
traceback.print_exc(None,s) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
688 |
store['__traceback'] = s.getvalue() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
689 |
cwd=os.getcwd() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
690 |
lcwd = os.listdir(cwd) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
691 |
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
|
692 |
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
|
693 |
'platform': sys.platform, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
694 |
'version': sys.version, |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
695 |
'hexversion': hex(sys.hexversion), |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
696 |
'executable': sys.executable, |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
697 |
'exec_prefix': sys.exec_prefix, |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
698 |
'prefix': sys.prefix, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
699 |
'path': sys.path, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
700 |
'argv': sys.argv, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
701 |
'cwd': cwd, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
702 |
'hostname': socket.gethostname(), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
703 |
'lcwd': lcwd, |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
704 |
'byteorder': sys.byteorder, |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
705 |
'maxint': sys.maxint, |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
706 |
'maxint': getattr(sys,'maxunicode','????'), |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
707 |
'api_version': getattr(sys,'api_version','????'), |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
708 |
'version_info': getattr(sys,'version_info','????'), |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
709 |
'winver': getattr(sys,'winver','????'), |
2459 | 710 |
'environment': os.environ, |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
711 |
}) |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
712 |
for M,A in ( |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
713 |
(sys,('getwindowsversion','getfilesystemencoding')), |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
714 |
(os,('uname', 'ctermid', 'getgid', 'getuid', 'getegid', |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
715 |
'geteuid', 'getlogin', 'getgroups', 'getpgrp', 'getpid', 'getppid', |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
716 |
)), |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
717 |
): |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
718 |
for a in A: |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
719 |
if hasattr(M,a): |
2300 | 720 |
try: |
721 |
store[a] = getattr(M,a)() |
|
722 |
except: |
|
723 |
pass |
|
724 |
if exed!=cwd: |
|
725 |
try: |
|
726 |
store.update({'exed': exed, 'lexed': os.listdir(exed),}) |
|
727 |
except: |
|
728 |
pass |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
729 |
if getScript: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
730 |
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
|
731 |
if os.path.isfile(fn): |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
732 |
try: |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
733 |
store['__script'] = (fn,open(fn,'r').read()) |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
734 |
except: |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
735 |
pass |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
736 |
module_versions = {} |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
737 |
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
|
738 |
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
|
739 |
v = getattr(m,'__version__',None) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
740 |
if v: module_versions[n] = v |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
741 |
store['__module_versions'] = module_versions |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
742 |
self.store['__payload'] = {} |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
743 |
self._add(kw) |
1538 | 744 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
745 |
def _add(self,D): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
746 |
payload = self.store['__payload'] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
747 |
for k, v in D.items(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
748 |
payload[k] = v |
1538 | 749 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
750 |
def add(self,**kw): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
751 |
self._add(kw) |
1538 | 752 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
753 |
def dump(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
754 |
import pickle |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
755 |
pickle.dump(self.store,open(self.fn,'wb')) |
1538 | 756 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
757 |
def load(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
758 |
import pickle |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
759 |
self.store = pickle.load(open(self.fn,'rb')) |
1538 | 760 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
761 |
def _show_module_versions(self,k,v): |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
762 |
self._writeln(k[2:]) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
763 |
K = v.keys() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
764 |
K.sort() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
765 |
for k in K: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
766 |
vk = v[k] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
767 |
try: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
768 |
m = recursiveImport(k,sys.path[:],1) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
769 |
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
|
770 |
except: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
771 |
m = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
772 |
d = '??????unknown??????' |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
773 |
self._writeln(' %s = %s (%s)' % (k,vk,d)) |
1538 | 774 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
775 |
def _banner(self,k,what): |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
776 |
self._writeln('###################%s %s##################' % (what,k[2:])) |
1538 | 777 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
778 |
def _start(self,k): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
779 |
self._banner(k,'Start ') |
1538 | 780 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
781 |
def _finish(self,k): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
782 |
self._banner(k,'Finish ') |
1538 | 783 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
784 |
def _show_lines(self,k,v): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
785 |
self._start(k) |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
786 |
self._writeln(v) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
787 |
self._finish(k) |
1538 | 788 |
|
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
789 |
def _show_file(self,k,v): |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
790 |
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
|
791 |
self._show_lines(k,v[1]) |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
792 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
793 |
def _show_payload(self,k,v): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
794 |
if v: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
795 |
import pprint |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
796 |
self._start(k) |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
797 |
pprint.pprint(v,self.stdout) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
798 |
self._finish(k) |
1538 | 799 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
800 |
specials = {'__module_versions': _show_module_versions, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
801 |
'__payload': _show_payload, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
802 |
'__traceback': _show_lines, |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
803 |
'__script': _show_file, |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
804 |
} |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
805 |
def show(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
806 |
K = self.store.keys() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
807 |
K.sort() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
808 |
for k in K: |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
809 |
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
|
810 |
for k in K: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
811 |
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
|
812 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
813 |
def payload(self,name): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
814 |
return self.store['__payload'][name] |
1561 | 815 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
816 |
def __setitem__(self,name,value): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
817 |
self.store['__payload'][name] = value |
1561 | 818 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1650
diff
changeset
|
819 |
def __getitem__(self,name): |
1833
135322abc191
Fix recursivImport for case when baseDir is a sequence
rgbecker
parents:
1821
diff
changeset
|
820 |
return self.store['__payload'][name] |
2297
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
821 |
|
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
822 |
def _writeln(self,msg): |
7c706ac8a6b7
Added some more stuff and script name to DebugMemo output
rgbecker
parents:
2296
diff
changeset
|
823 |
self.stdout.write(msg+'\n') |
2365
cda528e430aa
reportlab/lib/utils.py: added flatten utility function
rgbecker
parents:
2356
diff
changeset
|
824 |
|
cda528e430aa
reportlab/lib/utils.py: added flatten utility function
rgbecker
parents:
2356
diff
changeset
|
825 |
def _flatten(L,a): |
cda528e430aa
reportlab/lib/utils.py: added flatten utility function
rgbecker
parents:
2356
diff
changeset
|
826 |
for x in L: |
2499 | 827 |
if isSeqType(x): _flatten(x,a) |
2365
cda528e430aa
reportlab/lib/utils.py: added flatten utility function
rgbecker
parents:
2356
diff
changeset
|
828 |
else: a(x) |
cda528e430aa
reportlab/lib/utils.py: added flatten utility function
rgbecker
parents:
2356
diff
changeset
|
829 |
|
cda528e430aa
reportlab/lib/utils.py: added flatten utility function
rgbecker
parents:
2356
diff
changeset
|
830 |
def flatten(L): |
cda528e430aa
reportlab/lib/utils.py: added flatten utility function
rgbecker
parents:
2356
diff
changeset
|
831 |
'''recursively flatten the list or tuple L''' |
cda528e430aa
reportlab/lib/utils.py: added flatten utility function
rgbecker
parents:
2356
diff
changeset
|
832 |
R = [] |
cda528e430aa
reportlab/lib/utils.py: added flatten utility function
rgbecker
parents:
2356
diff
changeset
|
833 |
_flatten(L,R.append) |
cda528e430aa
reportlab/lib/utils.py: added flatten utility function
rgbecker
parents:
2356
diff
changeset
|
834 |
return R |
2406 | 835 |
|
836 |
def find_locals(func,depth=0): |
|
837 |
'''apply func to the locals at each stack frame till func returns a non false value''' |
|
838 |
while 1: |
|
839 |
_ = func(sys._getframe(depth).f_locals) |
|
840 |
if _: return _ |
|
841 |
depth += 1 |
|
2454 | 842 |
|
843 |
class _FmtSelfDict: |
|
2479 | 844 |
def __init__(self,obj,overrideArgs): |
2454 | 845 |
self.obj = obj |
2479 | 846 |
self._overrideArgs = overrideArgs |
2454 | 847 |
def __getitem__(self,k): |
848 |
try: |
|
2479 | 849 |
return self._overrideArgs[k] |
2454 | 850 |
except KeyError: |
2479 | 851 |
try: |
852 |
return self.obj.__dict__[k] |
|
853 |
except KeyError: |
|
854 |
return getattr(self.obj,k) |
|
2454 | 855 |
|
856 |
class FmtSelfDict: |
|
857 |
'''mixin to provide the _fmt method''' |
|
2479 | 858 |
def _fmt(self,fmt,**overrideArgs): |
859 |
D = _FmtSelfDict(self, overrideArgs) |
|
2454 | 860 |
return fmt % D |
2715 | 861 |
|
2716 | 862 |
def _simpleSplit(txt,mW,SW): |
863 |
L = [] |
|
864 |
ws = SW(' ') |
|
865 |
O = [] |
|
866 |
w = -ws |
|
867 |
for t in txt.split(): |
|
868 |
lt = SW(t) |
|
869 |
if w+ws+lt<=mW or O==[]: |
|
870 |
O.append(t) |
|
871 |
w = w + ws + lt |
|
872 |
else: |
|
873 |
L.append(string.join(O,' ')) |
|
874 |
O = [t] |
|
875 |
w = lt |
|
876 |
if O!=[]: L.append(string.join(O,' ')) |
|
877 |
return L |
|
878 |
||
2715 | 879 |
def simpleSplit(text,fontName,fontSize,maxWidth): |
2716 | 880 |
from reportlab.pdfbase.pdfmetrics import stringWidth |
2715 | 881 |
lines = text.split('\n') |
882 |
SW = lambda text, fN=fontName, fS=fontSize: stringWidth(text, fN, fS) |
|
883 |
if maxWidth: |
|
884 |
L = [] |
|
885 |
for l in lines: |
|
886 |
L[-1:-1] = _simpleSplit(l,maxWidth,SW) |
|
887 |
lines = L |
|
888 |
return lines |
|
2764 | 889 |
|
890 |
def escapeTextOnce(text): |
|
891 |
"Escapes once only" |
|
892 |
from xml.sax.saxutils import escape |
|
893 |
if text is None: |
|
894 |
return text |
|
895 |
text = escape(text) |
|
896 |
text = text.replace('&amp;', '&') |
|
897 |
text = text.replace('&gt;', '>') |
|
898 |
text = text.replace('&lt;', '<') |
|
899 |
return text |
|
900 |
||
2813
2b9091df4470
reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents:
2794
diff
changeset
|
901 |
def fileName2Utf8(fn): |
2b9091df4470
reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents:
2794
diff
changeset
|
902 |
'''attempt to convert a filename to utf8''' |
2b9091df4470
reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents:
2794
diff
changeset
|
903 |
from reportlab.rl_config import fsEncodings |
2b9091df4470
reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents:
2794
diff
changeset
|
904 |
for enc in fsEncodings: |
2b9091df4470
reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents:
2794
diff
changeset
|
905 |
try: |
2b9091df4470
reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents:
2794
diff
changeset
|
906 |
return fn.decode(enc).encode('utf8') |
2b9091df4470
reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents:
2794
diff
changeset
|
907 |
except: |
2b9091df4470
reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents:
2794
diff
changeset
|
908 |
pass |
2b9091df4470
reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents:
2794
diff
changeset
|
909 |
raise ValueError('cannot convert %r to utf8' % fn) |
2929 | 910 |
|
911 |
||
912 |
import itertools |
|
913 |
def prev_this_next(items): |
|
914 |
""" |
|
915 |
Loop over a collection with look-ahead and look-back. |
|
916 |
||
917 |
From Thomas Guest, |
|
918 |
http://wordaligned.org/articles/zippy-triples-served-with-python |
|
919 |
||
920 |
Seriously useful looping tool (Google "zippy triples") |
|
921 |
lets you loop a collection and see the previous and next items, |
|
922 |
which get set to None at the ends. |
|
923 |
||
924 |
To be used in layout algorithms where one wants a peek at the |
|
925 |
next item coming down the pipe. |
|
926 |
||
927 |
""" |
|
928 |
||
929 |
extend = itertools.chain([None], items, [None]) |
|
930 |
prev, this, next = itertools.tee(extend, 3) |
|
931 |
try: |
|
932 |
this.next() |
|
933 |
next.next() |
|
934 |
next.next() |
|
935 |
except StopIteration: |
|
936 |
pass |
|
937 |
return itertools.izip(prev, this, next) |