1
1
# -*- coding: utf-8 -*-
2
+ from functools import wraps
3
+ from reportlab .lib .colors import *
4
+ from reportlab .lib .enums import *
5
+ from reportlab .lib .pagesizes import *
6
+ from reportlab .lib .styles import *
7
+ from reportlab .lib .units import inch , cm
8
+ from reportlab .pdfbase import pdfmetrics
9
+ import base64
10
+ import copy
11
+ import httplib
12
+ import logging
13
+ import mimetypes
14
+ import os
15
+ import os .path
16
+ import pprint
17
+ import re
18
+ import reportlab
19
+ import shutil
20
+ import string
21
+ import sys
22
+ import tempfile
23
+ import types
24
+ import urllib
25
+ import urllib2
26
+ import urlparse
2
27
3
28
# Copyright 2010 Dirk Holtwick, holtwick.it
4
29
#
19
44
__author__ = "$Author: holtwick $"
20
45
__date__ = "$Date: 2007-10-09 12:58:24 +0200 (Di, 09 Okt 2007) $"
21
46
22
- from reportlab .lib .units import inch , cm
23
- from reportlab .lib .styles import *
24
- from reportlab .lib .enums import *
25
- from reportlab .lib .colors import *
26
- from reportlab .lib .pagesizes import *
27
- from reportlab .pdfbase import pdfmetrics
28
47
29
48
# from reportlab.platypus import *
30
49
# from reportlab.platypus.flowables import Flowable
31
50
# from reportlab.platypus.tableofcontents import TableOfContents
32
51
# from reportlab.platypus.para import Para, PageNumberObject, UNDERLINE, HotLink
33
52
34
- import reportlab
35
- import copy
36
- import types
37
- import os
38
- import os .path
39
- import pprint
40
- import sys
41
- import string
42
- import re
43
- import base64
44
- import urlparse
45
- import mimetypes
46
- import urllib2
47
- import urllib
48
- import httplib
49
- import tempfile
50
- import shutil
51
53
52
54
rgb_re = re .compile ("^.*?rgb[(]([0-9]+).*?([0-9]+).*?([0-9]+)[)].*?[ ]*$" )
53
55
57
59
REPORTLAB22 = (reportlab .Version [0 ] == "2" and reportlab .Version [2 ] >= "2" )
58
60
# print "***", reportlab.Version, REPORTLAB22, reportlab.__file__
59
61
60
- import logging
61
62
log = logging .getLogger ("ho.pisa" )
62
63
63
64
try :
@@ -221,17 +222,66 @@ def getBorderStyle(value, default=None):
221
222
222
223
MIN_FONT_SIZE = 1.0
223
224
225
+ #===============================================================================
226
+ # Memoize decorators
227
+ #===============================================================================
228
+ class memoized (object ):
229
+
230
+ def __init__ (self , func ):
231
+ self .funct = func
232
+ self .cache = {}
233
+
234
+ def __call__ (self , * args ):
235
+ try :
236
+ return self .cache [args ]
237
+ except KeyError :
238
+ result = self .funct (* args )
239
+ self .cache [args ] = result
240
+ return result
241
+ except TypeError :
242
+ return self .funct (* args )
243
+
244
+ class Cache (object ):
245
+ def __init__ (self , func ):
246
+ self .cache = {}
247
+ self .func = func
248
+
249
+ def __call__ (self , * args ):
250
+ if args not in self .cache :
251
+ res = self .func (* args )
252
+ self .cache [args ] = res
253
+ else :
254
+ print 'Cache hit!'
255
+ return self .cache [args ]
256
+
257
+ #===============================================================================
258
+ # end memoize decorators
259
+ #===============================================================================
260
+
261
+ SIZE_CACHE = {}
262
+
224
263
def getSize (value , relative = 0 , base = None , default = 0.0 ):
225
264
"""
226
265
Converts strings to standard sizes
227
266
"""
267
+ # TODO: Figure out why it's not working with either of the memoize decorators!!!
268
+ cached = SIZE_CACHE .get ((value , relative , base , default ), None )
269
+ if cached :
270
+ #print 'CACHE HIT'
271
+ return cached
272
+ res = _get_size (value , relative , base , default )
273
+ SIZE_CACHE [(value , relative , base , default )] = res
274
+ return res
275
+
276
+ def _get_size (value , relative = 0 , base = None , default = 0.0 ):
277
+
228
278
try :
229
279
original = value
230
280
if value is None :
231
281
return relative
232
282
elif type (value ) is types .FloatType :
233
283
return value
234
- elif type (value ) is types . IntType :
284
+ elif isinstance (value , int ) :
235
285
return float (value )
236
286
elif type (value ) in (types .TupleType , types .ListType ):
237
287
value = "" .join (value )
@@ -313,7 +363,7 @@ def getBox(box, pagesize):
313
363
box = str (box ).split ()
314
364
if len (box ) != 4 :
315
365
raise Exception , "box not defined right way"
316
- x , y , w , h = map ( getSize , box )
366
+ x , y , w , h = [ getSize ( pos ) for pos in box ]
317
367
return getCoords (x , y , w , h , pagesize )
318
368
319
369
def getPos (position , pagesize ):
@@ -323,7 +373,7 @@ def getPos(position, pagesize):
323
373
position = str (position ).split ()
324
374
if len (position ) != 2 :
325
375
raise Exception , "position not defined right way"
326
- x , y = map ( getSize , position )
376
+ x , y = [ getSize ( pos ) for pos in position ]
327
377
return getCoords (x , y , None , None , pagesize )
328
378
329
379
def getBool (s ):
0 commit comments