pewutils 0.0.7__tar.gz → 0.0.8__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.
- {pewutils-0.0.7 → pewutils-0.0.8}/PKG-INFO +1 -1
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils/__init__.py +1 -1
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils/db_util.py +91 -2
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils.egg-info/PKG-INFO +1 -1
- {pewutils-0.0.7 → pewutils-0.0.8}/README.md +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils/base.py +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils/datetime_util.py +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils/oss_util.py +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils/text_util.py +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils/url_util.py +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils/wmill_util.py +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils.egg-info/SOURCES.txt +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils.egg-info/dependency_links.txt +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils.egg-info/requires.txt +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/pewutils.egg-info/top_level.txt +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/pyproject.toml +0 -0
- {pewutils-0.0.7 → pewutils-0.0.8}/setup.cfg +0 -0
|
@@ -20,8 +20,13 @@ pewutils.db_util - 通用数据库连接工具
|
|
|
20
20
|
Change History:
|
|
21
21
|
2026-08-18: 初始版本,迁移自业务脚本 Dbclass;重构为 DbClient,
|
|
22
22
|
新增 query 族、字典驱动 CRUD、事务上下文、流式查询
|
|
23
|
+
2026-08-18: 新增 variable 参数,支持从 Windmill Variable(u/admin/{name})
|
|
24
|
+
自动加载连接配置;字段 key 默认取 DEFAULT_VAR_KEYS,
|
|
25
|
+
命名不一致时在 Variable 内写 key_map 字段指定;
|
|
26
|
+
connect_timeout 按驱动条件注入
|
|
23
27
|
"""
|
|
24
28
|
|
|
29
|
+
import json
|
|
25
30
|
import logging
|
|
26
31
|
import re
|
|
27
32
|
from contextlib import contextmanager
|
|
@@ -54,6 +59,18 @@ def _check_ident(name: str) -> str:
|
|
|
54
59
|
return name
|
|
55
60
|
|
|
56
61
|
|
|
62
|
+
# Windmill Variable 连接配置的默认字段 key。
|
|
63
|
+
# Variable 的 key 命名与此不一致时,创建连接传 key_map 覆盖对应字段即可。
|
|
64
|
+
DEFAULT_VAR_KEYS = {
|
|
65
|
+
"host_internal": "host_internal",
|
|
66
|
+
"host_external": "host_external",
|
|
67
|
+
"dbname": "dbname",
|
|
68
|
+
"port": "port",
|
|
69
|
+
"user": "user",
|
|
70
|
+
"pwd": "pwd",
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
57
74
|
class DbClient:
|
|
58
75
|
"""通用数据库客户端。
|
|
59
76
|
|
|
@@ -73,16 +90,34 @@ class DbClient:
|
|
|
73
90
|
也可直接传完整 URL::
|
|
74
91
|
|
|
75
92
|
db = DbClient(url="postgresql+psycopg2://user:pwd@host:5432/mydb")
|
|
93
|
+
|
|
94
|
+
或从 Windmill Variable 自动加载连接配置(Variable 路径固定为 u/admin/{variable})::
|
|
95
|
+
|
|
96
|
+
# Variable u/admin/data_db_prod 内容为 JSON:
|
|
97
|
+
# {"RDS_DATA_DB_HOST_EXTERNAL": "...", "RDS_DATA_DB_HOST_INTERNAL": "...",
|
|
98
|
+
# "RDS_DATA_DB_NAME": "data_db", "RDS_DATA_DB_PORT": "5432",
|
|
99
|
+
# "RDS_DATA_DB_USER": "postgres", "RDS_DATA_DB_PSW": "..."}
|
|
100
|
+
db = DbClient(variable="data_db_prod")
|
|
101
|
+
|
|
102
|
+
# Variable 的 key 命名与默认不一致时,在 Variable 内写 key_map 字段:
|
|
103
|
+
# {"host": "...", "port": "3306", "user": "root", "secret": "...",
|
|
104
|
+
# "key_map": {"host_internal": "host", "pwd": "secret"}}
|
|
105
|
+
db = DbClient(variable="some_db")
|
|
76
106
|
"""
|
|
77
107
|
|
|
78
108
|
DEFAULT_SCHEME = "postgresql+psycopg2"
|
|
79
109
|
|
|
110
|
+
# Windmill Variable 固定前缀(数据库连接配置统一存放在 u/admin/ 下)
|
|
111
|
+
VARIABLE_PREFIX = "u/admin/"
|
|
112
|
+
|
|
80
113
|
def __init__(
|
|
81
114
|
self,
|
|
82
115
|
db_setting: dict | None = None,
|
|
83
116
|
dbname: str | None = None,
|
|
84
117
|
*,
|
|
85
118
|
url: str | None = None,
|
|
119
|
+
variable: str | None = None,
|
|
120
|
+
host_type: str = "internal",
|
|
86
121
|
scheme: str | None = None,
|
|
87
122
|
connect_timeout: int = 30,
|
|
88
123
|
echo: bool = False,
|
|
@@ -91,14 +126,22 @@ class DbClient:
|
|
|
91
126
|
:param db_setting: 连接配置 dict,需含 user/pwd/host/port
|
|
92
127
|
(pwd 也可写作 password;可含 scheme 覆盖默认驱动)
|
|
93
128
|
:param dbname: 数据库名
|
|
94
|
-
:param url: 完整连接 URL
|
|
129
|
+
:param url: 完整连接 URL;提供时忽略其他配置来源
|
|
130
|
+
:param variable: Windmill Variable 名(不含 u/admin/ 前缀),如 "data_db_prod";
|
|
131
|
+
提供时从 u/admin/{variable} 读取 JSON 配置自动构建连接,
|
|
132
|
+
忽略 db_setting/dbname。字段 key 默认取 DEFAULT_VAR_KEYS,
|
|
133
|
+
命名不一致时在 Variable 内写 key_map 字段指定
|
|
134
|
+
:param host_type: variable 模式下选用的 host,"internal"(默认)或 "external"
|
|
95
135
|
:param scheme: 驱动 scheme,默认 postgresql+psycopg2
|
|
96
136
|
:param connect_timeout: 建连超时秒数
|
|
97
137
|
:param echo: 是否打印 SQL 日志(调试用)
|
|
98
138
|
"""
|
|
139
|
+
if url is None and variable is not None:
|
|
140
|
+
db_setting, dbname = self._load_from_variable(variable, host_type)
|
|
141
|
+
|
|
99
142
|
if url is None:
|
|
100
143
|
if not db_setting or not dbname:
|
|
101
|
-
raise ValueError("需提供 db_setting + dbname,或直接提供 url")
|
|
144
|
+
raise ValueError("需提供 db_setting + dbname,或直接提供 url,或提供 variable(Windmill Variable 名)")
|
|
102
145
|
self.db_setting = db_setting
|
|
103
146
|
self.dbname = dbname
|
|
104
147
|
self.scheme = scheme or db_setting.get("scheme") or self.DEFAULT_SCHEME
|
|
@@ -134,6 +177,52 @@ class DbClient:
|
|
|
134
177
|
port = self.db_setting["port"]
|
|
135
178
|
return f"{self.scheme}://{user}:{pwd}@{host}:{port}/{self.dbname}"
|
|
136
179
|
|
|
180
|
+
@classmethod
|
|
181
|
+
def _load_from_variable(cls, variable: str, host_type: str) -> tuple[dict, str]:
|
|
182
|
+
"""从 Windmill Variable 读取数据库连接配置。
|
|
183
|
+
|
|
184
|
+
Variable 路径固定为 u/admin/{variable},内容为 JSON。
|
|
185
|
+
字段 key 默认取 DEFAULT_VAR_KEYS;若 Variable 内含 "key_map" 字段
|
|
186
|
+
(字段名 → 实际 key 的映射),则按 key_map 取值。
|
|
187
|
+
|
|
188
|
+
:param variable: Variable 名(不含 u/admin/ 前缀)
|
|
189
|
+
:param host_type: "internal" 或 "external",决定选用哪个 host key
|
|
190
|
+
:return: (db_setting, dbname)
|
|
191
|
+
"""
|
|
192
|
+
try:
|
|
193
|
+
import wmill
|
|
194
|
+
except ImportError:
|
|
195
|
+
raise ImportError(
|
|
196
|
+
"wmill 未安装。windmill 脚本运行时无需手动安装;"
|
|
197
|
+
"本地开发请运行: pip install wmill"
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
if host_type not in ("internal", "external"):
|
|
201
|
+
raise ValueError(f"host_type 只能是 'internal' 或 'external',收到: {host_type!r}")
|
|
202
|
+
|
|
203
|
+
var_path = f"{cls.VARIABLE_PREFIX}{variable}"
|
|
204
|
+
info = json.loads(wmill.get_variable(var_path))
|
|
205
|
+
|
|
206
|
+
# mapping 写在 Variable 内部(key_map 字段),没有则用默认 key 名
|
|
207
|
+
keys = {**DEFAULT_VAR_KEYS, **(info.get("key_map") or {})}
|
|
208
|
+
|
|
209
|
+
def pick(field: str) -> Any:
|
|
210
|
+
actual_key = keys[field]
|
|
211
|
+
if actual_key not in info:
|
|
212
|
+
raise KeyError(
|
|
213
|
+
f"Variable {var_path} 缺少字段 {actual_key}(对应 {field}),"
|
|
214
|
+
f"请在 Variable 的 key_map 中指定实际 key"
|
|
215
|
+
)
|
|
216
|
+
return info[actual_key]
|
|
217
|
+
|
|
218
|
+
db_setting = {
|
|
219
|
+
"user": pick("user"),
|
|
220
|
+
"pwd": pick("pwd"),
|
|
221
|
+
"host": pick(f"host_{host_type}"),
|
|
222
|
+
"port": pick("port"),
|
|
223
|
+
}
|
|
224
|
+
return db_setting, pick("dbname")
|
|
225
|
+
|
|
137
226
|
def _build_connect_args(self) -> dict:
|
|
138
227
|
"""构造 connect_args。
|
|
139
228
|
|
|
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
|