Skip to content

Commit 9ee0f07

Browse files
committed
tidy: clenaup and refactoring
- add `save()` method to model - simplify code by inlining `datafilewriter`
1 parent 6b9d2b6 commit 9ee0f07

File tree

6 files changed

+52
-182
lines changed

6 files changed

+52
-182
lines changed

src/robot/parsing/builders.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323

2424
def get_model(source, data_only=False, curdir=None):
2525
tokens = get_tokens(source, data_only)
26-
return _build_model(get_statements(tokens, curdir))
26+
return _build_model(source, get_statements(tokens, curdir))
2727

2828

2929
def get_resource_model(source, data_only=False, curdir=None):
3030
tokens = get_resource_tokens(source, data_only)
31-
return _build_model(get_statements(tokens, curdir))
31+
return _build_model(source, get_statements(tokens, curdir))
3232

3333

34-
def _build_model(statements):
35-
builder = FileBuilder()
34+
def _build_model(source, statements):
35+
builder = FileBuilder(source)
3636
stack = [builder]
3737
for statement in statements:
3838
while not stack[-1].handles(statement):
@@ -57,8 +57,8 @@ def statement(self, statement):
5757

5858
class FileBuilder(Builder):
5959

60-
def __init__(self, model=None):
61-
Builder.__init__(self, model or File())
60+
def __init__(self, source=None):
61+
Builder.__init__(self, File(source))
6262

6363
def statement(self, statement):
6464
try:

src/robot/parsing/model/blocks.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,45 @@
1515

1616
import ast
1717

18-
from robot.utils import normalize_whitespace
18+
from robot.utils import normalize_whitespace, file_writer
1919

20+
from .visitor import ModelVisitor
2021
from ..lexer import Token
2122

2223

24+
class FileWriter(ModelVisitor):
25+
26+
def __init__(self, output):
27+
self.output = output
28+
29+
def visit_Statement(self, statement):
30+
for line in statement.lines:
31+
for token in line:
32+
self.output.write(token.value)
33+
34+
2335
class Block(ast.AST):
2436
_fields = ()
2537

2638

2739
class File(Block):
2840
_fields = ('sections',)
2941

30-
def __init__(self):
42+
def __init__(self, source=None):
43+
self.source = source
3144
self.sections = []
3245

3346
@property
3447
def has_tests(self):
3548
return any(isinstance(s, TestCaseSection) for s in self.sections)
3649

50+
def save(self, given_output, newline=None):
51+
output = given_output if given_output else \
52+
file_writer(self.source, newline=newline)
53+
FileWriter(output).visit(self)
54+
if given_output is None:
55+
output.close()
56+
3757

3858
class Section(Block):
3959
_fields = ('header', 'body')

src/robot/tidy.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939

4040
from robot.errors import DataError
4141
from robot.parsing import get_model, SuiteStructureBuilder, SuiteStructureVisitor
42-
from robot.tidypkg import DataFileWriter
42+
from robot.tidypkg import (Aligner, Cleaner, SeparatorCleaner, PipeAdder,
43+
NewlineAdder)
4344
from robot.utils import Application, file_writer
4445

4546
# TODO: maybe rename --format to --extension
@@ -180,8 +181,15 @@ def directory(self, path):
180181
data = SuiteStructureBuilder().build([path])
181182
data.visit(self)
182183

183-
def _tidy(self, ast, output=None):
184-
DataFileWriter(output=output, **self._options).write(ast)
184+
def _tidy(self, model, output=None):
185+
Cleaner().visit(model)
186+
NewlineAdder().visit(model)
187+
if self._options['pipe_separated']:
188+
PipeAdder().visit(model)
189+
else:
190+
SeparatorCleaner(self._options['txt_separating_spaces']).visit(model)
191+
Aligner().visit(model)
192+
model.save(output, self._options['line_separator'])
185193

186194
def visit_file(self, file):
187195
self.inplace(file.source)

src/robot/tidypkg/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from .datafilewriter import DataFileWriter
16+
from .transformers import (Aligner, Cleaner, SeparatorCleaner, PipeAdder,
17+
NewlineAdder)

src/robot/tidypkg/datafilewriter.py

Lines changed: 0 additions & 122 deletions
This file was deleted.

