本文整理汇总了Python中django.utils.importlib.import_module方法的典型用法代码示例。如果您正苦于以下问题:Python importlib.import_module方法的具体用法?Python importlib.import_module怎么用?Python importlib.import_module使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.importlib
的用法示例。
在下文中一共展示了importlib.import_module方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resolve_default_fixture
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def resolve_default_fixture(module, module_fragment, fixture_class, schema=None, *args, **kwargs):
"""
Return the default fixture_class version of the given module. The default_fixture is the fixture matching
the module [module].default_[module_fragment]. This is the top of the food-chain. All client fixtures
should incorporate the default fixtures into their own. The default also is used if a certain client
fixture is not defined.
:param module: module under footprint.client.configuration.default
:param module_fragment: the * fragment of the module default_* (e.g. 'built_form' for default_built_form)
:param fixture_class: The class to lookup in the module. Any subclass will match as well
:return:
"""
default_module_string = '%s.%s_%s' % (module, 'default', module_fragment) if module else \
'%s_%s' % ('default', module_fragment)
client_module = importlib.import_module(
"footprint.client.configuration.default.%s" % default_module_string)
class_members = inspect.getmembers(sys.modules[client_module.__name__], inspect.isclass)
for name, cls in class_members:
if issubclass(cls, fixture_class) and cls != fixture_class:
return cls(schema or 'global', *args, **kwargs)
示例2: parse_backend_conf
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def parse_backend_conf(backend, **kwargs):
"""
Helper function to parse the backend configuration
that doesn't use the URI notation.
"""
# Try to get the CACHES entry for the given backend name first
conf = settings.CACHES.get(backend, None)
if conf is not None:
args = conf.copy()
args.update(kwargs)
backend = args.pop('BACKEND')
location = args.pop('LOCATION', '')
return backend, location, args
else:
try:
# Trying to import the given backend, in case it's a dotted path
mod_path, cls_name = backend.rsplit('.', 1)
mod = importlib.import_module(mod_path)
backend_cls = getattr(mod, cls_name)
except (AttributeError, ImportError, ValueError):
raise InvalidCacheBackendError("Could not find backend '%s'" % backend)
location = kwargs.pop('LOCATION', '')
return backend, location, kwargs
示例3: register_serializer
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def register_serializer(format, serializer_module, serializers=None):
"""Register a new serializer.
``serializer_module`` should be the fully qualified module name
for the serializer.
If ``serializers`` is provided, the registration will be added
to the provided dictionary.
If ``serializers`` is not provided, the registration will be made
directly into the global register of serializers. Adding serializers
directly is not a thread-safe operation.
"""
if serializers is None and not _serializers:
_load_serializers()
module = importlib.import_module(serializer_module)
if serializers is None:
_serializers[format] = module
else:
serializers[format] = module
示例4: load_handler
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def load_handler(path, *args, **kwargs):
"""
Given a path to a handler, return an instance of that handler.
E.g.::
>>> load_handler('django.core.files.uploadhandler.TemporaryFileUploadHandler', request)
<TemporaryFileUploadHandler object at 0x...>
"""
i = path.rfind('.')
module, attr = path[:i], path[i+1:]
try:
mod = importlib.import_module(module)
except ImportError as e:
raise ImproperlyConfigured('Error importing upload handler module %s: "%s"' % (module, e))
except ValueError:
raise ImproperlyConfigured('Error importing upload handler module.'
'Is FILE_UPLOAD_HANDLERS a correctly defined list or tuple?')
try:
cls = getattr(mod, attr)
except AttributeError:
raise ImproperlyConfigured('Module "%s" does not define a "%s" upload handler backend' % (module, attr))
return cls(*args, **kwargs)
示例5: load
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def load(self):
if not self.loaded:
for app in settings.INSTALLED_APPS:
if protocol_module_re.match(app):
try:
importlib.import_module(app+'.protocol')
except ImportError:
logger.exception()
self.loaded = True
示例6: discover_providers
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def discover_providers(self):
if not self.discovered:
for app in settings.INSTALLED_APPS:
provider_module = app + '.provider'
try:
importlib.import_module(provider_module)
except ImportError:
pass
self.discovered = True
示例7: resolve_client_module
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def resolve_client_module(module, module_fragment, schema=settings.CLIENT):
"""
Resolve the client module under footprint.client.configuration.[client], which client is the
specified or default client
:param module: The base module. This can be set to none to fetch a module directly under [client]
:param module_fragment: The sub module under base module (if base is not None) minus the client name.
So 'land_use_definition' resolves to '[client]_land_use_definition'
:param schema: Optional schema string. Defaults to settings.CLIENT
:return:
"""
module_string = form_module_name(module, module_fragment, schema)
return importlib.import_module("footprint.client.configuration.%s" % module_string)
示例8: _configure_logging
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def _configure_logging(self):
"""
Setup logging from LOGGING_CONFIG and LOGGING settings.
"""
if not sys.warnoptions:
try:
# Route warnings through python logging
logging.captureWarnings(True)
# Allow DeprecationWarnings through the warnings filters
warnings.simplefilter("default", DeprecationWarning)
except AttributeError:
# No captureWarnings on Python 2.6, DeprecationWarnings are on anyway
pass
if self.LOGGING_CONFIG:
from django.utils.log import DEFAULT_LOGGING
# First find the logging configuration function ...
logging_config_path, logging_config_func_name = self.LOGGING_CONFIG.rsplit('.', 1)
logging_config_module = importlib.import_module(logging_config_path)
logging_config_func = getattr(logging_config_module, logging_config_func_name)
logging_config_func(DEFAULT_LOGGING)
if self.LOGGING:
# Backwards-compatibility shim for #16288 fix
compat_patch_logging_config(self.LOGGING)
# ... then invoke it with the logging settings
logging_config_func(self.LOGGING)
示例9: import_attribute
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def import_attribute(path):
assert isinstance(path, str)
pkg, attr = path.rsplit('.', 1)
ret = getattr(importlib.import_module(pkg), attr)
return ret
示例10: ready
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def ready(self):
importlib.import_module("pinax.teams.receivers")
示例11: import_submodules
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def import_submodules(module):
"""Import all submodules and make them available in a dict."""
submodules = {}
for loader, name, ispkg in pkgutil.iter_modules(module.__path__,
module.__name__ + '.'):
try:
submodule = importlib.import_module(name)
except ImportError as e:
# FIXME: Make the errors non-fatal (do we want that?).
logging.warning("Error importing %s" % name)
logging.exception(e)
else:
parent, child = name.rsplit('.', 1)
submodules[child] = submodule
return submodules
示例12: get_serializer
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def get_serializer(self, **kwargs):
serializer = self.model.Elasticsearch.serializer_class
if isinstance(serializer, basestring):
module, kls = self.model.Elasticsearch.serializer_class.rsplit(".", 1)
mod = importlib.import_module(module)
return getattr(mod, kls)(self.model, **kwargs)
else:
return serializer(self.model, **kwargs)
示例13: get_cache
# 需要导入模块: from django.utils import importlib [as 别名]
# 或者: from django.utils.importlib import import_module [as 别名]
def get_cache(backend, **kwargs):
"""
Function to load a cache backend dynamically. This is flexible by design
to allow different use cases:
To load a backend with the old URI-based notation::
cache = get_cache('locmem://')
To load a backend that is pre-defined in the settings::
cache = get_cache('default')
To load a backend with its dotted import path,
including arbitrary options::
cache = get_cache('django.core.cache.backends.memcached.MemcachedCache', **{
'LOCATION': '127.0.0.1:11211', 'TIMEOUT': 30,
})
"""
try:
if '://' in backend:
# for backwards compatibility
backend, location, params = parse_backend_uri(backend)
if backend in BACKENDS:
backend = 'django.core.cache.backends.%s' % BACKENDS[backend]
params.update(kwargs)
mod = importlib.import_module(backend)
backend_cls = mod.CacheClass
else:
backend, location, params = parse_backend_conf(backend, **kwargs)
mod_path, cls_name = backend.rsplit('.', 1)
mod = importlib.import_module(mod_path)
backend_cls = getattr(mod, cls_name)
except (AttributeError, ImportError) as e:
raise InvalidCacheBackendError(
"Could not find backend '%s': %s" % (backend, e))
cache = backend_cls(location, params)
# Some caches -- python-memcached in particular -- need to do a cleanup at the
# end of a request cycle. If the cache provides a close() method, wire it up
# here.
if hasattr(cache, 'close'):
signals.request_finished.connect(cache.close)
return cache