2019-08-22 16:40:12 +00:00
|
|
|
"""Format python code according to QMK's style.
|
|
|
|
"""
|
2021-05-19 22:24:46 +00:00
|
|
|
from subprocess import CalledProcessError, DEVNULL
|
2019-08-22 16:40:12 +00:00
|
|
|
|
2021-05-19 22:24:46 +00:00
|
|
|
from milc import cli
|
2019-08-22 16:40:12 +00:00
|
|
|
|
|
|
|
|
2021-05-10 18:18:44 +00:00
|
|
|
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.")
|
2019-11-27 20:27:06 +00:00
|
|
|
@cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True)
|
2019-09-22 20:25:33 +00:00
|
|
|
def pyformat(cli):
|
2019-08-22 16:40:12 +00:00
|
|
|
"""Format python code according to QMK's style.
|
|
|
|
"""
|
2021-05-10 18:18:44 +00:00
|
|
|
edit = '--diff' if cli.args.dry_run else '--in-place'
|
|
|
|
yapf_cmd = ['yapf', '-vv', '--recursive', edit, 'bin/qmk', 'lib/python']
|
2019-08-22 16:40:12 +00:00
|
|
|
try:
|
2021-05-19 22:24:46 +00:00
|
|
|
cli.run(yapf_cmd, check=True, capture_output=False, stdin=DEVNULL)
|
2021-05-10 18:18:44 +00:00
|
|
|
cli.log.info('Python code in `bin/qmk` and `lib/python` is correctly formatted.')
|
|
|
|
return True
|
2019-09-22 20:25:33 +00:00
|
|
|
|
2021-05-19 22:24:46 +00:00
|
|
|
except CalledProcessError:
|
2021-05-10 18:18:44 +00:00
|
|
|
if cli.args.dry_run:
|
|
|
|
cli.log.error('Python code in `bin/qmk` and `lib/python` incorrectly formatted!')
|
|
|
|
else:
|
|
|
|
cli.log.error('Error formatting python code!')
|
|
|
|
|
|
|
|
return False
|