# Diff of /trunk/harmonious/lib/template.py

Revision 132 Revision 133
322 322
323 retval = value 323 retval = value
324 for func in funcs: 324 for func in funcs:
325 325
326 if func == "len": 326 if func == "len":
327 if retval is None:
328 retval = 0
329 continue
330
331 try:
327 retval = len(retval) 332 retval = len(retval)
333 except TypeError:
334 retval = len(str(retval))
328 335
329 if func == "iterator": 336 if func == "iterator":
330 """Returns the current index in loop iterations.""" 337 """Returns the current index in loop iterations."""
331 if var.rfind('.') == -1: 338 if var.rfind('.') == -1:
332 retval = None 339 retval = None
336 if func == "is_odd": 343 if func == "is_odd":
337 if int(retval) % 2 == 1: 344 if int(retval) % 2 == 1:
338 retval = 1 345 retval = 1
339 else: 346 else:
340 retval = 0 347 retval = 0
348
349 if func == "isset":
350 if value == "" or value == "{%s}" % var:
351 retval = 0
352 else:
353 retval = 1
341 354
342 return retval 355 return retval
343 356
344 def _split_functions(self, var): 357 def _split_functions(self, var):
345 """Seperate the function calls from the variable they're acting on. 358 """Seperate the function calls from the variable they're acting on.
368 >>> tmpl._get_value('test_term') 381 >>> tmpl._get_value('test_term')
369 'j' 382 'j'
370 >>> tmpl._get_value('list1.list2') 383 >>> tmpl._get_value('list1.list2')
371 'found me' 384 'found me'
372 """ 385 """
373 386
374 # Is this a function call? Break out the actual variable if it is. 387 # Is this a function call? Break out the actual variable if it is.
375 funcs, var = self._split_functions(var) 388 funcs, var = self._split_functions(var)
376 389
377 # Convert the template parameter into a python interpretable variable. 390 # Convert the template parameter into a python interpretable variable.
378 pyvar = self._term_to_pyvar(var) 391 pyvar = self._term_to_pyvar(var)
381 # Make sure no evilness can get passed to the eval 394 # Make sure no evilness can get passed to the eval
382 if re.sub(r'[\w\'"\[\]]', "", pyvar) != "": 395 if re.sub(r'[\w\'"\[\]]', "", pyvar) != "":
383 raise ValueError, "Can't interpret '%s'!" % pyvar 396 raise ValueError, "Can't interpret '%s'!" % pyvar
384 397
385 value = eval("self._terms" + pyvar) 398 value = eval("self._terms" + pyvar)
386 if len(funcs):
387 value = self._call_function(funcs, value, var)
388 return value
389 399
390 except KeyError: 400 except KeyError:
391 if not __debug__: 401 if not __debug__:
392 return "" 402 value = ""
393 if len(funcs): 403 if len(funcs):
394 return '{%s(%s)}' % (funcs, self._pyvar_to_term(pyvar)) 404 value = '{%s(%s)}' % (funcs, self._pyvar_to_term(pyvar))
395 return '{%s}' % self._pyvar_to_term(pyvar) 405 value = '{%s}' % self._pyvar_to_term(pyvar)
396 406
407 if len(funcs):
408 value = self._call_function(funcs, value, var)
409
410 return value
411