src/robot/tidypkg/transformers.py

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ def visit_Keyword(self, node):
111111

112112
class SeparatorCleaner(ModelTransformer):
113113

114-
def __init__(self, ctx):
114+
def __init__(self, separator_width):
115115
self.indent = 0
116-
self.separator = ' ' * ctx.txt_separating_spaces
116+
self.separator = ' ' * separator_width
117117

118118
def visit_TestCase(self, node):
119119
self.indent += 1
@@ -155,8 +155,8 @@ def normalize_separator(self, index, token):
155155

156156
class ColumnAligner(ModelTransformer):
157157

158-
def __init__(self, ctx, widths):
159-
self.ctx = ctx
158+
def __init__(self, short_test_name_length, widths):
159+
self.short_test_name_length = short_test_name_length
160160
self.widths = widths
161161
self.test_name_len = 0
162162
self.indent = 0
@@ -195,8 +195,7 @@ def align_statement(self, statement):
195195
for token, width in zip(line, widths):
196196
exp_pos += width
197197
if self.should_write_content_after_name(line_pos):
198-
# FIXME, this hack ony works with indent = 4
199-
exp_pos -= (self.test_name_len - 4)
198+
exp_pos -= self.test_name_len
200199
self.first_statement_after_name_seen = True
201200
token.value = (exp_pos - line_pos) * ' ' + token.value
202201
line_pos += len(token.value)
@@ -213,7 +212,7 @@ def _should_be_indented(self, line):
213212

214213
def should_write_content_after_name(self, line_pos):
215214
return line_pos == 0 and not self.first_statement_after_name_seen \
216-
and self.test_name_len < self.ctx.short_test_name_length
215+
and self.test_name_len < self.short_test_name_length
217216

218217

219218
class ColumnWidthCounter(ModelTransformer):
@@ -239,15 +238,15 @@ def _count_widths_from_statement(self, statement, indent=0):
239238

240239

241240
class Aligner(ModelTransformer):
242-
243-
def __init__(self, ctx):
244-
self.ctx = ctx
241+
setting_and_variable_name_length = 14
242+
short_test_name_length = 18
245243

246244
def visit_TestCaseSection(self, section):
247245
if len(section.header.data_tokens) > 1:
248246
counter = ColumnWidthCounter()
249247
counter.visit(section)
250-
ColumnAligner(self.ctx, counter.widths).visit(section)
248+
ColumnAligner(self.short_test_name_length,
249+
counter.widths).visit(section)
251250
return section
252251

253252
def visit_KeywordSection(self, section):
@@ -262,7 +261,7 @@ def visit_Statement(self, statement):
262261
first_value = [t for t in line if t.type != Token.SEPARATOR][0]
263262
value_index = line.index(first_value)
264263
line[value_index].value = line[value_index].value.ljust(
265-
self.ctx.setting_and_variable_name_length)
264+
self.setting_and_variable_name_length)
266265
return statement
267266

268267

@@ -339,39 +338,3 @@ def visit_ForLoop(self, loop):
339338
loop.end.tokens[0], Token(Token.EOL, '\n'))
340339
self.generic_visit(loop)
341340
return loop
342-
343-
344-
class Formatter(ModelTransformer):
345-
346-
def __init__(self, ctx, row_writer):
347-
self.ctx = ctx
348-
self.writer = row_writer
349-
self.indent = 0
350-
351-
def visit_Section(self, section):
352-
self.generic_visit(section)
353-
354-
def visit_TestCase(self, node):
355-
self._write_name(node.name_tokens)
356-
self.generic_visit(node.body)
357-
358-
def visit_Keyword(self, node):
359-
self._write_name(node.name_tokens)
360-
self.generic_visit(node.body)
361-
362-
def _write_name(self, name):
363-
self._write_statement(name)
364-
365-
def visit_ForLoop(self, node):
366-
self._write_statement(node.header)
367-
self.generic_visit(node.body)
368-
if node.end:
369-
self._write_statement(node.end)
370-
371-
def visit_Statement(self, statement):
372-
self._write_statement(statement)
373-
374-
def _write_statement(self, statement):
375-
for line in statement.lines:
376-
self.writer.write(line)
377-

0 commit comments

Comments
 (0)