Skip to content

Commit d023395

Browse files
vallentinandialbrecht
authored andcommitted
Revamped pprint_tree
1 parent 08cb6da commit d023395

File tree

2 files changed

+61
-57
lines changed

2 files changed

+61
-57
lines changed

sqlparse/sql.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,23 @@ def __getitem__(self, item):
159159
def _get_repr_name(self):
160160
return type(self).__name__
161161

162-
def _pprint_tree(self, max_depth=None, depth=0, f=None):
162+
def _pprint_tree(self, max_depth=None, depth=0, f=None, _pre=''):
163163
"""Pretty-print the object tree."""
164-
indent = u' | ' * depth
164+
token_count = len(self.tokens)
165165
for idx, token in enumerate(self.tokens):
166166
cls = token._get_repr_name()
167167
value = token._get_repr_value()
168168

169+
last = idx == (token_count - 1)
170+
pre = u'`- ' if last else u'|- '
171+
169172
q = u'"' if value.startswith("'") and value.endswith("'") else u"'"
170-
print(u"{indent}{idx:2d} {cls} {q}{value}{q}"
173+
print(u"{_pre}{pre}{idx} {cls} {q}{value}{q}"
171174
.format(**locals()), file=f)
172175

173176
if token.is_group and (max_depth is None or depth < max_depth):
174-
token._pprint_tree(max_depth, depth + 1, f)
177+
parent_pre = u' ' if last else u'| '
178+
token._pprint_tree(max_depth, depth + 1, f, _pre + parent_pre)
175179

176180
def get_token_at_offset(self, offset):
177181
"""Returns the token that is on position offset."""

tests/test_parse.py

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -313,59 +313,59 @@ def test_pprint():
313313

314314
p._pprint_tree(f=output)
315315
pprint = '\n'.join([
316-
" 0 DML 'select'",
317-
" 1 Whitespace ' '",
318-
" 2 IdentifierList 'a0, b0...'",
319-
" | 0 Identifier 'a0'",
320-
" | | 0 Name 'a0'",
321-
" | 1 Punctuation ','",
322-
" | 2 Whitespace ' '",
323-
" | 3 Identifier 'b0'",
324-
" | | 0 Name 'b0'",
325-
" | 4 Punctuation ','",
326-
" | 5 Whitespace ' '",
327-
" | 6 Identifier 'c0'",
328-
" | | 0 Name 'c0'",
329-
" | 7 Punctuation ','",
330-
" | 8 Whitespace ' '",
331-
" | 9 Identifier 'd0'",
332-
" | | 0 Name 'd0'",
333-
" | 10 Punctuation ','",
334-
" | 11 Whitespace ' '",
335-
" | 12 Float 'e0'",
336-
" 3 Whitespace ' '",
337-
" 4 Keyword 'from'",
338-
" 5 Whitespace ' '",
339-
" 6 Identifier '(selec...'",
340-
" | 0 Parenthesis '(selec...'",
341-
" | | 0 Punctuation '('",
342-
" | | 1 DML 'select'",
343-
" | | 2 Whitespace ' '",
344-
" | | 3 Wildcard '*'",
345-
" | | 4 Whitespace ' '",
346-
" | | 5 Keyword 'from'",
347-
" | | 6 Whitespace ' '",
348-
" | | 7 Identifier 'dual'",
349-
" | | | 0 Name 'dual'",
350-
" | | 8 Punctuation ')'",
351-
" | 1 Whitespace ' '",
352-
" | 2 Identifier 'q0'",
353-
" | | 0 Name 'q0'",
354-
" 7 Whitespace ' '",
355-
" 8 Where 'where ...'",
356-
" | 0 Keyword 'where'",
357-
" | 1 Whitespace ' '",
358-
" | 2 Comparison '1=1'",
359-
" | | 0 Integer '1'",
360-
" | | 1 Comparison '='",
361-
" | | 2 Integer '1'",
362-
" | 3 Whitespace ' '",
363-
" | 4 Keyword 'and'",
364-
" | 5 Whitespace ' '",
365-
" | 6 Comparison '2=2'",
366-
" | | 0 Integer '2'",
367-
" | | 1 Comparison '='",
368-
" | | 2 Integer '2'",
316+
"|- 0 DML 'select'",
317+
"|- 1 Whitespace ' '",
318+
"|- 2 IdentifierList 'a0, b0...'",
319+
"| |- 0 Identifier 'a0'",
320+
"| | `- 0 Name 'a0'",
321+
"| |- 1 Punctuation ','",
322+
"| |- 2 Whitespace ' '",
323+
"| |- 3 Identifier 'b0'",
324+
"| | `- 0 Name 'b0'",
325+
"| |- 4 Punctuation ','",
326+
"| |- 5 Whitespace ' '",
327+
"| |- 6 Identifier 'c0'",
328+
"| | `- 0 Name 'c0'",
329+
"| |- 7 Punctuation ','",
330+
"| |- 8 Whitespace ' '",
331+
"| |- 9 Identifier 'd0'",
332+
"| | `- 0 Name 'd0'",
333+
"| |- 10 Punctuation ','",
334+
"| |- 11 Whitespace ' '",
335+
"| `- 12 Float 'e0'",
336+
"|- 3 Whitespace ' '",
337+
"|- 4 Keyword 'from'",
338+
"|- 5 Whitespace ' '",
339+
"|- 6 Identifier '(selec...'",
340+
"| |- 0 Parenthesis '(selec...'",
341+
"| | |- 0 Punctuation '('",
342+
"| | |- 1 DML 'select'",
343+
"| | |- 2 Whitespace ' '",
344+
"| | |- 3 Wildcard '*'",
345+
"| | |- 4 Whitespace ' '",
346+
"| | |- 5 Keyword 'from'",
347+
"| | |- 6 Whitespace ' '",
348+
"| | |- 7 Identifier 'dual'",
349+
"| | | `- 0 Name 'dual'",
350+
"| | `- 8 Punctuation ')'",
351+
"| |- 1 Whitespace ' '",
352+
"| `- 2 Identifier 'q0'",
353+
"| `- 0 Name 'q0'",
354+
"|- 7 Whitespace ' '",
355+
"`- 8 Where 'where ...'",
356+
" |- 0 Keyword 'where'",
357+
" |- 1 Whitespace ' '",
358+
" |- 2 Comparison '1=1'",
359+
" | |- 0 Integer '1'",
360+
" | |- 1 Comparison '='",
361+
" | `- 2 Integer '1'",
362+
" |- 3 Whitespace ' '",
363+
" |- 4 Keyword 'and'",
364+
" |- 5 Whitespace ' '",
365+
" `- 6 Comparison '2=2'",
366+
" |- 0 Integer '2'",
367+
" |- 1 Comparison '='",
368+
" `- 2 Integer '2'",
369369
""])
370370
assert output.getvalue() == pprint
371371

0 commit comments

Comments
 (0)