Package advene :: Package model :: Package tal :: Module context
[hide private]
[frames] | no frames]

Source Code for Module advene.model.tal.context

  1  # 
  2  # Advene: Annotate Digital Videos, Exchange on the NEt 
  3  # Copyright (C) 2008 Olivier Aubert <olivier.aubert@liris.cnrs.fr> 
  4  # 
  5  # Advene is free software; you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation; either version 2 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Advene is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU General Public License 
 16  # along with Advene; if not, write to the Free Software 
 17  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 18  # 
 19  import sys 
 20   
 21  from cStringIO import StringIO 
 22   
 23  from simpletal import simpleTAL 
 24  from simpletal import simpleTALES 
 25   
 26  from advene.model.exception import AdveneException 
 27   
 28  import global_methods 
 29   
30 -class AdveneTalesException(AdveneException): pass
31
32 -class AdveneTalesPathException(AdveneTalesException): pass
33
34 -class DebugLogger:
35 36 verbosity = 3 37
38 - def debug (self, *args):
39 if self.verbosity > 3: 40 print >>sys.stderr, "TAL: Debug: ", args
41
42 - def info (self, *args):
43 if self.verbosity > 3: 44 print >>sys.stderr, "TAL: Info : ", args
45
46 - def warn (self, *args):
47 if self.verbosity > 2: 48 print >>sys.stderr, "TAL: Warning: ", args
49
50 - def error (self, *args):
51 if self.verbosity > 1: 52 print >>sys.stderr, "TAL: Error: ", args
53
54 - def critical (self, *args):
55 if self.verbosity > 0: 56 print >>sys.stderr, "TAL: Critical:", args
57
58 -class NoCallVariable(simpleTALES.ContextVariable):
59 """Not callable variable. 60 61 Used for view wrappers, that should not be called if intermediate values. 62 Such a value (if used in a _advene_context) can hold a callable, that will 63 be called only if final element in the path. 64 """
65 - def value (self, currentPath=None):
66 return self.ourValue
67
68 -class _advene_context (simpleTALES.Context):
69 """Advene specific implementation of TALES. 70 It is based on simpletal.simpleTALES.Context, 71 but whenever a path item is not resolved as an attribute nor a key of 72 the object to which it is applied, it is searched in a set of Methods 73 contained in the context.""" 74
75 - def __init__ (self, options):
76 simpleTALES.Context.__init__(self, options, allowPythonPath=True)
77
78 - def wrap_method(self, method):
79 return simpleTALES.PathFunctionVariable(method)
80
81 - def wrap_nocall(self, o):
82 return NoCallVariable(o)
83
84 - def wrap_object(self, o):
85 """Wraps an object into a ContextVariable.""" 86 return simpleTALES.ContextVariable(o)
87
88 - def addLocals (self, localVarList):
89 # For compatibility with code using simpletal 3.6 (migration phase) 90 # Pop the current locals onto the stack 91 self.pushLocals() 92 for name, value in localVarList: 93 self.setLocal(name, value)
94
95 - def traversePathPreHook(self, obj, path):
96 """Called before any TALES standard evaluation""" 97 98 #print "Prehook for %s on %s" % (obj, path) 99 100 val = None 101 102 if self.methods.has_key(path): 103 #print "Evaluating %s on %s" % (path, obj) 104 val = self.methods[path](obj, self) 105 # If the result is None, the method is not appliable 106 # and we should try other access ways (attributes,...) on the 107 # object 108 109 if val is None and hasattr (obj, 'getQName'): 110 if self.locals.has_key('view'): 111 ref = self.locals['view'] 112 elif self.globals.has_key('view'): 113 ref = self.globals['view'] 114 elif self.locals.has_key('here'): 115 ref = self.locals['here'] 116 elif self.globals.has_key('here'): 117 ref = self.globals['here'] 118 else: 119 ref = None 120 if ref is None: 121 ref = obj 122 pkg = ref.getOwnerPackage () 123 ns_dict = pkg.getImports ().getInverseDict () 124 ns_dict[''] = pkg.getUri (absolute=True) 125 val = obj.getQName (path, ns_dict, None) 126 127 return val
128
129 - def traversePath (self, expr, canCall=1):
130 # canCall only applies to the *final* path destination, not points down the path. 131 # Check for and correct for trailing/leading quotes 132 if (expr.startswith ('"') or expr.startswith ("'")): 133 if (expr.endswith ('"') or expr.endswith ("'")): 134 expr = expr [1:-1] 135 else: 136 expr = expr [1:] 137 elif (expr.endswith ('"') or expr.endswith ("'")): 138 expr = expr [0:-1] 139 pathList = expr.split ('/') 140 141 path = pathList[0] 142 if path.startswith ('?'): 143 path = path[1:] 144 if self.locals.has_key(path): 145 path = self.locals[path] 146 if (isinstance (path, simpleTALES.ContextVariable)): path = path.value() 147 elif (callable (path)):path = apply (path, ()) 148 149 elif self.globals.has_key(path): 150 path = self.globals[path] 151 if (isinstance (path, simpleTALES.ContextVariable)): path = path.value() 152 elif (callable (path)):path = apply (path, ()) 153 #self.log.debug ("Dereferenced to %s" % path) 154 if self.locals.has_key(path): 155 val = self.locals[path] 156 elif self.globals.has_key(path): 157 val = self.globals[path] 158 else: 159 # If we can't find it then raise an exception 160 raise simpleTALES.PATHNOTFOUNDEXCEPTION 161 162 # Advene hook: store the resolved_stack 163 resolved_stack = [ (path, val) ] 164 self.pushLocals() 165 self.setLocal( '__resolved_stack', resolved_stack ) 166 167 index = 1 168 for path in pathList[1:]: 169 #self.log.debug ("Looking for path element %s" % path) 170 if path.startswith ('?'): 171 path = path[1:] 172 if self.locals.has_key(path): 173 path = self.locals[path] 174 if (isinstance (path, simpleTALES.ContextVariable)): path = path.value() 175 elif (callable (path)):path = apply (path, ()) 176 elif self.globals.has_key(path): 177 path = self.globals[path] 178 if (isinstance (path, simpleTALES.ContextVariable)): path = path.value() 179 elif (callable (path)):path = apply (path, ()) 180 #self.log.debug ("Dereferenced to %s" % path) 181 try: 182 if (isinstance (val, simpleTALES.ContextVariable)): temp = val.value((index, pathList)) 183 elif (callable (val)):temp = apply (val, ()) 184 else: temp = val 185 except simpleTALES.ContextVariable, e: 186 # Fast path for those functions that return values 187 self.popLocals() 188 return e.value() 189 190 # Advene hook: 191 val = self.traversePathPreHook (temp, path) 192 if val is not None: 193 pass 194 elif (hasattr (temp, path)): 195 val = getattr (temp, path) 196 else: 197 try: 198 try: 199 val = temp[path] 200 except TypeError: 201 val = temp[int(path)] 202 except: 203 #self.log.debug ("Not found.") 204 raise simpleTALES.PATHNOTFOUNDEXCEPTION 205 206 # Advene hook: stack resolution 207 resolved_stack.insert(0, (path, val) ) 208 209