-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.py
More file actions
82 lines (72 loc) · 2.92 KB
/
Config.py
File metadata and controls
82 lines (72 loc) · 2.92 KB
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
75
76
77
78
79
80
81
82
from pywebio.input import *
from pywebio.output import *
import os
import json
config_path = 'config/config.json'
def GetConfig():
try:
if os.path.exists(config_path):
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
if not set(['ESURL', 'INDEX', 'KEY', 'BASE', 'PATH']).issubset(config.keys()):
toast("⚠️ 配置文件损坏,重新初始化配置", color='error')
config = ChangeConfig(config)
else:
toast("⚠️ 配置文件丢失,重新初始化配置", color='error')
config = ChangeConfig(config)
except json.JSONDecodeError:
toast("⚠️ 配置文件损坏,使重新初始化配置", color='error')
config = ChangeConfig(config)
return config
def FormateConfig():
os.makedirs('config', exist_ok=True)
default_config = {
'ESURL': 'http://localhost:9200',
'INDEX': 'model',
'KEY': 'your-api-key',
'BASE': 'https://api.openai.com/v1',
'PATH': './log'
}
try:
if os.path.exists(config_path):
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
if not set(['ESURL', 'INDEX', 'KEY', 'BASE', 'PATH']).issubset(config.keys()):
toast("⚠️ 配置文件损坏,使用默认设置", color='error')
config = default_config
else:
toast("⚠️ 配置文件丢失,使用默认设置", color='error')
config = default_config
except json.JSONDecodeError:
toast("⚠️ 配置文件损坏,使用默认设置", color='error')
config = default_config
return config
def ChangeConfig(config):
config = input_group(
"系统初始化设置",
[
input("ElasticSearch 地址", name="ESURL", value=config['ESURL']),
input("索引名称", name="INDEX", value=config['INDEX']),
input("API Key", name="KEY", type=PASSWORD, value=config['KEY']),
input("Base URL", name="BASE", value=config['BASE']),
input("导出文件路径", name="PATH", value=config['PATH']),
checkbox(name="SAVE", options=[{"label": "保存这些设置", "value": "save", "selected": True}])
]
)
toast("✅ 配置文件修改成功!")
if "save" in config['SAVE']:
config.pop('SAVE')
config['FIRST'] = False
with open(config_path, 'w', encoding='utf-8') as f:
json.dump(config, f, indent=4)
toast("✅ 配置文件保存成功!")
return config
def SetConfig():
config = FormateConfig()
return ChangeConfig(config)
def CheckFirst():
if os.path.exists(config_path):
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
return config.get('FIRST') is False
return True