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