-
Notifications
You must be signed in to change notification settings - Fork 108
/
translation-diff.py
executable file
·43 lines (37 loc) · 1.34 KB
/
translation-diff.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
#!/usr/bin/env python3
import sys
import glob
import os
import json
import io
def onDiffFromList(a,b):
return "No difference" if a == b else "Missing {} items".format(a-b)
def onExtractFromObj(d):
return set(d.keys())
def onDiffFromObj(a,b):
diff = a-b
return "No difference" if len(diff) == 0 else "Missing items: {}".format(diff)
handlers = \
{ dict: [onExtractFromObj, onDiffFromObj ],
list: [len, onDiffFromList ]
}
if __name__ == '__main__':
if len( sys.argv ) == 3:
dirA, dirB = sys.argv[1:]
filesA = set(glob.glob(dirA + "/*.json"))
for fileA in filesA:
fileName = os.path.basename(fileA)
fileB = os.path.join(dirB,fileName)
if os.path.isfile( fileB ):
def loadFile(fn):
with io.open(fn,encoding='utf_8_sig') as f:
return json.load(f)
print("Comparing {} and {}".format(fileA,fileB))
jsonA, jsonB = loadFile(fileA), loadFile(fileB)
extract, diff = handlers[jsonA.__class__]
resultA, resultB = extract(jsonA), extract(jsonB)
print(" Difference: {}".format( diff(resultA,resultB) ) )
else:
print("Missing file: {}".format(fileName))
else:
print( "{} <dirA> <dirB>".format(sys.argv[0]))