magic-base 0.1.0__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.
- magic_base/__init__.py +0 -0
- magic_base/config/__init__.py +0 -0
- magic_base/config/base_config.py +36 -0
- magic_base/crypto/__init__.py +0 -0
- magic_base/crypto/base_cert.py +26 -0
- magic_base/crypto/base_crypto.py +30 -0
- magic_base/data_access/__init__.py +0 -0
- magic_base/data_access/config/__init__.py +0 -0
- magic_base/data_access/config/base_database_config.py +85 -0
- magic_base/data_access/manager/__init__.py +0 -0
- magic_base/data_access/manager/base_database_manager.py +0 -0
- magic_base/data_access/model/__init__.py +0 -0
- magic_base/data_access/model/base_model.py +10 -0
- magic_base/data_access/repository/__init__.py +0 -0
- magic_base/data_access/repository/base_repository.py +84 -0
- magic_base/data_access/service/__init__.py +0 -0
- magic_base/data_access/service/base_service.py +0 -0
- magic_base/detector/__init__.py +0 -0
- magic_base/detector/base_detector.py +85 -0
- magic_base/detector/base_hardware_define.py +35 -0
- magic_base/detector/base_hardware_result.py +35 -0
- magic_base/exceptions/__init__.py +0 -0
- magic_base/exceptions/base_exceptions.py +39 -0
- magic_base/platform/__init__.py +0 -0
- magic_base/platform/base_platform_adapter.py +84 -0
- magic_base/types/__init__.py +0 -0
- magic_base/types/base_types.py +24 -0
- magic_base-0.1.0.dist-info/METADATA +142 -0
- magic_base-0.1.0.dist-info/RECORD +32 -0
- magic_base-0.1.0.dist-info/WHEEL +5 -0
- magic_base-0.1.0.dist-info/licenses/LICENSE +21 -0
- magic_base-0.1.0.dist-info/top_level.txt +1 -0
magic_base/__init__.py
ADDED
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# magic_base/config/base.py
|
|
2
|
+
"""
|
|
3
|
+
配置管理基类
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from typing import Any, Dict, Optional
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ConfigBase(ABC):
|
|
11
|
+
"""配置管理基类"""
|
|
12
|
+
|
|
13
|
+
@abstractmethod
|
|
14
|
+
def get(self, key: str, default: Any = None) -> Any:
|
|
15
|
+
"""获取配置值"""
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def set(self, key: str, value: Any) -> None:
|
|
20
|
+
"""设置配置值"""
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def load(self) -> Dict[str, Any]:
|
|
25
|
+
"""加载配置"""
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
@abstractmethod
|
|
29
|
+
def save(self) -> bool:
|
|
30
|
+
"""保存配置"""
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
@abstractmethod
|
|
34
|
+
def reload(self) -> None:
|
|
35
|
+
"""重新加载配置"""
|
|
36
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# magic_base/crypto/cert.py
|
|
2
|
+
"""
|
|
3
|
+
证书验证基类
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from typing import Dict, Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class CertValidatorBase(ABC):
|
|
11
|
+
"""证书验证器基类"""
|
|
12
|
+
|
|
13
|
+
@abstractmethod
|
|
14
|
+
def verify_signature(self, data: bytes, signature: bytes, cert_path: str) -> bool:
|
|
15
|
+
"""验证签名"""
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def get_cert_info(self, cert_path: str) -> Dict[str, Any]:
|
|
20
|
+
"""获取证书信息"""
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def verify_chain(self, cert_path: str, ca_bundle_path: str) -> bool:
|
|
25
|
+
"""验证证书链"""
|
|
26
|
+
pass
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# magic_base/crypto/crypto.py
|
|
2
|
+
"""
|
|
3
|
+
加密基类
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class CryptoBase(ABC):
|
|
10
|
+
"""加密工具基类"""
|
|
11
|
+
|
|
12
|
+
@abstractmethod
|
|
13
|
+
def encrypt(self, data: bytes, key: bytes) -> bytes:
|
|
14
|
+
"""加密数据"""
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
@abstractmethod
|
|
18
|
+
def decrypt(self, data: bytes, key: bytes) -> bytes:
|
|
19
|
+
"""解密数据"""
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
@abstractmethod
|
|
23
|
+
def hash(self, data: bytes) -> str:
|
|
24
|
+
"""计算哈希值"""
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
@abstractmethod
|
|
28
|
+
def generate_key(self) -> bytes:
|
|
29
|
+
"""生成密钥"""
|
|
30
|
+
pass
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# magic_base/data_access/base.py
|
|
2
|
+
"""
|
|
3
|
+
数据访问基类 - 提供数据库连接和会话管理
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from contextlib import contextmanager
|
|
8
|
+
from typing import Generator, Optional, Dict, Any
|
|
9
|
+
from sqlalchemy import create_engine, Engine
|
|
10
|
+
from sqlalchemy.orm import sessionmaker, Session
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class DatabaseConfigBase(ABC):
|
|
14
|
+
"""数据库配置基类"""
|
|
15
|
+
|
|
16
|
+
@abstractmethod
|
|
17
|
+
def get_connection_string(self) -> str:
|
|
18
|
+
"""获取数据库连接字符串"""
|
|
19
|
+
pass
|
|
20
|
+
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def get_engine_options(self) -> Dict[str, Any]:
|
|
23
|
+
"""获取引擎选项"""
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class DatabaseManagerBase(ABC):
|
|
28
|
+
"""
|
|
29
|
+
数据库管理器基类
|
|
30
|
+
|
|
31
|
+
提供统一的会话管理,具体模块继承此类实现自己的数据库管理。
|
|
32
|
+
|
|
33
|
+
Example:
|
|
34
|
+
>>> class MyDBManager(DatabaseManagerBase):
|
|
35
|
+
... def init_database(self, drop_first=False):
|
|
36
|
+
... Base.metadata.create_all(self.engine)
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
def __init__(self, config: DatabaseConfigBase):
|
|
40
|
+
self._config = config
|
|
41
|
+
self._engine: Optional[Engine] = None
|
|
42
|
+
self._SessionLocal: Optional[sessionmaker] = None
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def engine(self) -> Engine:
|
|
46
|
+
"""获取数据库引擎(延迟初始化)"""
|
|
47
|
+
if self._engine is None:
|
|
48
|
+
self._engine = create_engine(
|
|
49
|
+
self._config.get_connection_string(),
|
|
50
|
+
**self._config.get_engine_options()
|
|
51
|
+
)
|
|
52
|
+
return self._engine
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def SessionLocal(self) -> sessionmaker:
|
|
56
|
+
"""获取会话工厂"""
|
|
57
|
+
if self._SessionLocal is None:
|
|
58
|
+
self._SessionLocal = sessionmaker(bind=self.engine)
|
|
59
|
+
return self._SessionLocal
|
|
60
|
+
|
|
61
|
+
@contextmanager
|
|
62
|
+
def session(self) -> Generator[Session, None, None]:
|
|
63
|
+
"""获取数据库会话(上下文管理器)"""
|
|
64
|
+
session = self.SessionLocal()
|
|
65
|
+
try:
|
|
66
|
+
yield session
|
|
67
|
+
session.commit()
|
|
68
|
+
except Exception:
|
|
69
|
+
session.rollback()
|
|
70
|
+
raise
|
|
71
|
+
finally:
|
|
72
|
+
session.close()
|
|
73
|
+
|
|
74
|
+
def get_session(self) -> Session:
|
|
75
|
+
"""获取数据库会话(手动管理)"""
|
|
76
|
+
return self.SessionLocal()
|
|
77
|
+
|
|
78
|
+
@abstractmethod
|
|
79
|
+
def init_database(self, drop_first: bool = False):
|
|
80
|
+
"""
|
|
81
|
+
初始化数据库
|
|
82
|
+
|
|
83
|
+
具体模块需要实现此方法,创建自己的表。
|
|
84
|
+
"""
|
|
85
|
+
pass
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# magic_base/data_access/repository.py
|
|
2
|
+
"""
|
|
3
|
+
仓储基类 - 提供通用 CRUD 操作模板
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from typing import Generic, TypeVar, Type, Optional, List, Dict, Any
|
|
8
|
+
from sqlalchemy.orm import Session
|
|
9
|
+
from .models import ORMBase
|
|
10
|
+
|
|
11
|
+
ModelType = TypeVar("ModelType", bound=ORMBase)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BaseRepository(ABC, Generic[ModelType]):
|
|
15
|
+
"""
|
|
16
|
+
仓储基类
|
|
17
|
+
|
|
18
|
+
提供通用的 CRUD 模板方法,具体模块需要继承并绑定自己的 Model。
|
|
19
|
+
|
|
20
|
+
Example:
|
|
21
|
+
>>> class VendorRepository(BaseRepository[VendorModel]):
|
|
22
|
+
... def __init__(self, session):
|
|
23
|
+
... super().__init__(session, VendorModel)
|
|
24
|
+
...
|
|
25
|
+
... def get_certified(self):
|
|
26
|
+
... return self.session.query(VendorModel).filter_by(status='certified').all()
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(self, session: Session, model_class: Type[ModelType]):
|
|
30
|
+
self.session = session
|
|
31
|
+
self.model_class = model_class
|
|
32
|
+
|
|
33
|
+
def get_by_id(self, id: int) -> Optional[ModelType]:
|
|
34
|
+
"""根据 ID 获取"""
|
|
35
|
+
return self.session.query(self.model_class).filter_by(id=id).first()
|
|
36
|
+
|
|
37
|
+
def get_by(self, **kwargs) -> Optional[ModelType]:
|
|
38
|
+
"""根据条件获取一条"""
|
|
39
|
+
return self.session.query(self.model_class).filter_by(**kwargs).first()
|
|
40
|
+
|
|
41
|
+
def get_all(self, **kwargs) -> List[ModelType]:
|
|
42
|
+
"""获取所有符合条件的"""
|
|
43
|
+
query = self.session.query(self.model_class)
|
|
44
|
+
if kwargs:
|
|
45
|
+
query = query.filter_by(**kwargs)
|
|
46
|
+
return query.all()
|
|
47
|
+
|
|
48
|
+
def add(self, instance: ModelType) -> ModelType:
|
|
49
|
+
"""添加"""
|
|
50
|
+
self.session.add(instance)
|
|
51
|
+
return instance
|
|
52
|
+
|
|
53
|
+
def add_all(self, instances: List[ModelType]) -> List[ModelType]:
|
|
54
|
+
"""批量添加"""
|
|
55
|
+
self.session.add_all(instances)
|
|
56
|
+
return instances
|
|
57
|
+
|
|
58
|
+
def update(self, id: int, **kwargs) -> Optional[ModelType]:
|
|
59
|
+
"""更新"""
|
|
60
|
+
instance = self.get_by_id(id)
|
|
61
|
+
if instance:
|
|
62
|
+
for key, value in kwargs.items():
|
|
63
|
+
if hasattr(instance, key):
|
|
64
|
+
setattr(instance, key, value)
|
|
65
|
+
return instance
|
|
66
|
+
|
|
67
|
+
def delete(self, id: int) -> bool:
|
|
68
|
+
"""删除"""
|
|
69
|
+
instance = self.get_by_id(id)
|
|
70
|
+
if instance:
|
|
71
|
+
self.session.delete(instance)
|
|
72
|
+
return True
|
|
73
|
+
return False
|
|
74
|
+
|
|
75
|
+
def exists(self, **kwargs) -> bool:
|
|
76
|
+
"""检查是否存在"""
|
|
77
|
+
return self.session.query(self.model_class).filter_by(**kwargs).first() is not None
|
|
78
|
+
|
|
79
|
+
def count(self, **kwargs) -> int:
|
|
80
|
+
"""计数"""
|
|
81
|
+
query = self.session.query(self.model_class)
|
|
82
|
+
if kwargs:
|
|
83
|
+
query = query.filter_by(**kwargs)
|
|
84
|
+
return query.count()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# magic_base/detector/base.py
|
|
2
|
+
"""
|
|
3
|
+
检测器基类 - 定义所有检测器的接口
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from typing import List, Optional, Dict, Any
|
|
8
|
+
from .base_hardware_define import HardwareInfoBase
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class DetectorBase(ABC):
|
|
12
|
+
"""
|
|
13
|
+
检测器基类
|
|
14
|
+
|
|
15
|
+
所有具体硬件检测器(GPUDetector、CPUDetector 等)必须继承此类。
|
|
16
|
+
|
|
17
|
+
Example:
|
|
18
|
+
>>> class MyDetector(DetectorBase):
|
|
19
|
+
... def detect(self) -> List[HardwareInfoBase]:
|
|
20
|
+
... return [MyHardwareInfo(...)]
|
|
21
|
+
...
|
|
22
|
+
... def is_supported(self) -> bool:
|
|
23
|
+
... return True
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
@abstractmethod
|
|
27
|
+
def detect(self) -> List[HardwareInfoBase]:
|
|
28
|
+
"""
|
|
29
|
+
检测硬件设备
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
List[HardwareInfoBase]: 硬件信息列表
|
|
33
|
+
"""
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def is_supported(self) -> bool:
|
|
38
|
+
"""
|
|
39
|
+
检查当前平台是否支持此检测器
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
bool: 支持返回 True
|
|
43
|
+
"""
|
|
44
|
+
pass
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class BatchDetectorBase(ABC):
|
|
48
|
+
"""
|
|
49
|
+
批量检测器基类
|
|
50
|
+
|
|
51
|
+
同时检测多种硬件的检测器应继承此类。
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
@abstractmethod
|
|
55
|
+
def detect_all(self) -> Any:
|
|
56
|
+
"""检测所有支持的硬件"""
|
|
57
|
+
pass
|
|
58
|
+
|
|
59
|
+
@abstractmethod
|
|
60
|
+
def detect_with_progress(self, callback) -> Any:
|
|
61
|
+
"""带进度回调的检测"""
|
|
62
|
+
pass
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class CachingDetectorBase(ABC):
|
|
66
|
+
"""
|
|
67
|
+
缓存检测器基类(可选接口)
|
|
68
|
+
|
|
69
|
+
需要实现缓存机制的检测器可以实现此接口。
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
@abstractmethod
|
|
73
|
+
def clear_cache(self) -> None:
|
|
74
|
+
"""清除所有缓存"""
|
|
75
|
+
pass
|
|
76
|
+
|
|
77
|
+
@abstractmethod
|
|
78
|
+
def get_cache_stats(self) -> Dict[str, Any]:
|
|
79
|
+
"""获取缓存统计信息"""
|
|
80
|
+
pass
|
|
81
|
+
|
|
82
|
+
@abstractmethod
|
|
83
|
+
def set_cache_ttl(self, seconds: int) -> None:
|
|
84
|
+
"""设置缓存有效期"""
|
|
85
|
+
pass
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# magic_base/detector/hardware.py
|
|
2
|
+
"""
|
|
3
|
+
硬件信息基类 - 定义统一的硬件数据接口
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from typing import Dict, Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class HardwareInfoBase(ABC):
|
|
11
|
+
"""
|
|
12
|
+
硬件信息基类
|
|
13
|
+
|
|
14
|
+
所有具体硬件信息类(GPUInfo、CPUInfo 等)必须继承此类。
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
@abstractmethod
|
|
18
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
19
|
+
"""
|
|
20
|
+
转换为字典格式
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
Dict[str, Any]: 字典格式的硬件信息
|
|
24
|
+
"""
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
@abstractmethod
|
|
28
|
+
def get_type(self) -> str:
|
|
29
|
+
"""
|
|
30
|
+
获取硬件类型
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
str: 硬件类型标识(gpu/cpu/memory 等)
|
|
34
|
+
"""
|
|
35
|
+
pass
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# magic_base/detector/result.py
|
|
2
|
+
"""
|
|
3
|
+
检测结果基类
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from typing import Dict, Any, List
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class DetectionResultBase(ABC):
|
|
11
|
+
"""
|
|
12
|
+
检测结果基类
|
|
13
|
+
|
|
14
|
+
所有具体检测结果类必须继承此类。
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
@abstractmethod
|
|
18
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
19
|
+
"""转换为字典"""
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
@abstractmethod
|
|
23
|
+
def to_json(self) -> str:
|
|
24
|
+
"""转换为 JSON 字符串"""
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
@abstractmethod
|
|
28
|
+
def to_table(self) -> str:
|
|
29
|
+
"""转换为表格格式(用于 CLI)"""
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
@abstractmethod
|
|
33
|
+
def add_error(self, component: str, error: str) -> None:
|
|
34
|
+
"""添加错误记录"""
|
|
35
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# magic_base/exceptions/base.py
|
|
2
|
+
"""
|
|
3
|
+
异常基类
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class MagicBaseError(Exception):
|
|
8
|
+
"""magic-base 基础异常类"""
|
|
9
|
+
pass
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class PlatformNotSupportedError(MagicBaseError):
|
|
13
|
+
"""当前平台不支持"""
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class DatabaseError(MagicBaseError):
|
|
18
|
+
"""数据库错误"""
|
|
19
|
+
pass
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class ConfigurationError(MagicBaseError):
|
|
23
|
+
"""配置错误"""
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class DetectionError(MagicBaseError):
|
|
28
|
+
"""检测错误"""
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class CryptoError(MagicBaseError):
|
|
33
|
+
"""加密错误"""
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ValidationError(MagicBaseError):
|
|
38
|
+
"""验证错误"""
|
|
39
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# magic_base/platform/adapter.py
|
|
2
|
+
"""
|
|
3
|
+
平台适配器基类 - 定义不同操作系统的适配接口
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from typing import List, Dict, Any, Tuple, Optional
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PlatformAdapterBase(ABC):
|
|
11
|
+
"""
|
|
12
|
+
平台适配器基类
|
|
13
|
+
|
|
14
|
+
封装不同操作系统(Linux、Windows、macOS)的特定实现。
|
|
15
|
+
具体适配器由上层模块实现。
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def get_platform_name(self) -> str:
|
|
20
|
+
"""
|
|
21
|
+
获取平台名称
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
str: "linux", "windows", "darwin"
|
|
25
|
+
"""
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
@abstractmethod
|
|
29
|
+
def get_pci_devices(self) -> List[Dict[str, str]]:
|
|
30
|
+
"""
|
|
31
|
+
获取所有 PCI 设备列表
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
List[Dict]: 设备列表,包含 address, vendor_id, device_id 等
|
|
35
|
+
"""
|
|
36
|
+
pass
|
|
37
|
+
|
|
38
|
+
@abstractmethod
|
|
39
|
+
def get_cpu_info(self) -> Dict[str, Any]:
|
|
40
|
+
"""
|
|
41
|
+
获取 CPU 信息
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
Dict: 包含 model_name, vendor_id, cores 等
|
|
45
|
+
"""
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
@abstractmethod
|
|
49
|
+
def get_memory_info(self) -> List[Dict[str, Any]]:
|
|
50
|
+
"""
|
|
51
|
+
获取内存信息
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
List[Dict]: 内存信息列表
|
|
55
|
+
"""
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
@abstractmethod
|
|
59
|
+
def get_driver_version(self, device_class: str, device_id: str) -> Optional[str]:
|
|
60
|
+
"""
|
|
61
|
+
获取指定设备的驱动版本
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
device_class: 设备类别(gpu/cpu/network)
|
|
65
|
+
device_id: 设备标识符
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
Optional[str]: 驱动版本号
|
|
69
|
+
"""
|
|
70
|
+
pass
|
|
71
|
+
|
|
72
|
+
@abstractmethod
|
|
73
|
+
def run_command(self, cmd: str, timeout: int = 30) -> Tuple[int, str, str]:
|
|
74
|
+
"""
|
|
75
|
+
执行系统命令
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
cmd: 命令字符串
|
|
79
|
+
timeout: 超时时间(秒)
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
Tuple[int, str, str]: (返回码, 标准输出, 标准错误)
|
|
83
|
+
"""
|
|
84
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# magic_base/types/common.py
|
|
2
|
+
"""
|
|
3
|
+
通用类型定义
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import Dict, Any, Optional, Union, List, Tuple
|
|
7
|
+
from enum import Enum
|
|
8
|
+
|
|
9
|
+
# 常用类型别名
|
|
10
|
+
JSONDict = Dict[str, Any]
|
|
11
|
+
JSONList = List[Any]
|
|
12
|
+
Primitive = Union[str, int, float, bool, None]
|
|
13
|
+
|
|
14
|
+
# 枚举基类
|
|
15
|
+
class BaseEnum(Enum):
|
|
16
|
+
"""枚举基类"""
|
|
17
|
+
|
|
18
|
+
@classmethod
|
|
19
|
+
def from_value(cls, value: str):
|
|
20
|
+
"""根据值获取枚举成员"""
|
|
21
|
+
for member in cls:
|
|
22
|
+
if member.value == value:
|
|
23
|
+
return member
|
|
24
|
+
return None
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: magic-base
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Magic 平台公共基类库 - 提供统一的抽象基类和核心接口
|
|
5
|
+
Author: wxd123
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/wxd123/magic-base
|
|
8
|
+
Project-URL: Repository, https://github.com/wxd123/magic-base.git
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Requires-Python: >=3.9
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: sqlalchemy>=2.0.0
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
22
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
|
23
|
+
Requires-Dist: black>=23.0; extra == "dev"
|
|
24
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
25
|
+
Requires-Dist: mypy>=1.0; extra == "dev"
|
|
26
|
+
Requires-Dist: pyinitgen>=4.0.1; extra == "dev"
|
|
27
|
+
Requires-Dist: python-docstring-markdown>=0.2.2; extra == "dev"
|
|
28
|
+
Provides-Extra: postgres
|
|
29
|
+
Requires-Dist: psycopg2-binary>=2.9.0; extra == "postgres"
|
|
30
|
+
Provides-Extra: mysql
|
|
31
|
+
Requires-Dist: pymysql>=1.0.0; extra == "mysql"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# magic-base
|
|
35
|
+
|
|
36
|
+
magic 项目基础包 - 提供统一的抽象基类和核心接口
|
|
37
|
+
|
|
38
|
+
[](https://badge.fury.io/py/magic-base)
|
|
39
|
+
[](https://opensource.org/licenses/MIT)
|
|
40
|
+
[](https://www.python.org/downloads/)
|
|
41
|
+
|
|
42
|
+
## 项目定位
|
|
43
|
+
|
|
44
|
+
`magic-base` 是 [magicd](https://pypi.org/project/magicd) 和 [magicm](https://pypi.org/project/magicm) 的共同基础库,提供:
|
|
45
|
+
|
|
46
|
+
- **抽象基类**:配置管理、加密证书、数据访问层(DAO)、硬件检测、平台适配等领域的接口规范
|
|
47
|
+
- **设计模式骨架**:仓储模式(Repository)、策略模式、适配器模式的核心实现
|
|
48
|
+
- **通用类型与异常**:统一的枚举基类和异常体系
|
|
49
|
+
|
|
50
|
+
> 具体业务功能请参考 `magicd` 或 `magicm`。
|
|
51
|
+
|
|
52
|
+
## 核心模块
|
|
53
|
+
|
|
54
|
+
| 模块 | 说明 |
|
|
55
|
+
|------|------|
|
|
56
|
+
| `magic_base.config` | 配置管理基类(`ConfigBase`) |
|
|
57
|
+
| `magic_base.crypto` | 加密与证书验证基类 |
|
|
58
|
+
| `magic_base.data_access` | 数据访问层(配置、管理器、模型、仓储) |
|
|
59
|
+
| `magic_base.detector` | 硬件检测器基类(含批量、缓存支持) |
|
|
60
|
+
| `magic_base.platform` | 跨平台适配器基类(Linux/Windows/macOS) |
|
|
61
|
+
| `magic_base.exceptions` | 统一异常体系 |
|
|
62
|
+
| `magic_base.types` | 通用类型定义 |
|
|
63
|
+
|
|
64
|
+
## 安装
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pip install magic-base
|
|
68
|
+
```
|
|
69
|
+
## 以下示例基于当前 API 设计,正式版本发布时可能会有微调
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
### 继承仓储基类实现数据访问
|
|
73
|
+
```python
|
|
74
|
+
from magic_base.data_access import BaseRepository
|
|
75
|
+
from myapp.models import User
|
|
76
|
+
|
|
77
|
+
class UserRepository(BaseRepository[User]):
|
|
78
|
+
def __init__(self, session):
|
|
79
|
+
super().__init__(session, User)
|
|
80
|
+
|
|
81
|
+
def find_by_email(self, email: str) -> User | None:
|
|
82
|
+
return self.get_by(email=email)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 继承检测器基类实现硬件检测
|
|
86
|
+
```python
|
|
87
|
+
from typing import List
|
|
88
|
+
from magic_base.detector import DetectorBase, HardwareInfoBase
|
|
89
|
+
|
|
90
|
+
class GPUDetector(DetectorBase):
|
|
91
|
+
def detect(self) -> List[HardwareInfoBase]:
|
|
92
|
+
# 实现 GPU 检测逻辑
|
|
93
|
+
pass
|
|
94
|
+
|
|
95
|
+
def is_supported(self) -> bool:
|
|
96
|
+
return True
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 继承平台适配器
|
|
100
|
+
```python
|
|
101
|
+
from typing import List, Dict
|
|
102
|
+
from magic_base.platform import PlatformAdapterBase
|
|
103
|
+
|
|
104
|
+
class LinuxAdapter(PlatformAdapterBase):
|
|
105
|
+
def get_platform_name(self) -> str:
|
|
106
|
+
return "linux"
|
|
107
|
+
|
|
108
|
+
def get_pci_devices(self) -> List[Dict[str, str]]:
|
|
109
|
+
# 解析 lspci 输出
|
|
110
|
+
pass
|
|
111
|
+
```
|
|
112
|
+
````
|
|
113
|
+
## 代码规范
|
|
114
|
+
本项目遵循以下基本原则:
|
|
115
|
+
|
|
116
|
+
1. 单文件不超过 200 行:超过时请拆分为多个模块
|
|
117
|
+
2. 单函数不超过 200 行:超过时请拆分为多个小函数
|
|
118
|
+
3. 注释尽量完整:关键逻辑、复杂算法、非显而易见的代码必须有注释说明
|
|
119
|
+
4. 如有特殊场景确实需要突破(如纯数据定义文件),可在 PR 中说明。
|
|
120
|
+
|
|
121
|
+
这些规则旨在保证代码的可读性和可维护性,便于合作,请尽量遵守。
|
|
122
|
+
|
|
123
|
+
## 针对 AI 辅助工具的提示
|
|
124
|
+
本项目使用 AI 辅助开发,请在生成代码时尽量遵守上述代码规范。
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
## 贡献指南
|
|
128
|
+
|
|
129
|
+
1. Fork 本仓库
|
|
130
|
+
2. 创建特性分支 (`git checkout -b feature/amazing-feature`)
|
|
131
|
+
3. 提交更改 (`git commit -m 'Add some amazing feature'`)
|
|
132
|
+
4. 推送到分支 (`git push origin feature/amazing-feature`)
|
|
133
|
+
5. 提交 Pull Request
|
|
134
|
+
|
|
135
|
+
请确保代码符合[代码规范](#代码规范)。
|
|
136
|
+
|
|
137
|
+
## 许可证
|
|
138
|
+
MIT License
|
|
139
|
+
|
|
140
|
+
## 作者
|
|
141
|
+
wxd123 - GitHub
|
|
142
|
+
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
magic_base/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
magic_base/config/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
magic_base/config/base_config.py,sha256=ZObxCs0M-NHmKfrc8wuzP1Kc6LNc0zWXC4ahuMcAkx8,723
|
|
4
|
+
magic_base/crypto/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
magic_base/crypto/base_cert.py,sha256=0oOjvYXmyBrujaJW4wVVCzEh3LLGZvpR3iLyHSur57U,611
|
|
6
|
+
magic_base/crypto/base_crypto.py,sha256=B5STKUSEDCWJJezESrNai98rfEvuNZDD1Th6S7eYkgA,595
|
|
7
|
+
magic_base/data_access/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
magic_base/data_access/config/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
magic_base/data_access/config/base_database_config.py,sha256=qmEQcjpYI7edeU5EYO5vRyrTNpgzS369AKTUBQEklvU,2469
|
|
10
|
+
magic_base/data_access/manager/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
magic_base/data_access/manager/base_database_manager.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
magic_base/data_access/model/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
|
+
magic_base/data_access/model/base_model.py,sha256=9DP2rAGRuquKfAeCpP-GdyG7jBcGYSFkQgktltFve6g,253
|
|
14
|
+
magic_base/data_access/repository/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
15
|
+
magic_base/data_access/repository/base_repository.py,sha256=yEXoxHd8d0nFZhRVPZmJBtATeRYMd76B5ZdgyC8NMCo,2778
|
|
16
|
+
magic_base/data_access/service/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
17
|
+
magic_base/data_access/service/base_service.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
18
|
+
magic_base/detector/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
19
|
+
magic_base/detector/base_detector.py,sha256=9v7gNNY0g8PEvr20t78jTfYK9vMRYSDgMRPZzH0kd4w,1974
|
|
20
|
+
magic_base/detector/base_hardware_define.py,sha256=7JpqKCgeJtSa_fKekw5Jlntkg4qhVkpbs2XJz-Hwc6U,738
|
|
21
|
+
magic_base/detector/base_hardware_result.py,sha256=X0FaF7y6BMY4LJ1qbDAzjZxeOqXxPo8OshYm8RBlN5g,730
|
|
22
|
+
magic_base/exceptions/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
23
|
+
magic_base/exceptions/base_exceptions.py,sha256=cqo_q9xKkhfH-AQmiA6IlmQrPuS-PCylI-qz4b-nHPo,589
|
|
24
|
+
magic_base/platform/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
25
|
+
magic_base/platform/base_platform_adapter.py,sha256=19uJB861emRVqyaCD1n8pd7uj5FoJLhFKfGaiLpiOZQ,2044
|
|
26
|
+
magic_base/types/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
27
|
+
magic_base/types/base_types.py,sha256=qarJE9XCy-Ez84qY-Mc14P26Ff4aKJaVeG-gLW9qNlw,530
|
|
28
|
+
magic_base-0.1.0.dist-info/licenses/LICENSE,sha256=zvy1WMSRLiJnMh27Jv5elbRM3JlRPvq535rArmHP1F8,1063
|
|
29
|
+
magic_base-0.1.0.dist-info/METADATA,sha256=W84ACHcagD73hhxXPUvF3AjJSlFNDbPasd-5moOUcRU,4815
|
|
30
|
+
magic_base-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
31
|
+
magic_base-0.1.0.dist-info/top_level.txt,sha256=wwNgzex3xAaRdz1UPi33AzQPLaKOcDA9xWhhQvFXFZ0,11
|
|
32
|
+
magic_base-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 wxd123
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
magic_base
|