mirror of
https://github.com/ZGCA-Forge/MsgCenterPy.git
synced 2025-12-15 13:34:36 +00:00
Compare commits
36 Commits
0.0.4
...
e5b99ca134
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e5b99ca134 | ||
|
|
31c89ccc26 | ||
|
|
58628910f4 | ||
|
|
4153d99344 | ||
|
|
3d0cdcd423 | ||
|
|
419ed15d70 | ||
|
|
5262e9bfd5 | ||
|
|
2d5fd8b74e | ||
|
|
043f934a42 | ||
|
|
0faaa9ebdb | ||
|
|
c4d55fed1b | ||
|
|
0684e823c0 | ||
|
|
98e0b9966e | ||
|
|
041710d331 | ||
|
|
cb9dd57f19 | ||
|
|
3d032c0b98 | ||
|
|
1ccf2d4ac7 | ||
|
|
477d7075d5 | ||
|
|
ec2fafb363 | ||
|
|
74e7a15ef9 | ||
|
|
661ee43a1c | ||
|
|
0dff62e738 | ||
|
|
823b04dcbc | ||
|
|
80726163dd | ||
|
|
656ef7ca80 | ||
|
|
cc394fd63c | ||
|
|
2d83bad8ce | ||
|
|
9d52781134 | ||
|
|
89169ca7be | ||
|
|
cda31464d3 | ||
|
|
20a904ef8a | ||
|
|
d8070b5e49 | ||
|
|
d5704e78c3 | ||
|
|
9d6d21de62 | ||
|
|
502d4c4c04 | ||
|
|
02f3216456 |
@@ -1,5 +1,5 @@
|
||||
[bumpversion]
|
||||
current_version = 0.0.4
|
||||
current_version = 0.1.5
|
||||
commit = True
|
||||
tag = True
|
||||
tag_name = v{new_version}
|
||||
|
||||
53
.github/workflows/ci.yml
vendored
53
.github/workflows/ci.yml
vendored
@@ -16,10 +16,10 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
uses: actions/setup-python@v6
|
||||
with:
|
||||
python-version: "3.10" # Use minimum version for consistency
|
||||
|
||||
@@ -40,10 +40,10 @@ jobs:
|
||||
needs: [code-format] # Only run after code formatting passes
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Set up Python 3.10
|
||||
uses: actions/setup-python@v5
|
||||
uses: actions/setup-python@v6
|
||||
with:
|
||||
python-version: "3.10"
|
||||
|
||||
@@ -72,7 +72,7 @@ jobs:
|
||||
needs: [basic-build] # Only run after basic build passes
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Setup Miniconda
|
||||
uses: conda-incubator/setup-miniconda@v3
|
||||
@@ -112,23 +112,17 @@ jobs:
|
||||
needs: [basic-build] # Run in parallel with ROS2 test after basic build
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
- name: Run Safety CLI to check for vulnerabilities
|
||||
uses: pyupio/safety-action@v1
|
||||
with:
|
||||
python-version: "3.10" # Use minimum version for consistency
|
||||
|
||||
- name: Install security tools
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install "safety>=3.0.0" "typer<0.12.0" "marshmallow<4.0.0"
|
||||
|
||||
- name: Run safety security scan
|
||||
run: safety check --output json > safety-report.json
|
||||
api-key: ${{ secrets.SAFETY_CHECK }}
|
||||
output-format: json
|
||||
args: --detailed-output --output-format json
|
||||
|
||||
- name: Upload security reports
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@v5
|
||||
with:
|
||||
name: security-reports
|
||||
path: |
|
||||
@@ -142,10 +136,10 @@ jobs:
|
||||
needs: [basic-build] # Run in parallel with other checks
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
uses: actions/setup-python@v6
|
||||
with:
|
||||
python-version: "3.10" # Use minimum version for consistency
|
||||
|
||||
@@ -161,7 +155,7 @@ jobs:
|
||||
run: twine check dist/*
|
||||
|
||||
- name: Upload build artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@v5
|
||||
with:
|
||||
name: dist
|
||||
path: dist/
|
||||
@@ -182,10 +176,10 @@ jobs:
|
||||
python-version: "3.10"
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Set up Python ${{ matrix.python-version }}
|
||||
uses: actions/setup-python@v5
|
||||
uses: actions/setup-python@v6
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
|
||||
@@ -201,20 +195,9 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install flake8 pytest
|
||||
python -m pip install pytest
|
||||
pip install -e .[dev]
|
||||
|
||||
- name: Lint with flake8
|
||||
run: |
|
||||
# stop the build if there are Python syntax errors or undefined names
|
||||
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
|
||||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
|
||||
flake8 . --count --exit-zero --max-line-length=200 --extend-ignore=E203,W503,F401,E402,E721,F841 --statistics
|
||||
|
||||
- name: Type checking with mypy
|
||||
run: |
|
||||
mypy msgcenterpy --disable-error-code=unused-ignore
|
||||
|
||||
- name: Test with pytest
|
||||
run: |
|
||||
pytest
|
||||
|
||||
8
.github/workflows/docs.yml
vendored
8
.github/workflows/docs.yml
vendored
@@ -36,12 +36,12 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
ref: ${{ github.event.inputs.branch || github.ref }}
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
uses: actions/setup-python@v6
|
||||
with:
|
||||
python-version: "3.10"
|
||||
|
||||
@@ -55,7 +55,7 @@ jobs:
|
||||
|
||||
- name: Setup Pages
|
||||
id: pages
|
||||
uses: actions/configure-pages@v4
|
||||
uses: actions/configure-pages@v5
|
||||
if: github.ref == 'refs/heads/main' || (github.event_name == 'workflow_dispatch' && github.event.inputs.deploy_to_pages == 'true')
|
||||
|
||||
- name: Build Sphinx documentation
|
||||
@@ -64,7 +64,7 @@ jobs:
|
||||
make html
|
||||
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-pages-artifact@v3
|
||||
uses: actions/upload-pages-artifact@v4
|
||||
if: github.ref == 'refs/heads/main' || (github.event_name == 'workflow_dispatch' && github.event.inputs.deploy_to_pages == 'true')
|
||||
with:
|
||||
path: docs/_build/html
|
||||
|
||||
58
.github/workflows/publish.yml
vendored
58
.github/workflows/publish.yml
vendored
@@ -10,7 +10,7 @@ name: Upload PyPI package
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
types: [published, edited]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
test_pypi:
|
||||
@@ -29,10 +29,10 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
uses: actions/setup-python@v6
|
||||
with:
|
||||
python-version: "3.10" # Use minimum version for consistency
|
||||
|
||||
@@ -53,10 +53,10 @@ jobs:
|
||||
needs: [code-format] # Only run after code formatting passes
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Set up Python 3.10
|
||||
uses: actions/setup-python@v5
|
||||
uses: actions/setup-python@v6
|
||||
with:
|
||||
python-version: "3.10"
|
||||
|
||||
@@ -85,7 +85,7 @@ jobs:
|
||||
needs: [basic-build] # Only run after basic build passes
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Setup Miniconda
|
||||
uses: conda-incubator/setup-miniconda@v3
|
||||
@@ -125,23 +125,17 @@ jobs:
|
||||
needs: [basic-build] # Run in parallel with ROS2 test after basic build
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
- name: Run Safety CLI to check for vulnerabilities
|
||||
uses: pyupio/safety-action@v1
|
||||
with:
|
||||
python-version: "3.10" # Use minimum version for consistency
|
||||
|
||||
- name: Install security tools
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install "safety>=3.0.0" "typer<0.12.0" "marshmallow<4.0.0"
|
||||
|
||||
- name: Run safety security scan
|
||||
run: safety check --output json > safety-report.json
|
||||
api-key: ${{ secrets.SAFETY_CHECK }}
|
||||
output-format: json
|
||||
args: --detailed-output --output-format json
|
||||
|
||||
- name: Upload security reports
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@v5
|
||||
with:
|
||||
name: security-reports
|
||||
path: |
|
||||
@@ -154,10 +148,10 @@ jobs:
|
||||
needs: [test-with-ros2]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
uses: actions/setup-python@v6
|
||||
with:
|
||||
python-version: "3.10" # Use minimum version for consistency
|
||||
|
||||
@@ -167,7 +161,7 @@ jobs:
|
||||
python -m pip install build twine
|
||||
|
||||
- name: Verify version consistency
|
||||
if: github.event_name == 'release'
|
||||
if: github.event_name == 'release' && (github.event.action == 'published' || (github.event.action == 'edited' && !github.event.release.prerelease))
|
||||
run: |
|
||||
# Install package first
|
||||
pip install -e .
|
||||
@@ -203,7 +197,7 @@ jobs:
|
||||
twine check dist/*
|
||||
|
||||
- name: Upload distributions
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@v5
|
||||
with:
|
||||
name: release-dists
|
||||
path: dist/
|
||||
@@ -213,7 +207,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
needs:
|
||||
- release-build
|
||||
if: github.event_name == 'release' && !github.event.release.prerelease && github.event.inputs.test_pypi != 'true'
|
||||
if: github.event_name == 'release' && !github.event.release.prerelease && github.event.inputs.test_pypi != 'true' && (github.event.action == 'published' || github.event.action == 'edited')
|
||||
permissions:
|
||||
# IMPORTANT: this permission is mandatory for trusted publishing
|
||||
id-token: write
|
||||
@@ -223,7 +217,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Retrieve release distributions
|
||||
uses: actions/download-artifact@v4
|
||||
uses: actions/download-artifact@v6
|
||||
with:
|
||||
name: release-dists
|
||||
path: dist/
|
||||
@@ -236,7 +230,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
needs:
|
||||
- release-build
|
||||
if: github.event.inputs.test_pypi == 'true' || (github.event_name == 'release' && github.event.release.prerelease)
|
||||
if: github.event.inputs.test_pypi == 'true' || (github.event_name == 'release' && github.event.release.prerelease && (github.event.action == 'published' || github.event.action == 'edited'))
|
||||
permissions:
|
||||
# IMPORTANT: this permission is mandatory for trusted publishing
|
||||
id-token: write
|
||||
@@ -246,7 +240,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Retrieve release distributions
|
||||
uses: actions/download-artifact@v4
|
||||
uses: actions/download-artifact@v6
|
||||
with:
|
||||
name: release-dists
|
||||
path: dist/
|
||||
@@ -260,17 +254,19 @@ jobs:
|
||||
name: Add assets to GitHub release
|
||||
runs-on: ubuntu-latest
|
||||
needs: release-build
|
||||
if: github.event_name == 'release'
|
||||
if: github.event_name == 'release' && (github.event.action == 'published' || github.event.action == 'edited')
|
||||
permissions:
|
||||
contents: write # Need write access to upload release assets
|
||||
|
||||
steps:
|
||||
- name: Retrieve release distributions
|
||||
uses: actions/download-artifact@v4
|
||||
uses: actions/download-artifact@v6
|
||||
with:
|
||||
name: release-dists
|
||||
path: dist/
|
||||
|
||||
- name: Upload release assets
|
||||
uses: softprops/action-gh-release@v1
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
files: dist/*
|
||||
env:
|
||||
@@ -283,7 +279,7 @@ jobs:
|
||||
if: always() && (needs.pypi-publish.result == 'success' || needs.test-pypi-publish.result == 'success')
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
- name: Create deployment summary
|
||||
run: |
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -48,6 +48,9 @@ docs/_static/
|
||||
|
||||
# Visual Studio Code
|
||||
.vscode/
|
||||
.cursor/
|
||||
.cursorignore
|
||||
pyrightconfig.json
|
||||
|
||||
# ================================
|
||||
# Operating System files
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -184,7 +184,7 @@
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
Copyright 2025 ZGCA-Forge/MsgCenterPy
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
25
README.md
25
README.md
@@ -4,6 +4,7 @@
|
||||
|
||||
[](https://badge.fury.io/py/msgcenterpy)
|
||||
[](https://pypi.org/project/msgcenterpy/)
|
||||
[](https://pypi.org/project/msgcenterpy/)
|
||||
[](https://github.com/ZGCA-Forge/MsgCenterPy/actions)
|
||||
[](https://zgca-forge.github.io/MsgCenterPy/)
|
||||
|
||||
@@ -59,7 +60,25 @@ Please visit: [https://zgca-forge.github.io/MsgCenterPy/](https://zgca-forge.git
|
||||
|
||||
## Development
|
||||
|
||||
### Development Environment Setup
|
||||
### Quick Development Setup
|
||||
|
||||
For **Linux/macOS**:
|
||||
|
||||
```bash
|
||||
git clone https://github.com/ZGCA-Forge/MsgCenterPy.git
|
||||
cd MsgCenterPy
|
||||
./scripts/setup-dev.sh
|
||||
```
|
||||
|
||||
For **Windows**:
|
||||
|
||||
```powershell
|
||||
git clone https://github.com/ZGCA-Forge/MsgCenterPy.git
|
||||
cd MsgCenterPy
|
||||
.\scripts\setup-dev.ps1
|
||||
```
|
||||
|
||||
### Manual Development Setup
|
||||
|
||||
```bash
|
||||
git clone https://github.com/ZGCA-Forge/MsgCenterPy.git
|
||||
@@ -70,6 +89,10 @@ pre-commit install
|
||||
|
||||
For API documentation, please refer to Quick Start
|
||||
|
||||
## Star History
|
||||
|
||||
[](https://star-history.com/#ZGCA-Forge/MsgCenterPy&Date)
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under Apache-2.0 License - see the [LICENSE](LICENSE) file for details.
|
||||
|
||||
@@ -5,7 +5,7 @@ A multi-format message conversion system supporting seamless conversion
|
||||
between ROS2, Pydantic, Dataclass, JSON, Dict, YAML and JSON Schema.
|
||||
"""
|
||||
|
||||
__version__ = "0.0.4"
|
||||
__version__ = "0.1.5"
|
||||
__license__ = "Apache-2.0"
|
||||
|
||||
from msgcenterpy.core.envelope import MessageEnvelope, create_envelope
|
||||
|
||||
@@ -9,6 +9,8 @@ class Properties(TypedDict, total=False):
|
||||
ros_msg_cls_path: str
|
||||
ros_msg_cls_namespace: str
|
||||
json_schema: Dict[str, Any]
|
||||
typed_dict_class_module: str
|
||||
typed_dict_class_name: str
|
||||
|
||||
|
||||
class FormatMetadata(TypedDict, total=False):
|
||||
|
||||
@@ -358,14 +358,14 @@ class TypeInfoProvider(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def get_field_type_info(
|
||||
self, field_name: str, field_value: Any, field_accessor: "FieldAccessor"
|
||||
self, field_name: str, field_value: Any, parent_field_accessor: "FieldAccessor"
|
||||
) -> Optional[TypeInfo]:
|
||||
"""获取指定字段的类型信息
|
||||
|
||||
Args:
|
||||
field_name: 字段名,简单字段名如 'field'
|
||||
field_value: 字段的当前值,用于动态类型推断,不能为None
|
||||
field_accessor: 字段访问器,提供额外的上下文信息,不能为None
|
||||
parent_field_accessor: 父字段访问器,提供额外的上下文信息,不能为None
|
||||
|
||||
Returns:
|
||||
字段的TypeInfo,如果字段不存在则返回None
|
||||
|
||||
@@ -11,6 +11,7 @@ class MessageType(Enum):
|
||||
JSON_SCHEMA = "json_schema"
|
||||
DICT = "dict"
|
||||
YAML = "yaml"
|
||||
TYPED_DICT = "typed_dict" # Experimental
|
||||
|
||||
|
||||
class ConversionError(Exception):
|
||||
|
||||
375
msgcenterpy/instances/typed_dict_instance.py
Normal file
375
msgcenterpy/instances/typed_dict_instance.py
Normal file
@@ -0,0 +1,375 @@
|
||||
"""
|
||||
TypedDict Message Instance - Experimental
|
||||
|
||||
This module provides support for TypedDict message instances with type information
|
||||
extraction and field access capabilities.
|
||||
|
||||
WARNING: This implementation is EXPERIMENTAL and may change in future versions.
|
||||
"""
|
||||
|
||||
import warnings
|
||||
from typing import TYPE_CHECKING, Any, Dict, Optional, Type, get_type_hints
|
||||
|
||||
from msgcenterpy.core.envelope import MessageEnvelope, create_envelope
|
||||
from msgcenterpy.core.message_instance import MessageInstance
|
||||
from msgcenterpy.core.type_converter import TypeConverter
|
||||
from msgcenterpy.core.type_info import ConstraintType, Consts, TypeInfo
|
||||
from msgcenterpy.core.types import MessageType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from msgcenterpy.core.field_accessor import FieldAccessor
|
||||
|
||||
|
||||
class TypedDictMessageInstance(MessageInstance[Dict[str, Any]]):
|
||||
"""TypedDict消息实例,支持类型信息提取和字段访问器(实验性)
|
||||
|
||||
EXPERIMENTAL: This class is experimental and may change in future versions.
|
||||
|
||||
Attributes:
|
||||
_typed_dict_class: TypedDict类型定义
|
||||
_typed_dict_data: 实际的字典数据
|
||||
_pydantic_model: 缓存的Pydantic模型(懒加载)
|
||||
_json_schema: 缓存的JSON Schema(懒加载)
|
||||
"""
|
||||
|
||||
_typed_dict_class: Type[Any]
|
||||
_typed_dict_data: Dict[str, Any]
|
||||
_pydantic_model: Optional[Any] = None
|
||||
_json_schema: Optional[Dict[str, Any]] = None
|
||||
|
||||
def __init__(self, inner_data: Dict[str, Any], typed_dict: Type[Any], **kwargs: Any) -> None:
|
||||
"""
|
||||
初始化TypedDict消息实例
|
||||
|
||||
Args:
|
||||
inner_data: 字典数据
|
||||
typed_dict: TypedDict类型定义
|
||||
**kwargs: 额外的关键字参数
|
||||
"""
|
||||
# 发出实验性警告
|
||||
warnings.warn(
|
||||
"TypedDictMessageInstance is experimental and may change in future versions",
|
||||
FutureWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
|
||||
# 验证typed_dict是否为TypedDict类型
|
||||
if not self._is_typed_dict(typed_dict):
|
||||
raise TypeError(f"Expected a TypedDict class, got {type(typed_dict)}")
|
||||
|
||||
self._typed_dict_class = typed_dict
|
||||
self._typed_dict_data = inner_data
|
||||
self._pydantic_model = None
|
||||
self._json_schema = None
|
||||
|
||||
super().__init__(inner_data, MessageType.TYPED_DICT)
|
||||
|
||||
@staticmethod
|
||||
def _is_typed_dict(typed_dict_class: Type[Any]) -> bool:
|
||||
"""检查给定的类是否为TypedDict"""
|
||||
try:
|
||||
# TypedDict类会有__annotations__和__total__等特殊属性
|
||||
return (
|
||||
hasattr(typed_dict_class, "__annotations__")
|
||||
and hasattr(typed_dict_class, "__total__")
|
||||
and hasattr(typed_dict_class, "__required_keys__")
|
||||
and hasattr(typed_dict_class, "__optional_keys__")
|
||||
)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
@property
|
||||
def typed_dict_class(self) -> Type[Any]:
|
||||
"""获取TypedDict类型定义"""
|
||||
return self._typed_dict_class
|
||||
|
||||
@property
|
||||
def typed_dict_class_module(self) -> str:
|
||||
"""获取TypedDict类的模块路径"""
|
||||
return self._typed_dict_class.__module__
|
||||
|
||||
@property
|
||||
def typed_dict_class_name(self) -> str:
|
||||
"""获取TypedDict类名"""
|
||||
return self._typed_dict_class.__name__
|
||||
|
||||
@classmethod
|
||||
def get_pydantic_model_from_typed_dict(cls, typed_dict: Type[Any]) -> Any:
|
||||
"""从TypedDict类型创建Pydantic模型(类方法版本)
|
||||
|
||||
优先使用TypeAdapter(Pydantic V2),如果不可用则使用create_model_from_typeddict(Pydantic V1)
|
||||
|
||||
Args:
|
||||
typed_dict: TypedDict类型定义
|
||||
|
||||
Returns:
|
||||
Pydantic模型实例
|
||||
|
||||
Raises:
|
||||
ImportError: 如果Pydantic未安装
|
||||
RuntimeError: 如果无法创建Pydantic模型
|
||||
"""
|
||||
# 尝试使用Pydantic V2的TypeAdapter
|
||||
try:
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
adapter = TypeAdapter(typed_dict)
|
||||
return adapter
|
||||
except ImportError:
|
||||
pass # Pydantic V2不可用,尝试V1
|
||||
except Exception as e:
|
||||
# TypeAdapter创建失败,尝试V1方法
|
||||
warnings.warn(f"Failed to create TypeAdapter: {e}, trying V1 approach", RuntimeWarning)
|
||||
|
||||
# 尝试使用Pydantic V1的create_model_from_typeddict
|
||||
try:
|
||||
from pydantic.main import create_model_from_typeddict
|
||||
|
||||
model = create_model_from_typeddict(typed_dict)
|
||||
return model
|
||||
except ImportError as e:
|
||||
raise ImportError(
|
||||
"Pydantic is required for get_pydantic_model_from_typed_dict(). "
|
||||
"Please install it with: pip install pydantic"
|
||||
) from e
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Failed to create Pydantic model from TypedDict: {e}") from e
|
||||
|
||||
def get_pydantic_model(self) -> Any:
|
||||
"""获取或创建Pydantic模型(实例方法版本)
|
||||
|
||||
优先使用TypeAdapter(Pydantic V2),如果不可用则使用create_model_from_typeddict(Pydantic V1)
|
||||
|
||||
Returns:
|
||||
Pydantic模型实例
|
||||
|
||||
Raises:
|
||||
ImportError: 如果Pydantic未安装
|
||||
RuntimeError: 如果无法创建Pydantic模型
|
||||
"""
|
||||
if self._pydantic_model is not None:
|
||||
return self._pydantic_model
|
||||
|
||||
self._pydantic_model = self.get_pydantic_model_from_typed_dict(self._typed_dict_class)
|
||||
return self._pydantic_model
|
||||
|
||||
@classmethod
|
||||
def get_json_schema_from_typed_dict(cls, typed_dict: Type[Any]) -> Dict[str, Any]:
|
||||
"""从TypedDict类型生成JSON Schema(类方法版本)
|
||||
|
||||
Args:
|
||||
typed_dict: TypedDict类型定义
|
||||
|
||||
Returns:
|
||||
JSON Schema字典
|
||||
|
||||
Raises:
|
||||
ImportError: 如果Pydantic未安装
|
||||
RuntimeError: 如果无法生成JSON Schema
|
||||
"""
|
||||
pydantic_model = cls.get_pydantic_model_from_typed_dict(typed_dict)
|
||||
|
||||
try:
|
||||
schema: Dict[str, Any]
|
||||
# Pydantic V2 API
|
||||
if hasattr(pydantic_model, "json_schema"):
|
||||
# TypeAdapter的情况
|
||||
schema = pydantic_model.json_schema()
|
||||
return schema
|
||||
elif hasattr(pydantic_model, "model_json_schema"):
|
||||
# BaseModel的情况(V2)
|
||||
schema = pydantic_model.model_json_schema()
|
||||
return schema
|
||||
# Pydantic V1 API
|
||||
elif hasattr(pydantic_model, "schema"):
|
||||
schema = pydantic_model.schema()
|
||||
return schema
|
||||
else:
|
||||
raise RuntimeError("Unable to extract JSON schema from Pydantic model")
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Failed to generate JSON schema: {e}") from e
|
||||
|
||||
def get_json_schema(self) -> Dict[str, Any]:
|
||||
"""获取JSON Schema(实例方法版本,利用Pydantic转换)
|
||||
|
||||
Returns:
|
||||
JSON Schema字典
|
||||
|
||||
Raises:
|
||||
ImportError: 如果Pydantic未安装
|
||||
RuntimeError: 如果无法生成JSON Schema
|
||||
"""
|
||||
if self._json_schema is not None:
|
||||
return self._json_schema
|
||||
|
||||
self._json_schema = self.get_json_schema_from_typed_dict(self._typed_dict_class)
|
||||
return self._json_schema
|
||||
|
||||
def export_to_envelope(self, **kwargs: Any) -> MessageEnvelope:
|
||||
"""导出为统一信封字典
|
||||
|
||||
将 typed_dict_class_module, typed_dict_class_name 和 json_schema 保存到 properties,
|
||||
确保 import_from_envelope 可以独立重建实例
|
||||
"""
|
||||
base_dict = self.get_python_dict()
|
||||
|
||||
# 尝试获取 json_schema(如果 Pydantic 可用)
|
||||
json_schema = None
|
||||
try:
|
||||
json_schema = self.get_json_schema()
|
||||
except (ImportError, RuntimeError):
|
||||
# Pydantic 不可用或生成失败,继续但不保存 schema
|
||||
pass
|
||||
|
||||
envelope = create_envelope(
|
||||
format_name=self.message_type.value,
|
||||
content=base_dict,
|
||||
metadata={
|
||||
"current_format": self.message_type.value,
|
||||
"source_cls_name": self.__class__.__name__,
|
||||
"source_cls_module": self.__class__.__module__,
|
||||
**self._metadata,
|
||||
},
|
||||
)
|
||||
|
||||
# 将 typed_dict_class_module, typed_dict_class_name 和 json_schema 保存到 properties
|
||||
if "properties" not in envelope["metadata"]:
|
||||
envelope["metadata"]["properties"] = {} # type: ignore[typeddict-item]
|
||||
envelope["metadata"]["properties"]["typed_dict_class_module"] = self.typed_dict_class_module # type: ignore[typeddict-item]
|
||||
envelope["metadata"]["properties"]["typed_dict_class_name"] = self.typed_dict_class_name # type: ignore[typeddict-item]
|
||||
if json_schema is not None:
|
||||
envelope["metadata"]["properties"]["json_schema"] = json_schema # type: ignore[typeddict-item]
|
||||
|
||||
return envelope
|
||||
|
||||
@classmethod
|
||||
def import_from_envelope(cls, data: MessageEnvelope, **kwargs: Any) -> "TypedDictMessageInstance":
|
||||
"""从规范信封创建TypedDict实例
|
||||
|
||||
优先从 envelope.metadata.properties 读取 json_schema,
|
||||
如果没有 json_schema,则尝试从 typed_dict 参数或 typed_dict_class_path 恢复。
|
||||
|
||||
Args:
|
||||
data: 消息信封
|
||||
**kwargs: 可选的'typed_dict'参数
|
||||
|
||||
Returns:
|
||||
TypedDict实例
|
||||
|
||||
Raises:
|
||||
ValueError: 如果无法确定TypedDict类型
|
||||
"""
|
||||
content = data["content"]
|
||||
properties = data.get("metadata", {}).get("properties", {})
|
||||
|
||||
# 优先从 kwargs 获取 typed_dict
|
||||
typed_dict = kwargs.pop("typed_dict", None)
|
||||
|
||||
# 如果没有提供 typed_dict,尝试从 properties 恢复
|
||||
if typed_dict is None:
|
||||
typed_dict_class_module = properties.get("typed_dict_class_module")
|
||||
typed_dict_class_name = properties.get("typed_dict_class_name")
|
||||
|
||||
if typed_dict_class_module and typed_dict_class_name:
|
||||
# 尝试从模块导入 TypedDict
|
||||
try:
|
||||
import importlib
|
||||
|
||||
module = importlib.import_module(typed_dict_class_module)
|
||||
typed_dict = getattr(module, typed_dict_class_name)
|
||||
except Exception as e:
|
||||
raise ValueError(
|
||||
f"Unable to import TypedDict '{typed_dict_class_name}' from module '{typed_dict_class_module}': {e}. "
|
||||
"Please provide 'typed_dict' parameter explicitly."
|
||||
) from e
|
||||
|
||||
if typed_dict is None:
|
||||
raise ValueError(
|
||||
"Unable to determine TypedDict type. "
|
||||
"Please provide 'typed_dict' parameter or ensure envelope contains valid type information "
|
||||
"(typed_dict_class_module and typed_dict_class_name)."
|
||||
)
|
||||
|
||||
instance = cls(content, typed_dict, **kwargs)
|
||||
return instance
|
||||
|
||||
def get_python_dict(self) -> Dict[str, Any]:
|
||||
"""获取当前所有的字段名和对应的原始值"""
|
||||
return self._typed_dict_data.copy()
|
||||
|
||||
def set_python_dict(self, value: Dict[str, Any], **kwargs: Any) -> bool:
|
||||
"""设置所有字段的值,只做已有字段的更新"""
|
||||
# 获取根访问器
|
||||
root_accessor = self._field_accessor
|
||||
if root_accessor is not None:
|
||||
root_accessor.update_from_dict(source_data=value)
|
||||
return True
|
||||
|
||||
# TypeInfoProvider 接口实现
|
||||
def get_field_type_info(
|
||||
self, field_name: str, field_value: Any, parent_field_accessor: "FieldAccessor"
|
||||
) -> Optional[TypeInfo]:
|
||||
"""从TypedDict定义中提取字段类型信息
|
||||
|
||||
Args:
|
||||
field_name: 字段名
|
||||
field_value: 字段值
|
||||
parent_field_accessor: 父级字段访问器
|
||||
|
||||
Returns:
|
||||
字段的类型信息
|
||||
"""
|
||||
# 构建完整路径
|
||||
full_path = f"{parent_field_accessor.full_path_from_root}.{field_name}"
|
||||
|
||||
# 获取TypedDict的类型提示
|
||||
try:
|
||||
type_hints = get_type_hints(self._typed_dict_class)
|
||||
except Exception:
|
||||
type_hints = {}
|
||||
|
||||
# 获取字段的类型注解
|
||||
field_type_annotation = type_hints.get(field_name)
|
||||
|
||||
# 确定类型信息
|
||||
python_type = type(field_value)
|
||||
if field_type_annotation is not None:
|
||||
# 从类型注解推断标准类型
|
||||
standard_type = TypeConverter.python_type_to_standard(field_type_annotation)
|
||||
else:
|
||||
# 如果没有类型注解,从值的类型推断
|
||||
standard_type = TypeConverter.python_type_to_standard(python_type)
|
||||
|
||||
# 创建基础TypeInfo
|
||||
type_info = TypeInfo(
|
||||
field_name=field_name,
|
||||
field_path=full_path,
|
||||
standard_type=standard_type,
|
||||
python_type=python_type,
|
||||
original_type=field_type_annotation if field_type_annotation is not None else python_type,
|
||||
current_value=field_value,
|
||||
)
|
||||
|
||||
# 检查字段是否为必需字段
|
||||
if hasattr(self._typed_dict_class, "__required_keys__"):
|
||||
required_keys = getattr(self._typed_dict_class, "__required_keys__")
|
||||
if field_name in required_keys:
|
||||
type_info.add_constraint(
|
||||
ConstraintType.REQUIRED,
|
||||
True,
|
||||
"Field is required by TypedDict definition",
|
||||
)
|
||||
|
||||
# 处理列表/数组类型
|
||||
if isinstance(field_value, list):
|
||||
type_info.is_array = True
|
||||
# 可以进一步提取元素类型信息,但这需要更复杂的类型解析
|
||||
# 暂时留给后续版本实现
|
||||
|
||||
# 处理字典/对象类型
|
||||
elif isinstance(field_value, dict):
|
||||
type_info.is_object = True
|
||||
# 可以进一步提取对象字段信息,但这需要递归解析
|
||||
# 暂时留给后续版本实现
|
||||
|
||||
return type_info
|
||||
@@ -72,7 +72,7 @@ version = {attr = "msgcenterpy.__version__"}
|
||||
|
||||
[tool.black]
|
||||
line-length = 120
|
||||
target-version = ['py310']
|
||||
target-version = ['py310', 'py311', 'py312']
|
||||
|
||||
[tool.isort]
|
||||
profile = "black"
|
||||
|
||||
Reference in New Issue
Block a user