-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_config.py
64 lines (42 loc) · 1.58 KB
/
json_config.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
import json
import os
from utils import path_join, create_folder, basement
def get_current_dir():
return os.getcwd()
def get_project_config(path):
full_path = path_join(get_current_dir(), path)
create_folder(basement(full_path))
if not os.path.exists(full_path):
return None
with open(full_path, "r") as f:
return json.loads(f.read())
def write_project_config(path, cfg):
full_path = path_join(get_current_dir(), path)
create_folder(basement(full_path))
if not os.path.exists(full_path):
return None
with open(full_path, "w") as f:
json.dump(cfg, f, indent=4, ensure_ascii=False)
def init_project_config(path):
cfg = json.loads('{"font_size": 12, "indent": 20}')
full_path = path_join(get_current_dir(), path)
create_folder(basement(full_path))
with open(full_path, "w") as f:
json.dump(cfg, f, indent=4, ensure_ascii=False)
def set_font_size(path, font_type):
font_sizes = {"small": 10, "medium": 12, "large": 14}
cfg = get_project_config(path)
if cfg is None:
raise ValueError("Wrong config path: " + f"{path}")
if font_type not in font_sizes:
raise ValueError("Wrong font size")
cfg["font_size"] = font_sizes[font_type]
write_project_config(path, cfg)
def set_indent_size(path, indent): # indent in mm
cfg = get_project_config(path)
if cfg is None:
raise ValueError("Wrong config path: " + f"{path}")
if indent < 0:
raise ValueError("Indent can not be less than 0")
cfg["indent"] = indent
write_project_config(path, cfg)