|
1 | | -import json |
2 | | -import os |
3 | 1 | from pathlib import Path |
4 | 2 |
|
5 | | -from utils import filter_rows, get_commit_hash, read_definitions, read_yaml_file, write_json |
| 3 | +from utils import filter_rows, get_commit_hash, read_definitions, read_yaml_file, setup_logs, write_json |
6 | 4 |
|
7 | 5 | URL = 'https://protocol.isimip.org/schema/' |
8 | 6 | EXCLUDE = ['model'] |
9 | 7 |
|
| 8 | +setup_logs() |
10 | 9 |
|
11 | 10 | def main(): |
12 | 11 | definitions = read_definitions() |
13 | 12 |
|
14 | | - for root, dirs, files in os.walk('schema'): |
15 | | - for file_name in files: |
16 | | - schema_path = Path(root) / file_name |
17 | | - schema_path_components = schema_path.with_suffix('').parts |
18 | | - output_path = (Path('output') / schema_path).with_suffix('.json') |
19 | | - |
20 | | - simulation_round = schema_path_components[1] |
21 | | - product = schema_path_components[2] |
22 | | - if product.endswith('InputData'): |
23 | | - category = schema_path_components[3] |
24 | | - sector = None |
25 | | - else: |
26 | | - category = None |
27 | | - sector = schema_path_components[3] |
28 | | - |
29 | | - # step 1: read schema template |
30 | | - schema_template = read_yaml_file(schema_path) |
31 | | - |
32 | | - schema = { |
33 | | - '$schema': 'http://json-schema.org/draft-07/schema#', |
34 | | - '$id': URL + schema_path.as_posix(), |
35 | | - 'commit': get_commit_hash() |
36 | | - } |
37 | | - schema.update(schema_template) |
38 | | - |
39 | | - # step 2: loop over properties/specifiers/properties and add enums from definition files |
40 | | - for identifier, properties in schema['properties']['specifiers']['properties'].items(): |
41 | | - if identifier in definitions: |
42 | | - if identifier not in EXCLUDE: |
43 | | - rows = definitions[identifier] |
44 | | - enum = [] |
45 | | - if product.endswith('InputData'): |
46 | | - for row in filter_rows(rows, simulation_round, product, category=category): |
47 | | - enum.append(row.get('specifier_file') or row.get('specifier')) |
48 | | - elif product == 'DerivedOutputData': |
49 | | - for row in filter_rows(rows, simulation_round, product): |
50 | | - enum.append(row.get('specifier_file') or row.get('specifier')) |
51 | | - else: |
52 | | - for row in filter_rows(rows, simulation_round, product, sector=sector): |
53 | | - enum.append(row.get('specifier_file') or row.get('specifier')) |
54 | | - |
55 | | - properties['enum'] = list(set(enum)) |
56 | | - |
57 | | - # step 3: write json schema |
58 | | - write_json(output_path, schema) |
| 13 | + for schema_path in Path('schema').rglob('**/*.yaml'): |
| 14 | + schema_path_components = schema_path.with_suffix('').parts |
| 15 | + output_path = (Path('output') / schema_path).with_suffix('.json') |
| 16 | + |
| 17 | + simulation_round = schema_path_components[1] |
| 18 | + product = schema_path_components[2] |
| 19 | + if product.endswith('InputData'): |
| 20 | + category = schema_path_components[3] |
| 21 | + sector = None |
| 22 | + else: |
| 23 | + category = None |
| 24 | + sector = schema_path_components[3] |
| 25 | + |
| 26 | + # read schema template |
| 27 | + schema_template = read_yaml_file(schema_path) |
| 28 | + |
| 29 | + # create schema dict |
| 30 | + schema = { |
| 31 | + '$schema': 'http://json-schema.org/draft-07/schema#', |
| 32 | + '$id': URL + schema_path.as_posix(), |
| 33 | + 'commit': get_commit_hash() |
| 34 | + } |
| 35 | + schema.update(schema_template) |
| 36 | + |
| 37 | + # loop over properties/specifiers/properties and add enums from definition files |
| 38 | + for identifier, properties in schema['properties']['specifiers']['properties'].items(): |
| 39 | + if identifier in definitions: |
| 40 | + if identifier not in EXCLUDE: |
| 41 | + rows = definitions[identifier] |
| 42 | + enum = [] |
| 43 | + if product.endswith('InputData'): |
| 44 | + for row in filter_rows(rows, simulation_round, product, category=category): |
| 45 | + enum.append(row.get('specifier_file') or row.get('specifier')) |
| 46 | + elif product == 'DerivedOutputData': |
| 47 | + for row in filter_rows(rows, simulation_round, product): |
| 48 | + enum.append(row.get('specifier_file') or row.get('specifier')) |
| 49 | + else: |
| 50 | + for row in filter_rows(rows, simulation_round, product, sector=sector): |
| 51 | + enum.append(row.get('specifier_file') or row.get('specifier')) |
| 52 | + |
| 53 | + properties['enum'] = list(set(enum)) |
| 54 | + |
| 55 | + # write json schema |
| 56 | + write_json(output_path, schema) |
59 | 57 |
|
60 | 58 |
|
61 | 59 | if __name__ == "__main__": |
|
0 commit comments