|
4 | 4 | CONFIG_FILE_PATH = "config.toml"
|
5 | 5 |
|
6 | 6 |
|
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 | + |
9 | 15 | with open(CONFIG_FILE_PATH, "w") as config_file:
|
10 | 16 | toml.dump(config_data, config_file)
|
11 | 17 |
|
12 | 18 |
|
13 |
| -def load_credentials_from_config(): |
| 19 | +def load_config_from_file(): |
14 | 20 | if os.path.exists(CONFIG_FILE_PATH):
|
15 | 21 | 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") |
19 | 37 |
|
20 | 38 |
|
21 | 39 | 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() |
27 | 42 |
|
28 | 43 |
|
29 | 44 | def save_user_data_to_config(user_lang):
|
30 | 45 | 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