Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_name_expression(self):
"""
Parse any name.
"""
token = self.stream.expect('name')
self.test_name(token.value)
return nodes.NameExpression(token.value, token.lineno, self.filename)
nodes.Text: self.handle_template_text,
nodes.NodeList: self.handle_node_list,
nodes.ForLoop: self.handle_for_loop,
nodes.IfCondition: self.handle_if_condition,
nodes.Cycle: self.handle_cycle,
nodes.Print: self.handle_print,
nodes.Macro: self.handle_macro,
nodes.Call: self.handle_call,
nodes.Set: self.handle_set,
nodes.Filter: self.handle_filter,
nodes.Block: self.handle_block,
nodes.Include: self.handle_include,
nodes.Trans: self.handle_trans,
# expression nodes
nodes.NameExpression: self.handle_name,
nodes.CompareExpression: self.handle_compare,
nodes.TestExpression: self.handle_test,
nodes.ConstantExpression: self.handle_const,
nodes.RegexExpression: self.handle_regex,
nodes.SubscriptExpression: self.handle_subscript,
nodes.FilterExpression: self.handle_filter_expr,
nodes.CallExpression: self.handle_call_expr,
nodes.AddExpression: self.handle_add,
nodes.SubExpression: self.handle_sub,
nodes.ConcatExpression: self.handle_concat,
nodes.DivExpression: self.handle_div,
nodes.FloorDivExpression: self.handle_floor_div,
nodes.MulExpression: self.handle_mul,
nodes.ModExpression: self.handle_mod,
nodes.PosExpression: self.handle_pos,
nodes.NegExpression: self.handle_neg,
def collect_translations(ast):
"""
Collect all translatable strings for the given ast. The
return value is a list of tuples in the form ``(lineno, singular,
plural)``. If a translation doesn't require a plural form the
third item is `None`.
"""
todo = [ast]
result = []
while todo:
node = todo.pop()
if node.__class__ is nodes.Trans:
result.append((node.lineno, node.singular, node.plural))
elif node.__class__ is nodes.CallExpression and \
node.node.__class__ is nodes.NameExpression and \
node.node.name == '_':
if len(node.args) == 1 and not node.kwargs and not node.dyn_args \
and not node.dyn_kwargs and \
node.args[0].__class__ is nodes.ConstantExpression:
result.append((node.lineno, node.args[0].value, None))
todo.extend(node.get_child_nodes())
result.sort(lambda a, b: cmp(a[0], b[0]))
return result
def parse_gettext_call(self):
"""
parse {{ _('foo') }}.
"""
# XXX: check if only one argument was passed and if
# it is a string literal. Maybe that should become a special
# expression anyway.
token = self.stream.expect('name', '_')
node = nodes.NameExpression(token.value, token.lineno, self.filename)
return self.parse_call_expression(node)
return nodes.Trans(text.value, None, None, None,
trans_token.lineno, self.filename)
# block based translations
replacements = {}
plural_var = None
while self.stream.current.type != 'block_end':
if replacements:
self.stream.expect('comma')
name = self.stream.expect('name')
if self.stream.current.type == 'assign':
self.stream.next()
value = self.parse_expression()
else:
value = nodes.NameExpression(name.value, name.lineno,
self.filename)
if name.value in replacements:
raise TemplateSyntaxError('translation variable %r '
'is defined twice' % name.value,
name.lineno, self.filename)
replacements[name.value] = value
if plural_var is None:
plural_var = name.value
self.stream.expect('block_end')
def process_variable():
var_name = self.stream.expect('name')
if var_name.value not in replacements:
raise TemplateSyntaxError('unregistered translation variable'
" '%s'." % var_name.value,
var_name.lineno, self.filename)