-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_config.py
More file actions
165 lines (146 loc) · 4.52 KB
/
Copy pathload_config.py
File metadata and controls
165 lines (146 loc) · 4.52 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import yaml
import os
def init_application():
"""初始化应用程序,确保必要的文件和目录存在"""
# 确保必要的目录存在
directories = ["logs"]
for directory in directories:
if not os.path.exists(directory):
try:
os.makedirs(directory, exist_ok=True)
print(f"已创建目录: {directory}")
except Exception as e:
print(f"创建目录 {directory} 失败: {e}")
# 检查配置文件是否存在
if not os.path.exists("config.yml"):
print("配置文件 config.yml 不存在,正在创建默认配置文件...")
return create_default_config()
return True
def create_default_config():
"""创建默认配置文件"""
default_config = {
"captcha": {
"captcha_retry_times": 5,
"device": ["CUDA"],
"enable": True,
"proxy_retry_delay": 1,
"save_failed_img": False,
"save_failed_img_path": "",
},
"log": {
"backup_count": 7,
"dir": "logs",
"file_head": "ymicp",
"output_console": True,
"save_log": False,
},
"proxy": {
"extra_api": {
"auto_maintenace": True,
"check_proxy": True,
"check_proxy_num": 20,
"enable": False,
"extra_interval": 3,
"pool_num": 100,
"proxy_timeout": 0.5,
"timeout": 100,
"timeout_drop": 8,
"url": None,
},
"static_proxy": {
"enable": False,
"host": "127.0.0.1",
"password": "",
"port": 8800,
"type": "http",
"username": "",
},
},
"query": {
"captcha_retry_times": 5,
"coding_code": "auto",
"coding_show": False,
"default_page_size": 20,
"max_page_size": 100,
"proxy_retry_delay": 1,
"retry_times": 2,
"risk_delay_enable": False,
"risk_delay_seconds": 1,
},
"risk_avoidance": {
"allow_type": [
"web",
"app",
"mapp",
"kapp",
"bweb",
"bapp",
"bmapp",
"bkapp",
],
"prohibit_suffix": [],
},
"system": {
"detail_concurrency": 5,
"host": "0.0.0.0",
"http_client_timeout": 5,
"port": 16181,
"web_ui": True,
},
}
try:
with open("config.yml", "w", encoding="utf-8") as file:
yaml.dump(
default_config,
file,
default_flow_style=False,
allow_unicode=True,
indent=2,
)
print("已创建默认配置文件 config.yml")
return True
except Exception as e:
print(f"创建默认配置文件失败: {e}")
return False
# 初始化应用程序
init_application()
try:
class Config:
def __init__(self, **kwargs):
for key, value in kwargs.items():
# 如果值是字典,则递归转换为对象
if isinstance(value, dict):
value = Config(**value)
setattr(self, key, value)
def __repr__(self):
return str(self.__dict__)
def __getattr__(self, name):
return None
def load_config(file_path):
with open(file_path, "r", encoding="utf-8") as file:
data = yaml.safe_load(file)
return Config(**data)
config = load_config("config.yml")
print("配置文件加载成功")
except FileNotFoundError:
print("配置文件 config.yml 不存在,正在创建默认配置文件...")
if create_default_config():
try:
config = load_config("config.yml")
print("默认配置文件创建成功并已加载")
except Exception as e:
print(f"加载默认配置文件失败: {e}")
import sys
sys.exit()
else:
print("无法创建默认配置文件,程序退出")
import sys
sys.exit()
except yaml.YAMLError as e:
print(f"配置文件格式错误: {e}")
import sys
sys.exit()
except Exception as e:
print(f"加载配置文件失败: {e}")
import sys
sys.exit()