-
Notifications
You must be signed in to change notification settings - Fork 52
/
lang.py
executable file
·70 lines (56 loc) · 1.74 KB
/
lang.py
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
63
64
65
66
67
68
69
70
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json
import sys
import os.path
from collections import OrderedDict
VERBOSE = True
def eprint(*args, **kwargs):
print(*args, file = sys.stderr, **kwargs)
if __name__ == "__main__":
if len(sys.argv) != 4:
eprint("Usage: {0} <reference> <input> <output>".format(sys.argv[0]))
exit(1)
REFERENCE = sys.argv[1]
INPUT = sys.argv[2]
OUTPUT = sys.argv[3]
RefFP = open(REFERENCE, "rb")
RefTXT = RefFP.read().decode("utf-8-sig")
RefFP.close()
Ref = json.loads(RefTXT, object_pairs_hook = OrderedDict, strict = False)
InFP = open(INPUT, "rb")
InTXT = InFP.read().decode("utf-8-sig")
InFP.close()
In = json.loads(InTXT, object_pairs_hook = OrderedDict, strict = False)
Out = OrderedDict()
NotFound = 0
Excessive = 0
for k, v in Ref.items():
if k in In and not k in Out:
Out[k] = In[k]
for k, v in Ref.items():
if not k in In:
if VERBOSE:
eprint("[NOT FOUND] \"{0}\"".format(k))
NotFound += 1
Out[k] = v
for k, v in In.items():
if not k in Ref:
if VERBOSE:
eprint("[EXCESSIVE] \"{0}\"".format(k))
Excessive += 1
OutTXT = json.dumps(Out, ensure_ascii = False, separators=(",\n", ":"))
OutTXT = OutTXT.replace("{", "{\n", 1)
OutTXT = "\n}".join(OutTXT.rsplit("}", 1))
OutFP = open(OUTPUT, "wb")
OutFP.write(OutTXT.encode("utf-8-sig"))
OutFP.close()
eprint("--- Statistics ---")
eprint("Reference: {0}".format(os.path.basename(REFERENCE)))
eprint("Reference items: {0}".format(len(Ref)))
eprint("Input: {0}".format(os.path.basename(INPUT)))
eprint("Input items: {0}".format(len(In)))
eprint("Output items: {0}".format(len(Out)))
eprint("Found: {0}".format(len(Out) - NotFound))
eprint("Not found: {0}".format(NotFound))
eprint("Excessive: {0}".format(Excessive))