|
1 | 1 | import os
|
2 | 2 | import re
|
| 3 | +import yaml |
3 | 4 |
|
4 | 5 |
|
5 |
| -with open('config.yaml.in', 'r', encoding='utf-8') as templ: |
6 |
| - lines = templ.readlines() |
| 6 | +config = yaml.load( |
| 7 | + open("config.yaml.in", "r", encoding="utf-8"), Loader=yaml.SafeLoader |
| 8 | +) |
7 | 9 |
|
8 |
| -pattern = re.compile('< content\/\w\w\/\w*.yaml >') |
9 |
| -with open('config.yaml', 'w', encoding='utf-8') as f: |
10 |
| - for line in lines: |
11 |
| - match = pattern.search(line) |
12 |
| - if match: |
13 |
| - with open(match.group()[2:-2], 'r', encoding='utf-8') as f2: |
14 |
| - for f2_line in f2.readlines(): |
15 |
| - # indent to get correct yaml formatting |
16 |
| - f.write(' ' + f2_line) |
17 |
| - elif line.startswith('disableLanguages'): |
18 |
| - if os.environ.get('NUMPYORG_WITH_TRANSLATIONS'): |
19 |
| - line = "#" + line |
20 | 10 |
|
21 |
| - f.write(line) |
| 11 | +def merge_dicts(d1, d2): |
| 12 | + for key, value in d2.items(): |
| 13 | + if key in d1: |
| 14 | + if isinstance(value, list): |
| 15 | + d1[key].extend(value) |
| 16 | + elif isinstance(value, dict): |
| 17 | + merge_dicts(d1[key], value) |
22 | 18 | else:
|
23 |
| - f.write(line) |
| 19 | + d1[key] = value |
| 20 | + |
| 21 | + return d1 |
| 22 | + |
| 23 | + |
| 24 | +def include_files(d): |
| 25 | + external = {} |
| 26 | + for key, val in d.items(): |
| 27 | + if isinstance(val, dict): |
| 28 | + d[key] = include_files(val) |
| 29 | + elif key == "include-files": |
| 30 | + for otherfile in val: |
| 31 | + external_data = yaml.load( |
| 32 | + open(otherfile, "r", encoding="utf-8"), Loader=yaml.SafeLoader |
| 33 | + ) |
| 34 | + external = merge_dicts(external, external_data) |
| 35 | + |
| 36 | + d.pop("include-files", None) |
| 37 | + return {**d, **external} |
| 38 | + |
| 39 | + |
| 40 | +config = include_files(config) |
| 41 | +if os.environ.get("NUMPYORG_WITH_TRANSLATIONS"): |
| 42 | + del config["disableLanguages"] |
| 43 | + |
| 44 | + |
| 45 | +yaml.dump(config, open('config.yaml', 'w', encoding='utf-8'), sort_keys=False) |
0 commit comments