sycommon-python-lib 0.1.1__tar.gz → 0.1.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.

Potentially problematic release.


This version of sycommon-python-lib might be problematic. Click here for more details.

Files changed (52) hide show
  1. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/PKG-INFO +2 -1
  2. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/pyproject.toml +2 -1
  3. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/config/EmbeddingConfig.py +0 -1
  4. sycommon_python_lib-0.1.3/src/sycommon/config/MQConfig.py +15 -0
  5. sycommon_python_lib-0.1.3/src/sycommon/database/base_db_service.py +30 -0
  6. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/logging/kafka_log.py +14 -38
  7. sycommon_python_lib-0.1.3/src/sycommon/middleware/middleware.py +39 -0
  8. sycommon_python_lib-0.1.3/src/sycommon/middleware/mq.py +9 -0
  9. sycommon_python_lib-0.1.3/src/sycommon/models/base_http.py +99 -0
  10. sycommon_python_lib-0.1.3/src/sycommon/models/mqlistener_config.py +38 -0
  11. sycommon_python_lib-0.1.3/src/sycommon/models/mqmsg_model.py +11 -0
  12. sycommon_python_lib-0.1.3/src/sycommon/models/mqsend_config.py +8 -0
  13. sycommon_python_lib-0.1.3/src/sycommon/models/sso_user.py +60 -0
  14. sycommon_python_lib-0.1.3/src/sycommon/rabbitmq/rabbitmq_client.py +721 -0
  15. sycommon_python_lib-0.1.3/src/sycommon/rabbitmq/rabbitmq_service.py +476 -0
  16. sycommon_python_lib-0.1.3/src/sycommon/services.py +217 -0
  17. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/synacos/feign.py +14 -10
  18. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/synacos/nacos_service.py +252 -188
  19. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon_python_lib.egg-info/PKG-INFO +2 -1
  20. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon_python_lib.egg-info/SOURCES.txt +10 -0
  21. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon_python_lib.egg-info/requires.txt +1 -0
  22. sycommon_python_lib-0.1.1/src/sycommon/middleware/middleware.py +0 -32
  23. sycommon_python_lib-0.1.1/src/sycommon/services.py +0 -29
  24. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/README.md +0 -0
  25. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/setup.cfg +0 -0
  26. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/__init__.py +0 -0
  27. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/config/Config.py +0 -0
  28. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/config/DatabaseConfig.py +0 -0
  29. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/config/LLMConfig.py +0 -0
  30. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/config/RerankerConfig.py +0 -0
  31. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/config/__init__.py +0 -0
  32. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/database/database_service.py +0 -0
  33. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/health/__init__.py +0 -0
  34. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/health/health_check.py +0 -0
  35. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/health/ping.py +0 -0
  36. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/logging/__init__.py +0 -0
  37. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/logging/logger_wrapper.py +0 -0
  38. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/middleware/__init__.py +0 -0
  39. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/middleware/context.py +0 -0
  40. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/middleware/cors.py +0 -0
  41. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/middleware/exception.py +0 -0
  42. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/middleware/monitor_memory.py +0 -0
  43. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/middleware/timeout.py +0 -0
  44. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/middleware/traceid.py +0 -0
  45. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/models/__init__.py +0 -0
  46. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/models/log.py +0 -0
  47. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/synacos/__init__.py +0 -0
  48. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/tools/__init__.py +0 -0
  49. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/tools/snowflake.py +0 -0
  50. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon/tools/timing.py +0 -0
  51. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon_python_lib.egg-info/dependency_links.txt +0 -0
  52. {sycommon_python_lib-0.1.1 → sycommon_python_lib-0.1.3}/src/sycommon_python_lib.egg-info/top_level.txt +0 -0
@@ -1,9 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sycommon-python-lib
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: Add your description here
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
7
+ Requires-Dist: aio-pika>=9.5.7
7
8
  Requires-Dist: aiohttp>=3.12.13
8
9
  Requires-Dist: decorator>=5.2.1
9
10
  Requires-Dist: fastapi>=0.115.14
@@ -1,10 +1,11 @@
1
1
  [project]
2
2
  name = "sycommon-python-lib"
