diff options
Diffstat (limited to 'lib/python/qmk/cli/format')
-rwxr-xr-x | lib/python/qmk/cli/format/python.py | 66 |
1 files changed, 54 insertions, 12 deletions
diff --git a/lib/python/qmk/cli/format/python.py b/lib/python/qmk/cli/format/python.py index b32a72640..95868d18a 100755 --- a/lib/python/qmk/cli/format/python.py +++ b/lib/python/qmk/cli/format/python.py | |||
@@ -4,23 +4,65 @@ from subprocess import CalledProcessError, DEVNULL | |||
4 | 4 | ||
5 | from milc import cli | 5 | from milc import cli |
6 | 6 | ||
7 | from qmk.path import normpath | ||
7 | 8 | ||
8 | @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually format.") | 9 | py_file_suffixes = ('py',) |
9 | @cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True) | 10 | py_dirs = ['lib/python'] |
10 | def format_python(cli): | 11 | |
11 | """Format python code according to QMK's style. | 12 | |
12 | """ | 13 | def yapf_run(files): |
13 | edit = '--diff' if cli.args.dry_run else '--in-place' | 14 | edit = '--diff' if cli.args.dry_run else '--in-place' |
14 | yapf_cmd = ['yapf', '-vv', '--recursive', edit, 'lib/python'] | 15 | yapf_cmd = ['yapf', '-vv', '--recursive', edit, *files] |
15 | try: | 16 | try: |
16 | cli.run(yapf_cmd, check=True, capture_output=False, stdin=DEVNULL) | 17 | cli.run(yapf_cmd, check=True, capture_output=False, stdin=DEVNULL) |
17 | cli.log.info('Python code in `lib/python` is correctly formatted.') | 18 | cli.log.info('Successfully formatted the python code.') |
18 | return True | ||
19 | 19 | ||
20 | except CalledProcessError: | 20 | except CalledProcessError: |
21 | if cli.args.dry_run: | 21 | cli.log.error(f'Python code in {",".join(py_dirs)} incorrectly formatted!') |
22 | cli.log.error('Python code in `lib/python` is incorrectly formatted!') | 22 | return False |
23 | |||
24 | |||
25 | def filter_files(files): | ||
26 | """Yield only files to be formatted and skip the rest | ||
27 | """ | ||
28 | |||
29 | for file in files: | ||
30 | if file and file.name.split('.')[-1] in py_file_suffixes: | ||
31 | yield file | ||
23 | else: | 32 | else: |
24 | cli.log.error('Error formatting python code!') | 33 | cli.log.debug('Skipping file %s', file) |
34 | |||
35 | |||
36 | @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually format.") | ||
37 | @cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.') | ||
38 | @cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all files.') | ||
39 | @cli.argument('files', nargs='*', arg_only=True, type=normpath, help='Filename(s) to format.') | ||
40 | @cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True) | ||
41 | def format_python(cli): | ||
42 | """Format python code according to QMK's style. | ||
43 | """ | ||
44 | # Find the list of files to format | ||
45 | if cli.args.files: | ||
46 | files = list(filter_files(cli.args.files)) | ||
47 | |||
48 | if not files: | ||
49 | cli.log.error('No Python files in filelist: %s', ', '.join(map(str, cli.args.files))) | ||
50 | exit(0) | ||
51 | |||
52 | if cli.args.all_files: | ||
53 | cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files))) | ||
54 | |||
55 | elif cli.args.all_files: | ||
56 | files = py_dirs | ||
57 | |||
58 | else: | ||
59 | git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *py_dirs] | ||
60 | git_diff = cli.run(git_diff_cmd, stdin=DEVNULL) | ||
61 | files = list(filter(None, git_diff.stdout.split('\n'))) | ||
62 | |||
63 | # Sanity check | ||
64 | if not files: | ||
65 | cli.log.error('No changed files detected. Use "qmk format-python -a" to format all files') | ||
66 | return False | ||
25 | 67 | ||
26 | return False | 68 | return yapf_run(files) |