我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用werkzeug.import_string()。
def _set_cache(self, app, config): import_me = config['CACHE_TYPE'] if '.' not in import_me: from . import backends try: cache_obj = getattr(backends, import_me) except AttributeError: raise ImportError("%s is not a valid FlaskCache backend" % ( import_me)) else: cache_obj = import_string(import_me) cache_args = config['CACHE_ARGS'][:] cache_options = {'default_timeout': config['CACHE_DEFAULT_TIMEOUT']} if config['CACHE_OPTIONS']: cache_options.update(config['CACHE_OPTIONS']) if not hasattr(app, 'extensions'): app.extensions = {} app.extensions.setdefault('cache', {}) app.extensions['cache'][self] = cache_obj( app, config, cache_args, cache_options)
def cmd(): """ Help to run the command line :return: """ global application mochapyfile = os.path.join(os.path.join(CWD, "brew.py")) if os.path.isfile(mochapyfile): cwd_to_sys_path() application = import_string("brew") else: print("-" * 80) print("** Missing << 'brew.py' >> @ %s" % CWD) print("-" * 80) [cmd(cli.command, click) for cmd in Manager.__subclasses__()] cli()
def install_app(self, app): install_apps = app.config.setdefault('INSTALLED_APPS', []) for blueprint in install_apps: kwargs = {} if isinstance(blueprint, dict): kwargs = blueprint['kwargs'] blueprint = blueprint['blueprint'] app.register_blueprint(import_string(blueprint), **kwargs)
def process(self, app): for middleware_string in self.middleware: middleware = import_string(middleware_string) response = middleware() if hasattr(response, 'preprocess_request'): before_request = response.preprocess_request app.before_request(before_request) if hasattr(response, 'process_response'): after_request = response.process_response app.after_request(after_request)
def view(self): view = import_string(self.name) if isinstance(view, (object, )): assert self.options.get('endpoint') is not None endpoint = self.options.pop('endpoint') view = view.as_view(endpoint) return view
def _single(self, app): blueprint = import_string(self.module + self.blueprint) app.register_blueprint(blueprint, **self.options)
def _multi(self, app): blueprints = list(set(self.blueprint)) for name in blueprints: blueprint = import_string(self.module + name) app.register_blueprint(blueprint, **self.options)
def _multi(self, app): extensions = list(set(self.extension)) for name in extensions: extension = import_string(self.module + name) extension.init_app(app)
def process(self, app, middleware): for middleware_string in middleware: middleware = import_string(middleware_string) response = middleware() if hasattr(response, 'preprocess_request'): before_request = response.preprocess_request app.before_request(before_request) if hasattr(response, 'process_response'): after_request = response.process_response app.after_request(after_request)
def from_object(self, obj): """Updates the values from the given object. An object can be of one of the following two types: - a string: in this case the object with that name will be imported - an actual object reference: that object is used directly Objects are usually either modules or classes. Just the uppercase variables in that object are stored in the config after lowercasing. Example usage:: app.config.from_object('yourapplication.default_config') from yourapplication import default_config app.config.from_object(default_config) You should not use this function to load the actual configuration but rather configuration defaults. The actual config should be loaded with :meth:`from_pyfile` and ideally from a location not within the package because the package might be installed system wide. :param obj: an import name or object """ if isinstance(obj, basestring): obj = import_string(obj) for key in dir(obj): if key.isupper(): self[key] = getattr(obj, key)
def setup_installed_apps(cls): """ To import 3rd party applications along with associated properties It is a list of dict or string. When a dict, it contains the `app` key and the configuration, if it's a string, it is just the app name If you require dependencies from other packages, dependencies must be placed before the calling package. It is required that __init__ in the package app has an entry point method -> 'main(**kw)' which will be used to setup the default app. As a dict INSTALLED_APPS = [ "it.can.be.a.string.to.the.module", ("in.a.tuple.with.props.dict", {options}), [ ("multi.app.list.in.a.list.of.tuple", {options}), ("multi.app.list.in.a.list.of.tuple2", {options}) ] ] :return: """ cls._installed_apps = cls._app.config.get("INSTALLED_APPS", []) if cls._installed_apps: def import_app(module, props={}): _ = werkzeug.import_string(module) setattr(_, "__options__", utils.dict_dot(props)) for k in cls._installed_apps: if isinstance(k, six.string_types): # One string import_app(k, {}) elif isinstance(k, tuple): import_app(k[0], k[1]) elif isinstance(k, list): # list of tuple[(module props), ...] for t in k: import_app(t[0], t[1])