3
- version = "0.1.1"
3
+ version = "0.1.3"
4
4
  description = "Add your description here"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
7
7
  dependencies = [
8
+ "aio-pika>=9.5.7",
8
9
  "aiohttp>=3.12.13",
9
10
  "decorator>=5.2.1",
10
11
  "fastapi>=0.115.14",
@@ -0,0 +1,15 @@
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class MQConfig(BaseModel):
5
+ host: str
6
+ port: int
7
+ username: str
8
+ password: str
9
+ publisherConfirms: bool
10
+ publisherConfirmType: str
11
+ virtualHost: str
12
+
13
+
14
+ class MQConsumer(BaseModel):
15
+ enabled: bool
@@ -0,0 +1,30 @@
1
+ from contextlib import contextmanager
2
+ from sqlalchemy.orm import sessionmaker
3
+ from sycommon.config.Config import SingletonMeta
4
+ from sycommon.database.database_service import DatabaseService
5
+ from sycommon.logging.kafka_log import SYLogger
6
+
7
+
8
+ class BaseDBService(metaclass=SingletonMeta):
9
+ """数据库操作基础服务类,封装会话管理功能"""
10
+
11
+ def __init__(self):
12
+ self.engine = DatabaseService.engine()
13
+ self.Session = sessionmaker(bind=self.engine)
14
+
15
+ @contextmanager
16
+ def session(self):
17
+ """
18
+ 数据库会话上下文管理器
19
+ 自动处理会话的创建、提交、回滚和关闭
20
+ """
21
+ session = self.Session()
22
+ try:
23
+ yield session
24
+ session.commit()
25
+ except Exception as e:
26
+ session.rollback()
27
+ SYLogger.error(f"Database operation failed: {str(e)}")
28
+ raise
29
+ finally:
30
+ session.close()
@@ -1,3 +1,4 @@
1
+ import os
1
2
  import sys
2
3
  import traceback
3
4
  import asyncio
@@ -8,10 +9,10 @@ import re
8
9
  import socket
9
10
  import time
10
11
  import threading
11
- import loguru
12
12
  from queue import Queue, Full, Empty
13
13
  from kafka import KafkaProducer
14
14
  from loguru import logger
15
+ import loguru
15
16
  from sycommon.config.Config import Config, SingletonMeta
16
17
  from sycommon.middleware.context import current_trace_id
17
18
  from sycommon.tools.snowflake import Snowflake
@@ -39,7 +40,7 @@ class KafkaLogger(metaclass=SingletonMeta):
39
40
  _queue_warning_interval = 60 # 秒
40
41
  _last_queue_warning = 0
41
42
  _shutdown_timeout = 15 # 关闭超时时间,秒
42
- _config = None # 新增配置变量存储
43
+ _config = None # 配置变量存储
43
44
 
44
45
  @staticmethod
45
46
  def setup_logger(config: dict):
@@ -84,38 +85,6 @@ class KafkaLogger(metaclass=SingletonMeta):
84
85
  # 设置全局异常处理器
85
86
  sys.excepthook = KafkaLogger._handle_exception
86
87
 
87
- def pars_log(record):
88
- match = KafkaLogger._log_pattern.match(record.strip())
89
- if match:
90
- time_str = match.group(1)
91
- level = match.group(2)
92
- module = match.group(3)
93
- function = match.group(4)
94
- line = int(match.group(5))
95
- message_str = match.group(6)
96
-
97
- try:
98
- message = json.loads(message_str)
99
- except json.JSONDecodeError:
100
- message = message_str
101
-
102
- return {
103
- "time": time_str,
104
- "level": level,
105
- "module": module,
106
- "function": function,
107
- "line": line,
108
- "message": message
109
- }
110
- else:
111
- # 使用当前时间避免重复创建相同的时间字符串
112
- current_time = datetime.now()
113
- return {
114
- "message": record,
115
- "level": "ERROR",
116
- "time": current_time.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
117
- }
118
-
119
88
  def custom_log_handler(record):
120
89
  # 检查record是否是Message对象
121
90
  if isinstance(record, loguru._handler.Message):
@@ -156,7 +125,7 @@ class KafkaLogger(metaclass=SingletonMeta):
156
125
 
157
126
  # 检查是否有错误信息并设置detail字段
158
127
  error_detail = ""
159
- if level == "ERROR" and record.get("exception"):
128
+ if level == "ERROR" and record["exception"] is not None:
160
129
  error_detail = "".join(traceback.format_exception(
161
130
  record["exception"].type,
162
131
  record["exception"].value,
@@ -506,12 +475,19 @@ class SYLogger:
506
475
  }
507
476
 
508
477
  # 选择日志级别
478
+ _log = ''
509
479
  if level == "ERROR":
510
- logger.error(json.dumps(request_log, ensure_ascii=False))
480
+ _log = json.dumps(request_log, ensure_ascii=False)
481
+ logger.error(_log)
511
482
  elif level == "WARNING":
512
- logger.warning(json.dumps(request_log, ensure_ascii=False))
483
+ _log = json.dumps(request_log, ensure_ascii=False)
484
+ logger.warning(_log)
513
485
  else:
514
- logger.info(json.dumps(request_log, ensure_ascii=False))
486
+ _log = json.dumps(request_log, ensure_ascii=False)
487
+ logger.info(_log)
488
+
489
+ if os.getenv('DEV-LOG', 'false').lower() == 'true':
490
+ print(_log)
515
491
 
516
492
  @staticmethod
517
493
  def info(msg: any):
@@ -0,0 +1,39 @@
1
+ from sycommon.health.ping import setup_ping_handler
2
+ from sycommon.middleware.cors import setup_cors_handler
3
+ from sycommon.middleware.exception import setup_exception_handler
4
+ from sycommon.middleware.monitor_memory import setup_monitor_memory_middleware
5
+ from sycommon.middleware.mq import setup_mq_middleware
6
+ from sycommon.middleware.timeout import setup_request_timeout_middleware
7
+ from sycommon.middleware.traceid import setup_trace_id_handler
8
+ from sycommon.health.health_check import setup_health_handler
9
+
10
+
11
+ class Middleware:
12
+
13
+ @classmethod
14
+ def setup_middleware(cls, app, config: dict):
15
+ # 设置请求超时中间件
16
+ app = setup_request_timeout_middleware(app, config)
17
+
18
+ # 设置异常处理
19
+ app = setup_exception_handler(app, config)
20
+
21
+ # 设置 trace_id 处理中间件
22
+ app = setup_trace_id_handler(app)
23
+
24
+ # 设置内存监控中间件
25
+ # app = setup_monitor_memory_middleware(app)
26
+
27
+ # 设置cors
28
+ # app = setup_cors_handler(app)
29
+
30
+ # 健康检查
31
+ app = setup_health_handler(app)
32
+
33
+ # ping
34
+ app = setup_ping_handler(app)
35
+
36
+ # 添加mq中间件
37
+ # app = setup_mq_middleware(app)
38
+
39
+ return app
@@ -0,0 +1,9 @@
1
+
2
+ from sycommon.services import Services
3
+
4
+
5
+ def setup_mq_middleware(app):
6
+ @app.on_event("shutdown")
7
+ async def shutdown_event():
8
+ await Services.shutdown()
9
+ return app
@@ -0,0 +1,99 @@
1
+ from typing import TypeVar, Any, Generic
2
+ from pydantic import BaseModel, Field
3
+ from fastapi.responses import JSONResponse
4
+ from fastapi import status
5
+
6
+ # 修改泛型约束,支持任意类型(包括基础类型和BaseModel)
7
+ T = TypeVar('T')
8
+
9
+
10
+ class BaseResponseModel(BaseModel, Generic[T]):
11
+ """基础响应模型,支持多种数据类型(包括字符串、字典和Pydantic模型)"""
12
+ code: int = Field(default=0, description="业务响应码,成功0,失败非0")
13
+ message: str = Field(default="success", description="业务响应信息")
14
+ data: T | None = Field(default=None, description="业务响应数据,支持任意类型")
15
+ traceId: str | None = Field(default=None, description="请求链路追踪ID")
16
+
17
+ class Config:
18
+ arbitrary_types_allowed = True
19
+ from_attributes = True
20
+
21
+
22
+ def build_response_content(
23
+ data: T | Any = None,
24
+ code: int = 0,
25
+ message: str = "success"
26
+ ) -> dict:
27
+ """
28
+ 只构建响应内容的字典部分
29
+
30
+ Args:
31
+ data: 响应数据(支持字符串、字典、Pydantic模型等)
32
+ code: 业务响应码
33
+ message: 响应信息
34
+
35
+ Returns:
36
+ 响应内容字典,格式为{"code": int, "message": str, "data": Any}
37
+ """
38
+ response = BaseResponseModel(
39
+ code=code,
40
+ message=message,
41
+ data=data
42
+ )
43
+
44
+ if isinstance(response.data, BaseModel):
45
+ return {
46
+ "code": response.code,
47
+ "message": response.message,
48
+ "data": response.data.model_dump()
49
+ }
50
+ else:
51
+ return response.model_dump()
52
+
53
+
54
+ def create_response(
55
+ data: T | Any = None,
56
+ code: int = 0,
57
+ message: str = "success",
58
+ status_code: int = status.HTTP_200_OK
59
+ ) -> JSONResponse:
60
+ """创建完整的JSONResponse响应"""
61
+ content = build_response_content(data=data, code=code, message=message)
62
+ return JSONResponse(
63
+ content=content,
64
+ status_code=status_code
65
+ )
66
+
67
+
68
+ def success_response(data: T | Any = None, message: str = "success") -> JSONResponse:
69
+ """快捷创建成功响应"""
70
+ return create_response(data=data, message=message)
71
+
72
+
73
+ def error_response(
74
+ message: str = "error",
75
+ code: int = 1,
76
+ data: T | Any = None,
77
+ status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR
78
+ ) -> JSONResponse:
79
+ """快捷创建错误响应"""
80
+ return create_response(
81
+ data=data,
82
+ code=code,
83
+ message=message,
84
+ status_code=status_code
85
+ )
86
+
87
+
88
+ def success_content(data: T | Any = None, message: str = "success") -> dict:
89
+ """只构建成功响应的内容字典"""
90
+ return build_response_content(data=data, message=message)
91
+
92
+
93
+ def error_content(
94
+ message: str = "error",
95
+ code: int = 1,
96
+ data: T | Any = None
97
+ ) -> dict:
98
+ """只构建错误响应的内容字典"""
99
+ return build_response_content(data=data, code=code, message=message)
@@ -0,0 +1,38 @@
1
+ from pydantic import BaseModel, Field
2
+ from typing import Callable, Coroutine, Optional
3
+ from aio_pika.abc import AbstractIncomingMessage
4
+
5
+ from sycommon.models.mqmsg_model import MQMsgModel
6
+
7
+
8
+ class RabbitMQListenerConfig(BaseModel):
9
+ """RabbitMQ监听器配置模型"""
10
+ # 监听器唯一名称
11
+ # name: str = Field(..., description="监听器唯一标识名称")
12
+
13
+ # 队列配置
14
+ queue_name: str = Field(..., description="队列名称")
15
+ # 使用.分割queue_name取出第一部分
16
+ # routing_key: str = Field(..., description="路由键")
17
+
18
+ # 消息处理器
19
+ handler: Callable[[MQMsgModel, AbstractIncomingMessage], Coroutine] | None = Field(
20
+ None, description="消息处理函数"
21
+ )
22
+
23
+ # 可选配置参数
24
+ host: Optional[str] = Field(None, description="RabbitMQ主机地址")
25
+ port: Optional[int] = Field(None, description="RabbitMQ端口")
26
+ username: Optional[str] = Field(None, description="用户名")
27
+ password: Optional[str] = Field(None, description="密码")
28
+ virtualhost: Optional[str] = Field(None, description="虚拟主机")
29
+ exchange_name: Optional[str] = Field(None, description="交换机名称")
30
+ exchange_type: str = Field("topic", description="交换机类型")
31
+ durable: bool = Field(True, description="是否持久化")
32
+ auto_delete: bool = Field(False, description="是否自动删除队列")
33
+ auto_parse_json: bool = Field(True, description="是否自动解析JSON消息")
34
+
35
+ class Config:
36
+ """模型配置"""
37
+ # 允许存储函数类型
38
+ arbitrary_types_allowed = True
@@ -0,0 +1,11 @@
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class MQMsgModel(BaseModel):
5
+ topicCode: str
6
+ msg: str | None = None
7
+ correlationDataId: str | None = None
8
+ dataKey: str | None = None
9
+ manualFlag: bool | None = None
10
+ groupId: str | None = None
11
+ traceId: str | None = None
@@ -0,0 +1,8 @@
1
+ from typing import Optional
2
+ from sycommon.models.mqlistener_config import RabbitMQListenerConfig
3
+
4
+
5
+ class RabbitMQSendConfig(RabbitMQListenerConfig):
6
+ """MQ消息发送配置模型,继承自监听器配置"""
7
+ # 是否自动连接
8
+ auto_connect: bool = True
@@ -0,0 +1,60 @@
1
+ from pydantic import BaseModel
2
+ from typing import List, Optional
3
+ from datetime import datetime
4
+
5
+
6
+ class SsoUser(BaseModel):
7
+ """SSO用户模型,对应Java的SsoUser类"""
8
+ tenant_id: Optional[str] = None
9
+ customer_id: Optional[str] = None
10
+ customer_name: Optional[str] = None
11
+ user_type: Optional[str] = None # 用户类型
12
+ auth_type: Optional[str] = None # 认证类型
13
+ system_type: Optional[str] = None # 系统类型
14
+ role_type: Optional[str] = None # 角色类型
15
+ user_id: Optional[str] = None # 用户代码
16
+ user_name: Optional[str] = None # 用户名称
17
+ real_name: Optional[str] = None # 用户真实名称
18
+ user_status: Optional[str] = None # 用户状态
19
+ last_logon_time: Optional[datetime] = None # 上次登录时间
20
+ num_logon_try: Optional[int] = None # 登录重试次数
21
+ working_right_codes: Optional[List[str]] = None # 用户资源代码
22
+ group_ids: Optional[List[str]] = None # 用户组Ids
23
+ password: Optional[str] = None # 密码
24
+ old_password: Optional[str] = None # 盛易通旧密码
25
+ request_path: Optional[str] = None # 请求ID
26
+ pwd_expiry_date: Optional[datetime] = None # 过期时间
27
+ model_flag: Optional[str] = None # 当前模式
28
+ sign_key: Optional[str] = None # 签名key
29
+ security_token: Optional[str] = None # 安全秘钥
30
+ mur: Optional[str] = None # 指纹
31
+ init_status: str = "N" # 默认是N初始化(没有权限),Y表示权限初始化
32
+ mobile_brand: Optional[str] = None # 手机品牌
33
+ mobile_type: Optional[str] = None # 手机型号
34
+ mobile_system_version: Optional[str] = None # 系统版本
35
+ platform: Optional[str] = None # 手机平台
36
+ ip: Optional[str] = None # ip地址
37
+ browser_type: Optional[str] = None # 浏览器类型
38
+ os_type: Optional[str] = None # 系统类型
39
+ access_token: Optional[str] = None # 用于给在线用户使用
40
+
41
+ # 申请渠道 PC-“PC”、APP-“APP”、SYSTEM-“系统生成”、YYD-“租户端”
42
+ req_type: Optional[str] = None
43
+ pri_req_source: Optional[str] = None # 原始请求来源
44
+ req_source: Optional[str] = None # 请求来源
45
+ identitys: Optional[List[str]] = None # 客户身份
46
+
47
+ # 修可登录终端,为空默认全部,1=PC,2=小程序,9=其他
48
+ login_terminal: Optional[str] = None
49
+
50
+ # 用户来源 3=无担保撮合(小微贷)
51
+ user_source: Optional[str] = None
52
+
53
+ # 登录方式 手机验证码登录:phoneCode
54
+ login_type: Optional[str] = None
55
+
56
+ # 是否个人账户 Y是/N否
57
+ is_person_user: Optional[str] = None
58
+
59
+ # 请求上下文traceId,用于MQ重发等场景
60
+ trace_id: Optional[str] = None