src/reportlab/lib/extformat.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
2280
c1bedbc21804 Added header lines
rgbecker
parents: 2278
diff changeset
     2
#see license.txt for license details
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3783
diff changeset
     3
__version__='3.3.0'
3029
eded59f94021 adding docstrings to lib
andy
parents: 2964
diff changeset
     4
__doc__='''Apparently not used anywhere, purpose unknown!'''
3783
b4d048dad7e6 attempt to replace missing pattern for python3.3
robin
parents: 3721
diff changeset
     5
try:
b4d048dad7e6 attempt to replace missing pattern for python3.3
robin
parents: 3721
diff changeset
     6
	from tokenize import tokenprog
b4d048dad7e6 attempt to replace missing pattern for python3.3
robin
parents: 3721
diff changeset
     7
except ImportError:
b4d048dad7e6 attempt to replace missing pattern for python3.3
robin
parents: 3721
diff changeset
     8
	from tokenize import Token
b4d048dad7e6 attempt to replace missing pattern for python3.3
robin
parents: 3721
diff changeset
     9
	import re
b4d048dad7e6 attempt to replace missing pattern for python3.3
robin
parents: 3721
diff changeset
    10
	tokenprog = re.compile(Token)
b4d048dad7e6 attempt to replace missing pattern for python3.3
robin
parents: 3721
diff changeset
    11
	del Token, re
2278
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    12
import sys
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    13
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    14
def _matchorfail(text, pos):
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    15
	match = tokenprog.match(text, pos)
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    16
	if match is None: raise ValueError(text, pos)
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    17
	return match, match.end()
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    18
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    19
'''
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    20
	Extended dictionary formatting
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    21
	We allow expressions in the parentheses instead of
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    22
	just a simple variable.
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    23
'''
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    24
def dictformat(_format, L={}, G={}):
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    25
	format = _format
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    26
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    27
	S = {}
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    28
	chunks = []
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    29
	pos = 0
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    30
	n = 0
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    31
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    32
	while 1:
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    33
		pc = format.find("%", pos)
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    34
		if pc < 0: break
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    35
		nextchar = format[pc+1]
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    36
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    37
		if nextchar == "(":
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    38
			chunks.append(format[pos:pc])
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    39
			pos, level = pc+2, 1
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    40
			while level:
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    41
				match, pos = _matchorfail(format, pos)
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    42
				tstart, tend = match.regs[3]
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    43
				token = format[tstart:tend]
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    44
				if token == "(": level = level+1
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    45
				elif token == ")": level = level-1
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    46
			vname = '__superformat_%d' % n
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    47
			n += 1
2825
9bcf7ef27be3 extformat.py: fix bad bug in dictformat
rgbecker
parents: 2332
diff changeset
    48
			S[vname] = eval(format[pc+2:pos-1],G,L)
2278
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    49
			chunks.append('%%(%s)' % vname)
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    50
		else:
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    51
			nc = pc+1+(nextchar=="%")
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    52
			chunks.append(format[pos:nc])
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    53
			pos = nc
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    54
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    55
	if pos < len(format): chunks.append(format[pos:])
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    56
	return (''.join(chunks)) % S
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    57
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    58
def magicformat(format):
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    59
	"""Evaluate and substitute the appropriate parts of the string."""
3331
8eb7bb3d5872 extformat.py: compatibilty fixes
rgbecker
parents: 3029
diff changeset
    60
	frame = sys._getframe(1)
2278
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    61
	return dictformat(format,frame.f_locals, frame.f_globals)
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    62
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    63
if __name__=='__main__':
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    64
	from reportlab.lib.formatters import DecimalFormatter
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    65
	_DF={}
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    66
	def df(n,dp=2,ds='.',ts=','):
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    67
		try:
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    68
			_df = _DF[dp,ds]
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    69
		except KeyError:
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    70
			_df = _DF[dp,ds] = DecimalFormatter(places=dp,decimalSep=ds,thousandSep=ts)
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    71
		return _df(n)
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    72
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    73
	from reportlab.lib.extformat import magicformat
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    74
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    75
	Z={'abc': ('ab','c')}
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    76
	x = 300000.23
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    77
	percent=79.2
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    78
	class dingo:
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    79
		a=3
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    80
	print((magicformat('''
2278
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    81
$%%(df(x,dp=3))s --> $%(df(x,dp=3))s
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    82
$%%(df(x,dp=2,ds=',',ts='.'))s --> $%(df(x,dp=2,ds=',',ts='.'))s
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    83
%%(percent).2f%%%% --> %(percent).2f%%
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    84
%%(dingo.a)s --> %(dingo.a)s
fcdaeea386f2 Added for better formatting
rgbecker
parents:
diff changeset
    85
%%(Z['abc'][0])s --> %(Z['abc'][0])s
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    86
''')))
3331
8eb7bb3d5872 extformat.py: compatibilty fixes
rgbecker
parents: 3029
diff changeset
    87
	def func0(aa=1):
8eb7bb3d5872 extformat.py: compatibilty fixes
rgbecker
parents: 3029
diff changeset
    88
		def func1(bb=2):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    89
			print((magicformat('bb=%(bb)s Z=%(Z)r')))
3331
8eb7bb3d5872 extformat.py: compatibilty fixes
rgbecker
parents: 3029
diff changeset
    90
		func1('BB')
8eb7bb3d5872 extformat.py: compatibilty fixes
rgbecker
parents: 3029
diff changeset
    91
	func0('AA')