-
Notifications
You must be signed in to change notification settings - Fork 9
/
mergeIHasAppMappings.py
executable file
·76 lines (61 loc) · 2.09 KB
/
mergeIHasAppMappings.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
71
72
73
74
75
76
#!/usr/bin/env python
from pprint import pprint
import json
import sys
import optparse
def print_stats(mappings):
all_schemes = []
all_app_ids = []
for scheme, app_ids in mappings.items():
all_schemes.append(scheme)
all_app_ids.extend(app_ids)
all_app_ids = set(all_app_ids)
print "schemes: %d, apps: %d" % (len(all_schemes), len(all_app_ids))
def loadMappings(filename):
print "loading %s" % filename
mappings = json.load(open(filename, 'r'))
if isinstance(mappings, dict):
result = mappings
else:
result = {}
for mapping in mappings:
for scheme in mapping['url_schemes']:
ids = result.get(scheme, [])
ids.append(mapping['item_id'])
result[scheme] = ids
print_stats(result)
return result
def mergeMappings(mappings_list):
print "merging %d mappings" % len(mappings_list)
merged = {}
for mappings in mappings_list:
for scheme, ids in mappings.items():
if scheme != "":
merged_ids = merged.get(scheme, [])
merged_ids.extend(ids)
merged_ids = list(set(merged_ids))
merged_ids.sort()
merged[scheme] = merged_ids
return merged
def get_options():
optp = optparse.OptionParser('usage: %prog [options] file1 file2 .. fileN"')
optp.add_option('-o', '--outputfile', action='store', dest='output_file', default=sys.stdout,
help='location to write the JSON to (default: stdout)')
args = optp.parse_args()
if len(args[1]) <= 0:
optp.print_help()
sys.exit(1)
return args
def main():
args = get_options()
mappings_list = [loadMappings(f) for f in args[1]]
merged = mergeMappings(mappings_list)
print_stats(merged)
# sorted keys and line breaks at start, end and per scheme
s= json.dumps(merged, sort_keys=True).replace('],', '],\n').replace('{', '{\n ').replace('}','\n}')
fo = args[0].output_file
if isinstance(fo, str):
fo = open(fo, 'w')
fo.writelines(s)
if __name__ == '__main__':
main()