Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert isinstance(c, List)
assert len(c) == 2
assert repr(c) == '[2*{C}]'
c1 = c[0]
c2 = c[1]
assert c1.parent_attribute == ('c', 0)
assert c2.parent_attribute == ('c', 1)
assert c1 != c2
assert c1.parent_node[c1.parent_attribute] == c1
assert str(c1) == 'c[0]={C}'
assert str(c2) == 'c[1]={C}'
x1 = c1['x']
x2 = c2['x']
assert isinstance(x1, Scalar)
assert x1 != x2
assert x1.value == x2.value
def __new__(cls, details, parent=None, name=None):
if not isinstance(parent, (Node, NoneType)):
raise ValueError("Unexpected value for 'parent', must be either None"
" or a Node instance, got %r" % type(parent))
if not isinstance(name, (NoneType, str, tuple)):
raise ValueError("Unexpected value for 'name', must be either None,"
" a string or a tuple, got %r" % type(name))
if isinstance(details, list):
self = super().__new__(List)
elif isinstance(details, dict):
self = super().__new__(Node)
else:
self = super().__new__(Scalar)
self._init(details, parent, name)
return self
def _parse_string(text):
"""
Use ``pglast`` to turn ``text`` into a SQL AST node.
Returns ``pglast.node.Scalar(None)`` when no AST nodes could be
parsed. This is a hack, but prevents convoluting the downstream
logic too much, as ``Context.traverse`` will simply ignore scalar
values.
>>> _parse_string('SELECT 1')
[1*{RawStmt}]
>>> _parse_string('-- just a comment')
"""
ast = pglast.parse_sql(text)
return pglast.Node(ast) if ast else pglast.node.Scalar(None)
def print_node(self, node, is_name=False, is_symbol=False):
"""Lookup the specific printer for the given `node` and execute it.
:param node: an instance of :class:`~.node.Node` or :class:`~.node.Scalar`
:param bool is_name:
whether this is a *name* of something, that may need to be double quoted
:param bool is_symbol:
whether this is the name of an *operator*, should not be double quoted
"""
if isinstance(node, Scalar):
self._print_scalar(node, is_name, is_symbol)
elif is_name and isinstance(node, (List, list)):
self.print_list(node, '.', standalone_items=False, are_names=True)
else:
parent_node_tag = node.parent_node and node.parent_node.node_tag
printer = get_printer_for_node_tag(parent_node_tag, node.node_tag)
if is_name and node.node_tag == 'String':
printer(node, self, is_name=is_name, is_symbol=is_symbol)
else:
printer(node, self)
self.separator()