|
1 | 1 | # Copyright (c) 2021 The ZMK Contributors
|
2 | 2 | # SPDX-License-Identifier: MIT
|
3 |
| -'''Metadata command for ZMK.''' |
| 3 | +"""Metadata command for ZMK.""" |
4 | 4 |
|
5 | 5 | from functools import cached_property
|
6 | 6 | import glob
|
7 | 7 | import json
|
8 |
| -from jsonschema import validate, ValidationError |
9 |
| -import os |
| 8 | +import jsonschema |
10 | 9 | import sys
|
11 | 10 | import yaml
|
12 |
| -from textwrap import dedent # just for nicer code indentation |
13 | 11 |
|
14 | 12 | 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 |
16 | 14 |
|
17 | 15 |
|
18 | 16 | class Metadata(WestCommand):
|
19 | 17 | def __init__(self):
|
20 | 18 | 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 | + ) |
25 | 23 |
|
26 | 24 | 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 | + ) |
30 | 28 |
|
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 |
34 | 36 |
|
35 | 37 | @cached_property
|
36 | 38 | 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")) |
39 | 40 |
|
40 | 41 | def validate_file(self, file):
|
41 | 42 | print("Validating: " + file)
|
42 |
| - with open(file, 'r') as stream: |
| 43 | + with open(file, "r") as stream: |
43 | 44 | try:
|
44 |
| - validate(yaml.safe_load(stream), self.schema) |
| 45 | + jsonschema.validate(yaml.safe_load(stream), self.schema) |
45 | 46 | except yaml.YAMLError as exc:
|
46 | 47 | print("Failed loading metadata yaml: " + file)
|
47 | 48 | print(exc)
|
48 | 49 | return False
|
49 |
| - except ValidationError as vexc: |
| 50 | + except jsonschema.ValidationError as vexc: |
50 | 51 | print("Failed validation of: " + file)
|
51 | 52 | print(vexc)
|
52 | 53 | return False
|
53 | 54 | return True
|
54 | 55 |
|
55 | 56 | 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 | + ) |
58 | 63 |
|
59 | 64 | sys.exit(0 if status else 1)
|
0 commit comments