Skip to content

Commit 279d429

Browse files
committed
satisfy flake8 - check_schema.py
1 parent 7d83f6b commit 279d429

File tree

1 file changed

+42
-69
lines changed

1 file changed

+42
-69
lines changed

config_schema/check_schema.py

+42-69
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,21 @@
1414

1515
use_thread = True
1616

17-
skip_path = (
18-
"snapshots/ebrainsquery/v1",
19-
"venv"
20-
)
17+
skip_path = ("snapshots/ebrainsquery/v1", "venv")
2118

2219
skip_types = []
2320

21+
2422
class ValidationResult(Enum):
25-
SKIPPED="SKIPPED"
26-
PASSED="PASSED"
27-
FAILED="FAILED"
23+
SKIPPED = "SKIPPED"
24+
PASSED = "PASSED"
25+
FAILED = "FAILED"
26+
2827

2928
BASE_URI = "http://example.com"
3029

31-
ROOT_DIR = os.path.abspath(
32-
f"{os.path.dirname(os.path.realpath(__file__))}/.."
33-
)
30+
ROOT_DIR = os.path.abspath(f"{os.path.dirname(os.path.realpath(__file__))}/..")
31+
3432

3533
def get_ref(schema):
3634
resolver = RefResolver(base_uri=BASE_URI, referrer=schema)
@@ -40,7 +38,6 @@ def get_ref(schema):
4038
if not filename.endswith(".json"):
4139
continue
4240
with open(f"{dirpath}/{filename}", "r") as fp:
43-
4441
relative_path = dirpath.replace(walk_path, "") + "/" + filename
4542

4643
key0 = urljoin(BASE_URI, f"config_schema/{relative_path}")
@@ -50,9 +47,10 @@ def get_ref(schema):
5047

5148
resolver.store[key0] = schema
5249
resolver.store[key1] = schema
53-
50+
5451
return resolver
5552

53+
5654
def validate_json(path_to_json, fail_fast=False):
5755
if any([path_fragment in path_to_json for path_fragment in skip_path]):
5856
return (
@@ -65,43 +63,22 @@ def validate_json(path_to_json, fail_fast=False):
6563

6664
# skip list
6765
if isinstance(json_obj, list):
68-
return (
69-
path_to_json,
70-
ValidationResult.SKIPPED,
71-
None
72-
)
66+
return (path_to_json, ValidationResult.SKIPPED, None)
7367
_type = json_obj.get("@type", None)
7468
if not _type:
7569
# TODO consolidate how error are raied
7670
if fail_fast:
7771
raise ValidationError(f"type does not exist: {path_to_json}")
78-
return (
79-
path_to_json,
80-
ValidationResult.FAILED,
81-
None
82-
)
83-
72+
return (path_to_json, ValidationResult.FAILED, None)
73+
8474
# assert _schema is None
8575
if not _type or not _type.startswith("siibra"):
86-
return (
87-
path_to_json,
88-
ValidationResult.SKIPPED,
89-
None
90-
)
76+
return (path_to_json, ValidationResult.SKIPPED, None)
9177
if _type in skip_types:
92-
return (
93-
path_to_json,
94-
ValidationResult.SKIPPED,
95-
None
96-
)
97-
abspath = os.path.join(
98-
ROOT_DIR, "config_schema", (_type + ".json")
99-
)
78+
return (path_to_json, ValidationResult.SKIPPED, None)
79+
abspath = os.path.join(ROOT_DIR, "config_schema", (_type + ".json"))
10080
path_to_schema = os.path.abspath(abspath)
101-
with open(
102-
path_to_schema,
103-
"r"
104-
) as fp:
81+
with open(path_to_schema, "r") as fp:
10582
schema = json.load(fp)
10683
try:
10784
resolver = get_ref(schema)
@@ -110,52 +87,48 @@ def validate_json(path_to_json, fail_fast=False):
11087
if fail_fast:
11188
# TODO consolidate how error are raied
11289
raise e
113-
return (
114-
path_to_json,
115-
ValidationResult.FAILED,
116-
e
117-
)
118-
return (
119-
path_to_json,
120-
ValidationResult.PASSED,
121-
None
122-
)
123-
90+
return (path_to_json, ValidationResult.FAILED, e)
91+
return (path_to_json, ValidationResult.PASSED, None)
92+
12493

12594
def main(path_to_configuration: str, *args):
126-
json_files = [f"{dirpath}/{filename}"
127-
for dirpath, dirnames, filenames in os.walk(path_to_configuration)
128-
for filename in filenames
129-
if filename.endswith(".json")
130-
]
95+
json_files = [
96+
f"{dirpath}/{filename}"
97+
for dirpath, dirnames, filenames in os.walk(path_to_configuration)
98+
for filename in filenames
99+
if filename.endswith(".json")
100+
]
131101
# TODO use argparse
132102
fail_fast = "--fail-fast" in args
133103
if use_thread:
134-
135-
with ThreadPoolExecutor(max_workers=4) as executor:
136-
result = [progress for progress in tqdm(
137-
executor.map(
138-
validate_json,
139-
json_files,
140-
repeat(fail_fast)
141-
),
142-
total=len(json_files)
143-
)]
104+
with ThreadPoolExecutor(max_workers=1) as executor:
105+
result = [
106+
progress
107+
for progress in tqdm(
108+
executor.map(validate_json, json_files, repeat(fail_fast)),
109+
total=len(json_files),
110+
)
111+
]
144112
else:
145113
result = [validate_json(f, fail_fast) for f in json_files]
146114

147115
passed = [r for r in result if r[1] == ValidationResult.PASSED]
148116
failed = [r for r in result if r[1] == ValidationResult.FAILED]
149117
skipped = [r for r in result if r[1] == ValidationResult.SKIPPED]
150-
print(f"Validation results: PASSED: {len(passed)} SKIPPED: {len(skipped)} FAILED: {len(failed)}")
118+
print(
119+
f"Validation results: PASSED: {len(passed)} SKIPPED: {len(skipped)} FAILED: {len(failed)}"
120+
)
151121

152122
if len(failed) > 0:
153123
print(failed)
154124
# TODO consolidate how error are raied
155-
raise ValidationError(message="\n-----\n".join([f"{f[0]}: {str(f[2])}" for f in failed]))
125+
raise ValidationError(
126+
message="\n-----\n".join([f"{f[0]}: {str(f[2])}" for f in failed])
127+
)
128+
156129

157130
if __name__ == "__main__":
158131
args = sys.argv[1:]
159132
if len(args) == 0:
160-
raise RuntimeError(f"Need path to configuration directory")
133+
raise RuntimeError("Need path to configuration directory")
161134
main(*args)

0 commit comments

Comments
 (0)