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