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

Source Code for Module qm.external.DocumentTemplate.pDocumentTemplate

  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  """Python implementations of document template some features 
 14   
 15   
 16  $Id: pDocumentTemplate.py 1069 2008-11-13 21:55:43Z stefan $""" 
 17  __version__='$Revision: 1069 $'[11:-2] 
 18   
 19  import string, sys, types 
 20  from string import join 
 21   
 22  StringType=type('') 
 23  TupleType=type(()) 
 24  isFunctionType={} 
 25  for name in ['BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 
 26               'FunctionType', 'LambdaType', 'MethodType', 'UnboundMethodType']: 
 27      try: isFunctionType[getattr(types,name)]=1 
 28      except: pass 
 29   
 30  try: # Add function and method types from Extension Classes 
 31      import ExtensionClass 
 32      isFunctionType[ExtensionClass.PythonMethodType]=1 
 33      isFunctionType[ExtensionClass.ExtensionMethodType]=1 
 34  except: pass 
 35   
 36  isFunctionType=isFunctionType.has_key 
 37   
 38  isSimpleType={} 
 39  for n in dir(types): 
 40      if (n[-4:]=='Type' and n != 'InstanceType' and 
 41          not isFunctionType(getattr(types, n))): 
 42          isSimpleType[getattr(types, n)]=1 
 43   
 44  isSimpleType=isSimpleType.has_key 
 45   
46 -class InstanceDict:
47 48 validate=None 49
50 - def __init__(self,o,namespace,validate=None):
51 self.self=o 52 self.cache={} 53 self.namespace=namespace 54 if validate is None: self.validate=namespace.validate 55 else: self.validate=validate
56
57 - def has_key(self,key):
58 return hasattr(self.self,key)
59
60 - def keys(self):
61 return self.self.__dict__.keys()
62
63 - def __repr__(self): return 'InstanceDict(%s)' % str(self.self)
64
65 - def __getitem__(self,key):
66 67 cache=self.cache 68 if cache.has_key(key): return cache[key] 69 70 inst=self.self 71 72 if key[:1]=='_': 73 if key != '__str__': 74 raise KeyError, key # Don't divuldge private data 75 r=str(inst) 76 else: 77 try: r=getattr(inst,key) 78 except AttributeError: raise KeyError, key 79 80 v=self.validate 81 if v is not None: v(inst,inst,key,r,self.namespace) 82 83 self.cache[key]=r 84 return r
85
86 -class MultiMapping:
87
88 - def __init__(self): self.dicts=[]
89
90 - def __getitem__(self, key):
91 for d in self.dicts: 92 try: return d[key] 93 except KeyError, AttributeError: pass 94 raise KeyError, key
95
96 - def push(self,d): self.dicts.insert(0,d)
97
98 - def pop(self, n=1):
99 r = self.dicts[-1] 100 del self.dicts[:n] 101 return r
102
103 - def keys(self):
104 kz = [] 105 for d in self.dicts: 106 kz = kz + d.keys() 107 return kz
108
109 -class DictInstance:
110
111 - def __init__(self, mapping):
112 self.__d=mapping
113
114 - def __getattr__(self, name):
115 try: return self.__d[name] 116 except KeyError: raise AttributeError, name
117
118 -class TemplateDict:
119 120 level=0 121
122 - def _pop(self, n=1): return self.dicts.pop(n)
123 - def _push(self, d): return self.dicts.push(d)
124
125 - def __init__(self):
126 m=self.dicts=MultiMapping() 127 self._pop=m.pop 128 self._push=m.push 129 try: self.keys=m.keys 130 except: pass
131
132 - def __getitem__(self,key,call=1, 133 simple=isSimpleType, 134 isFunctionType=isFunctionType, 135 ):
136 137 v = self.dicts[key] 138 if call: 139 if hasattr(v, '__render_with_namespace__'): 140 return v.__render_with_namespace__(self) 141 vbase = getattr(v, 'aq_base', v) 142 if callable(vbase): 143 if getattr(vbase, 'isDocTemp', None): 144 return v(None, self) 145 return v() 146 return v
147
148 - def has_key(self,key):
149 try: 150 v=self.dicts[key] 151 except KeyError: 152 return 0 153 return 1
154 155 getitem=__getitem__ 156
157 - def __call__(self, *args, **kw):
158 if args: 159 if len(args)==1 and not kw: 160 m=args[0] 161 else: 162 m=self.__class__() 163 for a in args: m._push(a) 164 if kw: m._push(kw) 165 else: m=kw 166 return (DictInstance(m),)
167
168 -def render_blocks(blocks, md):
169 rendered = [] 170 append=rendered.append 171 for section in blocks: 172 if type(section) is TupleType: 173 l=len(section) 174 if l==1: 175 # Simple var 176 section=section[0] 177 if type(section) is StringType: section=md[section] 178 else: section=section(md) 179 section=str(section) 180 else: 181 # if 182 cache={} 183 md._push(cache) 184 try: 185 i=0 186 m=l-1 187 while i < m: 188 cond=section[i] 189 if type(cond) is StringType: 190 n=cond 191 try: 192 cond=md[cond] 193 cache[n]=cond 194 except KeyError, v: 195 v=str(v) 196 if n != v: raise KeyError, v, sys.exc_traceback 197 cond=None 198 else: cond=cond(md) 199 if cond: 200 section=section[i+1] 201 if section: section=render_blocks(section,md) 202 else: section='' 203 m=0 204 break 205 i=i+2 206 if m: 207 if i==m: section=render_blocks(section[i],md) 208 else: section='' 209 210 finally: md._pop() 211 212 elif type(section) is not StringType: 213 section=section(md) 214 215 if section: rendered.append(section) 216 217 l=len(rendered) 218 if l==0: return '' 219 elif l==1: return rendered[0] 220 return join(rendered, '') 221 return rendered
222