forked from TandoorRecipes/open-tandoor-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
97 lines (73 loc) · 2.71 KB
/
utils.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import traceback
import json
from glob import glob
from pathlib import Path
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR_DATA = os.path.join(BASE_DIR, "data")
BASE_DIR_LOCALE = os.path.join(BASE_DIR, "locale")
BASE_DIR_BUILD = os.path.join(BASE_DIR, "build")
LOG_LEVEL = "NONE"
def log(level, msg):
if LOG_LEVEL.lower() == level.lower():
print(msg)
def load_data(datatype, language="base"):
log("debug", f"loading datatype {datatype}")
try:
f = open(
os.path.join(BASE_DIR_DATA, datatype, language, "data.json"),
encoding="UTF-8",
)
data = json.loads(f.read())
return data["data"]
except FileNotFoundError:
log("debug", f"no data found for {datatype} in version {language}")
return {}
def save_data(datatype, data, language="base"):
log("debug", f"saving datatype {datatype}")
data_to_store = {'data': data}
Path(os.path.join(BASE_DIR_DATA, datatype, language)).mkdir(parents=True, exist_ok=True)
with open(os.path.join(BASE_DIR_DATA, datatype, language, "data.json"), "w", encoding="UTF-8", ) as f:
f.write(json.dumps(data_to_store))
def load_schema():
log("debug", f"loading schema")
f = open(
os.path.join(BASE_DIR, "schema.json"),
encoding="UTF-8",
)
return json.loads(f.read())
def load_translations(language):
try:
log("debug", f"loading translations for {language}")
f = open(os.path.join(BASE_DIR_LOCALE, f"{language}.json"), encoding="UTF-8")
file_data = f.read()
if file_data == "":
return {}
else:
data = json.loads(file_data)
return data
except FileNotFoundError:
return {}
def save_translations(language, data):
log("debug", f"saving translations for {language}")
f = open(os.path.join(BASE_DIR_LOCALE, f"{language}.json"), "w", encoding="UTF-8")
f.write(json.dumps(data))
def save_build_version(version, data):
log("debug", f"saving translations for {version}")
f = open(os.path.join(BASE_DIR_BUILD, f"{version}.json"), "w", encoding="UTF-8")
f.write(json.dumps(data))
def get_available_versions():
versions = []
schema = load_schema()
for s in list(schema.keys()):
for f in os.scandir(os.path.join(BASE_DIR_DATA, s)):
if f.is_dir():
if not f.name in versions and not f.name == "base":
versions.append(f.name)
for f in os.scandir(BASE_DIR_LOCALE):
if not f.name in versions and not f.name == "base":
versions.append(f.name.replace('.json',''))
return versions
def get_available_datatypes():
schema = load_schema()
return list(schema.keys())