-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld_json.py
47 lines (31 loc) · 1017 Bytes
/
world_json.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
import re
import itertools
import json
from dirs import dest
_wj = dest / 'serve/world.json'
def delete():
rewrite({})
def load():
outData = {}
try:
with open(_wj) as worldJsonPrev:
outData = json.load(worldJsonPrev)
except IOError:
pass
return outData
def rewrite(outData):
with open(_wj, 'w') as worldJson:
json.dump(outData, worldJson, indent=2, sort_keys=True)
def dump():
print(open(_wj).read())
def json_serialize_with_pretty_matrices(obj):
j = json.dumps(obj, indent=2, sort_keys=True)
def reindent_mat(match: re.Match):
pre, cells, post = match.groups()
seen = itertools.count(1)
def maybe_break(m: re.Match) -> str:
post = m.groups()[0] if next(seen) % 4 == 0 else ' '
return ',' + post
cells = re.sub(r',(\n\s*)', maybe_break, cells)
return pre + cells + post
return re.sub(r'((?:_baby|_blender)": \[)(.*?)(\s*\])', reindent_mat, j, flags=re.DOTALL)