-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_import.py
More file actions
executable file
·60 lines (57 loc) · 1.74 KB
/
json_import.py
File metadata and controls
executable file
·60 lines (57 loc) · 1.74 KB
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
#!/usr/bin/env python
import os
import sys
import json
import settings
def import_changes(location, path):
p = path
l = []
for i in range(3):
p, last = os.path.split(p)
l.append(last)
objs = os.path.join(location, "objects")
dest = os.path.join(objs, *reversed(l))
with open(path) as f:
d = json.load(f)
exists = os.path.exists(dest)
if exists:
with open(dest) as f:
obj = json.load(f)
else:
obj = d
for k, v in d.items():
if k in obj:
obj[k].update(v)
else:
obj[k] = v
if "_delete" in v:
for f, x in v["_delete"].items():
if isinstance(obj[k][f], dict):
for t in x:
del obj[k][f][t]
else:
obj[k][f] = sorted(t for t in obj[k][f] if t not in x)
del obj[k]["_delete"]
if "_add" in v:
for f, x in v["_add"].items():
if isinstance(obj[k][f], dict):
obj[k][f].update(x)
else:
obj[k][f] += x
obj[k][f].sort()
del obj[k]["_add"]
for f, x in obj[k].items():
if x is None:
del obj[k][f]
with open(dest, "w") as f:
json.dump(obj, f, indent = 4, sort_keys = True)
if not exists:
algo, uid = l[2], l[1] + l[0]
for a, u in d["object"]["unique_id"].items():
if (a, u) == (algo, uid):
continue
os.symlink(os.path.join("..", "..", *reversed(l)),
os.path.join(objs, a, u[:2], u[2:]))
if __name__ == "__main__":
for path in sys.argv[1:]:
import_changes(settings.DATA_REPO, path)