允许在相关模块中进行增量依赖注入的微框架。
incremental-module-loader的Python项目详细描述
增量模块加载器
为模块提出注入模式。每个模块都是一个简单的可调用模块,它将其他命名模块作为参数。它非常简单,只有不到40条代码语句,并且允许很大的灵活性,同时仍然删除了我们必须在每个项目上编写的频繁样板。在
这个模块由我们目前正在开发的SpinHub应用程序(https://www.spinhub.ca)使用。它是我们基于模块/服务的体系结构的主要构建块。在
用例1:模块化flask应用程序
您有一个auth服务、一个数据库服务和一个blueprint creator函数。如何将所有这些连接在一起,同时保持良好的关注点分离并允许注入模拟服务?让我们看看:
fromincremental_module_loaderimportIncrementalModuleLoaderfromflaskimportrequest,jsonifyclassRequestParsingModule(object):defget_authorization_header(self):returnrequest.headers.get('Authorization',None)classMyAuthModule(object):def__init__(self,config,request_parser):self.url=config['AUTH_URL']self.request_parser=request_parserdefget_user_id_from_token(self):header=self.request_parser.get_authorization_header()pass# Extract user id from header token...classMyDatabaseModule(object):def__init__(self,config,auth):self.db_uri=config['SQLALCHEMY_DATABASE_URI']self.auth=authdefget_user(self):user_id=self.auth.get_user_id_from_token()pass# Retrieve user data from database...# Note the name of the arguments here:defroute_app(app,config,database):@app.route('/userinfo')defuserinfo():# This triggers the auth verification from the header extracted from the request parseruser=database.get_user()returnjsonify(user.dump())# ...defcreate_app():app=# (...) Standard app creation with config loadingmodule_loader=IncrementalModuleLoader()# First init the modules with already created objects:module_loader.update(app=app,config=app.config)# Then load in order ("incrementally")# Make sure to specify a CALLABLE object (function or class)# The module_loader will call the parameter with the modules it is asking as parameters.module_loader.load(request_parser=RequestParsingModule)module_loader.load(auth=MyAuthModule)module_loader.load(database=MyDatabaseModule)# Can be loaded anonymously, returns nothing.module_loader.load(route_app)# There you have it !
创造者
由Tack Verification创建,这家公司致力于降低与硬件系统验证相关的运营成本。 https://www.tackv.ca
- 项目
标签: