| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
# Copyright © 2024 Intel Corporation |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
import argparse |
| 7 |
import typing as T |
| 8 |
|
| 9 |
if T.TYPE_CHECKING: |
| 10 |
class Arguments(T.Protocol): |
| 11 |
|
| 12 |
input: str |
| 13 |
output: str |
| 14 |
replacements: T.List[T.Tuple[str, str]] |
| 15 |
|
| 16 |
|
| 17 |
def process(txt: str, replacements: T.List[T.Tuple[str, str]]) -> str: |
| 18 |
for k, v in replacements: |
| 19 |
txt = txt.replace(k, v) |
| 20 |
return txt |
| 21 |
|
| 22 |
|
| 23 |
def split_arg(arg: str) -> T.Tuple[str, str]: |
| 24 |
args = arg.split('=', maxsplit=1) |
| 25 |
assert len(args) == 2, 'Did not get the right number of args?' |
| 26 |
return T.cast('T.Tuple[str, str]', tuple(args)) |
| 27 |
|
| 28 |
|
| 29 |
def main() -> None: |
| 30 |
parser = argparse.ArgumentParser() |
| 31 |
parser.add_argument('input') |
| 32 |
parser.add_argument('output') |
| 33 |
parser.add_argument('--replace', action='append', required=True, dest='replacements', type=split_arg) |
| 34 |
args = T.cast('Arguments', parser.parse_args()) |
| 35 |
|
| 36 |
with open(args.input, 'r', encoding='utf-8') as f: |
| 37 |
content = f.read() |
| 38 |
|
| 39 |
content = process(content, args.replacements) |
| 40 |
|
| 41 |
with open(args.output, 'w', encoding='utf-8') as f: |
| 42 |
f.write(content) |
| 43 |
|
| 44 |
if __name__ == "__main__": |
| 45 |
main() |