def load_middleware(self): """ 从 settings.MIDDLEWARE_CLASSES 载入中间件 """ for middleware_path in settings.MIDDLEWARE_CLASSES: mw_class = import_string(middleware_path) self.middleware_list.append(mw_class) logger.debug('middleware_list: \n%s' % '\n'.join([text_type(m) for m in self.middleware_list]))
def load_handler(path, *args, **kwargs): """ Given a path to a handler, return an instance of that handler. E.g.:: >>> from django.http import HttpRequest >>> request = HttpRequest() >>> load_handler('django.core.files.uploadhandler.TemporaryFileUploadHandler', request) <TemporaryFileUploadHandler object at 0x...> """ return import_string(path)(*args, **kwargs)
def load_builtin_endpoints(self): """ 从 settings.BUILTIN_ENDPOINTS 载入内置的 endpoints """ handlers = [] for endpoint in settings.BUILTIN_ENDPOINTS: c = endpoint['config'] for url, handler_path in endpoint['handlers']: h_class = import_string(handler_path) handlers.append((r'/%s/%s%s' % (c['name'], c['version'], url), h_class)) logger.debug('builtin_endpoints: \n%s' % '\n'.join([text_type(h) for h in handlers])) return handlers
def _load_builtin_endpoints(self): """ 从 settings.BUILTIN_ENDPOINTS 载入内置的 endpoints """ handlers = {} for endpoint in settings.BUILTIN_ENDPOINTS: c = endpoint['config'] key = '%s/%s' % (c['name'], c['version']) handlers[key] = [] for reg_uri, handler_path in endpoint['handlers']: h_class = import_string(handler_path) handlers[key].append((reg_uri, h_class)) self.builtin_endpoints = handlers
def load_builtin_endpoints(self): """ 从 settings.BUILTIN_ENDPOINTS 载入内置的 endpoints """ handlers = [] for endpoint in settings.BUILTIN_ENDPOINTS: c = endpoint['config'] for url, handler_path in endpoint['handlers']: h_class = import_string(handler_path) handlers.append( (r'/%s/%s%s' % (c['name'], c['version'], url), h_class)) logger.debug('builtin_endpoints: \n%s' % '\n'.join([text_type(h) for h in handlers])) return handlers
def main(): args = parse_args() text = 'test plugin.' print 'Default: %s' % text # 找出插件目录下所有的插件 modules = find_modules('plugins', silent=True) for module in modules: enabled_plugins = ['plugins.' + x for x in args.plugins] # 根据命令行选项 --enable 的值过滤插件 if module in enabled_plugins: # 导入插件 plugin = import_string(module, silent=True) if plugin: # 执行约定的每个插件内必须有的函数 print plugin.output(text)
def register_package(pkg): """ Allow to register packages by loading and exposing: templates, static, and exceptions for abort() Structure of package root | $package_name | __init__.py | | exceptions.py | | /templates | | | | /static | | assets.yml :param pkg: str - __package__ or __name__ or The root dir or the dotted resource package (package.path.path, usually __name__ of templates and static """ root_pkg_dir = pkg if not os.path.isdir(pkg) and "." in pkg: root_pkg_dir = utils.get_pkg_resources_filename(pkg) template_path = os.path.join(root_pkg_dir, "templates") static_path = os.path.join(root_pkg_dir, "static") logging.info("Registering Package: " + pkg) if os.path.isdir(template_path): template_path = jinja2.FileSystemLoader(template_path) View._template_paths.add(template_path) if os.path.isdir(static_path): View._static_paths.add(static_path) View._add_asset_bundle(static_path) if os.path.isfile(os.path.join(root_pkg_dir, "exceptions.py")): exceptions = utils.import_string(pkg + ".exceptions") init_app(lambda x: abort.map_from_module(exceptions))
def run_hooks(self, import_names, args): """Executes a list of functions defined as strings. They are imported dynamically so their modules must be in sys.path. If any of the functions isn't found, none will be executed. """ # Import all first. hooks = [] for import_name in import_names: hook = utils.import_string(import_name, True) if hook is None: self.error('Could not import %r.' % import_name) hooks.append(hook) # Execute all. for hook in hooks: hook(self.manager, args)
def create_product(self): return usecases.CreateProduct(product_repo=import_string( settings.STORAGE.get("product")), )