From 70ae57af01463ea0b97b8ec2657a49d58e0f74ec Mon Sep 17 00:00:00 2001 From: Sho Iwamoto Date: Fri, 8 Jun 2018 17:31:17 +0200 Subject: [PATCH 1/4] Return diff only if --diff is specified --- pyformat.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pyformat.py b/pyformat.py index de5e33b..fadbd36 100755 --- a/pyformat.py +++ b/pyformat.py @@ -103,12 +103,14 @@ def format_file(filename, args, standard_out): with autopep8.open_with_encoding(filename, mode='w', encoding=encoding) as output_file: output_file.write(formatted_source) - else: + elif args.diff: diff = autopep8.get_diff_text( io.StringIO(source).readlines(), io.StringIO(formatted_source).readlines(), filename) standard_out.write(''.join(diff)) + else: + standard_out.write(formatted_source) return True @@ -169,6 +171,8 @@ def parse_args(argv): parser = argparse.ArgumentParser(description=__doc__, prog='pyformat') parser.add_argument('-i', '--in-place', action='store_true', help='make changes to files instead of printing diffs') + parser.add_argument('-d', '--diff', action='store_true', + help='print the diff for the fixed source') parser.add_argument('-r', '--recursive', action='store_true', help='drill down directories recursively') parser.add_argument('-a', '--aggressive', action='count', default=0, From 9b92245e79bf75bebf1938de0c76eb2107805e79 Mon Sep 17 00:00:00 2001 From: Sho Iwamoto Date: Fri, 8 Jun 2018 18:29:47 +0200 Subject: [PATCH 2/4] Adopt test for '--diff' --- test_pyformat.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test_pyformat.py b/test_pyformat.py index dbe5fdb..f712f8a 100755 --- a/test_pyformat.py +++ b/test_pyformat.py @@ -150,7 +150,7 @@ def test_diff(self): x = "abc" ''') as filename: output_file = io.StringIO() - pyformat._main(argv=['my_fake_program', filename], + pyformat._main(argv=['my_fake_program','--diff', filename], standard_out=output_file, standard_error=None) self.assertEqual('''\ @@ -166,7 +166,7 @@ def test_diff_with_aggressive(self): x = "abc" ''') as filename: output_file = io.StringIO() - pyformat._main(argv=['my_fake_program', '--aggressive', filename], + pyformat._main(argv=['my_fake_program', '--diff', '--aggressive', filename], standard_out=output_file, standard_error=None) self.assertEqual('''\ @@ -193,7 +193,7 @@ def test_diff_with_encoding_declaration(self): x = 1 """) as filename: output_file = io.StringIO() - pyformat._main(argv=['my_fake_program', '--aggressive', filename], + pyformat._main(argv=['my_fake_program', '--diff', '--aggressive', filename], standard_out=output_file, standard_error=None) self.assertEqual("""\ @@ -321,6 +321,7 @@ def test_recursive(self): '--recursive', '--exclude=zap', '--exclude=x*oo*', + '--diff', directory], standard_out=output_file, standard_error=None) @@ -397,7 +398,7 @@ def test_end_to_end(self): import os x = "abc" """) as filename: - output = subprocess.check_output(PYFORMAT_COMMAND + [filename]) + output = subprocess.check_output(PYFORMAT_COMMAND + ['--diff', filename]) self.assertEqual("""\ import os -x = "abc" @@ -436,6 +437,7 @@ def test_no_config(self): output_file = io.StringIO() pyformat._main(argv=['my_fake_program', + '--diff', '--aggressive', '--no-config', filename], From 767987d186df6f0a08d7463c7c43f07881a83cd3 Mon Sep 17 00:00:00 2001 From: Sho Iwamoto Date: Wed, 31 Oct 2018 18:42:16 +0100 Subject: [PATCH 3/4] Allow options for autopep8 --- pyformat.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/pyformat.py b/pyformat.py index fadbd36..ecc57a3 100755 --- a/pyformat.py +++ b/pyformat.py @@ -41,7 +41,22 @@ __version__ = '1.0a0' -def formatters(aggressive, apply_config, filename='', +def _arrange_autopep8_options(filename='', args=None): + options = [filename] + if args: + options += int(args.aggressive) * ['--aggressive'] + if args.max_line_length: + options.append('--max-line-length={}'.format(args.max_line_length)) + if args.ignore: + options.append('--ignore={}'.format(args.ignore)) + apply_config = args.config + else: + apply_config = None + + return autopep8.parse_args(options, apply_config=apply_config) + + +def formatters(aggressive, autopep8_options, remove_all_unused_imports=False, remove_unused_variables=False): """Return list of code formatters.""" if aggressive: @@ -49,27 +64,20 @@ def formatters(aggressive, apply_config, filename='', code, remove_all_unused_imports=remove_all_unused_imports, remove_unused_variables=remove_unused_variables) - - autopep8_options = autopep8.parse_args( - [filename] + int(aggressive) * ['--aggressive'], - apply_config=apply_config) - else: - 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, filename='', +def format_code(source, args=None, aggressive=False, filename='', remove_all_unused_imports=False, remove_unused_variables=False): """Return formatted source code.""" formatted_source = source for fix in formatters( - aggressive, apply_config, filename, + aggressive, + _arrange_autopep8_options(filename, args), remove_all_unused_imports, remove_unused_variables): formatted_source = fix(formatted_source) @@ -80,7 +88,6 @@ def format_file(filename, args, standard_out): """Run format_code() on a file. Return True if the new formatting differs from the original. - """ encoding = autopep8.detect_encoding(filename) with autopep8.open_with_encoding(filename, @@ -92,8 +99,8 @@ def format_file(filename, args, standard_out): formatted_source = format_code( source, + args=args, aggressive=args.aggressive, - apply_config=args.config, filename=filename, remove_all_unused_imports=args.remove_all_unused_imports, remove_unused_variables=args.remove_unused_variables) @@ -144,7 +151,6 @@ def format_multiple_files(filenames, args, standard_out, standard_error): """Format files and return booleans (any_changes, any_errors). Optionally format files recursively. - """ filenames = autopep8.find_files(list(filenames), args.recursive, @@ -193,6 +199,10 @@ def parse_args(argv): help='exclude files this pattern; ' 'specify this multiple times for multiple ' 'patterns') + parser.add_argument('--max-line-length', type=int, metavar='n', default=79, + help='set maximum allowed line length (default: 79)') + parser.add_argument('--ignore', metavar='errors', default='', + help='do not fix these errors/warnings') parser.add_argument('--no-config', action='store_false', dest='config', help="don't look for and apply local configuration " 'files; if not passed, defaults are updated with ' @@ -215,7 +225,6 @@ def _main(argv, standard_out, standard_error): """Internal main entry point. Return exit status. 0 means no error. - """ args = parse_args(argv) From 8530dc38ef05e6038887cdc5170d17548b0ecb28 Mon Sep 17 00:00:00 2001 From: Sho Iwamoto Date: Fri, 25 Jan 2019 19:43:35 +0100 Subject: [PATCH 4/4] Revert the previous commit Options for autopep8 should be better configured in project-configuration files --- pyformat.py | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/pyformat.py b/pyformat.py index ecc57a3..fadbd36 100755 --- a/pyformat.py +++ b/pyformat.py @@ -41,22 +41,7 @@ __version__ = '1.0a0' -def _arrange_autopep8_options(filename='', args=None): - options = [filename] - if args: - options += int(args.aggressive) * ['--aggressive'] - if args.max_line_length: - options.append('--max-line-length={}'.format(args.max_line_length)) - if args.ignore: - options.append('--ignore={}'.format(args.ignore)) - apply_config = args.config - else: - apply_config = None - - return autopep8.parse_args(options, apply_config=apply_config) - - -def formatters(aggressive, autopep8_options, +def formatters(aggressive, apply_config, filename='', remove_all_unused_imports=False, remove_unused_variables=False): """Return list of code formatters.""" if aggressive: @@ -64,20 +49,27 @@ def formatters(aggressive, autopep8_options, code, remove_all_unused_imports=remove_all_unused_imports, remove_unused_variables=remove_unused_variables) + + autopep8_options = autopep8.parse_args( + [filename] + int(aggressive) * ['--aggressive'], + apply_config=apply_config) + else: + 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, args=None, aggressive=False, filename='', +def format_code(source, aggressive=False, apply_config=False, filename='', remove_all_unused_imports=False, remove_unused_variables=False): """Return formatted source code.""" formatted_source = source for fix in formatters( - aggressive, - _arrange_autopep8_options(filename, args), + aggressive, apply_config, filename, remove_all_unused_imports, remove_unused_variables): formatted_source = fix(formatted_source) @@ -88,6 +80,7 @@ def format_file(filename, args, standard_out): """Run format_code() on a file. Return True if the new formatting differs from the original. + """ encoding = autopep8.detect_encoding(filename) with autopep8.open_with_encoding(filename, @@ -99,8 +92,8 @@ def format_file(filename, args, standard_out): formatted_source = format_code( source, - args=args, aggressive=args.aggressive, + apply_config=args.config, filename=filename, remove_all_unused_imports=args.remove_all_unused_imports, remove_unused_variables=args.remove_unused_variables) @@ -151,6 +144,7 @@ def format_multiple_files(filenames, args, standard_out, standard_error): """Format files and return booleans (any_changes, any_errors). Optionally format files recursively. + """ filenames = autopep8.find_files(list(filenames), args.recursive, @@ -199,10 +193,6 @@ def parse_args(argv): help='exclude files this pattern; ' 'specify this multiple times for multiple ' 'patterns') - parser.add_argument('--max-line-length', type=int, metavar='n', default=79, - help='set maximum allowed line length (default: 79)') - parser.add_argument('--ignore', metavar='errors', default='', - help='do not fix these errors/warnings') parser.add_argument('--no-config', action='store_false', dest='config', help="don't look for and apply local configuration " 'files; if not passed, defaults are updated with ' @@ -225,6 +215,7 @@ def _main(argv, standard_out, standard_error): """Internal main entry point. Return exit status. 0 means no error. + """ args = parse_args(argv)