Add a similar basis pluggable middleware settings on design
Pluggable design settings before you can see the article
https://www.cnblogs.com/zx125/p/11735505.html
Design ideas
Django middleware and achieve similar functionality, you can simply add and delete configuration settings will be able to, whether to perform the function of the middleware operation
File Directory
--about_settings
--default
--conf
--__init__.py
--global_settings.py 默认配置文件
--notify
--__init__.py
--email.py
--msg.py
--wechat.py
--user
--settings.py 用户配置文件
--start.py 启动文件
__init__.py
import importlib
def send_all(settings,content):
for module_path in settings.NOTIFY_LIST:#遍历配置里面的字符串
module, class_name = module_path.rsplit('.',maxsplit=1)#切割出包,和类名
mod = importlib.import_module(module)#获取模块对象
cls_obj = getattr(mod,class_name)#反射获取类对象
cls_obj().send(content)#实例化对象,并执行指定方法
email,msg,wechat
Duck type, a common method of a plurality of classes
class Email():
def __init__(self):
pass
def send(self,content):
print(content)
class Msg():
def __init__(self):
pass
def send(self,content):
print(content)
class Msg():
def __init__(self):
pass
def send(self,content):
print(content)
start.py
import os
import sys
import notify
BASE_DIR = os.path.dirname(__file__)
sys.path.append(BASE_DIR)
os.environ.setdefault('settings','user.settings')
from default.conf import settings
notify.send_all(settings,"大家好")