Skip to content

Commit

Permalink
Changed the way that '_' is handled. It is now an alias for the templ…
Browse files Browse the repository at this point in the history
…ate dict.
  • Loading branch information
Jim Fulton committed May 13, 1998
1 parent faa865c commit bb11479
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
59 changes: 35 additions & 24 deletions DT_Util.py
@@ -1,4 +1,4 @@
'''$Id: DT_Util.py,v 1.31 1998/04/02 17:37:37 jim Exp $'''
'''$Id: DT_Util.py,v 1.32 1998/05/13 21:09:23 jim Exp $'''

############################################################################
# Copyright
Expand Down Expand Up @@ -52,7 +52,7 @@
# (540) 371-6909
#
############################################################################
__version__='$Revision: 1.31 $'[11:-2]
__version__='$Revision: 1.32 $'[11:-2]

import sys, regex, string, types, math, os
from string import rfind, strip, joinfields, atoi,lower,upper,capitalize
Expand Down Expand Up @@ -92,6 +92,22 @@ def careful_getattr(md, inst, name):

raise ValidationError, name

def careful_hasattr(md, inst, name):
try:
if name[:1]!='_':
validate=md.validate

if validate is None: return hasattr(inst, name)

if hasattr(inst,'aq_acquire'):
inst.aq_acquire(name, validate, md)
return 1

v=getattr(inst, name)
if validate(inst,inst,name,v,md): return 1
except: pass
return 0

def careful_getitem(md, mapping, key):
v=mapping[key]

Expand Down Expand Up @@ -119,21 +135,20 @@ def careful_getslice(md, seq, *indexes):

return v

import string, math, rand, whrandom
import string, math, whrandom

class expr_globals: pass
expr_globals=expr_globals()
try: from cDocumentTemplate import InstanceDict, TemplateDict, render_blocks
except: from pDocumentTemplate import InstanceDict, TemplateDict, render_blocks

d=expr_globals.__dict__
d=TemplateDict.__dict__
for name in ('None', 'abs', 'chr', 'divmod', 'float', 'hash', 'hex', 'int',
'len', 'max', 'min', 'oct', 'ord', 'pow', 'round', 'str'):
d[name]=__builtins__[name]
d['string']=string
d['math']=math
d['rand']=rand
d['whrandom']=whrandom

def test(*args):
def test(self, *args):
l=len(args)
for i in range(1, l, 2):
if args[i-1]: return args[i]
Expand All @@ -142,14 +157,13 @@ def test(*args):

d['test']=test

def _attr(inst, name, md={}):
return careful_getattr(md, inst, name)

d['attr']=_attr
d['attr']=careful_getattr
d['getattr']=careful_getattr
d['hasattr']=careful_hasattr

class namespace_: pass

def namespace(**kw):
def namespace(self, **kw):
r=namespace_()
d=r.__dict__
for k, v in kw.items(): d[k]=v
Expand All @@ -159,7 +173,6 @@ def namespace(**kw):

expr_globals={
'__builtins__':{},
'_': expr_globals,
'__guarded_mul__': VSEval.careful_mul,
'__guarded_getattr__': careful_getattr,
'__guarded_getitem__': careful_getitem,
Expand Down Expand Up @@ -221,15 +234,15 @@ def name_param(params,tag='',expr=0, attr='name', default_unnamed=1):
In addition, certain special additional names are available:
'_vars' -- Provides access to the document template namespace as a
'_' -- Provides access to the document template namespace as a
mapping object. This variable can be useful for accessing
objects in a document template namespace that have names that are
not legal Python variable names::
<!--#var expr="_vars['sequence-number']*5"-->
<!--#var expr="_['sequence-number']*5"-->
'_' -- Provides access to a Python module containing standard
utility objects. These utility objects include:
This variable also has attributes that provide access to standard
utility objects. These attributes include:
- The objects: 'None', 'abs', 'chr', 'divmod', 'float', 'hash',
'hex', 'int', 'len', 'max', 'min', 'oct', 'ord', 'pow',
Expand All @@ -252,7 +265,7 @@ def name_param(params,tag='',expr=0, attr='name', default_unnamed=1):
<!--#var expr="_.string.lower(title)"-->
""" #"
"""

def parse_params(text,
result=None,
Expand Down Expand Up @@ -324,13 +337,11 @@ def parse_params(text,
if text: return apply(parse_params,(text,result),parms)
else: return result

try: from cDocumentTemplate import InstanceDict, TemplateDict, render_blocks
except: from pDocumentTemplate import InstanceDict, TemplateDict, render_blocks

#from cDocumentTemplate import InstanceDict, TemplateDict, render_blocks

############################################################################
# $Log: DT_Util.py,v $
# Revision 1.32 1998/05/13 21:09:23 jim
# Changed the way that '_' is handled. It is now an alias for the template dict.
#
# Revision 1.31 1998/04/02 17:37:37 jim
# Major redesign of block rendering. The code inside a block tag is
# compiled as a template but only the templates blocks are saved, and
Expand Down
9 changes: 6 additions & 3 deletions VSEval.py
@@ -1,7 +1,7 @@

"""Very Safe Python Expressions
"""
__rcs_id__='$Id: VSEval.py,v 1.12 1998/04/02 17:37:39 jim Exp $'
__rcs_id__='$Id: VSEval.py,v 1.13 1998/05/13 21:10:30 jim Exp $'

############################################################################
# Copyright
Expand All @@ -11,7 +11,7 @@
# rights reserved.
#
############################################################################
__version__='$Revision: 1.12 $'[11:-2]
__version__='$Revision: 1.13 $'[11:-2]

from string import join, find, split, translate
import sys, gparse, string
Expand Down Expand Up @@ -110,7 +110,7 @@ def HAS_ARG(op): ((op) >= HAVE_ARGUMENT)
self.used=tuple(used.keys())

def eval(self, mapping):
d={'_vars': mapping}
d={'_vars': mapping, '_': mapping}
code=self.code
globals=self.globals
for name in self.used:
Expand All @@ -132,6 +132,9 @@ def __call__(self, **kw):
############################################################################
#
# $Log: VSEval.py,v $
# Revision 1.13 1998/05/13 21:10:30 jim
# Changed the way that '_' is handled. It is now an alias for the template dict.
#
# Revision 1.12 1998/04/02 17:37:39 jim
# Major redesign of block rendering. The code inside a block tag is
# compiled as a template but only the templates blocks are saved, and
Expand Down

0 comments on commit bb11479

Please sign in to comment.