Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
output.write('y')
output.print_node(node.y)
else:
if output.test_child_z_is_expression:
with output.push_indent(2, relative=False):
output.print_list(node.z, '+')
else:
output.print_list(node.z, '/', standalone_items=False)
output.write('}')
output = printer.RawStream()
output.test_child_z_is_expression = True
result = output(root)
assert result == 'bar = a({x0, y0} * {x1,{x2, y2} + {x3,{x4, y4} + {x5, y5}}})'
output = printer.RawStream()
output.test_child_z_is_expression = False
result = output(root)
assert result == 'bar = a({x0, y0} * {x1,{x2, y2} / {x3,{x4, y4} / {x5, y5}}})'
finally:
printer.NODE_PRINTERS.pop('TestRoot', None)
printer.NODE_PRINTERS.pop('TestChild', None)
printer.NODE_PRINTERS.pop('TestNiece', None)
def do(meth, node):
output = printer.RawStream()
getattr(output, meth)(node)
return output.getvalue()
def _normalize_columns(table_elts):
"""
Return a list of (column name, column type) after pretty printing
them using pglast.
>>> import pglast.printer
>>> create_table = pglast.parse_sql(
... 'CREATE TABLE _(COL1 foo.bar(baz,123), Col2 integer);')
>>> table_elts = pglast.Node(create_table)[0].stmt.tableElts
>>> _normalize_columns(table_elts)
[('col1', 'foo.bar(baz, 123)'), ('col2', 'integer')]
"""
# This is a pretty hacky implementation
printer = pglast.printer.RawStream()
columns = printer(table_elts).split(';')
res = []
for col in columns:
name, typ = col.strip().split(' ', 1)
res.append((name, typ))
return res
first = False
else:
if self.comma_at_eoln:
self.write(sublist_sep)
self.newline()
self.write(sublist_open)
else:
self.newline()
self.write(sublist_sep)
self.write(' ')
self.write(sublist_open)
self.print_list(lst, sep, relative_indent, standalone_items, are_names)
self.write(sublist_close)
class IndentedStream(RawStream):
r"""Indented SQL parse tree writer.
:param int compact_lists_margin:
an integer value that, if given, is used to print lists on a single line, when they
do not exceed the given margin on the right
:param int split_string_literals_threshold:
an integer value that, if given, is used as the threshold beyond that a string
literal gets splitted in successive chunks of that length
:param \*\*options: other options accepted by :class:`RawStream`
This augments :class:`RawStream` to emit a prettified representation of a *parse tree*.
"""
def __init__(self, compact_lists_margin=None, split_string_literals_threshold=None,
**options):
super().__init__(**options)