summaryrefslogtreecommitdiff
path: root/test cases/fortran/7 generated/gen.py
blob: 86d9bf71226106da9baf3bbe1eb5300a371f2927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
# Copyright © 2024 Intel Corporation

from __future__ import annotations
import argparse
import typing as T

if T.TYPE_CHECKING:
    class Arguments(T.Protocol):

        input: str
        output: str
        replacements: T.List[T.Tuple[str, str]]


def process(txt: str, replacements: T.List[T.Tuple[str, str]]) -> str:
    for k, v in replacements:
        txt = txt.replace(k, v)
    return txt


def split_arg(arg: str) -> T.Tuple[str, str]:
    args = arg.split('=', maxsplit=1)
    assert len(args) == 2, 'Did not get the right number of args?'
    return T.cast('T.Tuple[str, str]', tuple(args))


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument('input')
    parser.add_argument('output')
    parser.add_argument('--replace', action='append', required=True, dest='replacements', type=split_arg)
    args = T.cast('Arguments', parser.parse_args())

    with open(args.input, 'r', encoding='utf-8') as f:
        content = f.read()

    content = process(content, args.replacements)

    with open(args.output, 'w', encoding='utf-8') as f:
        f.write(content)

if __name__ == "__main__":
    main()