插件模式(使用import_module导入子模块)
# 目录结构
- plugins
- __init__.py # ProcessFactory 工厂对象
- base_data_analysis.py # 基类
- board_data_analysis.py # 具体实现自类
### __init__.py 实现
# -*-coding:utf-8-*-
import importlib
from AutoServer.settings import CMDB_PLUGIN_DICT
class ProcessSeverInfoFactory(object):
def __init__(self):
pass
@staticmethod
def process_server_info(asset_data, server_obj):
"""
# 处理中控机,采集的资产信息
:param asset_data: # 全部资产数据
:param server_obj: # 主机外键
:return:
"""
for asset_class, path in CMDB_PLUGIN_DICT.items():
data = asset_data.get(asset_class, {}) # 每一种解析类对应的采集数据
if not data: # 没有采集该种类的数据,跳过
continue
module_path, class_name = path.rsplit(".", maxsplit=1)
module = importlib.import_module(module_path)
cls = getattr(module, class_name)
print("#" * 40)
print("资产采集正在解析:", cls.__name__)
cls_obj = cls(asset_class=asset_class)
cls_obj.process(data, server_obj)
print("资产采集解析完毕:", cls.__name__)
print("#" * 40)
psi_factory = ProcessSeverInfoFactory()