Skip to content

Commit e69e6ee

Browse files
committed
Rename TerminalPrinter to TokenPrinter
1 parent 380a155 commit e69e6ee

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
lines changed

src/python_minifier/expression_printer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from python_minifier.util import is_ast_node
55

6-
from python_minifier.terminal_printer import TerminalPrinter, Delimiter
6+
from python_minifier.token_printer import TokenPrinter, Delimiter
77

88

99
class ExpressionPrinter(object):
@@ -34,7 +34,7 @@ def __init__(self):
3434
'Tuple': 18, 'Set': 18, 'List': 18, 'Dict': 18, 'ListComp': 18, 'SetComp': 18, 'DictComp': 18, 'GeneratorExp': 18, # Container
3535
}
3636

37-
self.printer = TerminalPrinter()
37+
self.printer = TokenPrinter()
3838

3939
def __call__(self, module):
4040
"""

src/python_minifier/f_string.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from python_minifier.ast_compare import compare_ast
1616
from python_minifier.expression_printer import ExpressionPrinter
1717
from python_minifier.ministring import MiniString
18-
from python_minifier.terminal_printer import TokenTypes
18+
from python_minifier.token_printer import TokenTypes
1919
from python_minifier.util import is_ast_node
2020

2121

src/python_minifier/module_printer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33

44
from .expression_printer import ExpressionPrinter
5-
from .terminal_printer import Delimiter
5+
from .token_printer import Delimiter
66
from .util import is_ast_node
77

88

@@ -523,7 +523,7 @@ def visit_ClassDef(self, node):
523523
self.printer.keyword('class')
524524
self.printer.identifier(node.name)
525525

526-
with Delimiter(self.printer, group_chars=('(', ')')) as delimiter:
526+
with Delimiter(self.printer, add_parens=True) as delimiter:
527527

528528
for b in node.bases:
529529
delimiter.new_item()
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class TokenTypes(object):
1616
EndStatement = 9
1717

1818
class Delimiter(object):
19-
def __init__(self, terminal_printer, delimiter=',', group_chars=None):
19+
def __init__(self, terminal_printer, delimiter=',', add_parens=False):
2020
"""
2121
Delimited group printer
2222
2323
A group of items that should be delimited by a delimiter character.
2424
Each call to new_item() will insert the delimiter character if necessary.
2525
26-
When used as a context manager, the group will be enclosed by the start and end characters.
26+
When used as a context manager, the group will be enclosed by the start and end characters if the group has any items.
2727
2828
>>> d = Delimiter(terminal_printer)
2929
... d.new_item()
@@ -36,24 +36,22 @@ def __init__(self, terminal_printer, delimiter=',', group_chars=None):
3636
... print(terminal_printer.code)
3737
a,b
3838
39-
>>> with Delimiter(terminal_printer, group_chars=('(', ')')) as d:
39+
>>> with Delimiter(terminal_printer, add_parens=True) as d:
4040
... d.new_item()
4141
... terminal_printer.identifier('a')
4242
... print(terminal_printer.code)
4343
(a)
4444
4545
:param terminal_printer: The terminal printer to use.
4646
:param delimiter: The delimiter to use.
47-
:param group_chars: The characters to use to open and close the delimited group.
47+
:param add_parens: If the group should be enclosed by parentheses. Only used when used as a context manager.
4848
"""
4949

5050
self._terminal_printer = terminal_printer
5151
self._delimiter = delimiter
52+
self._add_parens = add_parens
5253

53-
if group_chars:
54-
self._start_char, self._end_char = group_chars
55-
56-
self.first = True
54+
self._first = True
5755

5856
self._context_manager = False
5957

@@ -64,25 +62,23 @@ def __enter__(self):
6462

6563
def __exit__(self, exc_type, exc_val, exc_tb):
6664
"""Close the delimited group."""
67-
if not self.first and self._end_char:
68-
self._terminal_printer.delimiter(self._end_char)
65+
if not self._first and self._add_parens:
66+
self._terminal_printer.delimiter(')')
6967

7068
def new_item(self):
7169
"""Add a new item to the delimited group."""
72-
if self.first:
73-
self.first = False
74-
if self._context_manager and self._start_char:
75-
self._terminal_printer.delimiter(self._start_char)
70+
if self._first:
71+
self._first = False
72+
if self._context_manager and self._add_parens:
73+
self._terminal_printer.delimiter('(')
7674
else:
7775
self._terminal_printer.delimiter(self._delimiter)
7876

79-
class TerminalPrinter(object):
77+
class TokenPrinter(object):
8078
"""
8179
Concatenates terminal symbols of the python grammar
8280
"""
8381

84-
#__slots__ = ['__code', 'indent', 'unicode_literals', 'previous_token', '_prefer_single_line', '_allow_invalid_num_warnings']
85-
8682
def __init__(self, prefer_single_line=False, allow_invalid_num_warnings=False):
8783
"""
8884
:param prefer_single_line: If True, chooses to put as much code as possible on a single line.

0 commit comments

Comments
 (0)