|
1 | | -#! /usr/bin/env python |
| 1 | +#! /usr/bin/env python |
2 | 2 | """Bread and butter inversion of global example number to example sug mapping.""" |
3 | 3 | import json |
4 | 4 | import pathlib |
5 | 5 |
|
6 | 6 | ETC_PATH = pathlib.Path('etc') |
7 | | -IN_PATH = ETC_PATH / 'example-global-to-local.json' |
8 | | -OUT_PATH = ETC_PATH / 'example-local-to-global.json' |
| 7 | +G2L_PATH = ETC_PATH / 'example-global-to-local.json' |
| 8 | +L2G_PATH = ETC_PATH / 'example-local-to-global.json' |
| 9 | + |
| 10 | +ENCODING = 'utf-8' |
| 11 | +ENC_ERRS = 'ignore' |
| 12 | +NL = '\n' |
9 | 13 |
|
10 | 14 | if not ETC_PATH.is_dir(): |
11 | 15 | raise RuntimeError('Please execute me inside csaf_2.1/prose/edit/ because I am a simple tool') |
12 | 16 |
|
13 | | -with open(IN_PATH, 'rt', encoding='utf-8') as handle: |
| 17 | +with open(G2L_PATH, 'rt', encoding=ENCODING, errors=ENC_ERRS) as handle: |
14 | 18 | data = json.load(handle) |
15 | 19 |
|
16 | | -inverted = {v: k for k, v in data.items()} |
17 | | -ordered = {k: inverted[k] for k in sorted(inverted)} |
| 20 | +marker_value = "4321" |
| 21 | +marker_seen = False |
| 22 | +auto_serial = 1 |
| 23 | +cleansed = {} |
| 24 | +for k, v in data.items(): |
| 25 | + if not marker_seen: |
| 26 | + if k == marker_value: |
| 27 | + marker_seen = True |
| 28 | + cleansed[marker_value] = v |
| 29 | + continue |
| 30 | + cleansed[str(auto_serial)] = v |
| 31 | + auto_serial += 1 |
| 32 | + |
| 33 | +with open(G2L_PATH, 'wt', encoding=ENCODING, errors=ENC_ERRS) as handle: |
| 34 | + json.dump(cleansed, handle, indent=2) |
| 35 | + handle.write(NL) # For POSIX compliance |
| 36 | + |
| 37 | +inverted = {v: k for k, v in cleansed.items()} |
| 38 | +ordered = { |
| 39 | + 'Please do not edit manually!': ( |
| 40 | + "Instead, call 'make invert-examples' inside the clone-root/csaf_2.1/prose/edit folder." |
| 41 | + ) |
| 42 | +} |
| 43 | +for k in sorted(inverted): |
| 44 | + ordered[k] = inverted[k] |
18 | 45 |
|
19 | | -with open(OUT_PATH, 'wt', encoding='utf-8') as handle: |
| 46 | +with open(L2G_PATH, 'wt', encoding=ENCODING, errors=ENC_ERRS) as handle: |
20 | 47 | json.dump(ordered, handle, indent=2) |
| 48 | + handle.write(NL) # For POSIX compliance |
0 commit comments