ezKit 1.12.29__py3-none-any.whl → 1.12.31__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.
- ezKit/database.py +4 -6
- ezKit/fastapix.py +78 -0
- {ezkit-1.12.29.dist-info → ezkit-1.12.31.dist-info}/METADATA +1 -1
- {ezkit-1.12.29.dist-info → ezkit-1.12.31.dist-info}/RECORD +7 -6
- {ezkit-1.12.29.dist-info → ezkit-1.12.31.dist-info}/WHEEL +0 -0
- {ezkit-1.12.29.dist-info → ezkit-1.12.31.dist-info}/licenses/LICENSE +0 -0
- {ezkit-1.12.29.dist-info → ezkit-1.12.31.dist-info}/top_level.txt +0 -0
ezKit/database.py
CHANGED
@@ -705,7 +705,7 @@ class DatabaseAsyncSession:
|
|
705
705
|
# --------------------------------------------------------------------------------------------------
|
706
706
|
|
707
707
|
|
708
|
-
async def
|
708
|
+
async def async_create_and_count(db: DatabaseAsyncSession, schema: Type[DeclarativeBase], data: list) -> dict:
|
709
709
|
|
710
710
|
# 插入数据
|
711
711
|
result = await db.operater(insert(schema), data)
|
@@ -720,7 +720,7 @@ async def db_async_create(db: DatabaseAsyncSession, schema: Type[DeclarativeBase
|
|
720
720
|
# --------------------------------------------------------------------------------------------------
|
721
721
|
|
722
722
|
|
723
|
-
async def
|
723
|
+
async def async_read_and_count(db: DatabaseAsyncSession, table_name: str, data: dict, statement_prefix: str, statement_end: str = "") -> dict:
|
724
724
|
|
725
725
|
# 初始返回结果 (适用于 count 等于 0)
|
726
726
|
result = {"count": 0, "data": None, "pageIndex": data["pageIndex"], "pageSize": 0}
|
@@ -759,8 +759,6 @@ async def db_async_read(db: DatabaseAsyncSession, table_name: str, data: dict, s
|
|
759
759
|
{statement_prefix}
|
760
760
|
{where_clause}
|
761
761
|
{statement_end}
|
762
|
-
LIMIT :limit
|
763
|
-
OFFSET :offset;
|
764
762
|
"""
|
765
763
|
).bindparams(*bind_params)
|
766
764
|
|
@@ -782,7 +780,7 @@ async def db_async_read(db: DatabaseAsyncSession, table_name: str, data: dict, s
|
|
782
780
|
# --------------------------------------------------------------------------------------------------
|
783
781
|
|
784
782
|
|
785
|
-
async def
|
783
|
+
async def async_update_and_count(db: DatabaseAsyncSession, table_name: str, data: dict) -> dict:
|
786
784
|
|
787
785
|
# 构建 WHERE
|
788
786
|
|
@@ -832,7 +830,7 @@ async def db_async_update(db: DatabaseAsyncSession, table_name: str, data: dict)
|
|
832
830
|
# --------------------------------------------------------------------------------------------------
|
833
831
|
|
834
832
|
|
835
|
-
async def
|
833
|
+
async def async_delete_and_count(db: DatabaseAsyncSession, table_name: str, data: dict) -> dict:
|
836
834
|
|
837
835
|
# 构建 WHERE
|
838
836
|
|
ezKit/fastapix.py
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# FastAPI Extensions
|
2
|
+
# pylint: disable=W0613
|
3
|
+
|
4
|
+
import logging
|
5
|
+
import sys
|
6
|
+
from typing import Any
|
7
|
+
|
8
|
+
from fastapi import FastAPI, Request
|
9
|
+
from fastapi.exceptions import RequestValidationError
|
10
|
+
from fastapi.responses import JSONResponse
|
11
|
+
from loguru import logger
|
12
|
+
from pydantic import ValidationError
|
13
|
+
from starlette.exceptions import HTTPException as StarletteHTTPException
|
14
|
+
|
15
|
+
# 移除默认控制台日志器
|
16
|
+
logger.remove()
|
17
|
+
|
18
|
+
# 日志输出到控制台
|
19
|
+
logger.add(sys.stdout, level="INFO", format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan> - <level>{message}</level>")
|
20
|
+
|
21
|
+
# 日志输出到文件
|
22
|
+
logger.add(
|
23
|
+
sink="logs/runtime.log", # 日志文件
|
24
|
+
rotation="10 MB", # 文件达到 10MB 自动轮转
|
25
|
+
retention="7 days", # 保留 7 天日志
|
26
|
+
compression="zip", # 超过的日志自动压缩
|
27
|
+
level="INFO", # 记录等级
|
28
|
+
encoding="utf-8", # 文件编码
|
29
|
+
enqueue=True, # 多线程、多进程安全
|
30
|
+
diagnose=True, # 显示变量值
|
31
|
+
backtrace=True, # 捕获堆栈追踪
|
32
|
+
)
|
33
|
+
|
34
|
+
|
35
|
+
def Response(code: int = 200, data: Any = None, message: str | None = None, status_code: int = 200):
|
36
|
+
return JSONResponse(content={"code": code, "data": data, "message": message}, status_code=status_code)
|
37
|
+
|
38
|
+
|
39
|
+
def exceptions(app: FastAPI):
|
40
|
+
|
41
|
+
@app.exception_handler(StarletteHTTPException)
|
42
|
+
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
43
|
+
logger.warning(f"HTTP Exception: {exc.detail}")
|
44
|
+
return Response(status_code=exc.status_code, code=exc.status_code, message=exc.detail)
|
45
|
+
|
46
|
+
# 参数验证错误
|
47
|
+
@app.exception_handler(RequestValidationError)
|
48
|
+
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
49
|
+
logger.warning(f"Request Validation Error: {exc.errors()}")
|
50
|
+
return Response(code=422, data=exc.errors(), message="Request Validation Error")
|
51
|
+
|
52
|
+
# 参数验证错误
|
53
|
+
@app.exception_handler(ValidationError)
|
54
|
+
async def pydantic_validation_error_handler(request: Request, exc: ValidationError):
|
55
|
+
logger.warning(f"Pydantic Validation Error: {exc.errors()}")
|
56
|
+
return Response(code=422, data=exc.errors(), message="Pydantic Validation Error")
|
57
|
+
|
58
|
+
# 服务器内部错误
|
59
|
+
@app.exception_handler(Exception)
|
60
|
+
async def global_exception_handler(request: Request, exc: Exception):
|
61
|
+
logger.exception("Unhandled Exception")
|
62
|
+
return Response(code=500, data=None, message="Server Internal Error")
|
63
|
+
|
64
|
+
|
65
|
+
# 兼容 FastAPI/Uvicorn 的 logging(重要)
|
66
|
+
class InterceptHandler(logging.Handler):
|
67
|
+
"""Intercept Handler"""
|
68
|
+
|
69
|
+
def emit(self, record):
|
70
|
+
logger_opt = logger.opt(depth=6, exception=record.exc_info)
|
71
|
+
logger_opt.log(record.levelname, record.getMessage())
|
72
|
+
|
73
|
+
def write(self, message: str):
|
74
|
+
if message.strip():
|
75
|
+
logger.info(message.strip())
|
76
|
+
|
77
|
+
def flush(self):
|
78
|
+
pass
|
@@ -3,9 +3,10 @@ ezKit/_file.py,sha256=0qRZhwYuagTgTGrhm-tzAMvEQT4HTJA_xZKqF2bo0ho,1207
|
|
3
3
|
ezKit/bottle.py,sha256=43h4v1kzz6qrLvCt5IMN0H-gFtaT0koG9wETqteXsps,181666
|
4
4
|
ezKit/bottle_extensions.py,sha256=27rogmfK7mL2qUSjXH79IMGZbCVULtYEikql_N9O6Zs,1165
|
5
5
|
ezKit/cipher.py,sha256=7jBarRH7ukSYzkz-Anl8B8JzluhnRz4CLHidPRRj_cg,2939
|
6
|
-
ezKit/database.py,sha256=
|
6
|
+
ezKit/database.py,sha256=W3xV_0o317oRaxZJWt_XithwzfL5HcTiGOyqpj07qNk,30101
|
7
7
|
ezKit/dockerhub.py,sha256=j-wQO-71BsOgExHZhYynuy2k_hCX3on-vg0TH7QCit4,1996
|
8
8
|
ezKit/errors.py,sha256=wLLzJ-YWCdAUqdLzsHM-jfnfZJL9pHtZoG_O_bVNI_A,1334
|
9
|
+
ezKit/fastapix.py,sha256=59wqVLqJxNVesZTAwAqkzkiMQHKQQbVUijFP-IDu4ws,2935
|
9
10
|
ezKit/http.py,sha256=zhNxJF-x91UqGncXWxVXnhZVpFo_wmmpGnMXVT11y9E,1832
|
10
11
|
ezKit/markdown_to_html.template,sha256=21G2sSVGJn6aJvHd0NN4zY5YiDteKe4UtW36AzBwSdk,22274
|
11
12
|
ezKit/mongo.py,sha256=vsRCjJ2uWbNp-1bnGKICPohNYx25El8XnHp10o-lsM4,2397
|
@@ -16,8 +17,8 @@ ezKit/token.py,sha256=Ac-i9xfq4TqpGyfCzakjrh4NYzxHiN2sCQrMk1tzVi8,1716
|
|
16
17
|
ezKit/utils.py,sha256=U457ahFkxIXuB-qWvS3995xJs-LlkFIX5_ZWVgmL5cY,43130
|
17
18
|
ezKit/xftp.py,sha256=-XQXyhMqeigT63P6sXkSS7r4GROXyqqlkzKxITLWG-g,8278
|
18
19
|
ezKit/zabbix.py,sha256=PkMnfu7mcuotwwIIsHaC9FsNg-gap6hD1xvm0AwSL1Y,33777
|
19
|
-
ezkit-1.12.
|
20
|
-
ezkit-1.12.
|
21
|
-
ezkit-1.12.
|
22
|
-
ezkit-1.12.
|
23
|
-
ezkit-1.12.
|
20
|
+
ezkit-1.12.31.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
21
|
+
ezkit-1.12.31.dist-info/METADATA,sha256=8Ikl0M9e8okfWrbNyado9vHBAloM8KpLQTWynydtP_8,317
|
22
|
+
ezkit-1.12.31.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
23
|
+
ezkit-1.12.31.dist-info/top_level.txt,sha256=aYLB_1WODsqNTsTFWcKP-BN0KCTKcV-HZJ4zlHkCFw8,6
|
24
|
+
ezkit-1.12.31.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|