ErisPulse 1.0.12__py3-none-any.whl → 1.0.14.dev1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ErisPulse/__init__.py +146 -139
- ErisPulse/__main__.py +694 -931
- ErisPulse/{envManager.py → db.py} +227 -227
- ErisPulse/logger.py +123 -48
- ErisPulse/raiserr.py +44 -0
- ErisPulse/util.py +1 -3
- {erispulse-1.0.12.dist-info → erispulse-1.0.14.dev1.dist-info}/METADATA +73 -96
- erispulse-1.0.14.dev1.dist-info/RECORD +11 -0
- {erispulse-1.0.12.dist-info → erispulse-1.0.14.dev1.dist-info}/WHEEL +1 -1
- ErisPulse/errors.py +0 -16
- erispulse-1.0.12.dist-info/RECORD +0 -11
- {erispulse-1.0.12.dist-info → erispulse-1.0.14.dev1.dist-info}/entry_points.txt +0 -0
- {erispulse-1.0.12.dist-info → erispulse-1.0.14.dev1.dist-info}/top_level.txt +0 -0
|
@@ -1,227 +1,227 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import json
|
|
3
|
-
import sqlite3
|
|
4
|
-
import importlib.util
|
|
5
|
-
from pathlib import Path
|
|
6
|
-
|
|
7
|
-
class EnvManager:
|
|
8
|
-
_instance = None
|
|
9
|
-
db_path = os.path.join(os.path.dirname(__file__), "config.db")
|
|
10
|
-
|
|
11
|
-
def __new__(cls, *args, **kwargs):
|
|
12
|
-
if not cls._instance:
|
|
13
|
-
cls._instance = super().__new__(cls)
|
|
14
|
-
cls._instance.dev_mode = kwargs.get('dev_mode', False)
|
|
15
|
-
return cls._instance
|
|
16
|
-
|
|
17
|
-
def __init__(self, dev_mode=False):
|
|
18
|
-
if not hasattr(self, "_initialized"):
|
|
19
|
-
self.dev_mode = dev_mode
|
|
20
|
-
self._init_db()
|
|
21
|
-
|
|
22
|
-
def _init_db(self):
|
|
23
|
-
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
|
|
24
|
-
|
|
25
|
-
conn = sqlite3.connect(self.db_path)
|
|
26
|
-
cursor = conn.cursor()
|
|
27
|
-
cursor.execute("""
|
|
28
|
-
CREATE TABLE IF NOT EXISTS config (
|
|
29
|
-
key TEXT PRIMARY KEY,
|
|
30
|
-
value TEXT NOT NULL
|
|
31
|
-
)
|
|
32
|
-
""")
|
|
33
|
-
cursor.execute("""
|
|
34
|
-
CREATE TABLE IF NOT EXISTS modules (
|
|
35
|
-
module_name TEXT PRIMARY KEY,
|
|
36
|
-
status INTEGER NOT NULL,
|
|
37
|
-
version TEXT,
|
|
38
|
-
description TEXT,
|
|
39
|
-
author TEXT,
|
|
40
|
-
dependencies TEXT,
|
|
41
|
-
optional_dependencies TEXT,
|
|
42
|
-
pip_dependencies TEXT
|
|
43
|
-
)
|
|
44
|
-
""")
|
|
45
|
-
conn.commit()
|
|
46
|
-
conn.close()
|
|
47
|
-
|
|
48
|
-
def get(self, key, default=None):
|
|
49
|
-
try:
|
|
50
|
-
with sqlite3.connect(self.db_path) as conn:
|
|
51
|
-
cursor = conn.cursor()
|
|
52
|
-
cursor.execute("SELECT value FROM config WHERE key = ?", (key,))
|
|
53
|
-
result = cursor.fetchone()
|
|
54
|
-
if result:
|
|
55
|
-
try:
|
|
56
|
-
return json.loads(result[0])
|
|
57
|
-
except json.JSONDecodeError:
|
|
58
|
-
return result[0]
|
|
59
|
-
return default
|
|
60
|
-
except sqlite3.OperationalError as e:
|
|
61
|
-
if "no such table" in str(e):
|
|
62
|
-
self._init_db()
|
|
63
|
-
return self.get(key, default)
|
|
64
|
-
else:
|
|
65
|
-
raise
|
|
66
|
-
|
|
67
|
-
def set(self, key, value):
|
|
68
|
-
serialized_value = json.dumps(value) if isinstance(value, (dict, list)) else str(value)
|
|
69
|
-
conn = sqlite3.connect(self.db_path)
|
|
70
|
-
cursor = conn.cursor()
|
|
71
|
-
cursor.execute("INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)", (key, serialized_value))
|
|
72
|
-
conn.commit()
|
|
73
|
-
conn.close()
|
|
74
|
-
|
|
75
|
-
def delete(self, key):
|
|
76
|
-
conn = sqlite3.connect(self.db_path)
|
|
77
|
-
cursor = conn.cursor()
|
|
78
|
-
cursor.execute("DELETE FROM config WHERE key = ?", (key,))
|
|
79
|
-
conn.commit()
|
|
80
|
-
conn.close()
|
|
81
|
-
|
|
82
|
-
def clear(self):
|
|
83
|
-
conn = sqlite3.connect(self.db_path)
|
|
84
|
-
cursor = conn.cursor()
|
|
85
|
-
cursor.execute("DELETE FROM config")
|
|
86
|
-
conn.commit()
|
|
87
|
-
conn.close()
|
|
88
|
-
|
|
89
|
-
def load_env_file(self):
|
|
90
|
-
env_file = Path("env.py")
|
|
91
|
-
if env_file.exists():
|
|
92
|
-
spec = importlib.util.spec_from_file_location("env_module", env_file)
|
|
93
|
-
env_module = importlib.util.module_from_spec(spec)
|
|
94
|
-
spec.loader.exec_module(env_module)
|
|
95
|
-
for key, value in vars(env_module).items():
|
|
96
|
-
if not key.startswith("__") and isinstance(value, (dict, list, str, int, float, bool)):
|
|
97
|
-
self.set(key, value)
|
|
98
|
-
def set_module_status(self, module_name, status):
|
|
99
|
-
with sqlite3.connect(self.db_path) as conn:
|
|
100
|
-
cursor = conn.cursor()
|
|
101
|
-
cursor.execute("""
|
|
102
|
-
UPDATE modules SET status = ? WHERE module_name = ?
|
|
103
|
-
""", (int(status), module_name))
|
|
104
|
-
conn.commit()
|
|
105
|
-
|
|
106
|
-
def get_module_status(self, module_name):
|
|
107
|
-
with sqlite3.connect(self.db_path) as conn:
|
|
108
|
-
cursor = conn.cursor()
|
|
109
|
-
cursor.execute("""
|
|
110
|
-
SELECT status FROM modules WHERE module_name = ?
|
|
111
|
-
""", (module_name,))
|
|
112
|
-
result = cursor.fetchone()
|
|
113
|
-
return bool(result[0]) if result else True
|
|
114
|
-
|
|
115
|
-
def set_all_modules(self, modules_info):
|
|
116
|
-
with sqlite3.connect(self.db_path) as conn:
|
|
117
|
-
cursor = conn.cursor()
|
|
118
|
-
for module_name, module_info in modules_info.items():
|
|
119
|
-
meta = module_info.get('info', {}).get('meta', {})
|
|
120
|
-
dependencies = module_info.get('info', {}).get('dependencies', {})
|
|
121
|
-
cursor.execute("""
|
|
122
|
-
INSERT OR REPLACE INTO modules (
|
|
123
|
-
module_name, status, version, description, author,
|
|
124
|
-
dependencies, optional_dependencies, pip_dependencies
|
|
125
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
126
|
-
""", (
|
|
127
|
-
module_name,
|
|
128
|
-
int(module_info.get('status', True)),
|
|
129
|
-
meta.get('version', ''),
|
|
130
|
-
meta.get('description', ''),
|
|
131
|
-
meta.get('author', ''),
|
|
132
|
-
json.dumps(dependencies.get('requires', [])),
|
|
133
|
-
json.dumps(dependencies.get('optional', [])),
|
|
134
|
-
json.dumps(dependencies.get('pip', []))
|
|
135
|
-
))
|
|
136
|
-
conn.commit()
|
|
137
|
-
|
|
138
|
-
def get_all_modules(self):
|
|
139
|
-
with sqlite3.connect(self.db_path) as conn:
|
|
140
|
-
cursor = conn.cursor()
|
|
141
|
-
cursor.execute("SELECT * FROM modules")
|
|
142
|
-
rows = cursor.fetchall()
|
|
143
|
-
modules_info = {}
|
|
144
|
-
for row in rows:
|
|
145
|
-
module_name, status, version, description, author, dependencies, optional_dependencies, pip_dependencies = row
|
|
146
|
-
modules_info[module_name] = {
|
|
147
|
-
'status': bool(status),
|
|
148
|
-
'info': {
|
|
149
|
-
'meta': {
|
|
150
|
-
'version': version,
|
|
151
|
-
'description': description,
|
|
152
|
-
'author': author,
|
|
153
|
-
'pip_dependencies': json.loads(pip_dependencies) if pip_dependencies else []
|
|
154
|
-
},
|
|
155
|
-
'dependencies': {
|
|
156
|
-
'requires': json.loads(dependencies) if dependencies else [],
|
|
157
|
-
'optional': json.loads(optional_dependencies) if optional_dependencies else [],
|
|
158
|
-
'pip': json.loads(pip_dependencies) if pip_dependencies else []
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return modules_info
|
|
163
|
-
|
|
164
|
-
def set_module(self, module_name, module_info):
|
|
165
|
-
with sqlite3.connect(self.db_path) as conn:
|
|
166
|
-
cursor = conn.cursor()
|
|
167
|
-
meta = module_info.get('info', {}).get('meta', {})
|
|
168
|
-
dependencies = module_info.get('info', {}).get('dependencies', {})
|
|
169
|
-
cursor.execute("""
|
|
170
|
-
INSERT OR REPLACE INTO modules (
|
|
171
|
-
module_name, status, version, description, author,
|
|
172
|
-
dependencies, optional_dependencies, pip_dependencies
|
|
173
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
174
|
-
""", (
|
|
175
|
-
module_name,
|
|
176
|
-
int(module_info.get('status', True)),
|
|
177
|
-
meta.get('version', ''),
|
|
178
|
-
meta.get('description', ''),
|
|
179
|
-
meta.get('author', ''),
|
|
180
|
-
json.dumps(dependencies.get('requires', [])),
|
|
181
|
-
json.dumps(dependencies.get('optional', [])),
|
|
182
|
-
json.dumps(dependencies.get('pip', []))
|
|
183
|
-
))
|
|
184
|
-
conn.commit()
|
|
185
|
-
|
|
186
|
-
def get_module(self, module_name):
|
|
187
|
-
with sqlite3.connect(self.db_path) as conn:
|
|
188
|
-
cursor = conn.cursor()
|
|
189
|
-
cursor.execute("SELECT * FROM modules WHERE module_name = ?", (module_name,))
|
|
190
|
-
row = cursor.fetchone()
|
|
191
|
-
if row:
|
|
192
|
-
module_name, status, version, description, author, dependencies, optional_dependencies, pip_dependencies = row
|
|
193
|
-
return {
|
|
194
|
-
'status': bool(status),
|
|
195
|
-
'info': {
|
|
196
|
-
'meta': {
|
|
197
|
-
'version': version,
|
|
198
|
-
'description': description,
|
|
199
|
-
'author': author,
|
|
200
|
-
'pip_dependencies': json.loads(pip_dependencies) if pip_dependencies else []
|
|
201
|
-
},
|
|
202
|
-
'dependencies': {
|
|
203
|
-
'requires': json.loads(dependencies) if dependencies else [],
|
|
204
|
-
'optional': json.loads(optional_dependencies) if optional_dependencies else [],
|
|
205
|
-
'pip': json.loads(pip_dependencies) if pip_dependencies else []
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
return None
|
|
210
|
-
|
|
211
|
-
def update_module(self, module_name, module_info):
|
|
212
|
-
self.set_module(module_name, module_info)
|
|
213
|
-
|
|
214
|
-
def remove_module(self, module_name):
|
|
215
|
-
with sqlite3.connect(self.db_path) as conn:
|
|
216
|
-
cursor = conn.cursor()
|
|
217
|
-
cursor.execute("DELETE FROM modules WHERE module_name = ?", (module_name,))
|
|
218
|
-
conn.commit()
|
|
219
|
-
return cursor.rowcount > 0
|
|
220
|
-
|
|
221
|
-
def __getattr__(self, key):
|
|
222
|
-
try:
|
|
223
|
-
return self.get(key)
|
|
224
|
-
except KeyError:
|
|
225
|
-
raise AttributeError(f"配置项 {key} 不存在")
|
|
226
|
-
|
|
227
|
-
env = EnvManager()
|
|
1
|
+
import os
|
|
2
|
+
import json
|
|
3
|
+
import sqlite3
|
|
4
|
+
import importlib.util
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
class EnvManager:
|
|
8
|
+
_instance = None
|
|
9
|
+
db_path = os.path.join(os.path.dirname(__file__), "config.db")
|
|
10
|
+
|
|
11
|
+
def __new__(cls, *args, **kwargs):
|
|
12
|
+
if not cls._instance:
|
|
13
|
+
cls._instance = super().__new__(cls)
|
|
14
|
+
cls._instance.dev_mode = kwargs.get('dev_mode', False)
|
|
15
|
+
return cls._instance
|
|
16
|
+
|
|
17
|
+
def __init__(self, dev_mode=False):
|
|
18
|
+
if not hasattr(self, "_initialized"):
|
|
19
|
+
self.dev_mode = dev_mode
|
|
20
|
+
self._init_db()
|
|
21
|
+
|
|
22
|
+
def _init_db(self):
|
|
23
|
+
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
|
|
24
|
+
|
|
25
|
+
conn = sqlite3.connect(self.db_path)
|
|
26
|
+
cursor = conn.cursor()
|
|
27
|
+
cursor.execute("""
|
|
28
|
+
CREATE TABLE IF NOT EXISTS config (
|
|
29
|
+
key TEXT PRIMARY KEY,
|
|
30
|
+
value TEXT NOT NULL
|
|
31
|
+
)
|
|
32
|
+
""")
|
|
33
|
+
cursor.execute("""
|
|
34
|
+
CREATE TABLE IF NOT EXISTS modules (
|
|
35
|
+
module_name TEXT PRIMARY KEY,
|
|
36
|
+
status INTEGER NOT NULL,
|
|
37
|
+
version TEXT,
|
|
38
|
+
description TEXT,
|
|
39
|
+
author TEXT,
|
|
40
|
+
dependencies TEXT,
|
|
41
|
+
optional_dependencies TEXT,
|
|
42
|
+
pip_dependencies TEXT
|
|
43
|
+
)
|
|
44
|
+
""")
|
|
45
|
+
conn.commit()
|
|
46
|
+
conn.close()
|
|
47
|
+
|
|
48
|
+
def get(self, key, default=None):
|
|
49
|
+
try:
|
|
50
|
+
with sqlite3.connect(self.db_path) as conn:
|
|
51
|
+
cursor = conn.cursor()
|
|
52
|
+
cursor.execute("SELECT value FROM config WHERE key = ?", (key,))
|
|
53
|
+
result = cursor.fetchone()
|
|
54
|
+
if result:
|
|
55
|
+
try:
|
|
56
|
+
return json.loads(result[0])
|
|
57
|
+
except json.JSONDecodeError:
|
|
58
|
+
return result[0]
|
|
59
|
+
return default
|
|
60
|
+
except sqlite3.OperationalError as e:
|
|
61
|
+
if "no such table" in str(e):
|
|
62
|
+
self._init_db()
|
|
63
|
+
return self.get(key, default)
|
|
64
|
+
else:
|
|
65
|
+
raise
|
|
66
|
+
|
|
67
|
+
def set(self, key, value):
|
|
68
|
+
serialized_value = json.dumps(value) if isinstance(value, (dict, list)) else str(value)
|
|
69
|
+
conn = sqlite3.connect(self.db_path)
|
|
70
|
+
cursor = conn.cursor()
|
|
71
|
+
cursor.execute("INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)", (key, serialized_value))
|
|
72
|
+
conn.commit()
|
|
73
|
+
conn.close()
|
|
74
|
+
|
|
75
|
+
def delete(self, key):
|
|
76
|
+
conn = sqlite3.connect(self.db_path)
|
|
77
|
+
cursor = conn.cursor()
|
|
78
|
+
cursor.execute("DELETE FROM config WHERE key = ?", (key,))
|
|
79
|
+
conn.commit()
|
|
80
|
+
conn.close()
|
|
81
|
+
|
|
82
|
+
def clear(self):
|
|
83
|
+
conn = sqlite3.connect(self.db_path)
|
|
84
|
+
cursor = conn.cursor()
|
|
85
|
+
cursor.execute("DELETE FROM config")
|
|
86
|
+
conn.commit()
|
|
87
|
+
conn.close()
|
|
88
|
+
|
|
89
|
+
def load_env_file(self):
|
|
90
|
+
env_file = Path("env.py")
|
|
91
|
+
if env_file.exists():
|
|
92
|
+
spec = importlib.util.spec_from_file_location("env_module", env_file)
|
|
93
|
+
env_module = importlib.util.module_from_spec(spec)
|
|
94
|
+
spec.loader.exec_module(env_module)
|
|
95
|
+
for key, value in vars(env_module).items():
|
|
96
|
+
if not key.startswith("__") and isinstance(value, (dict, list, str, int, float, bool)):
|
|
97
|
+
self.set(key, value)
|
|
98
|
+
def set_module_status(self, module_name, status):
|
|
99
|
+
with sqlite3.connect(self.db_path) as conn:
|
|
100
|
+
cursor = conn.cursor()
|
|
101
|
+
cursor.execute("""
|
|
102
|
+
UPDATE modules SET status = ? WHERE module_name = ?
|
|
103
|
+
""", (int(status), module_name))
|
|
104
|
+
conn.commit()
|
|
105
|
+
|
|
106
|
+
def get_module_status(self, module_name):
|
|
107
|
+
with sqlite3.connect(self.db_path) as conn:
|
|
108
|
+
cursor = conn.cursor()
|
|
109
|
+
cursor.execute("""
|
|
110
|
+
SELECT status FROM modules WHERE module_name = ?
|
|
111
|
+
""", (module_name,))
|
|
112
|
+
result = cursor.fetchone()
|
|
113
|
+
return bool(result[0]) if result else True
|
|
114
|
+
|
|
115
|
+
def set_all_modules(self, modules_info):
|
|
116
|
+
with sqlite3.connect(self.db_path) as conn:
|
|
117
|
+
cursor = conn.cursor()
|
|
118
|
+
for module_name, module_info in modules_info.items():
|
|
119
|
+
meta = module_info.get('info', {}).get('meta', {})
|
|
120
|
+
dependencies = module_info.get('info', {}).get('dependencies', {})
|
|
121
|
+
cursor.execute("""
|
|
122
|
+
INSERT OR REPLACE INTO modules (
|
|
123
|
+
module_name, status, version, description, author,
|
|
124
|
+
dependencies, optional_dependencies, pip_dependencies
|
|
125
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
126
|
+
""", (
|
|
127
|
+
module_name,
|
|
128
|
+
int(module_info.get('status', True)),
|
|
129
|
+
meta.get('version', ''),
|
|
130
|
+
meta.get('description', ''),
|
|
131
|
+
meta.get('author', ''),
|
|
132
|
+
json.dumps(dependencies.get('requires', [])),
|
|
133
|
+
json.dumps(dependencies.get('optional', [])),
|
|
134
|
+
json.dumps(dependencies.get('pip', []))
|
|
135
|
+
))
|
|
136
|
+
conn.commit()
|
|
137
|
+
|
|
138
|
+
def get_all_modules(self):
|
|
139
|
+
with sqlite3.connect(self.db_path) as conn:
|
|
140
|
+
cursor = conn.cursor()
|
|
141
|
+
cursor.execute("SELECT * FROM modules")
|
|
142
|
+
rows = cursor.fetchall()
|
|
143
|
+
modules_info = {}
|
|
144
|
+
for row in rows:
|
|
145
|
+
module_name, status, version, description, author, dependencies, optional_dependencies, pip_dependencies = row
|
|
146
|
+
modules_info[module_name] = {
|
|
147
|
+
'status': bool(status),
|
|
148
|
+
'info': {
|
|
149
|
+
'meta': {
|
|
150
|
+
'version': version,
|
|
151
|
+
'description': description,
|
|
152
|
+
'author': author,
|
|
153
|
+
'pip_dependencies': json.loads(pip_dependencies) if pip_dependencies else []
|
|
154
|
+
},
|
|
155
|
+
'dependencies': {
|
|
156
|
+
'requires': json.loads(dependencies) if dependencies else [],
|
|
157
|
+
'optional': json.loads(optional_dependencies) if optional_dependencies else [],
|
|
158
|
+
'pip': json.loads(pip_dependencies) if pip_dependencies else []
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return modules_info
|
|
163
|
+
|
|
164
|
+
def set_module(self, module_name, module_info):
|
|
165
|
+
with sqlite3.connect(self.db_path) as conn:
|
|
166
|
+
cursor = conn.cursor()
|
|
167
|
+
meta = module_info.get('info', {}).get('meta', {})
|
|
168
|
+
dependencies = module_info.get('info', {}).get('dependencies', {})
|
|
169
|
+
cursor.execute("""
|
|
170
|
+
INSERT OR REPLACE INTO modules (
|
|
171
|
+
module_name, status, version, description, author,
|
|
172
|
+
dependencies, optional_dependencies, pip_dependencies
|
|
173
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
174
|
+
""", (
|
|
175
|
+
module_name,
|
|
176
|
+
int(module_info.get('status', True)),
|
|
177
|
+
meta.get('version', ''),
|
|
178
|
+
meta.get('description', ''),
|
|
179
|
+
meta.get('author', ''),
|
|
180
|
+
json.dumps(dependencies.get('requires', [])),
|
|
181
|
+
json.dumps(dependencies.get('optional', [])),
|
|
182
|
+
json.dumps(dependencies.get('pip', []))
|
|
183
|
+
))
|
|
184
|
+
conn.commit()
|
|
185
|
+
|
|
186
|
+
def get_module(self, module_name):
|
|
187
|
+
with sqlite3.connect(self.db_path) as conn:
|
|
188
|
+
cursor = conn.cursor()
|
|
189
|
+
cursor.execute("SELECT * FROM modules WHERE module_name = ?", (module_name,))
|
|
190
|
+
row = cursor.fetchone()
|
|
191
|
+
if row:
|
|
192
|
+
module_name, status, version, description, author, dependencies, optional_dependencies, pip_dependencies = row
|
|
193
|
+
return {
|
|
194
|
+
'status': bool(status),
|
|
195
|
+
'info': {
|
|
196
|
+
'meta': {
|
|
197
|
+
'version': version,
|
|
198
|
+
'description': description,
|
|
199
|
+
'author': author,
|
|
200
|
+
'pip_dependencies': json.loads(pip_dependencies) if pip_dependencies else []
|
|
201
|
+
},
|
|
202
|
+
'dependencies': {
|
|
203
|
+
'requires': json.loads(dependencies) if dependencies else [],
|
|
204
|
+
'optional': json.loads(optional_dependencies) if optional_dependencies else [],
|
|
205
|
+
'pip': json.loads(pip_dependencies) if pip_dependencies else []
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return None
|
|
210
|
+
|
|
211
|
+
def update_module(self, module_name, module_info):
|
|
212
|
+
self.set_module(module_name, module_info)
|
|
213
|
+
|
|
214
|
+
def remove_module(self, module_name):
|
|
215
|
+
with sqlite3.connect(self.db_path) as conn:
|
|
216
|
+
cursor = conn.cursor()
|
|
217
|
+
cursor.execute("DELETE FROM modules WHERE module_name = ?", (module_name,))
|
|
218
|
+
conn.commit()
|
|
219
|
+
return cursor.rowcount > 0
|
|
220
|
+
|
|
221
|
+
def __getattr__(self, key):
|
|
222
|
+
try:
|
|
223
|
+
return self.get(key)
|
|
224
|
+
except KeyError:
|
|
225
|
+
raise AttributeError(f"配置项 {key} 不存在")
|
|
226
|
+
|
|
227
|
+
env = EnvManager()
|
ErisPulse/logger.py
CHANGED
|
@@ -1,48 +1,123 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
import inspect
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
_logger.
|
|
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
|
-
def
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
1
|
+
import logging
|
|
2
|
+
import inspect
|
|
3
|
+
import datetime
|
|
4
|
+
|
|
5
|
+
class Logger:
|
|
6
|
+
def __init__(self):
|
|
7
|
+
self._logs = {}
|
|
8
|
+
self._module_levels = {}
|
|
9
|
+
self._logger = logging.getLogger("ErisPulse")
|
|
10
|
+
self._logger.setLevel(logging.DEBUG)
|
|
11
|
+
self._file_handler = None
|
|
12
|
+
if not self._logger.handlers:
|
|
13
|
+
console_handler = logging.StreamHandler()
|
|
14
|
+
console_handler.setFormatter(logging.Formatter("%(message)s"))
|
|
15
|
+
self._logger.addHandler(console_handler)
|
|
16
|
+
|
|
17
|
+
def set_level(self, level: str):
|
|
18
|
+
level = level.upper()
|
|
19
|
+
if hasattr(logging, level):
|
|
20
|
+
self._logger.setLevel(getattr(logging, level))
|
|
21
|
+
|
|
22
|
+
def set_module_level(self, module_name: str, level: str) -> bool:
|
|
23
|
+
from .db import env
|
|
24
|
+
if not env.get_module_status(module_name):
|
|
25
|
+
self._logger.warning(f"模块 {module_name} 未启用,无法设置日志等级。")
|
|
26
|
+
return False
|
|
27
|
+
level = level.upper()
|
|
28
|
+
if hasattr(logging, level):
|
|
29
|
+
self._module_levels[module_name] = getattr(logging, level)
|
|
30
|
+
self._logger.info(f"模块 {module_name} 日志等级已设置为 {level}")
|
|
31
|
+
return True
|
|
32
|
+
else:
|
|
33
|
+
self._logger.error(f"无效的日志等级: {level}")
|
|
34
|
+
return False
|
|
35
|
+
|
|
36
|
+
def set_output_file(self, path: str | list):
|
|
37
|
+
if self._file_handler:
|
|
38
|
+
self._logger.removeHandler(self._file_handler)
|
|
39
|
+
self._file_handler.close()
|
|
40
|
+
|
|
41
|
+
if isinstance(path, str):
|
|
42
|
+
path = [path]
|
|
43
|
+
|
|
44
|
+
for p in path:
|
|
45
|
+
try:
|
|
46
|
+
file_handler = logging.FileHandler(p, encoding='utf-8')
|
|
47
|
+
file_handler.setFormatter(logging.Formatter("%(message)s"))
|
|
48
|
+
self._logger.addHandler(file_handler)
|
|
49
|
+
self._logger.info(f"日志输出已设置到文件: {p}")
|
|
50
|
+
except Exception as e:
|
|
51
|
+
self._logger.error(f"无法设置日志文件 {p}: {e}")
|
|
52
|
+
raise e
|
|
53
|
+
|
|
54
|
+
def save_logs(self, path: str | list):
|
|
55
|
+
if self._logs == None:
|
|
56
|
+
self._logger.warning("没有log记录可供保存。")
|
|
57
|
+
return
|
|
58
|
+
if isinstance(path, str):
|
|
59
|
+
path = [path]
|
|
60
|
+
|
|
61
|
+
for p in path:
|
|
62
|
+
try:
|
|
63
|
+
with open(p, "w", encoding="utf-8") as file:
|
|
64
|
+
for module, logs in self._logs.items():
|
|
65
|
+
file.write(f"Module: {module}\n")
|
|
66
|
+
for log in logs:
|
|
67
|
+
file.write(f" {log}\n")
|
|
68
|
+
self._logger.info(f"日志已被保存到:{p}。")
|
|
69
|
+
except Exception as e:
|
|
70
|
+
self._logger.error(f"无法保存日志到 {p}: {e}。")
|
|
71
|
+
raise e
|
|
72
|
+
|
|
73
|
+
def _save_in_memory(self, ModuleName, msg):
|
|
74
|
+
if ModuleName not in self._logs:
|
|
75
|
+
self._logs[ModuleName] = []
|
|
76
|
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
77
|
+
msg = f"{timestamp} - {msg}"
|
|
78
|
+
self._logs[ModuleName].append(msg)
|
|
79
|
+
|
|
80
|
+
def _get_effective_level(self, module_name):
|
|
81
|
+
return self._module_levels.get(module_name, self._logger.level)
|
|
82
|
+
|
|
83
|
+
def _get_caller(self):
|
|
84
|
+
frame = inspect.currentframe().f_back.f_back
|
|
85
|
+
module = inspect.getmodule(frame)
|
|
86
|
+
module_name = module.__name__
|
|
87
|
+
if module_name == "__main__":
|
|
88
|
+
module_name = "Main"
|
|
89
|
+
if module_name.endswith(".Core"):
|
|
90
|
+
module_name = module_name[:-5]
|
|
91
|
+
return module_name
|
|
92
|
+
|
|
93
|
+
def debug(self, msg, *args, **kwargs):
|
|
94
|
+
caller_module = self._get_caller()
|
|
95
|
+
if self._get_effective_level(caller_module) <= logging.DEBUG:
|
|
96
|
+
self._save_in_memory(caller_module, msg)
|
|
97
|
+
self._logger.debug(f"[{caller_module}] {msg}", *args, **kwargs)
|
|
98
|
+
|
|
99
|
+
def info(self, msg, *args, **kwargs):
|
|
100
|
+
caller_module = self._get_caller()
|
|
101
|
+
if self._get_effective_level(caller_module) <= logging.INFO:
|
|
102
|
+
self._save_in_memory(caller_module, msg)
|
|
103
|
+
self._logger.info(f"[{caller_module}] {msg}", *args, **kwargs)
|
|
104
|
+
|
|
105
|
+
def warning(self, msg, *args, **kwargs):
|
|
106
|
+
caller_module = self._get_caller()
|
|
107
|
+
if self._get_effective_level(caller_module) <= logging.WARNING:
|
|
108
|
+
self._save_in_memory(caller_module, msg)
|
|
109
|
+
self._logger.warning(f"[{caller_module}] {msg}", *args, **kwargs)
|
|
110
|
+
|
|
111
|
+
def error(self, msg, *args, **kwargs):
|
|
112
|
+
caller_module = self._get_caller()
|
|
113
|
+
if self._get_effective_level(caller_module) <= logging.ERROR:
|
|
114
|
+
self._save_in_memory(caller_module, msg)
|
|
115
|
+
self._logger.error(f"[{caller_module}] {msg}", *args, **kwargs)
|
|
116
|
+
|
|
117
|
+
def critical(self, msg, *args, **kwargs):
|
|
118
|
+
caller_module = self._get_caller()
|
|
119
|
+
if self._get_effective_level(caller_module) <= logging.CRITICAL:
|
|
120
|
+
self._save_in_memory(caller_module, msg)
|
|
121
|
+
self._logger.critical(f"[{caller_module}] {msg}", *args, **kwargs)
|
|
122
|
+
|
|
123
|
+
logger = Logger()
|