Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions pyformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,27 @@
__version__ = '0.6'


def formatters(aggressive, apply_config):
def formatters(aggressive, apply_config, filename=''):
"""Return list of code formatters."""
if aggressive:
yield autoflake.fix_code
autopep8_options = autopep8.parse_args(
[''] + int(aggressive) * ['--aggressive'],
[filename] + int(aggressive) * ['--aggressive'],
apply_config=apply_config)
else:
autopep8_options = autopep8.parse_args([''], apply_config=apply_config)
autopep8_options = autopep8.parse_args(
[filename], apply_config=apply_config)

yield lambda code: autopep8.fix_code(code, options=autopep8_options)
yield docformatter.format_code
yield unify.format_code


def format_code(source, aggressive=False, apply_config=False):
def format_code(source, aggressive=False, apply_config=False, filename=''):
"""Return formatted source code."""
formatted_source = source

for fix in formatters(aggressive, apply_config):
for fix in formatters(aggressive, apply_config, filename):
formatted_source = fix(formatted_source)

return formatted_source
Expand All @@ -82,7 +83,8 @@ def format_file(filename, args, standard_out):

formatted_source = format_code(source,
aggressive=args.aggressive,
apply_config=args.config)
apply_config=args.config,
filename=filename)

if source != formatted_source:
if args.in_place:
Expand Down
59 changes: 59 additions & 0 deletions test_pyformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,51 @@ def test_end_to_end(self):
+x = 'abc'
""", '\n'.join(output.decode().split('\n')[3:]))

def test_no_config(self):
self.maxDiff = None
source = """\
x = ['The limits are chosen to avoid wrapping in editors with the window', 'width set to 80, even if the ', 'tool places a marker glyph in the final column when wrapping lines.']
"""
expected = """\
@@ -1 +1,2 @@
-x = ['The limits are chosen to avoid wrapping in editors with the window', 'width set to 80, even if the ', 'tool places a marker glyph in the final column when wrapping lines.']
+x = ['The limits are chosen to avoid wrapping in editors with the window',
+ 'width set to 80, even if the ', 'tool places a marker glyph in the final column when wrapping lines.']
"""
expected_no_conifg = """\
@@ -1 +1,3 @@
-x = ['The limits are chosen to avoid wrapping in editors with the window', 'width set to 80, even if the ', 'tool places a marker glyph in the final column when wrapping lines.']
+x = ['The limits are chosen to avoid wrapping in editors with the window',
+ 'width set to 80, even if the ',
+ 'tool places a marker glyph in the final column when wrapping lines.']
"""
setup_cfg = """\
[pep8]
max-line-length = 120
"""
with temporary_directory() as directory:
with temporary_file(source, prefix='food', directory=directory) as filename, \
temporary_named_file(setup_cfg, name='setup.cfg', directory=directory):

output_file = io.StringIO()
pyformat._main(argv=['my_fake_program',
'--aggressive',
filename],
standard_out=output_file,
standard_error=None)
self.assertEqual(expected, '\n'.join(
output_file.getvalue().split('\n')[2:]))

output_file = io.StringIO()
pyformat._main(argv=['my_fake_program',
'--aggressive',
'--no-config',
filename],
standard_out=output_file,
standard_error=None)
self.assertEqual(expected_no_conifg, '\n'.join(
output_file.getvalue().split('\n')[2:]))


@contextlib.contextmanager
def temporary_file(contents, directory='.', prefix=''):
Expand All @@ -329,6 +374,20 @@ def temporary_file(contents, directory='.', prefix=''):
os.remove(f.name)


@contextlib.contextmanager
def temporary_named_file(contents, name, directory='.'):
"""Write contents to temporary file and yield it."""
path = os.path.join(directory, name)
try:
with open(path, 'wb') as f:
f.write(contents.encode())
f.close()
yield path
finally:
if os.path.exists(path):
os.remove(path)


@contextlib.contextmanager
def temporary_directory(directory='.', prefix=''):
"""Create temporary directory and yield its path."""
Expand Down