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