Package qm :: Package external :: Package DocumentTemplate :: Module DT_Let
[hide private]
[frames] | no frames]

Source Code for Module qm.external.DocumentTemplate.DT_Let

  1  ############################################################################## 
  2  # 
  3  # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved. 
  4  # 
  5  # This software is subject to the provisions of the Zope Public License, 
  6  # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution. 
  7  # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED 
  8  # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  9  # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS 
 10  # FOR A PARTICULAR PURPOSE 
 11  # 
 12  ############################################################################## 
 13   
 14  ''' The Let tag was contributed to Zope by and is copyright, 1999 
 15      Phillip J. Eby.  Permission has been granted to release the Let tag 
 16      under the Zope Public License. 
 17   
 18   
 19     Let name=value... 
 20   
 21     The 'let' tag is used to bind variables to values within a block. 
 22   
 23     The text enclosed in the let tag is rendered using information 
 24     from the given variables or expressions. 
 25   
 26     For example:: 
 27   
 28       <!--#let foofunc="foo()" my_bar=bar--> 
 29         foo() = <!--#var foofunc-->, 
 30         bar = <!--#var my_bar--> 
 31       <!--#/let--> 
 32   
 33     Notice that both 'name' and 'expr' style attributes may be used to 
 34     specify data.  'name' style attributes (e.g. my_bar=bar) will be 
 35     rendered as they are for var/with/in/etc.  Quoted attributes will 
 36     be treated as Python expressions. 
 37   
 38     Variables are processed in sequence, so later assignments can 
 39     reference and/or overwrite the results of previous assignments, 
 40     as desired. 
 41  ''' 
 42   
 43  from DT_Util import render_blocks, Eval, ParseError 
 44  from DT_Util import str # Probably needed due to hysterical pickles. 
 45  import re 
 46   
 47   
48 -class Let:
49 blockContinuations=() 50 name='let' 51
52 - def __init__(self, blocks):
53 tname, args, section = blocks[0] 54 self.__name__ = args 55 self.section = section.blocks 56 self.args = args = parse_let_params(args) 57 58 for i in range(len(args)): 59 name,expr = args[i] 60 if expr[:1]=='"' and expr[-1:]=='"' and len(expr) > 1: 61 # expr shorthand 62 expr=expr[1:-1] 63 try: args[i] = name, Eval(expr).eval 64 except SyntaxError, v: 65 m,(huh,l,c,src) = v 66 raise ParseError, ( 67 '<strong>Expression (Python) Syntax error</strong>:' 68 '\n<pre>\n%s\n</pre>\n' % v[0], 69 'let')
70 - def render(self, md):
71 d={}; md._push(d) 72 try: 73 for name,expr in self.args: 74 if type(expr) is type(''): d[name]=md[expr] 75 else: d[name]=expr(md) 76 return render_blocks(self.section, md) 77 finally: md._pop(1)
78 79 __call__ = render
80 81
82 -def parse_let_params(text, 83 result=None, 84 tag='let', 85 parmre=re.compile('([\000- ]*([^\000- ="]+)=([^\000- ="]+))'), 86 qparmre=re.compile('([\000- ]*([^\000- ="]+)="([^"]*)")'), 87 **parms):
88 89 result=result or [] 90 91 mo = parmre.match(text) 92 mo1= qparmre.match(text) 93 94 if mo is not None: 95 name=mo.group(2) 96 value=mo.group(3) 97 l=len(mo.group(1)) 98 elif mo1 is not None: 99 name=mo1.group(2) 100 value='"%s"' % mo1.group(3) 101 l=len(mo1.group(1)) 102 else: 103 if not text or not text.strip(): return result 104 raise ParseError, ('invalid parameter: "%s"' % text, tag) 105 106 result.append((name,value)) 107 108 text=text[l:].strip() 109 if text: return parse_let_params(text,result,tag,**parms) 110 else: return result
111