ezKit 3.0.43__tar.gz → 3.0.45__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezKit
3
- Version: 3.0.43
3
+ Version: 3.0.45
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -1,3 +1,5 @@
1
+ """Cipher library"""
2
+
1
3
  import base64
2
4
  from dataclasses import dataclass
3
5
  from os import environ, urandom
@@ -10,6 +12,7 @@ from cryptography.hazmat.primitives.kdf.hkdf import HKDF
10
12
  from loguru import logger
11
13
 
12
14
  DEBUG = bool(environ.get("DEBUG"))
15
+
13
16
  _SERIALIZED_PREFIX = b"\xffEZK1"
14
17
 
15
18
 
@@ -1,4 +1,4 @@
1
- """Database Library"""
1
+ """Database library"""
2
2
 
3
3
  from contextlib import asynccontextmanager, contextmanager
4
4
  from os import environ
@@ -1,3 +1,5 @@
1
+ """FastAPI extension"""
2
+
1
3
  import logging
2
4
  import sys
3
5
  from collections.abc import Awaitable, Callable
@@ -94,26 +96,25 @@ def Response(code: int = 200, data: Any = None, message: str | None = None, stat
94
96
  def exceptions(app: FastAPI):
95
97
 
96
98
  @app.exception_handler(StarletteHTTPException)
97
- async def http_exception_handler(request: Request, exc: StarletteHTTPException): # pylint: disable=unused-argument
99
+ async def http_exception_handler(request: Request, exc: StarletteHTTPException): # pylint: disable=W0613
98
100
  logger.warning(f"HTTP Exception: {exc.detail}")
99
101
  return Response(code=exc.status_code, message=f"HTTP Exception: {exc.detail}", status_code=exc.status_code)
100
- # return JSONResponse(content={"code": exc.status_code, "data": None, "message": exc.detail}, status_code=exc.status_code)
101
102
 
102
103
  # 参数验证错误
103
104
  @app.exception_handler(RequestValidationError)
104
- async def validation_exception_handler(request: Request, exc: RequestValidationError): # pylint: disable=unused-argument
105
+ async def validation_exception_handler(request: Request, exc: RequestValidationError): # pylint: disable=W0613
105
106
  logger.warning(f"Request Validation Error: {exc.errors()}")
106
107
  return Response(code=422, message=f"Request Validation Error: {exc.errors()}")
107
108
 
108
109
  # 参数验证错误
109
110
  @app.exception_handler(ValidationError)
110
- async def pydantic_validation_error_handler(request: Request, exc: ValidationError): # pylint: disable=unused-argument
111
+ async def pydantic_validation_error_handler(request: Request, exc: ValidationError): # # pylint: disable=W0613
111
112
  logger.warning(f"Pydantic Validation Error: {exc.errors()}")
112
113
  return Response(code=422, message=f"Pydantic Validation Error: {exc.errors()}")
113
114
 
114
115
  # 内部服务错误
115
116
  @app.exception_handler(Exception)
116
- async def global_exception_handler(request: Request, exc: Exception): # pylint: disable=unused-argument
117
+ async def global_exception_handler(request: Request, exc: Exception): # pylint: disable=W0613
117
118
  logger.exception(f"Internal Server Error: {exc}")
118
119
  return Response(code=500, message=f"Internal Server Error: {exc}")
119
120
 
@@ -145,7 +146,7 @@ async def generic_api_execute(
145
146
  params_object: Any,
146
147
  params_type: str = "text",
147
148
  ) -> JSONResponse:
148
- """通用执行函数"""
149
+ """Generic API Execution"""
149
150
 
150
151
  # param: operation 函数
151
152
  # param: params_object 参数对象
@@ -207,7 +208,7 @@ async def generic_database_create(
207
208
  model: type[DeclarativeBase],
208
209
  params: dict[str, Any] | list[dict[str, Any]],
209
210
  ) -> dict | None:
210
- """数据库通用创建对象函数"""
211
+ """Generic Database Create"""
211
212
 
212
213
  try:
213
214
 
@@ -238,7 +239,7 @@ async def generic_database_read(
238
239
  model: type[DeclarativeBase],
239
240
  params: dict[str, Any],
240
241
  ) -> list | None:
241
- """数据库通用读取对象函数"""
242
+ """Generic Database Read"""
242
243
 
243
244
  try:
244
245
 
@@ -274,7 +275,7 @@ async def generic_database_update(
274
275
  model: type[DeclarativeBase],
275
276
  params: dict[str, Any] | list[dict[str, Any]],
276
277
  ) -> dict | None:
277
- """数据库通用更新对象函数"""
278
+ """Generic Database Update"""
278
279
 
279
280
  try:
280
281
 
@@ -333,7 +334,7 @@ async def generic_database_delete(
333
334
  model: type[DeclarativeBase],
334
335
  params: dict[str, Any] | list[dict[str, Any]],
335
336
  ) -> dict | None:
336
- """数据库通用删除对象函数"""
337
+ """Generic Database Delete"""
337
338
 
338
339
  try:
339
340
 
@@ -342,7 +343,10 @@ async def generic_database_delete(
342
343
  if not conditions:
343
344
  return {"count": 0}
344
345
 
345
- filters = [and_(true(), *(getattr(model, key) == value for key, value in condition.items())) for condition in conditions]
346
+ filters = [
347
+ and_(true(), *(getattr(model, key) == value for key, value in condition.items()))
348
+ for condition in conditions
349
+ ]
346
350
 
347
351
  stmt = delete(model).where(or_(*filters))
348
352
 
@@ -1,4 +1,4 @@
1
- """HTTP Library"""
1
+ """HTTP library"""
2
2
 
3
3
  from os import environ
4
4
 
@@ -1,3 +1,5 @@
1
+ """Markdown extension"""
2
+
1
3
  import re
2
4
  from os import environ
3
5
 
@@ -1,3 +1,5 @@
1
+ """Send eMail"""
2
+
1
3
  from email.header import Header
2
4
  from email.mime.image import MIMEImage
3
5
  from email.mime.multipart import MIMEMultipart
@@ -1,4 +1,4 @@
1
- """股票"""
1
+ """Stock library"""
2
2
 
3
3
  from copy import deepcopy
4
4
  from os import environ
@@ -1,4 +1,4 @@
1
- """Utils Library"""
1
+ """Utils library"""
2
2
 
3
3
  import asyncio
4
4
  import subprocess
@@ -11,6 +11,7 @@ from functools import wraps
11
11
  from itertools import islice
12
12
  from os import environ
13
13
  from pathlib import Path
14
+ from secrets import choice
14
15
  from typing import Any, Callable, Coroutine, ParamSpec, TypeVar
15
16
  from uuid import UUID
16
17
 
@@ -603,3 +604,37 @@ def shell(
603
604
  logger.error(e)
604
605
 
605
606
  return None
607
+
608
+
609
+ # --------------------------------------------------------------------------------------------------
610
+
611
+
612
+ _ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
613
+
614
+
615
+ def generate_random_id(alphabet: str = _ALPHABET, size: int = 12) -> str | None:
616
+ """Generate a cryptographically random ID from a unique alphabet"""
617
+
618
+ # 基于唯一字符集生成密码学安全的随机 ID
619
+ # 用于生成唯一短码
620
+
621
+ try:
622
+
623
+ if not isinstance(alphabet, str) or not isinstance(size, int):
624
+ return None
625
+
626
+ size = 12 if size < 1 else size
627
+
628
+ if len(alphabet) < 2 or len(set(alphabet)) != len(alphabet):
629
+ alphabet = _ALPHABET
630
+
631
+ return "".join(choice(alphabet) for _ in range(size))
632
+
633
+ except Exception as e:
634
+
635
+ if DEBUG:
636
+ logger.exception(e)
637
+ else:
638
+ logger.error(e)
639
+
640
+ return None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezKit
3
- Version: 3.0.43
3
+ Version: 3.0.45
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -4,7 +4,7 @@ from setuptools import find_packages, setup
4
4
 
5
5
  setup(
6
6
  name="ezKit",
7
- version="3.0.43",
7
+ version="3.0.45",
8
8
  author="septvean",
9
9
  author_email="septvean@gmail.com",
10
10
  description="Easy Kit",
File without changes
File without changes
File without changes
File without changes
@@ -1,6 +1,6 @@
1
1
  [tool.ruff]
2
- target-version = "py313"
3
2
  line-length = 120
3
+ target-version = "py313"
4
4
 
5
5
  [tool.ruff.lint]
6
6
  select = ["I"]
File without changes