Commit ec6aa794 authored by Jim Fulton's avatar Jim Fulton

Added additional special formats, structured-text and sql-quote.

Also changed the way formats are handled.  This has (and will)
reveal now hidden fmt=invalid-thing errors.
parent 9b370798
...@@ -96,7 +96,7 @@ __doc__='''Variable insertion parameters ...@@ -96,7 +96,7 @@ __doc__='''Variable insertion parameters
'"blah blah blah blah"', then the tag '"blah blah blah blah"', then the tag
'<!--#var spam size=10-->' inserts '"blah blah ..."'. '<!--#var spam size=10-->' inserts '"blah blah ..."'.
''' # ' ''' # '
__rcs_id__='$Id: DT_Var.py,v 1.5 1997/10/27 17:39:27 jim Exp $' __rcs_id__='$Id: DT_Var.py,v 1.6 1997/12/12 16:19:06 jim Exp $'
############################################################################ ############################################################################
# Copyright # Copyright
...@@ -150,10 +150,12 @@ __rcs_id__='$Id: DT_Var.py,v 1.5 1997/10/27 17:39:27 jim Exp $' ...@@ -150,10 +150,12 @@ __rcs_id__='$Id: DT_Var.py,v 1.5 1997/10/27 17:39:27 jim Exp $'
# (540) 371-6909 # (540) 371-6909
# #
############################################################################ ############################################################################
__version__='$Revision: 1.5 $'[11:-2] __version__='$Revision: 1.6 $'[11:-2]
from DT_Util import * from DT_Util import *
from string import find, split, join
class Var: class Var:
name='var' name='var'
expr=None expr=None
...@@ -185,19 +187,12 @@ class Var: ...@@ -185,19 +187,12 @@ class Var:
# handle special formats defined using fmt= first # handle special formats defined using fmt= first
if have_arg('fmt'): if have_arg('fmt'):
try: fmt=args['fmt']
# first try a parameterless method of val if hasattr(val, fmt):
val = getattr(val,args['fmt'])() val = getattr(val,fmt)()
except AttributeError: elif special_formats.has_key(fmt):
try: val = special_formats[fmt](val, name, md)
# failing that, try a special format else: val = fmt % val
val = special_formats[args['fmt']](val)
except KeyError:
try:
# last chance - a format string itself?
val = args['fmt'] % val
except:
pass
# next, look for upper, lower, etc # next, look for upper, lower, etc
if have_arg('upper'): if have_arg('upper'):
...@@ -235,7 +230,7 @@ class Var: ...@@ -235,7 +230,7 @@ class Var:
__call__=render __call__=render
def html_quote(v, def html_quote(v, name='(Unknown name)', md={},
character_entities=( character_entities=(
(regex.compile('&'), '&amp;'), (regex.compile('&'), '&amp;'),
(regex.compile("<"), '&lt;' ), (regex.compile("<"), '&lt;' ),
...@@ -246,43 +241,60 @@ def html_quote(v, ...@@ -246,43 +241,60 @@ def html_quote(v,
text=gsub(re,name,text) text=gsub(re,name,text)
return text return text
def url_quote(v): def url_quote(v, name='(Unknown name)', md={}):
import urllib import urllib
return urllib.quote(str(v)) return urllib.quote(str(v))
def multi_line(v, nl=regex.compile('\r?\n')): def multi_line(v, name='(Unknown name)', md={},
nl=regex.compile('\r?\n')):
return gsub(nl,'<br>\n',str(v)) return gsub(nl,'<br>\n',str(v))
def whole_dollars(v): def whole_dollars(v, name='(Unknown name)', md={}):
try: return "$%d" % v try: return "$%d" % v
except: return '' except: return ''
def dollars_and_cents(v): def dollars_and_cents(v, name='(Unknown name)', md={}):
try: return "$%.2f" % v try: return "$%.2f" % v
except: return '' except: return ''
def commatify(v,thou=regex.compile("\([0-9]\)\([0-9][0-9][0-9]\([,.]\|$\)\)")): def commatify(v, name='(Unknown name)', md={},
thou=regex.compile("\([0-9]\)\([0-9][0-9][0-9]\([,.]\|$\)\)")):
v=str(v) v=str(v)
while thou.search(v) >= 0: while thou.search(v) >= 0:
v=sub(thou,"\\1,\\2",v) v=sub(thou,"\\1,\\2",v)
return v return v
def whole_dollars_with_commas(v): def whole_dollars_with_commas(v, name='(Unknown name)', md={}):
try: v= "$%d" % v try: v= "$%d" % v
except: v='' except: v=''
return commatify(v) return commatify(v)
def dollars_and_cents_with_commas(v): def dollars_and_cents_with_commas(v, name='(Unknown name)', md={}):
try: v= "$%.2f" % v try: v= "$%.2f" % v
except: v= '' except: v= ''
return commatify(v) return commatify(v)
def len_format(v): def len_format(v, name='(Unknown name)', md={}):
return str(len(v)) return str(len(v))
def len_comma(v): def len_comma(v, name='(Unknown name)', md={}):
return commatify(str(len(v))) return commatify(str(len(v)))
StructuredText=None
def structured_text(v, name='(Unknown name)', md={}):
global StructuredText
if StructuredText is None: import StructuredText
return str(StructuredText.html_with_references(str(v), 3))
def sql_quote(v, name='(Unknown name)', md={}):
"""Quote single quotes in a string by doubling them.
This is needed to securely insert values into sql
string literals in templates that generate sql.
"""
if find(c,"'") >= 0: return join(split(v,"'"),"''")
return v
special_formats={ special_formats={
'html-quote': html_quote, 'html-quote': html_quote,
'url-quote': url_quote, 'url-quote': url_quote,
...@@ -294,10 +306,18 @@ special_formats={ ...@@ -294,10 +306,18 @@ special_formats={
'dollars-and-cents-with-commas': dollars_and_cents_with_commas, 'dollars-and-cents-with-commas': dollars_and_cents_with_commas,
'collection-length': len_format, 'collection-length': len_format,
'collection-length-with-commas': len_comma, 'collection-length-with-commas': len_comma,
'structured-text': structured_text,
'sql-quote': sql_quote,
} }
############################################################################ ############################################################################
# $Log: DT_Var.py,v $ # $Log: DT_Var.py,v $
# Revision 1.6 1997/12/12 16:19:06 jim
# Added additional special formats, structured-text and sql-quote.
# Also changed the way formats are handled. This has (and will)
# reveal now hidden fmt=invalid-thing errors.
#
# Revision 1.5 1997/10/27 17:39:27 jim # Revision 1.5 1997/10/27 17:39:27 jim
# removed a comment burp. # removed a comment burp.
# #
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment