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