Commit 8e1bbffd authored by Jim Fulton's avatar Jim Fulton

Revamped attribute parsing to:

  - Handle valueless attributes a bit better.
    In particular, if a valueless parameter is not
    in the first position, it is not confused for a name,

  - Added "..." shorthand for exprs, so now you can:

      <!--#if "x < 1"-->
parent 5eb73e87
'''$Id: DT_Util.py,v 1.37 1998/05/20 17:54:34 jim Exp $''' '''$Id: DT_Util.py,v 1.38 1998/07/27 23:42:12 jim Exp $'''
############################################################################ ############################################################################
# Copyright # Copyright
...@@ -52,7 +52,7 @@ ...@@ -52,7 +52,7 @@
# (540) 371-6909 # (540) 371-6909
# #
############################################################################ ############################################################################
__version__='$Revision: 1.37 $'[11:-2] __version__='$Revision: 1.38 $'[11:-2]
import sys, regex, string, types, math, os import sys, regex, string, types, math, os
from string import rfind, strip, joinfields, atoi,lower,upper,capitalize from string import rfind, strip, joinfields, atoi,lower,upper,capitalize
...@@ -75,9 +75,6 @@ def int_param(params,md,name,default=0): ...@@ -75,9 +75,6 @@ def int_param(params,md,name,default=0):
v=atoi(v) v=atoi(v)
return v return v
def _tm(m, tag):
return m + tag and (' in %s' % tag)
def careful_getattr(md, inst, name): def careful_getattr(md, inst, name):
if name[:1]!='_': if name[:1]!='_':
validate=md.validate validate=md.validate
...@@ -220,23 +217,41 @@ def name_param(params,tag='',expr=0, attr='name', default_unnamed=1): ...@@ -220,23 +217,41 @@ def name_param(params,tag='',expr=0, attr='name', default_unnamed=1):
used=params.has_key used=params.has_key
__traceback_info__=params, tag, expr, attr __traceback_info__=params, tag, expr, attr
if expr and used('expr') and used('') and not used(params['']): #if expr and used('expr') and used('') and not used(params['']):
# Fix up something like: <!--#in expr="whatever" mapping--> # # Fix up something like: <!--#in expr="whatever" mapping-->
params[params['']]=default_unnamed # params[params['']]=default_unnamed
del params[''] # del params['']
if used(''): if used(''):
v=params['']
if v[:1]=='"' and v[-1:]=='"' and len(v) > 1: # expr shorthand
if used(attr):
raise ParseError, ('%s and expr given' % attr, tag)
if expr:
if used('expr'):
raise ParseError, ('two exprs given', tag)
v=v[1:-1]
expr=Eval(v, expr_globals)
return v, expr
else: raise ParseError, (
'The "..." shorthand for expr was used in a tag that doesn\'t support expr attributes.',
tag)
else: # name shorthand
if used(attr): if used(attr):
raise ParseError, _tm('Two %s values were given', (attr,tag)) raise ParseError, ('Two %s values were given' % attr, tag)
if expr: if expr:
if used('expr'): if used('expr'):
raise ParseError, _tm('%s and expr given', (attr,tag)) # raise 'Waaaaaa', 'waaa'
raise ParseError, ('%s and expr given' % attr, tag)
return params[''],None return params[''],None
return params[''] return params['']
elif used(attr): elif used(attr):
if expr: if expr:
if used('expr'): if used('expr'):
raise ParseError, _tm('%s and expr given', (attr,tag)) raise ParseError, ('%s and expr given' % attr, tag)
return params[attr],None return params[attr],None
return params[attr] return params[attr]
elif expr and used('expr'): elif expr and used('expr'):
...@@ -244,7 +259,7 @@ def name_param(params,tag='',expr=0, attr='name', default_unnamed=1): ...@@ -244,7 +259,7 @@ def name_param(params,tag='',expr=0, attr='name', default_unnamed=1):
expr=Eval(name, expr_globals) expr=Eval(name, expr_globals)
return name, expr return name, expr
raise ParseError, ('No %s given', (attr,tag)) raise ParseError, ('No %s given' % attr, tag)
Expr_doc=""" Expr_doc="""
...@@ -303,11 +318,14 @@ Python expression support ...@@ -303,11 +318,14 @@ Python expression support
""" """
ListType=type([])
def parse_params(text, def parse_params(text,
result=None, result=None,
tag='', tag='',
unparmre=regex.compile( unparmre=regex.compile(
'\([\0- ]*\([^\0- =\"]+\)\)'), '\([\0- ]*\([^\0- =\"]+\)\)'),
qunparmre=regex.compile(
'\([\0- ]*\("[^\0- =\"]+"\)\)'),
parmre=regex.compile( parmre=regex.compile(
'\([\0- ]*\([^\0- =\"]+\)=\([^\0- =\"]+\)\)'), '\([\0- ]*\([^\0- =\"]+\)=\([^\0- =\"]+\)\)'),
qparmre=regex.compile( qparmre=regex.compile(
...@@ -326,7 +344,7 @@ def parse_params(text, ...@@ -326,7 +344,7 @@ def parse_params(text,
keyword parameters give valid parameter names and default values. keyword parameters give valid parameter names and default values.
If a specification is not a name-value pair and it is not the If a specification is not a name-value pair and it is not the
first specification that is not a name-value pair and it is a first specification and it is a
valid parameter name, then it is treated as a name-value pair with valid parameter name, then it is treated as a name-value pair with
a value as given in the keyword argument. Otherwise, if it is not a value as given in the keyword argument. Otherwise, if it is not
a name-value pair, it is treated as an unnamed value. a name-value pair, it is treated as an unnamed value.
...@@ -348,7 +366,7 @@ def parse_params(text, ...@@ -348,7 +366,7 @@ def parse_params(text,
elif unparmre.match(text) >= 0: elif unparmre.match(text) >= 0:
name=unparmre.group(2) name=unparmre.group(2)
l=len(unparmre.group(1)) l=len(unparmre.group(1))
if result.has_key(''): if result:
if parms.has_key(name): if parms.has_key(name):
if parms[name] is None: raise ParseError, ( if parms[name] is None: raise ParseError, (
'Attribute %s requires a value' % name, tag) 'Attribute %s requires a value' % name, tag)
...@@ -359,6 +377,13 @@ def parse_params(text, ...@@ -359,6 +377,13 @@ def parse_params(text,
else: else:
result['']=name result['']=name
return apply(parse_params,(text[l:],result),parms) return apply(parse_params,(text[l:],result),parms)
elif qunparmre.match(text) >= 0:
name=qunparmre.group(2)
l=len(qunparmre.group(1))
if result: raise ParseError, (
'Invalid attribute name, "%s"' % name, tag)
else: result['']=name
return apply(parse_params,(text[l:],result),parms)
else: else:
if not text or not strip(text): return result if not text or not strip(text): return result
raise ParseError, ('invalid parameter: "%s"' % text, tag) raise ParseError, ('invalid parameter: "%s"' % text, tag)
...@@ -367,6 +392,12 @@ def parse_params(text, ...@@ -367,6 +392,12 @@ def parse_params(text,
raise ParseError, ( raise ParseError, (
'Invalid attribute name, "%s"' % name, tag) 'Invalid attribute name, "%s"' % name, tag)
if result.has_key(name):
p=parms[name]
if type(p) is not ListType or p:
raise ParseError, (
'Duplicate values for attribute "%s"' % name, tag)
result[name]=value result[name]=value
text=strip(text[l:]) text=strip(text[l:])
...@@ -375,6 +406,17 @@ def parse_params(text, ...@@ -375,6 +406,17 @@ def parse_params(text,
############################################################################ ############################################################################
# $Log: DT_Util.py,v $ # $Log: DT_Util.py,v $
# Revision 1.38 1998/07/27 23:42:12 jim
# Revamped attribute parsing to:
#
# - Handle valueless attributes a bit better.
# In particular, if a valueless parameter is not
# in the first position, it is not confused for a name,
#
# - Added "..." shorthand for exprs, so now you can:
#
# <!--#if "x < 1"-->
#
# Revision 1.37 1998/05/20 17:54:34 jim # Revision 1.37 1998/05/20 17:54:34 jim
# Fixed obsolete stupid attr function. Blech. # Fixed obsolete stupid attr function. Blech.
# #
......
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