| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import sys |
| 5 |
|
| 6 |
|
| 7 |
TOFU_RISK_PREFIX = "# @tofu-risk:" |
| 8 |
|
| 9 |
|
| 10 |
def extract_tofu_risk(input_path, output_path): |
| 11 |
with open(input_path, encoding="utf-8") as input_file: |
| 12 |
lines = input_file.readlines() |
| 13 |
|
| 14 |
extracted = [] |
| 15 |
for index, line in enumerate(lines): |
| 16 |
if not line.startswith(TOFU_RISK_PREFIX): |
| 17 |
continue |
| 18 |
if index + 1 >= len(lines): |
| 19 |
raise ValueError(f"Missing mapping after {TOFU_RISK_PREFIX} at line {index + 1}") |
| 20 |
mapping = lines[index + 1] |
| 21 |
if not mapping.strip() or mapping.startswith("#"): |
| 22 |
raise ValueError(f"Expected mapping after {TOFU_RISK_PREFIX} at line {index + 1}") |
| 23 |
key, values_text = mapping.rstrip("\n").split("\t", 1) |
| 24 |
values = values_text.split() |
| 25 |
if values and values[0] == key: |
| 26 |
values = values[1:] |
| 27 |
if not values: |
| 28 |
raise ValueError(f"Expected extension mapping after {TOFU_RISK_PREFIX} at line {index + 1}") |
| 29 |
extracted.append(f"{key}\t{' '.join(values)}\n") |
| 30 |
|
| 31 |
with open(output_path, "w", encoding="utf-8") as output_file: |
| 32 |
output_file.writelines(extracted) |
| 33 |
|
| 34 |
|
| 35 |
if len(sys.argv) != 3: |
| 36 |
print("Extract mappings following @tofu-risk annotations") |
| 37 |
print(("Usage: ", sys.argv[0], "[input] [output]")) |
| 38 |
exit(1) |
| 39 |
|
| 40 |
extract_tofu_risk(sys.argv[1], sys.argv[2]) |