CMDB服务器管理系统【s5day88】:采集资产之整合插件

以后导入配置文件不用去from conf而是导入from lib.config,因为在这可以导入global_settings和settings.py

import sys
import os
import importlib
import requests
BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASEDIR)
os.environ['AUTO_CLIENT_SETTINGS'] = "conf.settings"
from lib.config import settings server_info = {}
for k,v in settings.PLUGIN_ITEMS.items():
# 找到v字符串:src.plugins.nic.Nic,src.plugins.disk.Disk
module_path,cls_name = v.rsplit('.',maxsplit=1)
module = importlib.import_module(module_path)
cls = getattr(module, cls_name)
obj = cls()#就是咋们的对象
ret = obj.process()#执行process方法 ret是v那k是什么?
server_info[k] = ret requests.post(
url=settings.API,
data=server_info
)

写成函数 

auto_client\run.py

import sys
import os
import importlib
import requests
BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASEDIR)
os.environ['AUTO_CLIENT_SETTINGS'] = "conf.settings"
from lib.config import settings if __name__ == '__main__':
func()

auto_client\src\plugins\__init__.py

def func():
server_info = {}
for k,v in settings.PLUGIN_ITEMS.items():
# 找到v字符串:src.plugins.nic.Nic,src.plugins.disk.Disk
module_path,cls_name = v.rsplit('.',maxsplit=1)
module = importlib.import_module(module_path)
cls = getattr(module,cls_name)
obj = cls()
ret = obj.process()
server_info[k] = ret requests.post(
url=settings.API,
data=server_info
)

写成一个类(和函数没有什么区别)

auto_client\src\plugins\__init__.py

import importlib
import requests
from lib.config import settings class PluginManager(object):
def __init__(self):
pass
def exec_plugin(self):
server_info = {}
for k,v in settings.PLUGIN_ITEMS.items():
# 找到v字符串:src.plugins.nic.Nic,src.plugins.disk.Disk
module_path,cls_name = v.rsplit('.',maxsplit=1)
module = importlib.import_module(module_path)
cls = getattr(module,cls_name)
obj = cls()
ret = obj.process()
server_info[k] = ret
return server_info """
requests.post(
url=settings.API,
data=server_info
)
一个文件一个目录只做一件事情
"""

auto_client\run.py

import sys
import os
import importlib
import requests
BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASEDIR)
os.environ['AUTO_CLIENT_SETTINGS'] = "conf.settings" from src.plugins import PluginManager if __name__ == '__main__':
obj = PluginManager()
server_dict = obj.exec_plugin()
print(server_dict)

运行截图

CMDB服务器管理系统【s5day88】:采集资产之整合插件

写成一个类(实例化之前可以自定义很多操作)

auto_client\run.py

import sys
import os
import importlib
import requests
BASEDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASEDIR)
os.environ['AUTO_CLIENT_SETTINGS'] = "conf.settings" from src.plugins import PluginManager if __name__ == '__main__':
obj = PluginManager()
server_dict = obj.exec_plugin()
print(server_dict)

auto_client\src\plugins\__init__.py

import importlib
import requests
from lib.config import settings class PluginManager(object):
def __init__(self):
pass
def exec_plugin(self):
server_info = {}
for k,v in settings.PLUGIN_ITEMS.items():
# 找到v字符串:src.plugins.nic.Nic,src.plugins.disk.Disk
module_path,cls_name = v.rsplit('.',maxsplit=1)
module = importlib.import_module(module_path)
cls = getattr(module,cls_name)
if hasattr(cls,'initial'):
obj = cls.initial()
else:
obj = cls()
ret = obj.process()
server_info[k] = ret
return server_info

小结

  1. 看看这个类里有没有这个属性
  2. 如果类里有方法,我就执行initial
  3. 如果没有我就直接执行这个类cls()
  4. 如果写了必须有返回值
  5. 和上例的最大区别是在实例化之前可以自定义很多操作

客户端截图

CMDB服务器管理系统【s5day88】:采集资产之整合插件

上一篇:[HNOI2013]游走 期望+高斯消元


下一篇:HDU6166-求集合间的最短路