-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.py
74 lines (60 loc) · 2.52 KB
/
settings.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
70
71
72
73
74
import argparse
import json
import os
from subject import Subject
def set_http_proxy(address, port):
os.environ["HTTP_PROXY"] = 'http://127.0.0.1:7890'
os.environ["HTTPS_PROXY"] = 'https://127.0.0.1:7890'
class Settings:
def __init__(self):
self.pandoc_path: str
self.config_path: str
parser = argparse.ArgumentParser(description="Water Water WWW.. commandline")
parser.add_argument("-c", "--config", type=str, help="json configuration directory")
args = parser.parse_args()
self.config_path = args.config
self.pandoc_path = os.path.join(self.config_path, 'pandoc')
with open(os.path.join(self.config_path, 'config.json'), 'r') as file:
self.config_data = json.load(file)
# Access the individual values
self.api_key = self.config_data['gpt3.5-turbo.key']
self.proxy_address = self.config_data['proxy.address']
self.proxy_port = self.config_data['proxy.port']
# self.set_http_proxy(self.proxy_address,self.proxy_port)
self.verify_default_subject_name()
def get_pandoc_path(self):
return self.pandoc_path
def get_api_key(self):
return self.api_key
def get_default_subject_name(self) -> str:
return self.config_data['subject.default']
def verify_default_subject_name(self):
name = self.get_default_subject_name()
items = self.get_subject_list()
flag = False
random_name = ""
for item in items:
if name == item.get_name():
flag = True
if not flag:
random_name = item.get_name()
if not flag:
self.set_default_subject(random_name)
def get_default_subject(self):
with open(os.path.join(self.config_path, 'subjects.json'), 'r') as file:
data = json.load(file)
name = self.get_default_subject_name()
return Subject(name, data[name])
def set_default_subject(self, name: str):
self.config_data['subject.default'] = name
updated_json_data = json.dumps(self.config_data, indent=2)
with open(os.path.join(self.config_path, 'config.json'), "w") as file:
file.write(updated_json_data)
file.flush()
def get_subject_list(self):
subjects = []
with open(os.path.join(self.config_path, 'subjects.json'), 'r') as file:
data = json.load(file)
for name, fields in data.items():
subjects.append(Subject(name, fields))
return subjects