Skip to content

Commit f767abe

Browse files
okke-formsmapetejohanson
authored andcommitted
chore: make west scripts more pythonic and apply Black
1 parent e0620f1 commit f767abe

File tree

3 files changed

+50
-38
lines changed

3 files changed

+50
-38
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"files.associations": {
33
"*.overlay": "dts",
44
"*.keymap": "dts"
5-
}
5+
},
6+
"python.formatting.provider": "black"
67
}

app/scripts/west_commands/metadata.py

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,64 @@
11
# Copyright (c) 2021 The ZMK Contributors
22
# SPDX-License-Identifier: MIT
3-
'''Metadata command for ZMK.'''
3+
"""Metadata command for ZMK."""
44

55
from functools import cached_property
66
import glob
77
import json
8-
from jsonschema import validate, ValidationError
9-
import os
8+
import jsonschema
109
import sys
1110
import yaml
12-
from textwrap import dedent # just for nicer code indentation
1311

1412
from west.commands import WestCommand
15-
from west import log # use this for user output
13+
from west import log # use this for user output
1614

1715

1816
class Metadata(WestCommand):
1917
def __init__(self):
2018
super().__init__(
21-
'metadata', # gets stored as self.name
22-
'ZMK hardware metadata commands', # self.help
23-
# self.description:
24-
dedent('''Operate on the board/shield metadata.'''))
19+
name="metadata",
20+
help="ZMK hardware metadata commands",
21+
description="Operate on the board/shield metadata.",
22+
)
2523

2624
def do_add_parser(self, parser_adder):
27-
parser = parser_adder.add_parser(self.name,
28-
help=self.help,
29-
description=self.description)
25+
parser = parser_adder.add_parser(
26+
self.name, help=self.help, description=self.description
27+
)
3028

31-
parser.add_argument('subcommand', default="check",
32-
help='The subcommand to run. Defaults to "check".', nargs="?")
33-
return parser # gets stored as self.parser
29+
parser.add_argument(
30+
"subcommand",
31+
default="check",
32+
help='The subcommand to run. Defaults to "check".',
33+
nargs="?",
34+
)
35+
return parser # gets stored as self.parser
3436

3537
@cached_property
3638
def schema(self):
37-
return json.load(
38-
open("../schema/hardware-metadata.schema.json", 'r'))
39+
return json.load(open("../schema/hardware-metadata.schema.json", "r"))
3940

4041
def validate_file(self, file):
4142
print("Validating: " + file)
42-
with open(file, 'r') as stream:
43+
with open(file, "r") as stream:
4344
try:
44-
validate(yaml.safe_load(stream), self.schema)
45+
jsonschema.validate(yaml.safe_load(stream), self.schema)
4546
except yaml.YAMLError as exc:
4647
print("Failed loading metadata yaml: " + file)
4748
print(exc)
4849
return False
49-
except ValidationError as vexc:
50+
except jsonschema.ValidationError as vexc:
5051
print("Failed validation of: " + file)
5152
print(vexc)
5253
return False
5354
return True
5455

5556
def do_run(self, args, unknown_args):
56-
status = all([self.validate_file(f) for f in glob.glob(
57-
"boards/**/*.zmk.yml", recursive=True)])
57+
status = all(
58+
[
59+
self.validate_file(f)
60+
for f in glob.glob("boards/**/*.zmk.yml", recursive=True)
61+
]
62+
)
5863

5964
sys.exit(0 if status else 1)

app/scripts/west_commands/test.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
# Copyright (c) 2020 The ZMK Contributors
22
# SPDX-License-Identifier: MIT
3-
'''Test runner for ZMK.'''
3+
"""Test runner for ZMK."""
44

55
import os
66
import subprocess
7-
from textwrap import dedent # just for nicer code indentation
87

98
from west.commands import WestCommand
10-
from west import log # use this for user output
9+
from west import log # use this for user output
1110

1211

1312
class Test(WestCommand):
1413
def __init__(self):
1514
super().__init__(
16-
'test', # gets stored as self.name
17-
'run ZMK testsuite', # self.help
18-
# self.description:
19-
dedent('''Run the ZMK testsuite.'''))
15+
name="test",
16+
help="run ZMK testsuite",
17+
description="Run the ZMK testsuite.",
18+
)
2019

2120
def do_add_parser(self, parser_adder):
22-
parser = parser_adder.add_parser(self.name,
23-
help=self.help,
24-
description=self.description)
21+
parser = parser_adder.add_parser(
22+
self.name,
23+
help=self.help,
24+
description=self.description,
25+
)
2526

26-
parser.add_argument('test_path', default="all",
27-
help='The path to the test. Defaults to "all".', nargs="?")
28-
return parser # gets stored as self.parser
27+
parser.add_argument(
28+
"test_path",
29+
default="all",
30+
help='The path to the test. Defaults to "all".',
31+
nargs="?",
32+
)
33+
return parser
2934

3035
def do_run(self, args, unknown_args):
3136
# the run-test script assumes the app directory is the current dir.
32-
os.chdir(f'{self.topdir}/app')
37+
os.chdir(f"{self.topdir}/app")
3338
completed_process = subprocess.run(
34-
[f'{self.topdir}/app/run-test.sh', args.test_path])
39+
[f"{self.topdir}/app/run-test.sh", args.test_path]
40+
)
3541
exit(completed_process.returncode)

0 commit comments

Comments
 (0)