Commit 6544d2da authored by Jim Fulton's avatar Jim Fulton

added expr

parent cb833537
...@@ -110,8 +110,8 @@ __doc__='''Conditional insertion ...@@ -110,8 +110,8 @@ __doc__='''Conditional insertion
# (540) 371-6909 # (540) 371-6909
# #
############################################################################ ############################################################################
__rcs_id__='$Id: DT_If.py,v 1.2 1997/09/08 15:35:40 jim Exp $' __rcs_id__='$Id: DT_If.py,v 1.3 1997/09/22 14:42:49 jim Exp $'
__version__='$Revision: 1.2 $'[11:-2] __version__='$Revision: 1.3 $'[11:-2]
from DT_Util import * from DT_Util import *
...@@ -119,19 +119,22 @@ class If: ...@@ -119,19 +119,22 @@ class If:
blockContinuations='else','elif' blockContinuations='else','elif'
name='if' name='if'
elses=None elses=None
expr=''
def __init__(self, blocks): def __init__(self, blocks):
tname, args, section = blocks[0] tname, args, section = blocks[0]
args=parse_params(args, name='') args=parse_params(args, name='', expr='')
name=name_param(args) name,expr=name_param(args,'if',1)
self.__name__ = name self.__name__= name
self.sections=[(name, section)] self.sections=[(name, expr, section)]
if blocks[-1][0]=='else': if blocks[-1][0]=='else':
tname, args, section = blocks[-1] tname, args, section = blocks[-1]
blocks=blocks[:-1] blocks=blocks[:-1]
args=parse_params(args, name='') args=parse_params(args, name='')
if args: if args:
ename=name_param(args) ename,expr=name_param(args,'else',1)
if ename != name: if ename != name:
raise ParseError, 'name in else does not match if' raise ParseError, 'name in else does not match if'
self.elses=section self.elses=section
...@@ -139,16 +142,22 @@ class If: ...@@ -139,16 +142,22 @@ class If:
for tname, args, section in blocks[1:]: for tname, args, section in blocks[1:]:
if tname=='else': if tname=='else':
raise ParseError, 'more than one else tag for a single if tag' raise ParseError, 'more than one else tag for a single if tag'
name=name_param(args) args=parse_params(args, name='', expr='')
self.sections.append((name, section)) name,expr=name_param(args,'elif',1)
self.sections.append((name, expr, section))
def render(self,md): def render(self,md):
for name, section in self.sections: for name, expr, section in self.sections:
try: v=md[name] if expr is None:
except: v=None try: v=md[name]
except: v=None
else:
v=expr.eval(md)
if v: return section(None,md) if v: return section(None,md)
if self.elses: return self.elses(None, md) if self.elses: return self.elses(None, md)
return '' return ''
__call__=render __call__=render
...@@ -175,6 +184,9 @@ class Else: ...@@ -175,6 +184,9 @@ class Else:
########################################################################## ##########################################################################
# #
# $Log: DT_If.py,v $ # $Log: DT_If.py,v $
# Revision 1.3 1997/09/22 14:42:49 jim
# added expr
#
# Revision 1.2 1997/09/08 15:35:40 jim # Revision 1.2 1997/09/08 15:35:40 jim
# Fixed bug that caused else blocks to render if blocks. # Fixed bug that caused else blocks to render if blocks.
# #
......
...@@ -212,7 +212,7 @@ ...@@ -212,7 +212,7 @@
of the module 'Missing', if present. of the module 'Missing', if present.
''' '''
__rcs_id__='$Id: DT_In.py,v 1.2 1997/09/02 19:04:24 jim Exp $' __rcs_id__='$Id: DT_In.py,v 1.3 1997/09/22 14:42:50 jim Exp $'
############################################################################ ############################################################################
# Copyright # Copyright
...@@ -266,7 +266,7 @@ __rcs_id__='$Id: DT_In.py,v 1.2 1997/09/02 19:04:24 jim Exp $' ...@@ -266,7 +266,7 @@ __rcs_id__='$Id: DT_In.py,v 1.2 1997/09/02 19:04:24 jim Exp $'
# (540) 371-6909 # (540) 371-6909
# #
############################################################################ ############################################################################
__version__='$Revision: 1.2 $'[11:-2] __version__='$Revision: 1.3 $'[11:-2]
from DT_Util import * from DT_Util import *
...@@ -274,15 +274,16 @@ class In: ...@@ -274,15 +274,16 @@ class In:
blockContinuations=('else',) blockContinuations=('else',)
name='in' name='in'
elses=None elses=None
expr=None
def __init__(self, blocks): def __init__(self, blocks):
tname, args, section = blocks[0] tname, args, section = blocks[0]
args=parse_params(args, name='', start='1',end='-1',size='10', args=parse_params(args, name='', start='1',end='-1',size='10',
orphan='3',overlap='1',mapping=1, orphan='3',overlap='1',mapping=1,
previous=1, next=1) previous=1, next=1, expr='')
self.args=args self.args=args
name=name_param(args) name,expr=name_param(args,'in',1)
self.__name__ = name self.__name__, expr = name, expr
self.section=section self.section=section
if len(blocks) > 1: if len(blocks) > 1:
if len(blocks) != 2: raise ParseError, 'too many else blocks' if len(blocks) != 2: raise ParseError, 'too many else blocks'
...@@ -296,8 +297,11 @@ class In: ...@@ -296,8 +297,11 @@ class In:
def render(self, md): def render(self, md):
try: sequence=md[self.__name__] or None expr=self.expr
except: sequence=None if expr is None:
try: sequence=md[self.__name__] or None
except: sequence=None
else: sequence=expr.eval(md)
if not sequence: if not sequence:
if self.elses: return self.elses(None, md) if self.elses: return self.elses(None, md)
...@@ -714,6 +718,9 @@ class sequence_variables: ...@@ -714,6 +718,9 @@ class sequence_variables:
############################################################################ ############################################################################
# $Log: DT_In.py,v $ # $Log: DT_In.py,v $
# Revision 1.3 1997/09/22 14:42:50 jim
# added expr
#
# Revision 1.2 1997/09/02 19:04:24 jim # Revision 1.2 1997/09/02 19:04:24 jim
# Got rid of ^Ms # Got rid of ^Ms
# #
......
'''$Id: DT_Util.py,v 1.2 1997/09/02 20:35:09 jim Exp $''' '''$Id: DT_Util.py,v 1.3 1997/09/22 14:42:50 jim Exp $'''
############################################################################ ############################################################################
# Copyright # Copyright
...@@ -52,13 +52,14 @@ ...@@ -52,13 +52,14 @@
# (540) 371-6909 # (540) 371-6909
# #
############################################################################ ############################################################################
__version__='$Revision: 1.2 $'[11:-2] __version__='$Revision: 1.3 $'[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
from types import * from types import *
from regsub import gsub, sub, split from regsub import gsub, sub, split
from __builtin__ import * from __builtin__ import *
import VSEval
ParseError='Document Template Parse Error' ParseError='Document Template Parse Error'
...@@ -78,13 +79,28 @@ class func_code: ...@@ -78,13 +79,28 @@ class func_code:
self.co_varnames=varnames self.co_varnames=varnames
self.co_argcount=len(varnames) self.co_argcount=len(varnames)
def name_param(params): def _tm(m, tag):
if params.has_key(''): return m + tag and (' in %s' % tag)
if params.has_key('name'):
raise ParseError, 'Name given twice' def name_param(params,tag='',expr=0):
used=params.has_key
if used(''):
if used('name'):
raise ParseError, _tm('Two names were given', tag)
if expr:
if used('expr'): raise ParseError, _tm('name and expr given', tag)
return params[''],None
return params[''] return params['']
elif params.has_key('name'): return params['name'] elif used('name'):
if expr:
if used('expr'): raise ParseError, _tm('name and expr given', tag)
return params['name'],None
return params['name']
elif expr and used('expr'):
name=params['expr']
expr=VSEval.Eval(name, __mul__=VSEval.careful_mul, __getattr__=None)
return name, expr
raise ParseError, 'No name given' raise ParseError, 'No name given'
def parse_params(text, def parse_params(text,
...@@ -157,6 +173,9 @@ except: from pDocumentTemplate import InstanceDict, TemplateDict, render_blocks ...@@ -157,6 +173,9 @@ except: from pDocumentTemplate import InstanceDict, TemplateDict, render_blocks
############################################################################ ############################################################################
# $Log: DT_Util.py,v $ # $Log: DT_Util.py,v $
# Revision 1.3 1997/09/22 14:42:50 jim
# added expr
#
# Revision 1.2 1997/09/02 20:35:09 jim # Revision 1.2 1997/09/02 20:35:09 jim
# Various fixes to parsing code. # Various fixes to parsing code.
# #
......
...@@ -83,7 +83,7 @@ __doc__='''Variable insertion parameters ...@@ -83,7 +83,7 @@ __doc__='''Variable insertion parameters
The parameter 'spacify' may be provided to cause underscores in The parameter 'spacify' may be provided to cause underscores in
the inserted value to be converted to spaces. the inserted value to be converted to spaces.
''' '''
__rcs_id__='$Id: DT_Var.py,v 1.1 1997/08/27 18:55:44 jim Exp $' __rcs_id__='$Id: DT_Var.py,v 1.2 1997/09/22 14:42:51 jim Exp $'
############################################################################ ############################################################################
# Copyright # Copyright
...@@ -137,33 +137,33 @@ __rcs_id__='$Id: DT_Var.py,v 1.1 1997/08/27 18:55:44 jim Exp $' ...@@ -137,33 +137,33 @@ __rcs_id__='$Id: DT_Var.py,v 1.1 1997/08/27 18:55:44 jim Exp $'
# (540) 371-6909 # (540) 371-6909
# #
############################################################################ ############################################################################
__version__='$Revision: 1.1 $'[11:-2] __version__='$Revision: 1.2 $'[11:-2]
from DT_Util import * from DT_Util import *
class Var: class Var:
name='var' name='var'
expr=None
def __init__(self, args, fmt=''): def __init__(self, args, fmt=''):
args = parse_params(args, name='', lower=1, upper=1, args = parse_params(args, name='', lower=1, upper=1, expr='',
capitalize=1, spacify=1, null='', fmt='s') capitalize=1, spacify=1, null='', fmt='s')
self.args=args self.args=args
used=args.has_key
if args.has_key('name'): name, expr = name_param(args,'var',1)
name=args
if args.has_key(''):
raise ParseError, 'Two named given in var'
elif args.has_key(''):
name=args['']
else:
raise ParseError, 'No name given in var'
self.__name__ = name self.__name__, self.expr = name, expr
self.fmt = fmt self.fmt = fmt
def render(self, md): def render(self, md):
name=self.__name__ name=self.__name__
val = md[name] val=self.expr
if val is None:
val = md[name]
else:
val=val.eval(md)
__traceback_info__=name, val __traceback_info__=name, val
# handle special formats defined using fmt= first # handle special formats defined using fmt= first
...@@ -243,6 +243,12 @@ def dollars_and_cents_with_commas(v): ...@@ -243,6 +243,12 @@ def dollars_and_cents_with_commas(v):
except: v= '' except: v= ''
return commatify(v) return commatify(v)
def len_format(v):
return str(len(v))
def len_comma(v):
return commatify(str(len(v)))
special_formats={ special_formats={
'html-quote': html_quote, 'html-quote': html_quote,
'url-quote': url_quote, 'url-quote': url_quote,
...@@ -251,10 +257,15 @@ special_formats={ ...@@ -251,10 +257,15 @@ special_formats={
'dollars-and-cents': dollars_and_cents, 'dollars-and-cents': dollars_and_cents,
'dollars-with-commas': whole_dollars_with_commas, 'dollars-with-commas': whole_dollars_with_commas,
'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-with-commas': len_comma,
} }
############################################################################ ############################################################################
# $Log: DT_Var.py,v $ # $Log: DT_Var.py,v $
# Revision 1.2 1997/09/22 14:42:51 jim
# added expr
#
# Revision 1.1 1997/08/27 18:55:44 jim # Revision 1.1 1997/08/27 18:55:44 jim
# initial # initial
# #
......
*shared* *shared*
cDocumentTemplate cDocumentTemplate.c -I../ExtensionClass cDocumentTemplate cDocumentTemplate.c -I/projects/_/ExtensionClass
# install DT_Dict.py # install DT_Dict.py
# install DT_Doc.py # install DT_Doc.py
......
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