File size: 1,644 Bytes
29b87da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import argparse
import json
from pathlib import Path


DEFAULTS = {
    "prerequisites": [],
    "yields": [],
    "patterns": [],
    "links": [],
}


def fix_file(in_path, out_path, log_path):
    total = 0
    fixed = 0

    with open(in_path, "r", encoding="utf-8") as src, open(out_path, "w", encoding="utf-8") as dst:
        for line in src:
            line = line.strip()
            if not line:
                continue
            total += 1
            obj = json.loads(line)
            changed = False
            for k, v in DEFAULTS.items():
                if k not in obj:
                    obj[k] = list(v)
                    changed = True
            if changed:
                fixed += 1
            dst.write(json.dumps(obj, ensure_ascii=False) + "\n")

    if log_path:
        Path(log_path).parent.mkdir(parents=True, exist_ok=True)
        with open(log_path, "w", encoding="utf-8") as f:
            f.write(
                json.dumps(
                    {
                        "input": str(in_path),
                        "output": str(out_path),
                        "total": total,
                        "fixed": fixed,
                    },
                    ensure_ascii=False,
                    indent=2,
                )
            )


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--in", dest="in_path", required=True)
    ap.add_argument("--out", dest="out_path", required=True)
    ap.add_argument("--log", dest="log_path", default="")
    args = ap.parse_args()
    fix_file(args.in_path, args.out_path, args.log_path)


if __name__ == "__main__":
    main()