Fix problem with local variable assignment and typo in function name

This commit is contained in:
Charles Haley 2011-01-13 17:58:25 +00:00
parent d05b92a92b
commit cd66ed4bc6
2 changed files with 13 additions and 8 deletions

View File

@ -26,7 +26,7 @@ class _Parser(object):
if prog[1] != '': if prog[1] != '':
self.error(_('failed to scan program. Invalid input {0}').format(prog[1])) self.error(_('failed to scan program. Invalid input {0}').format(prog[1]))
self.parent = parent self.parent = parent
self.variables = {'$':val} self.parent.locals = {'$':val}
def error(self, message): def error(self, message):
m = 'Formatter: ' + message + _(' near ') m = 'Formatter: ' + message + _(' near ')
@ -88,18 +88,20 @@ class _Parser(object):
def expr(self): def expr(self):
if self.token_is_id(): if self.token_is_id():
funcs = formatter_functions.get_functions()
# We have an identifier. Determine if it is a function # We have an identifier. Determine if it is a function
id = self.token() id = self.token()
if not self.token_op_is_a('('): if not self.token_op_is_a('('):
if self.token_op_is_a('='): if self.token_op_is_a('='):
# classic assignment statement # classic assignment statement
self.consume() self.consume()
return self._assign(id, self.expr()) cls = funcs['assign']
return self.variables.get(id, _('unknown id ') + id) return cls.eval(self.parent, self.parent.kwargs,
self.parent.book, self.parent.locals, id, self.expr())
return self.parent.locals.get(id, _('unknown id ') + id)
# We have a function. # We have a function.
# Check if it is a known one. We do this here so error reporting is # Check if it is a known one. We do this here so error reporting is
# better, as it can identify the tokens near the problem. # better, as it can identify the tokens near the problem.
funcs = formatter_functions.get_functions()
if id not in funcs: if id not in funcs:
self.error(_('unknown function {0}').format(id)) self.error(_('unknown function {0}').format(id))
@ -129,7 +131,7 @@ class _Parser(object):
if cls.arg_count != -1 and len(args) != cls.arg_count: if cls.arg_count != -1 and len(args) != cls.arg_count:
self.error('incorrect number of arguments for function {}'.format(id)) self.error('incorrect number of arguments for function {}'.format(id))
return cls.eval(self.parent, self.parent.kwargs, return cls.eval(self.parent, self.parent.kwargs,
self.parent.book, locals, *args) self.parent.book, self.parent.locals, *args)
else: else:
f = self.parent.functions[id] f = self.parent.functions[id]
if f[0] != -1 and len(args) != f[0]+1: if f[0] != -1 and len(args) != f[0]+1:
@ -159,6 +161,7 @@ class TemplateFormatter(string.Formatter):
self.book = None self.book = None
self.kwargs = None self.kwargs = None
self.program_cache = {} self.program_cache = {}
self.locals = {}
def _do_format(self, val, fmt): def _do_format(self, val, fmt):
if not fmt or not val: if not fmt or not val:
@ -286,9 +289,9 @@ class TemplateFormatter(string.Formatter):
print args print args
raise ValueError('Incorrect number of arguments for function '+ fmt[0:p]) raise ValueError('Incorrect number of arguments for function '+ fmt[0:p])
if func.arg_count == 1: if func.arg_count == 1:
val = func.eval(self, self.kwargs, self.book, locals, val).strip() val = func.eval(self, self.kwargs, self.book, self.locals, val).strip()
else: else:
val = func.eval(self, self.kwargs, self.book, locals, val = func.eval(self, self.kwargs, self.book, self.locals,
val, *args).strip() val, *args).strip()
if val: if val:
val = self._do_format(val, dispfmt) val = self._do_format(val, dispfmt)
@ -309,6 +312,7 @@ class TemplateFormatter(string.Formatter):
self.kwargs = kwargs self.kwargs = kwargs
self.book = book self.book = book
self.composite_values = {} self.composite_values = {}
self.locals = {}
try: try:
ans = self.vformat(fmt, [], kwargs).strip() ans = self.vformat(fmt, [], kwargs).strip()
except Exception, e: except Exception, e:

View File

@ -74,6 +74,7 @@ class FormatterFunction(object):
if isinstance(ret, list): if isinstance(ret, list):
return ','.join(list) return ','.join(list)
except: except:
traceback.print_exc()
return _('Function threw exception' + traceback.format_exc()) return _('Function threw exception' + traceback.format_exc())
class BuiltinStrcmp(FormatterFunction): class BuiltinStrcmp(FormatterFunction):
@ -327,7 +328,7 @@ class BuiltinEvaluate(FormatterFunction):
return value_if_empty return value_if_empty
class BuiltinShorten(FormatterFunction): class BuiltinShorten(FormatterFunction):
name = 'shorten ' name = 'shorten'
arg_count = 4 arg_count = 4
doc = _('shorten(val, left chars, middle text, right chars) -- Return a ' doc = _('shorten(val, left chars, middle text, right chars) -- Return a '
'shortened version of the field, consisting of `left chars` ' 'shortened version of the field, consisting of `left chars` '