diff options
author | Zach White <skullydazed@gmail.com> | 2021-07-20 11:52:14 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-20 11:52:14 -0700 |
commit | 4ab8734d6edd6894757507e70264eddca5429052 (patch) | |
tree | b43ad3f47426d3c194b05d02ae5ff34d6b0c1301 /lib/python/qmk/cli/pyformat.py | |
parent | c4db9f7fb2a359abb1db06e3d74a52dce8bdf68c (diff) | |
download | qmk_firmware-4ab8734d6edd6894757507e70264eddca5429052.tar.gz qmk_firmware-4ab8734d6edd6894757507e70264eddca5429052.zip |
Move all our CLI file formatters to the format dir (#13296)
* move all our file formatters to the format dir
* Apply suggestions from code review
Co-authored-by: Erovia <Erovia@users.noreply.github.com>
Co-authored-by: Erovia <Erovia@users.noreply.github.com>
Diffstat (limited to 'lib/python/qmk/cli/pyformat.py')
-rwxr-xr-x | lib/python/qmk/cli/pyformat.py | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/lib/python/qmk/cli/pyformat.py b/lib/python/qmk/cli/pyformat.py index abe5f6de1..c624f74ae 100755 --- a/lib/python/qmk/cli/pyformat.py +++ b/lib/python/qmk/cli/pyformat.py | |||
@@ -1,26 +1,24 @@ | |||
1 | """Format python code according to QMK's style. | 1 | """Point people to the new command name. |
2 | """ | 2 | """ |
3 | from subprocess import CalledProcessError, DEVNULL | 3 | import sys |
4 | from pathlib import Path | ||
4 | 5 | ||
5 | from milc import cli | 6 | from milc import cli |
6 | 7 | ||
7 | 8 | ||
8 | @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.") | 9 | @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually format.") |
9 | @cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True) | 10 | @cli.subcommand('Pointer to the new command name: qmk format-python.', hidden=False if cli.config.user.developer else True) |
10 | def pyformat(cli): | 11 | def pyformat(cli): |
11 | """Format python code according to QMK's style. | 12 | """Pointer to the new command name: qmk format-python. |
12 | """ | 13 | """ |
13 | edit = '--diff' if cli.args.dry_run else '--in-place' | 14 | cli.log.warning('"qmk pyformat" has been renamed to "qmk format-python". Please use the new command in the future.') |
14 | yapf_cmd = ['yapf', '-vv', '--recursive', edit, 'bin/qmk', 'lib/python'] | 15 | argv = [sys.executable, *sys.argv] |
15 | try: | 16 | argv[argv.index('pyformat')] = 'format-python' |
16 | cli.run(yapf_cmd, check=True, capture_output=False, stdin=DEVNULL) | 17 | script_path = Path(argv[1]) |
17 | cli.log.info('Python code in `bin/qmk` and `lib/python` is correctly formatted.') | 18 | script_path_exe = Path(f'{argv[1]}.exe') |
18 | return True | ||
19 | 19 | ||
20 | except CalledProcessError: | 20 | if not script_path.exists() and script_path_exe.exists(): |
21 | if cli.args.dry_run: | 21 | # For reasons I don't understand ".exe" is stripped from the script name on windows. |
22 | cli.log.error('Python code in `bin/qmk` and `lib/python` incorrectly formatted!') | 22 | argv[1] = str(script_path_exe) |
23 | else: | ||
24 | cli.log.error('Error formatting python code!') | ||
25 | 23 | ||
26 | return False | 24 | return cli.run(argv, capture_output=False).returncode |