reportlab/lib/utils.py
author rgbecker
Fri, 26 Oct 2001 17:02:41 +0000
changeset 1375 cb8e4098def5
parent 1153 58081552b836
child 1387 715edbf17c7a
permissions -rw-r--r--
Added ArgvDictValue and getArgvDict
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 474
diff changeset
     1
#copyright ReportLab Inc. 2000
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 474
diff changeset
     2
#see license.txt for license details
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 474
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/lib/utils.py?cvsroot=reportlab
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/lib/utils.py,v 1.19 2001/10/26 17:02:41 rgbecker Exp $
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
     5
__version__=''' $Id: utils.py,v 1.19 2001/10/26 17:02:41 rgbecker Exp $ '''
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 519
diff changeset
     6
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
     7
import string, os, sys
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
     8
from types import *
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
     9
from reportlab.lib.logger import warnOnce
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    10
SeqTypes = (ListType,TupleType)
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 519
diff changeset
    11
1143
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    12
def _checkImportError(errMsg):
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    13
	if string.lower(string.strip(str(errMsg)[0:16]))!='no module named': raise
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    14
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    15
try:
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    16
	#raise ImportError
519
0452f833153c mods permitting late string conversions
aaron_watters
parents: 494
diff changeset
    17
	### NOTE!  FP_STR SHOULD PROBABLY ALWAYS DO A PYTHON STR() CONVERSION ON ARGS
0452f833153c mods permitting late string conversions
aaron_watters
parents: 494
diff changeset
    18
	### IN CASE THEY ARE "LAZY OBJECTS".  ACCELLERATOR DOESN'T DO THIS (YET)
474
5c5a9505fba1 Changes to make freezing easier
rgbecker
parents: 452
diff changeset
    19
	try:
1143
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    20
		from _rl_accel import fp_str				# in case of builtin version
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    21
	except ImportError, errMsg:
1143
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    22
		_checkImportError(errMsg)
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    23
		from reportlab.lib._rl_accel import fp_str	# specific
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    24
except ImportError, errMsg:
1153
58081552b836 eliminated requirement for _rl_accel
aaron_watters
parents: 1143
diff changeset
    25
	#_checkImportError(errMsg) # this effectively requires _rl_accel... should not be required
413
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    26
	def fp_str(*a):
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    27
		if len(a)==1 and type(a[0]) in SeqTypes: a = a[0]
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    28
		s = []
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    29
		for i in a:
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    30
			s.append('%0.2f' % i)
6312e8296c9b Initial version
rgbecker
parents:
diff changeset
    31
		return string.join(s)
448
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
    32
981
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
    33
#hack test for comma users
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
    34
if ',' in fp_str(0.25):
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
    35
	_FP_STR = fp_str
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
    36
	def fp_str(*a):
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
    37
		return string.replace(apply(_FP_STR,a),',','.')
b3dfa2ba427c Attempt to fix the locale mismatch problem
rgbecker
parents: 962
diff changeset
    38
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    39
def import_zlib():
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    40
	try:
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    41
		import zlib
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    42
	except ImportError, errMsg:
1143
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    43
		_checkImportError(errMsg)
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    44
		zlib = None
962
366d703f3436 Added PIL_WARNINGS/ZLIB_WARNINGS
rgbecker
parents: 822
diff changeset
    45
		if ZLIB_WARNINGS: warnOnce('zlib not available')
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    46
	return zlib
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    47
760
0e51a12a0e39 Changed to PIL_Image
rgbecker
parents: 677
diff changeset
    48
try:
761
ddacc2f18b6f Changed to prefer PIL as package
rgbecker
parents: 760
diff changeset
    49
	from PIL import Image
760
0e51a12a0e39 Changed to PIL_Image
rgbecker
parents: 677
diff changeset
    50
except ImportError, errMsg:
1143
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    51
	_checkImportError(errMsg)
998
501275d17bef Imported PIL_WARNINGS
rgbecker
parents: 981
diff changeset
    52
	from reportlab.rl_config import PIL_WARNINGS
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    53
	try:
761
ddacc2f18b6f Changed to prefer PIL as package
rgbecker
parents: 760
diff changeset
    54
		import Image
962
366d703f3436 Added PIL_WARNINGS/ZLIB_WARNINGS
rgbecker
parents: 822
diff changeset
    55
		if PIL_WARNINGS: warnOnce('Python Imaging Library not available as package; upgrade your installation!')
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    56
	except ImportError, errMsg:
1143
46a5ec2d3d2c Better import error checking
rgbecker
parents: 998
diff changeset
    57
		_checkImportError(errMsg)
760
0e51a12a0e39 Changed to PIL_Image
rgbecker
parents: 677
diff changeset
    58
		Image = None
962
366d703f3436 Added PIL_WARNINGS/ZLIB_WARNINGS
rgbecker
parents: 822
diff changeset
    59
		if PIL_WARNINGS: warnOnce('Python Imaging Library not available')
760
0e51a12a0e39 Changed to PIL_Image
rgbecker
parents: 677
diff changeset
    60
PIL_Image = Image
0e51a12a0e39 Changed to PIL_Image
rgbecker
parents: 677
diff changeset
    61
del Image
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
    62
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    63
class ArgvDictValue:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    64
	'''A type to allow clients of getArgvDict to specify a conversion function'''
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    65
	def __init__(self,value,func):
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    66
		self.value = value
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    67
		self.func = func
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    68
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    69
def getArgvDict(**kw):
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    70
	'''	Builds a dictionary from its keyword arguments with overrides from sys.argv.
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    71
		Attempts to be smart about conversions, but the value can be an instance
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    72
		of ArgDictValue to allow specifying a conversion function.
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    73
	'''
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    74
	A = sys.argv[1:]
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    75
	for k, v in kw.items():
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    76
		if isinstance(v,ArgvDictValue):
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    77
			v, func = v.value, v.func
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    78
		else:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    79
			func = None
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    80
		ke = k+'='
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    81
		for a in A:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    82
			if string.find(a,ke)==0:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    83
				av = a[len(ke):]
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    84
				A.remove(a)
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    85
				if func:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    86
					v = func(av)
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    87
				else:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    88
					t = type(v)
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    89
					if t is StringType:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    90
						v = av
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    91
					elif t is FloatType:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    92
						v = float(av)
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    93
					elif t is IntType:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    94
						v = int(av)
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    95
					elif t is ListType:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    96
						v = list(eval(av))
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    97
					elif t is TupleType:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    98
						v = tuple(eval(av))
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    99
					else:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   100
						raise TypeError, "Can't convert string '%s' to %s" % (av,str(t))
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   101
				kw[k] = v
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   102
				break
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   103
	return kw
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   104
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   105
def getHyphenater(hDict=None):
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   106
	try:
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   107
		from reportlab.lib.pyHnj import Hyphen
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   108
		if hDict is None: hDict=os.path.join(os.path.dirname(__file__),'hyphen.mashed')
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   109
		return Hyphen(hDict)
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
   110
	except ImportError, errMsg:
677
35ba945c5572 Exception tests need str()
rgbecker
parents: 674
diff changeset
   111
		if str(errMsg)!='No module named pyHnj': raise
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   112
		return None
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   113
448
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   114
def _className(self):
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   115
	'''Return a shortened class name'''
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   116
	try:
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   117
		name = self.__class__.__name__
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   118
		i=string.rfind(name,'.')
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   119
		if i>=0: return name[i+1:]
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   120
		return name
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   121
	except AttributeError:
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   122
		return str(self)