-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_setup.py
55 lines (38 loc) · 1.47 KB
/
config_setup.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
import toml
import os
CONFIG_FILE_PATH = "config.toml"
def update_config(key_value_dict):
if os.path.exists(CONFIG_FILE_PATH):
with open(CONFIG_FILE_PATH, "r") as config_file:
config_data = toml.load(config_file)
config_data.update(key_value_dict)
else:
config_data = key_value_dict
with open(CONFIG_FILE_PATH, "w") as config_file:
toml.dump(config_data, config_file)
def load_config_from_file():
if os.path.exists(CONFIG_FILE_PATH):
with open(CONFIG_FILE_PATH, "r") as config_file:
return toml.load(config_file)
return {}
def save_credentials_to_config(leetcode_session, csrf_token):
config_data = {
"LEETCODE_SESSION": leetcode_session,
"CSRF_TOKEN": csrf_token
}
update_config(config_data)
def load_credentials_from_config():
config_data = load_config_from_file()
return config_data.get("LEETCODE_SESSION"), config_data.get("CSRF_TOKEN")
def load_user_data_from_config():
config_data = load_config_from_file()
return config_data.get("USER_LANG", "").lower(), config_data.get("EDITOR_CLI", "").lower()
def save_user_data_to_config(user_lang):
config_data = {"USER_LANG": user_lang}
update_config(config_data)
def save_user_path_to_config(path):
config_data = {"LEETCODE_PATH": path}
update_config(config_data)
def load_user_path_from_config():
config_data = load_config_from_file()
return config_data.get("LEETCODE_PATH", "")