外掛模式(使用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()