eric-tools 1.3.2__tar.gz → 1.3.3__tar.gz
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.
- {eric_tools-1.3.2 → eric_tools-1.3.3}/PKG-INFO +9 -6
- {eric_tools-1.3.2 → eric_tools-1.3.3}/README.md +8 -5
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/__init__.py +2 -1
- eric_tools-1.3.3/eric_tools/dynamic_settings.py +174 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/send_email.py +9 -13
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools.egg-info/PKG-INFO +9 -6
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools.egg-info/SOURCES.txt +1 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/setup.py +1 -1
- {eric_tools-1.3.2 → eric_tools-1.3.3}/LICENSE +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/Abstract.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/async_queue.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/convert_json.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/decorator.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/downloader.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/encryption_classmethod.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/excel_generator.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/exception_class.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/ip.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/jwt_encrypt.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/logMixin.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/logger.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/nginx_log.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/pgsql.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/readconfig.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/remove.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/resize_image.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools/sftp.py +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools.egg-info/dependency_links.txt +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools.egg-info/requires.txt +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/eric_tools.egg-info/top_level.txt +0 -0
- {eric_tools-1.3.2 → eric_tools-1.3.3}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: eric_tools
|
3
|
-
Version: 1.3.
|
3
|
+
Version: 1.3.3
|
4
4
|
Summary: Python Daily Development Tools
|
5
5
|
Home-page: https://github.com/Eric-jxl/Tools
|
6
6
|
Author: Eric
|
@@ -16,8 +16,9 @@ License-File: LICENSE
|
|
16
16
|
|
17
17
|
# Tools
|
18
18
|
[](https://opensource.org/licenses/Apache-2.0)
|
19
|
-

|
20
20
|

|
21
|
+
[](https://github.com/eric-jxl/Tools/actions/workflows/publish-pypi.yml)
|
21
22
|
|
22
23
|
|
23
24
|
[Reids](https://eric-jxl.github.io/bak/index.html)
|
@@ -57,13 +58,15 @@ pip install eric_tools
|
|
57
58
|
|
58
59
|
>[!TIP]
|
59
60
|
>
|
60
|
-
> * async_queue.py
|
61
|
+
> * async_queue.py 异步队列操作
|
61
62
|
>
|
62
|
-
> * downloader.py
|
63
|
+
> * downloader.py 下载器
|
63
64
|
>
|
64
|
-
> * logMixIn.py
|
65
|
+
> * logMixIn.py 日志元类和高级日志装饰器
|
65
66
|
>
|
66
|
-
> * nginx_log.py
|
67
|
+
> * nginx_log.py nginx日志解析(默认access.log)
|
67
68
|
>
|
69
|
+
> * dynamic_settings 一个动态加载配置类(支持toml、yml、conf、csv、ini等)
|
70
|
+
>
|
68
71
|
|
69
72
|
|
@@ -1,7 +1,8 @@
|
|
1
1
|
# Tools
|
2
2
|
[](https://opensource.org/licenses/Apache-2.0)
|
3
|
-

|
4
4
|

|
5
|
+
[](https://github.com/eric-jxl/Tools/actions/workflows/publish-pypi.yml)
|
5
6
|
|
6
7
|
|
7
8
|
[Reids](https://eric-jxl.github.io/bak/index.html)
|
@@ -41,11 +42,13 @@ pip install eric_tools
|
|
41
42
|
|
42
43
|
>[!TIP]
|
43
44
|
>
|
44
|
-
> * async_queue.py
|
45
|
+
> * async_queue.py 异步队列操作
|
45
46
|
>
|
46
|
-
> * downloader.py
|
47
|
+
> * downloader.py 下载器
|
47
48
|
>
|
48
|
-
> * logMixIn.py
|
49
|
+
> * logMixIn.py 日志元类和高级日志装饰器
|
49
50
|
>
|
50
|
-
> * nginx_log.py
|
51
|
+
> * nginx_log.py nginx日志解析(默认access.log)
|
51
52
|
>
|
53
|
+
> * dynamic_settings 一个动态加载配置类(支持toml、yml、conf、csv、ini等)
|
54
|
+
>
|
@@ -7,12 +7,13 @@
|
|
7
7
|
from . import decorator, Abstract, convert_json, jwt_encrypt, send_email, readconfig, sftp
|
8
8
|
from . import ip, logger, resize_image, exception_class, pgsql, encryption_classmethod
|
9
9
|
from . import async_queue, downloader, logMixin, nginx_log, excel_generator
|
10
|
+
from .dynamic_settings import DynamicConfig
|
10
11
|
|
11
12
|
|
12
13
|
name = 'Eric-Tools'
|
13
14
|
__title__ = 'tools'
|
14
15
|
__description__ = 'Python HTTP for Humans.'
|
15
|
-
__version__ = "1.3.
|
16
|
+
__version__ = "1.3.3"
|
16
17
|
__author__ = 'Eric'
|
17
18
|
__doc__ = ["Python Daily Development Tools"]
|
18
19
|
__url__ = "https://github.com/Eric-jxl/Tools"
|
@@ -0,0 +1,174 @@
|
|
1
|
+
import os
|
2
|
+
import yaml
|
3
|
+
import toml
|
4
|
+
import csv
|
5
|
+
import configparser
|
6
|
+
import logging
|
7
|
+
from collections.abc import MutableMapping
|
8
|
+
from watchdog.observers import Observer
|
9
|
+
from watchdog.events import FileSystemEventHandler
|
10
|
+
from flask import Flask, jsonify, redirect
|
11
|
+
|
12
|
+
logging.basicConfig(level=logging.INFO)
|
13
|
+
logger = logging.getLogger(__file__.rsplit("/", 1)[1])
|
14
|
+
|
15
|
+
__all__ = ["DynamicConfig"]
|
16
|
+
|
17
|
+
# Flask Web Service for testing
|
18
|
+
app = Flask(__name__)
|
19
|
+
|
20
|
+
|
21
|
+
class ConfigFileHandler(FileSystemEventHandler):
|
22
|
+
"""Handles file changes and reloads the configuration."""
|
23
|
+
|
24
|
+
def __init__(self, config, file_path):
|
25
|
+
self.config = config
|
26
|
+
self.file_path = file_path
|
27
|
+
|
28
|
+
def on_modified(self, event):
|
29
|
+
if event.src_path == self.file_path:
|
30
|
+
print(f"Configuration file {self.file_path} changed, reloading...")
|
31
|
+
self.config.force_reload(self.file_path)
|
32
|
+
|
33
|
+
|
34
|
+
class DynamicConfig(MutableMapping):
|
35
|
+
def __init__(self, watch_file=None, **kwargs):
|
36
|
+
self.watch_file = watch_file
|
37
|
+
self.config = kwargs # Support setting variables directly
|
38
|
+
|
39
|
+
if watch_file:
|
40
|
+
self.load(watch_file)
|
41
|
+
self._start_watching()
|
42
|
+
|
43
|
+
def _start_watching(self):
|
44
|
+
"""Starts a background thread to monitor file changes."""
|
45
|
+
event_handler = ConfigFileHandler(self, self.watch_file)
|
46
|
+
observer = Observer()
|
47
|
+
observer.schedule(event_handler, path=os.path.dirname(
|
48
|
+
self.watch_file) or ".", recursive=False)
|
49
|
+
observer.start()
|
50
|
+
self._observer = observer
|
51
|
+
|
52
|
+
def load(self, file_path=None):
|
53
|
+
"""Loads configuration from a file and merges with existing values."""
|
54
|
+
if file_path:
|
55
|
+
self.file_path = file_path
|
56
|
+
|
57
|
+
if not self.file_path or not os.path.exists(self.file_path):
|
58
|
+
logger.error(f"Configuration file '{self.file_path}' not found.")
|
59
|
+
return
|
60
|
+
|
61
|
+
loaders = {
|
62
|
+
".yml": self._load_yaml, ".yaml": self._load_yaml,
|
63
|
+
".toml": self._load_toml, ".csv": self._load_csv,
|
64
|
+
".conf": self._load_conf, ".ini": self._load_conf
|
65
|
+
}
|
66
|
+
|
67
|
+
ext = os.path.splitext(self.file_path)[-1].lower()
|
68
|
+
if ext not in loaders:
|
69
|
+
raise ValueError(f"Unsupported file format: {ext}")
|
70
|
+
|
71
|
+
file_config = loaders[ext](self.file_path, self.config)
|
72
|
+
|
73
|
+
logger.debug("Updated Config:", self.config) # Debugging
|
74
|
+
|
75
|
+
def force_reload(self):
|
76
|
+
"""Manually trigger configuration reload (used in watchdog)."""
|
77
|
+
logger.info("Force reloading configuration...")
|
78
|
+
if self.watch_file:
|
79
|
+
self.load(self.watch_file)
|
80
|
+
|
81
|
+
def _load_yaml(self, file_path, config):
|
82
|
+
with open(file_path, 'r', encoding='utf-8') as f:
|
83
|
+
config.update(yaml.safe_load(f) or {})
|
84
|
+
|
85
|
+
def _load_toml(self, file_path, config):
|
86
|
+
with open(file_path, 'r', encoding='utf-8') as f:
|
87
|
+
config.update(toml.load(f))
|
88
|
+
|
89
|
+
def _load_csv(self, file_path, config):
|
90
|
+
with open(file_path, 'r', encoding='utf-8') as f:
|
91
|
+
reader = csv.reader(f)
|
92
|
+
config.update({row[0]: row[1] for row in reader if len(row) >= 2})
|
93
|
+
|
94
|
+
def _load_conf(self, file_path, config):
|
95
|
+
parser = configparser.ConfigParser()
|
96
|
+
parser.read(file_path, encoding='utf-8')
|
97
|
+
for section in parser.sections():
|
98
|
+
for key, value in parser.items(section):
|
99
|
+
config[f"{section}.{key}"] = value
|
100
|
+
|
101
|
+
def set(self, key, value):
|
102
|
+
"""Sets a configuration variable."""
|
103
|
+
self.config[key] = value
|
104
|
+
|
105
|
+
def get(self, key, default=None):
|
106
|
+
"""支持多层级 `.` 分隔键访问,如 `config.get("database.host")`"""
|
107
|
+
if key in self.config:
|
108
|
+
return self.config[key]
|
109
|
+
keys = key.split(".") # 按 `.` 分割多级键
|
110
|
+
value = self.config # 从 `self.config` 开始查找
|
111
|
+
|
112
|
+
try:
|
113
|
+
for k in keys:
|
114
|
+
if isinstance(value, dict): # 如果是字典,继续查找键
|
115
|
+
value = value[k]
|
116
|
+
elif isinstance(value, list) and k.isdigit(): # 如果是列表,并且 k 是索引
|
117
|
+
value = value[int(k)]
|
118
|
+
else:
|
119
|
+
return default # 找不到,返回默认值
|
120
|
+
return value
|
121
|
+
except (KeyError, IndexError, TypeError):
|
122
|
+
return default # 访问失败,返回默认值
|
123
|
+
|
124
|
+
def __getitem__(self, key):
|
125
|
+
return self.config[key]
|
126
|
+
|
127
|
+
def __setitem__(self, key, value):
|
128
|
+
self.config[key] = value
|
129
|
+
|
130
|
+
def __delitem__(self, key):
|
131
|
+
del self.config[key]
|
132
|
+
|
133
|
+
def __iter__(self):
|
134
|
+
return iter(self.config)
|
135
|
+
|
136
|
+
def __len__(self):
|
137
|
+
return len(self.config)
|
138
|
+
|
139
|
+
def __repr__(self):
|
140
|
+
return f"DynamicConfig({self.config})"
|
141
|
+
|
142
|
+
def stop_watching(self):
|
143
|
+
"""Stops the file watcher."""
|
144
|
+
if hasattr(self, "_observer"):
|
145
|
+
self._observer.stop()
|
146
|
+
self._observer.join()
|
147
|
+
|
148
|
+
|
149
|
+
config = DynamicConfig("config.yml", debug=True, mode="production")
|
150
|
+
|
151
|
+
|
152
|
+
@app.route("/", methods=["GET"])
|
153
|
+
def index():
|
154
|
+
return redirect("/config/")
|
155
|
+
|
156
|
+
|
157
|
+
@app.route("/config/", methods=["GET"])
|
158
|
+
def get_config():
|
159
|
+
config.force_reload()
|
160
|
+
return jsonify(config.config)
|
161
|
+
|
162
|
+
|
163
|
+
@app.route("/config/<path:key>", methods=["GET"]) # ✅ 允许 `/` 作为 key 的一部分
|
164
|
+
def get_config_key(key):
|
165
|
+
"""获取指定配置项的值,并支持自动重新加载"""
|
166
|
+
config.force_reload() # ✅ 先重新加载配置
|
167
|
+
value = config.get(key) # ✅ 取值
|
168
|
+
if value is None:
|
169
|
+
return jsonify({"error": "Key not found"}), 404 # ✅ 404 + 友好错误信息
|
170
|
+
return jsonify(value)
|
171
|
+
|
172
|
+
|
173
|
+
if __name__ == "__main__":
|
174
|
+
app.run(host="0.0.0.0", port=5001, debug=True)
|
@@ -10,26 +10,22 @@ __doc__ = ["测试QQ邮箱,pwd为邮箱授权码"]
|
|
10
10
|
import smtplib
|
11
11
|
from email.mime.text import MIMEText
|
12
12
|
from email.utils import formataddr
|
13
|
-
import sys
|
14
|
-
reload(sys)
|
15
|
-
sys.setdefaultencoding("utf-8")
|
16
13
|
|
17
14
|
|
18
|
-
|
19
|
-
|
20
|
-
def mail(send_email,receive_email,send_nickname,receive_nickname,pwd):
|
15
|
+
def mail(send_email, receive_email, send_nickname, receive_nickname, pwd):
|
21
16
|
try:
|
22
17
|
msg = MIMEText('Test QQ Email', 'plain', 'utf-8')
|
23
|
-
|
24
|
-
msg['
|
25
|
-
|
18
|
+
# 括号里的对应发件人邮箱昵称、发件人邮箱账号
|
19
|
+
msg['From'] = formataddr([send_nickname, send_email])
|
20
|
+
# 括号里的对应收件人邮箱昵称、收件人邮箱账号
|
21
|
+
msg['To'] = formataddr([receive_nickname, receive_email])
|
22
|
+
msg['Subject'] = "发送邮件测试" # 邮件的主题,也可以说是标题
|
26
23
|
|
27
24
|
server = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是25
|
28
25
|
server.login(send_email, pwd)
|
29
|
-
|
26
|
+
# 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
|
27
|
+
server.sendmail(send_email, [receive_email, ], msg.as_string())
|
30
28
|
server.quit() # 关闭连接
|
31
29
|
except Exception as e:
|
32
|
-
print
|
30
|
+
print('发送邮件失败:%s' % e)
|
33
31
|
return e
|
34
|
-
|
35
|
-
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: eric-tools
|
3
|
-
Version: 1.3.
|
3
|
+
Version: 1.3.3
|
4
4
|
Summary: Python Daily Development Tools
|
5
5
|
Home-page: https://github.com/Eric-jxl/Tools
|
6
6
|
Author: Eric
|
@@ -16,8 +16,9 @@ License-File: LICENSE
|
|
16
16
|
|
17
17
|
# Tools
|
18
18
|
[](https://opensource.org/licenses/Apache-2.0)
|
19
|
-

|
20
20
|

|
21
|
+
[](https://github.com/eric-jxl/Tools/actions/workflows/publish-pypi.yml)
|
21
22
|
|
22
23
|
|
23
24
|
[Reids](https://eric-jxl.github.io/bak/index.html)
|
@@ -57,13 +58,15 @@ pip install eric_tools
|
|
57
58
|
|
58
59
|
>[!TIP]
|
59
60
|
>
|
60
|
-
> * async_queue.py
|
61
|
+
> * async_queue.py 异步队列操作
|
61
62
|
>
|
62
|
-
> * downloader.py
|
63
|
+
> * downloader.py 下载器
|
63
64
|
>
|
64
|
-
> * logMixIn.py
|
65
|
+
> * logMixIn.py 日志元类和高级日志装饰器
|
65
66
|
>
|
66
|
-
> * nginx_log.py
|
67
|
+
> * nginx_log.py nginx日志解析(默认access.log)
|
67
68
|
>
|
69
|
+
> * dynamic_settings 一个动态加载配置类(支持toml、yml、conf、csv、ini等)
|
70
|
+
>
|
68
71
|
|
69
72
|
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|