aboutsummaryrefslogtreecommitdiff
path: root/lib/python
diff options
context:
space:
mode:
authorDrashna Jael're <drashna@live.com>2021-11-19 09:48:23 -0800
committerDrashna Jael're <drashna@live.com>2021-11-19 09:48:23 -0800
commit4be9919e9fd7c54c2e046b82e82f5fbc2fdd44e6 (patch)
tree5a56b18bd8aff40ec480833f8ba2f7c48ef3279a /lib/python
parenta5155b98fb29ad4ea6de73f39a0d413d79883356 (diff)
parent94b535024642bab6705f1e08ec62680d8e49b62b (diff)
downloadqmk_firmware-4be9919e9fd7c54c2e046b82e82f5fbc2fdd44e6.tar.gz
qmk_firmware-4be9919e9fd7c54c2e046b82e82f5fbc2fdd44e6.zip
Merge remote-tracking branch 'origin/master' into develop
Diffstat (limited to 'lib/python')
-rwxr-xr-xlib/python/qmk/cli/format/python.py66
-rw-r--r--lib/python/qmk/cli/list/keymaps.py5
-rw-r--r--lib/python/qmk/cli/list/layouts.py5
-rw-r--r--lib/python/qmk/tests/test_cli_commands.py4
4 files changed, 66 insertions, 14 deletions
diff --git a/lib/python/qmk/cli/format/python.py b/lib/python/qmk/cli/format/python.py
index b32a72640..95868d18a 100755
--- a/lib/python/qmk/cli/format/python.py
+++ b/lib/python/qmk/cli/format/python.py
@@ -4,23 +4,65 @@ from subprocess import CalledProcessError, DEVNULL
4 4
5from milc import cli 5from milc import cli
6 6
7from qmk.path import normpath
7 8
8@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually format.") 9py_file_suffixes = ('py',)
9@cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True) 10py_dirs = ['lib/python']
10def format_python(cli): 11
11 """Format python code according to QMK's style. 12
12 """ 13def yapf_run(files):
13 edit = '--diff' if cli.args.dry_run else '--in-place' 14 edit = '--diff' if cli.args.dry_run else '--in-place'
14 yapf_cmd = ['yapf', '-vv', '--recursive', edit, 'lib/python'] 15 yapf_cmd = ['yapf', '-vv', '--recursive', edit, *files]
15 try: 16 try:
16 cli.run(yapf_cmd, check=True, capture_output=False, stdin=DEVNULL) 17 cli.run(yapf_cmd, check=True, capture_output=False, stdin=DEVNULL)
17 cli.log.info('Python code in `lib/python` is correctly formatted.') 18 cli.log.info('Successfully formatted the python code.')
18 return True
19 19
20 except CalledProcessError: 20 except CalledProcessError:
21 if cli.args.dry_run: 21 cli.log.error(f'Python code in {",".join(py_dirs)} incorrectly formatted!')
22 cli.log.error('Python code in `lib/python` is incorrectly formatted!') 22 return False
23
24
25def filter_files(files):
26 """Yield only files to be formatted and skip the rest
27 """
28
29 for file in files:
30 if file and file.name.split('.')[-1] in py_file_suffixes:
31 yield file
23 else: 32 else:
24 cli.log.error('Error formatting python code!') 33 cli.log.debug('Skipping file %s', file)
34
35
36@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually format.")
37@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
38@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all files.')
39@cli.argument('files', nargs='*', arg_only=True, type=normpath, help='Filename(s) to format.')
40@cli.subcommand("Format python code according to QMK's style.", hidden=False if cli.config.user.developer else True)
41def format_python(cli):
42 """Format python code according to QMK's style.
43 """
44 # Find the list of files to format
45 if cli.args.files:
46 files = list(filter_files(cli.args.files))
47
48 if not files:
49 cli.log.error('No Python files in filelist: %s', ', '.join(map(str, cli.args.files)))
50 exit(0)
51
52 if cli.args.all_files:
53 cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files)))
54
55 elif cli.args.all_files:
56 files = py_dirs
57
58 else:
59 git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *py_dirs]
60 git_diff = cli.run(git_diff_cmd, stdin=DEVNULL)
61 files = list(filter(None, git_diff.stdout.split('\n')))
62
63 # Sanity check
64 if not files:
65 cli.log.error('No changed files detected. Use "qmk format-python -a" to format all files')
66 return False
25 67
26 return False 68 return yapf_run(files)
diff --git a/lib/python/qmk/cli/list/keymaps.py b/lib/python/qmk/cli/list/keymaps.py
index d79ab75b5..d2ef136c0 100644
--- a/lib/python/qmk/cli/list/keymaps.py
+++ b/lib/python/qmk/cli/list/keymaps.py
@@ -13,5 +13,10 @@ from qmk.keyboard import keyboard_completer, keyboard_folder
13def list_keymaps(cli): 13def list_keymaps(cli):
14 """List the keymaps for a specific keyboard 14 """List the keymaps for a specific keyboard
15 """ 15 """
16 if not cli.config.list_keymaps.keyboard:
17 cli.log.error('Missing required arguments: --keyboard')
18 cli.subcommands['list-keymaps'].print_help()
19 return False
20
16 for name in qmk.keymap.list_keymaps(cli.config.list_keymaps.keyboard): 21 for name in qmk.keymap.list_keymaps(cli.config.list_keymaps.keyboard):
17 print(name) 22 print(name)
diff --git a/lib/python/qmk/cli/list/layouts.py b/lib/python/qmk/cli/list/layouts.py
index 8e07afeec..df593dc39 100644
--- a/lib/python/qmk/cli/list/layouts.py
+++ b/lib/python/qmk/cli/list/layouts.py
@@ -13,6 +13,11 @@ from qmk.info import info_json
13def list_layouts(cli): 13def list_layouts(cli):
14 """List the layouts for a specific keyboard 14 """List the layouts for a specific keyboard
15 """ 15 """
16 if not cli.config.list_layouts.keyboard:
17 cli.log.error('Missing required arguments: --keyboard')
18 cli.subcommands['list-layouts'].print_help()
19 return False
20
16 info_data = info_json(cli.config.list_layouts.keyboard) 21 info_data = info_json(cli.config.list_layouts.keyboard)
17 for name in sorted(info_data.get('community_layouts', [])): 22 for name in sorted(info_data.get('community_layouts', [])):
18 print(name) 23 print(name)
diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py
index 9f2c258ab..1e3c64e73 100644
--- a/lib/python/qmk/tests/test_cli_commands.py
+++ b/lib/python/qmk/tests/test_cli_commands.py
@@ -81,9 +81,9 @@ def test_hello():
81 81
82 82
83def test_format_python(): 83def test_format_python():
84 result = check_subcommand('format-python', '--dry-run') 84 result = check_subcommand('format-python', '-n', '-a')
85 check_returncode(result) 85 check_returncode(result)
86 assert 'Python code in `lib/python` is correctly formatted.' in result.stdout 86 assert 'Successfully formatted the python code.' in result.stdout
87 87
88 88
89def test_list_keyboards(): 89def test_list_keyboards():