Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_genshi_builder():
"""Genshi template + tag builder"""
stream = tag.TABLE([
tag.tr([tag.td(c) for c in row.values()])
for row in table
]).generate()
stream = genshi_tmpl2.generate(table=stream)
stream.render('html', strip_whitespace=False)
if not isinstance(value, basestring): # None or other non-splitable
return value
default_query = self.ticketlink_query.startswith('?') and \
self.ticketlink_query[1:] or self.ticketlink_query
args = arg_list_to_args(parse_arg_list(default_query))
items = []
for i, word in enumerate(re.split(r'([;,\s]+)', value)):
if i % 2:
items.append(word)
elif word:
rendered = name != 'cc' and word \
or Chrome(self.env).format_emails(context, word)
if rendered == word:
word_args = args.copy()
word_args[name] = '~' + word
items.append(tag.a(word,
href=context.href.query(word_args)))
else:
items.append(rendered)
return tag(items)
def get_qname(self, name):
"""
Return a `QName` for a name.
:return: qname
"""
if ':' in name:
ns, localname = name.split(':')
name = self.get_namespace(ns)
if name is None:
self.fail('The namespace alias %s is unknown' % ns)
else:
localname = name
name = self.get_namespace('')
return QName(u'%s}%s' % (name, localname))
for attr, value in attrib:
if attr in boolean_attrs:
value = attr
elif attr == 'xml:lang' and 'lang' not in attrib:
buf += [' lang="', escape(value), '"']
elif attr == 'xml:space':
continue
buf += [' ', attr, '="', escape(value), '"']
if kind is EMPTY:
if tag in empty_elems:
buf.append(' />')
else:
buf.append('>' % tag)
else:
buf.append('>')
yield _emit(kind, data, Markup(''.join(buf)))
elif kind is END:
yield _emit(kind, data, Markup('' % data))
elif kind is TEXT:
if in_cdata:
yield _emit(kind, data, data)
else:
yield _emit(kind, data, escape(data, quotes=False))
elif kind is COMMENT:
yield _emit(kind, data, Markup('' % data))
elif kind is DOCTYPE and not have_doctype:
name, pubid, sysid = data
buf = ['
if result is True:
yield event
if event[0] is START:
depth = 1
while depth > 0:
subevent = stream.next()
if subevent[0] is START:
depth += 1
elif subevent[0] is END:
depth -= 1
yield subevent
test(subevent, namespaces, variables,
updateonly=True)
elif result:
yield result
return Stream(_generate(),
serializer=getattr(stream, 'serializer', None))
yield ENTER, event
depth = 1
while depth > 0:
mark, subevent = next()
if subevent[0] is START:
depth += 1
elif subevent[0] is END:
depth -= 1
if depth == 0:
yield EXIT, subevent
else:
yield INSIDE, subevent
test(subevent, namespaces, variables, updateonly=True)
else:
yield OUTSIDE, event
elif isinstance(result, Attrs):
# XXX Selected *attributes* are given a "kind" of None to
# indicate they are not really part of the stream.
yield ATTR, (ATTR, (QName(event[1][0] + '@*'), result), event[2])
yield None, event
elif isinstance(result, tuple):
yield OUTSIDE, result
elif result:
# XXX Assume everything else is "text"?
yield None, (TEXT, unicode(result), (None, -1, -1))
else:
yield None, event
def _visit_BoolOperator(opcls):
def _visit(self, node):
values = [self.visit(n) for n in node.nodes]
return self._new(_ast.BoolOp, opcls(), values)
return _visit
visit_And = _visit_BoolOperator(_ast.And)
visit_Or = _visit_BoolOperator(_ast.Or)
del _visit_BoolOperator
cmp_operators = {
'==': _ast.Eq,
'!=': _ast.NotEq,
'<': _ast.Lt,
'<=': _ast.LtE,
'>': _ast.Gt,
'>=': _ast.GtE,
'is': _ast.Is,
'is not': _ast.IsNot,
'in': _ast.In,
'not in': _ast.NotIn,
}
def visit_Compare(self, node):
left = self.visit(node.expr)
ops = []
comparators = []
for optype, expr in node.ops:
ops.append(self.cmp_operators[optype]())
comparators.append(self.visit(expr))
return self._new(_ast.Compare, left, ops, comparators)
def visit_Lambda(self, node):
values = [self.visit(n) for n in node.nodes]
return self._new(_ast.BoolOp, opcls(), values)
return _visit
visit_And = _visit_BoolOperator(_ast.And)
visit_Or = _visit_BoolOperator(_ast.Or)
del _visit_BoolOperator
cmp_operators = {
'==': _ast.Eq,
'!=': _ast.NotEq,
'<': _ast.Lt,
'<=': _ast.LtE,
'>': _ast.Gt,
'>=': _ast.GtE,
'is': _ast.Is,
'is not': _ast.IsNot,
'in': _ast.In,
'not in': _ast.NotIn,
}
def visit_Compare(self, node):
left = self.visit(node.expr)
ops = []
comparators = []
for optype, expr in node.ops:
ops.append(self.cmp_operators[optype]())
comparators.append(self.visit(expr))
return self._new(_ast.Compare, left, ops, comparators)
def visit_Lambda(self, node):
args = self._extract_args(node)
body = self.visit(node.code)
visit_Continue = _map_class(_ast.Continue)
def _visit_BinOperator(opcls):
def _visit(self, node):
return self._new(_ast.BinOp, self.visit(node.left),
opcls(), self.visit(node.right))
return _visit
visit_Add = _visit_BinOperator(_ast.Add)
visit_Div = _visit_BinOperator(_ast.Div)
visit_FloorDiv = _visit_BinOperator(_ast.FloorDiv)
visit_LeftShift = _visit_BinOperator(_ast.LShift)
visit_Mod = _visit_BinOperator(_ast.Mod)
visit_Mul = _visit_BinOperator(_ast.Mult)
visit_Power = _visit_BinOperator(_ast.Pow)
visit_RightShift = _visit_BinOperator(_ast.RShift)
visit_Sub = _visit_BinOperator(_ast.Sub)
del _visit_BinOperator
def _visit_BitOperator(opcls):
def _visit(self, node):
def _make(nodes):
if len(nodes) == 1:
return self.visit(nodes[0])
left = _make(nodes[:-1])
right = self.visit(nodes[-1])
return self._new(_ast.BinOp, left, opcls(), right)
return _make(node.nodes)
return _visit
visit_Bitand = _visit_BitOperator(_ast.BitAnd)
visit_Bitor = _visit_BitOperator(_ast.BitOr)
visit_Bitxor = _visit_BitOperator(_ast.BitXor)
del _visit_BitOperator
def _visit(self, node):
return self._new(to)
return _visit
visit_Pass = _map_class(_ast.Pass)
visit_Break = _map_class(_ast.Break)
visit_Continue = _map_class(_ast.Continue)
def _visit_BinOperator(opcls):
def _visit(self, node):
return self._new(_ast.BinOp, self.visit(node.left),
opcls(), self.visit(node.right))
return _visit
visit_Add = _visit_BinOperator(_ast.Add)
visit_Div = _visit_BinOperator(_ast.Div)
visit_FloorDiv = _visit_BinOperator(_ast.FloorDiv)
visit_LeftShift = _visit_BinOperator(_ast.LShift)
visit_Mod = _visit_BinOperator(_ast.Mod)
visit_Mul = _visit_BinOperator(_ast.Mult)
visit_Power = _visit_BinOperator(_ast.Pow)
visit_RightShift = _visit_BinOperator(_ast.RShift)
visit_Sub = _visit_BinOperator(_ast.Sub)
del _visit_BinOperator
def _visit_BitOperator(opcls):
def _visit(self, node):
def _make(nodes):
if len(nodes) == 1:
return self.visit(nodes[0])
left = _make(nodes[:-1])
right = self.visit(nodes[-1])
return self._new(_ast.BinOp, left, opcls(), right)