2021-07-20 18:52:14 +00:00
|
|
|
"""Format python code according to QMK's style.
|
|
|
|
"""
|
|
|
|
from subprocess import CalledProcessError, DEVNULL
|
|
|
|
|
|
|
|
from milc import cli
|
|
|
|
|
2021-11-17 23:02:45 +00:00
|
|
|
from qmk.path import normpath
|
2021-07-20 18:52:14 +00:00
|
|
|
|
2021-11-17 23:02:45 +00:00
|
|
|
py_file_suffixes = ('py',)
|
|
|
|
py_dirs = ['lib/python']
|
|
|
|
|
|
|
|
|
|
|
|
def yapf_run(files):
|
2021-07-20 18:52:14 +00:00
|
|
|
edit = '--diff' if cli.args.dry_run else '--in-place'
|
2022-10-12 18:41:45 +00:00
|
|
|
yapf_cmd = ['yapf', '-vv', '--exclude', '**/xap_client/*', '--recursive', edit, *files]
|
2021-07-20 18:52:14 +00:00
|
|
|
try:
|
|
|
|
cli.run(yapf_cmd, check=True, capture_output=False, stdin=DEVNULL)
|
2021-11-17 23:02:45 +00:00
|
|
|
cli.log.info('Successfully formatted the python code.')
|
2021-07-20 18:52:14 +00:00
|
|
|
|
|
|
|
except CalledProcessError:
|
2021-11-17 23:02:45 +00:00
|
|
|
cli.log.error(f'Python code in {",".join(py_dirs)} incorrectly formatted!')
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def filter_files(files):
|
|
|
|
"""Yield only files to be formatted and skip the rest
|
|
|
|
"""
|
2022-01-03 21:54:46 +00:00
|
|
|
files = list(map(normpath, filter(None, files)))
|
2021-11-17 23:02:45 +00:00
|
|
|
for file in files:
|
2022-01-03 21:54:46 +00:00
|
|
|
if file.suffix[1:] in py_file_suffixes:
|
2021-11-17 23:02:45 +00:00
|
|
|
yield file
|
2021-07-20 18:52:14 +00:00
|
|
|
else:
|
2021-11-17 23:02:45 +00:00
|
|
|
cli.log.debug('Skipping file %s', file)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually format.")
|
|
|
|
@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
|
|
|
|
@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all files.')
|
|
|
|
@cli.argument('files', nargs='*', arg_only=True, type=normpath, help='Filename(s) to format.')
|
|
|
|
@cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True)
|
|
|
|
def format_python(cli):
|
|
|
|
"""Format python code according to QMK's style.
|
|
|
|
"""
|
|
|
|
# Find the list of files to format
|
|
|
|
if cli.args.files:
|
|
|
|
files = list(filter_files(cli.args.files))
|
|
|
|
|
|
|
|
if not files:
|
|
|
|
cli.log.error('No Python files in filelist: %s', ', '.join(map(str, cli.args.files)))
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
if cli.args.all_files:
|
|
|
|
cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files)))
|
|
|
|
|
|
|
|
elif cli.args.all_files:
|
2021-11-23 00:23:23 +00:00
|
|
|
git_ls_cmd = ['git', 'ls-files', *py_dirs]
|
|
|
|
git_ls = cli.run(git_ls_cmd, stdin=DEVNULL)
|
|
|
|
files = list(filter_files(git_ls.stdout.split('\n')))
|
2021-11-17 23:02:45 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *py_dirs]
|
|
|
|
git_diff = cli.run(git_diff_cmd, stdin=DEVNULL)
|
2021-11-23 00:23:23 +00:00
|
|
|
files = list(filter_files(git_diff.stdout.split('\n')))
|
2021-11-17 23:02:45 +00:00
|
|
|
|
|
|
|
# Sanity check
|
|
|
|
if not files:
|
|
|
|
cli.log.error('No changed files detected. Use "qmk format-python -a" to format all files')
|
|
|
|
return False
|
2021-07-20 18:52:14 +00:00
|
|
|
|
2021-11-17 23:02:45 +00:00
|
|
|
return yapf_run(files)
|