Skip to content

Commit f2596c5

Browse files
authored
Update config_setup.py
optimized
1 parent ae32f28 commit f2596c5

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

config_setup.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,52 @@
44
CONFIG_FILE_PATH = "config.toml"
55

66

7-
def save_credentials_to_config(leetcode_session, csrf_token):
8-
config_data = {"LEETCODE_SESSION": leetcode_session, "CSRF_TOKEN": csrf_token}
7+
def update_config(key_value_dict):
8+
if os.path.exists(CONFIG_FILE_PATH):
9+
with open(CONFIG_FILE_PATH, "r") as config_file:
10+
config_data = toml.load(config_file)
11+
config_data.update(key_value_dict)
12+
else:
13+
config_data = key_value_dict
14+
915
with open(CONFIG_FILE_PATH, "w") as config_file:
1016
toml.dump(config_data, config_file)
1117

1218

13-
def load_credentials_from_config():
19+
def load_config_from_file():
1420
if os.path.exists(CONFIG_FILE_PATH):
1521
with open(CONFIG_FILE_PATH, "r") as config_file:
16-
config_data = toml.load(config_file)
17-
return config_data.get("LEETCODE_SESSION"), config_data.get("CSRF_TOKEN")
18-
return None, None
22+
return toml.load(config_file)
23+
return {}
24+
25+
26+
def save_credentials_to_config(leetcode_session, csrf_token):
27+
config_data = {
28+
"LEETCODE_SESSION": leetcode_session,
29+
"CSRF_TOKEN": csrf_token
30+
}
31+
update_config(config_data)
32+
33+
34+
def load_credentials_from_config():
35+
config_data = load_config_from_file()
36+
return config_data.get("LEETCODE_SESSION"), config_data.get("CSRF_TOKEN")
1937

2038

2139
def load_user_data_from_config():
22-
if os.path.exists(CONFIG_FILE_PATH):
23-
with open(CONFIG_FILE_PATH, "r") as config_file:
24-
config_data = toml.load(config_file)
25-
return config_data.get("USER_LANG", "").lower() , config_data.get("EDITOR_CLI", "").lower()
26-
return None
40+
config_data = load_config_from_file()
41+
return config_data.get("USER_LANG", "").lower(), config_data.get("EDITOR_CLI", "").lower()
2742

2843

2944
def save_user_data_to_config(user_lang):
3045
config_data = {"USER_LANG": user_lang}
31-
if os.path.exists(CONFIG_FILE_PATH):
32-
with open(CONFIG_FILE_PATH, "r") as config_file:
33-
existing_config_data = toml.load(config_file)
34-
existing_config_data.update(config_data)
35-
config_data = existing_config_data
36-
with open(CONFIG_FILE_PATH, "w") as config_file:
37-
toml.dump(config_data, config_file)
46+
update_config(config_data)
47+
48+
49+
def save_user_path_to_config(path):
50+
config_data = {"LEETCODE_PATH": path}
51+
update_config(config_data)
52+
53+
def load_user_path_from_config():
54+
config_data = load_config_from_file()
55+
return config_data.get("LEETCODE_PATH", "")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy