1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
31
33
35
36 verbosity = 3
37
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
51 if self.verbosity > 1:
52 print >>sys.stderr, "TAL: Error: ", args
53
55 if self.verbosity > 0:
56 print >>sys.stderr, "TAL: Critical:", args
57
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):
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):
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
90
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
99
100 val = None
101
102 if self.methods.has_key(path):
103
104 val = self.methods[path](obj, self)
105
106
107
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
131
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
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
160 raise simpleTALES.PATHNOTFOUNDEXCEPTION
161
162
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
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
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
187 self.popLocals()
188 return e.value()
189
190
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
204 raise simpleTALES.PATHNOTFOUNDEXCEPTION
205
206
207 resolved_stack.insert(0, (path, val) )
208
209