-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
69 lines (57 loc) · 1.81 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
"""
Common utilities.
"""
import json
import yaml
from app import app
PATH_CONTENT = 'content'
def save_as_json(file_name, data):
""" Saves the provided data as a JSON file """
try:
with open(file_name, 'w', encoding='utf8') as fout:
json.dump(data , fout, indent=4, sort_keys=True, ensure_ascii=False)
except Exception as e:
app.logger.error("saving file '{}' - {}".format(file_name, str(e)))
raise
def save_as_jsonl(file_name, dicts):
""" Saves a list of dictionaries as JSONL file """
try:
with open(file_name, 'w') as fout:
for d in dicts:
json.dump(d, fout)
fout.write('\n')
except Exception as e:
app.logger.error("saving file '{}' - {}".format(file_name, str(e)))
raise
def read_json(file_name):
""" Reads a JSON file and returns its content """
try:
with open(file_name, 'r', encoding='utf8') as fin:
data = json.load(fin)
except Exception as e:
app.logger.error("reading file '{}' - {}".format(file_name, str(e)))
raise
else:
return data
def read_yaml(file_name):
""" Reads a YAML file and returns its content """
try:
with open(file_name, 'r') as fin:
data = yaml.safe_load(fin)
except Exception as e:
app.logger.error("reading file '{}' - {}".format(file_name, str(e)))
raise
else:
return data
def get_content(lang, keys):
""" Gets a content in the corresponding language """
file_name = '{}/{}.yaml'.format(PATH_CONTENT, lang)
data = read_yaml(file_name)
try:
for k in keys:
data = data[k]
except Exception as e:
app.logger.error("in '{}' content - {}".format(lang, str(e)))
raise
else:
return data