reportlab/lib/utils.py
author rgbecker
Tue, 30 Oct 2001 08:47:09 +0000
changeset 1387 715edbf17c7a
parent 1375 cb8e4098def5
child 1389 6e5ba87ce34e
permissions -rw-r--r--
Improved argv stuff
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
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/lib/utils.py,v 1.20 2001/10/30 08:47:09 rgbecker Exp $
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
     5
__version__=''' $Id: utils.py,v 1.20 2001/10/30 08:47:09 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
	'''
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    74
	def handleValue(v,av,func):
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    75
		if func:
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    76
			v = func(av)
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    77
		else:
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    78
			t = type(v)
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    79
			if t is StringType:
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    80
				v = av
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    81
			elif t is FloatType:
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    82
				v = float(av)
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    83
			elif t is IntType:
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    84
				v = int(av)
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    85
			elif t is ListType:
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    86
				v = list(eval(av))
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    87
			elif t is TupleType:
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    88
				v = tuple(eval(av))
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    89
			else:
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    90
				raise TypeError, "Can't convert string '%s' to %s" % (av,str(t))
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    91
		return v
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    92
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    93
	A = sys.argv[1:]
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
    94
	R = {}
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    95
	for k, v in kw.items():
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    96
		if isinstance(v,ArgvDictValue):
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    97
			v, func = v.value, v.func
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    98
		else:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
    99
			func = None
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   100
		handled = 0
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   101
		ke = k+'='
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   102
		for a in A:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   103
			if string.find(a,ke)==0:
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   104
				av = a[len(ke):]
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   105
				A.remove(a)
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   106
				R[k] = handleValue(v,av,func)
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   107
				handled = 1
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   108
				break
1387
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   109
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   110
		if not handled: R[k] = handleValue(v,v,func)
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   111
715edbf17c7a Improved argv stuff
rgbecker
parents: 1375
diff changeset
   112
	return R
1375
cb8e4098def5 Added ArgvDictValue and getArgvDict
rgbecker
parents: 1153
diff changeset
   113
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   114
def getHyphenater(hDict=None):
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   115
	try:
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   116
		from reportlab.lib.pyHnj import Hyphen
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   117
		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
   118
		return Hyphen(hDict)
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 562
diff changeset
   119
	except ImportError, errMsg:
677
35ba945c5572 Exception tests need str()
rgbecker
parents: 674
diff changeset
   120
		if str(errMsg)!='No module named pyHnj': raise
452
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   121
		return None
6bb011a0d63e Initial version of D Yoo's pyHnj
rgbecker
parents: 448
diff changeset
   122
448
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   123
def _className(self):
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   124
	'''Return a shortened class name'''
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   125
	try:
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   126
		name = self.__class__.__name__
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   127
		i=string.rfind(name,'.')
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   128
		if i>=0: return name[i+1:]
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   129
		return name
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   130
	except AttributeError:
cb0c4d97e29b Added _className func
rgbecker
parents: 413
diff changeset
   131
		return str(self)