我们从Python开源项目中,提取了以下40个代码示例,用于说明如何使用django.utils.module_loading.module_has_submodule()。
def autodiscover_modules(*args, force_reload=True): """ An enhanced version of `django.utils.module_loading.autodiscover_modules`, which can force target modules to be reloaded. """ from django.apps import apps import sys for app_config in apps.get_app_configs(): for module_to_search in args: mod_name = '%s.%s' % (app_config.name, module_to_search) if force_reload: try: del sys.modules[mod_name] except KeyError: pass try: importlib.import_module(mod_name) except Exception: if module_has_submodule(app_config.module, module_to_search): raise
def autodiscover(): """ Auto-discover INSTALLED_APPS modules: * page_processors.py to fill out the default PageProcessorRegistry, * sitemap.py to populate sitemap_config.sitemaps. Inspired by django.contrib.admin.autodiscover. """ from django.conf import settings from django.utils.module_loading import module_has_submodule from powerpages import page_processor_registry for app in settings.INSTALLED_APPS: mod = import_module(app) # page processors: try: before_import_registry = copy.copy( page_processor_registry.registry.registry ) import_module('%s.page_processors' % app) except: page_processor_registry.registry.registry = before_import_registry if module_has_submodule(mod, 'page_processors'): raise # sitemaps: try: import_module('%s.sitemap' % app) except: if module_has_submodule(mod, 'sitemap'): raise
def import_models(self, all_models): # Dictionary of models for this app, primarily maintained in the # 'all_models' attribute of the Apps this AppConfig is attached to. # Injected as a parameter because it gets populated when models are # imported, which might happen before populate() imports models. self.models = all_models if module_has_submodule(self.module, MODELS_MODULE_NAME): models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME) self.models_module = import_module(models_module_name)
def handle(self, *args, **options): print("Commencing dev data import", file=stdout) for app in settings.INSTALLED_APPS: mod = import_module(app) # Attempt to import the app's test.data module. try: mod_data = import_module('%s.tests.data' % app) mod_data.load() except: # Decide whether to bubble up this error. If the app just # doesn't have an test.data module, we can ignore the error # attempting to import it, otherwise we want it to bubble up. if module_has_submodule(mod, 'test.data'): raise
def autoload_filters(): from django.conf import settings from django.utils.importlib import import_module from django.utils.module_loading import module_has_submodule for app in settings.INSTALLED_APPS: mod = import_module(app) try: import_module('%s.text_filters' % app) except: if module_has_submodule(mod, 'text_filters'): raise
def import_models(self): # Dictionary of models for this app, primarily maintained in the # 'all_models' attribute of the Apps this AppConfig is attached to. self.models = self.apps.all_models[self.label] if module_has_submodule(self.module, MODELS_MODULE_NAME): models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME) self.models_module = import_module(models_module_name)
def get_callable(lookup_view, can_fail=False): """ Convert a string version of a function name to the callable object. If the lookup_view is not an import path, it is assumed to be a URL pattern label and the original string is returned. If can_fail is True, lookup_view might be a URL pattern label, so errors during the import fail and the string is returned. """ if not callable(lookup_view): mod_name, func_name = get_mod_func(lookup_view) if func_name == '': return lookup_view try: mod = import_module(mod_name) except ImportError: parentmod, submod = get_mod_func(mod_name) if (not can_fail and submod != '' and not module_has_submodule(import_module(parentmod), submod)): raise ViewDoesNotExist( "Could not import %s. Parent module %s does not exist." % (lookup_view, mod_name)) if not can_fail: raise else: try: lookup_view = getattr(mod, func_name) if not callable(lookup_view): raise ViewDoesNotExist( "Could not import %s.%s. View is not callable." % (mod_name, func_name)) except AttributeError: if not can_fail: raise ViewDoesNotExist( "Could not import %s. View does not exist in module %s." % (lookup_view, mod_name)) return lookup_view
def autodiscover(): """ Auto-discover INSTALLED_APPS admin.py modules and fail silently when not present. This forces an import on them to register any admin bits they may want. """ import copy from django.conf import settings from django.utils.importlib import import_module from django.utils.module_loading import module_has_submodule for app in settings.INSTALLED_APPS: mod = import_module(app) # Attempt to import the app's admin module. try: before_import_registry = copy.copy(site._registry) import_module('%s.admin' % app) except: # Reset the model registry to the state before the last import as # this import will have to reoccur on the next request and this # could raise NotRegistered and AlreadyRegistered exceptions # (see #8245). site._registry = before_import_registry # Decide whether to bubble up this error. If the app just # doesn't have an admin module, we can ignore the error # attempting to import it, otherwise we want it to bubble up. if module_has_submodule(mod, 'admin'): raise
def autodiscover(): for app in settings.INSTALLED_APPS: mod = import_module(app) try: import_module('%s.%s' % (app, 'link_resolvers')) except: if module_has_submodule(mod, 'link_resolvers'): raise
def autodiscover(self): """ Auto-discover serializers in installed apps, fail silently when not present, re-raise exception when present and import fails. Borrowed form django.contrib.admin with added nested presence check. """ from importlib import import_module from django.apps import apps from django.utils.module_loading import module_has_submodule for app_config in apps.get_app_configs(): app = app_config.name mod = import_module(app) # Attempt to import the app's serializers. for item in self.paths: try: import_module('{}.{}'.format(app, item)) except (TypeError, ImportError): # Decide whether to bubble up this error. If the app just # doesn't have serializers module, we can ignore the error # attempting to import it, otherwise we want it to bubble up. curr = mod curr_path = app for part in item.split('.'): # pragma: no cover if not module_has_submodule(curr, part): break curr_path += '.' + part curr = import_module(curr_path) else: # pragma: no cover raise
def is_library_missing(name): """Check if library that failed to load cannot be found under any templatetags directory or does exist but fails to import. Non-existing condition is checked recursively for each subpackage in cases like <appdir>/templatetags/subpackage/package/module.py. """ # Don't bother to check if '.' is in name since any name will be prefixed # with some template root. path, module = name.rsplit('.', 1) try: package = import_module(path) return not module_has_submodule(package, module) except ImportError: return is_library_missing(path)
def autodiscover(): """ Auto-discover INSTALLED_APPS admin.py modules and fail silently when not present. This forces an import on them to register any admin bits they may want. """ from importlib import import_module from django.conf import settings from django.utils.module_loading import module_has_submodule setattr(settings, 'CRISPY_TEMPLATE_PACK', 'bootstrap3') setattr(settings, 'CRISPY_CLASS_CONVERTERS', { "textinput": "textinput textInput form-control", "fileinput": "fileinput fileUpload form-control", "passwordinput": "textinput textInput form-control", }) from xadmin.views import register_builtin_views register_builtin_views(site) # load xadmin settings from XADMIN_CONF module try: xadmin_conf = getattr(settings, 'XADMIN_CONF', 'xadmin_conf.py') conf_mod = import_module(xadmin_conf) except Exception: conf_mod = None if conf_mod: for key in dir(conf_mod): setting = getattr(conf_mod, key) try: if issubclass(setting, Settings): site.register_settings(setting.__name__, setting) except Exception: pass from xadmin.plugins import register_builtin_plugins register_builtin_plugins(site) for app in settings.INSTALLED_APPS: mod = import_module(app) # Attempt to import the app's admin module. try: before_import_registry = site.copy_registry() import_module('%s.adminx' % app) except: # Reset the model registry to the state before the last import as # this import will have to reoccur on the next request and this # could raise NotRegistered and AlreadyRegistered exceptions # (see #8245). site.restore_registry(before_import_registry) # Decide whether to bubble up this error. If the app just # doesn't have an admin module, we can ignore the error # attempting to import it, otherwise we want it to bubble up. if module_has_submodule(mod, 'adminx'): raise
def get_callable(lookup_view, can_fail=False): """ Return a callable corresponding to lookup_view. This function is used by both resolve() and reverse(), so can_fail allows the caller to choose between returning the input as is and raising an exception when the input string can't be interpreted as an import path. If lookup_view is already a callable, return it. If lookup_view is a string import path that can be resolved to a callable, import that callable and return it. If lookup_view is some other kind of string and can_fail is True, the string is returned as is. If can_fail is False, an exception is raised (either ImportError or ViewDoesNotExist). """ if callable(lookup_view): return lookup_view mod_name, func_name = get_mod_func(lookup_view) if not func_name: # No '.' in lookup_view if can_fail: return lookup_view else: raise ImportError( "Could not import '%s'. The path must be fully qualified." % lookup_view) try: mod = import_module(mod_name) except ImportError: if can_fail: return lookup_view else: parentmod, submod = get_mod_func(mod_name) if submod and not module_has_submodule(import_module(parentmod), submod): raise ViewDoesNotExist( "Could not import '%s'. Parent module %s does not exist." % (lookup_view, mod_name)) else: raise else: try: view_func = getattr(mod, func_name) except AttributeError: if can_fail: return lookup_view else: raise ViewDoesNotExist( "Could not import '%s'. View does not exist in module %s." % (lookup_view, mod_name)) else: if not callable(view_func): # For backwards compatibility this is raised regardless of can_fail raise ViewDoesNotExist( "Could not import '%s.%s'. View is not callable." % (mod_name, func_name)) return view_func
def autodiscover(): import xadmin from xadmin.sites import site xadmin.site = site # ? crispy_form ?????settings? setattr(settings, 'CRISPY_TEMPLATE_PACK', 'bootstrap3') setattr(settings, 'CRISPY_CLASS_CONVERTERS', { "textinput": "textinput textInput form-control", "fileinput": "fileinput fileUpload form-control", "passwordinput": "textinput textInput form-control", }) # ???????? register_builtin_views(site) # ???? register_builtin_plugins(site) # ???app? adminx for app in settings.INSTALLED_APPS: # ??app mod = import_module(app) if hasattr(mod,'app_label'): app_label = mod.app_label else: app_label = app.split('.')[-1] site.app_dict[app_label] = mod # app?????? site.sys_menu[app_label] = {'_default_group':{'title': u'??', 'icon': 'group_configure', 'menus': []} } if hasattr(mod,'menus'): m_menus = mod.menus for e in m_menus: site.sys_menu[app_label][e[0]] = {'title': e[1], 'icon': e[2], 'menus': []} # ?? adminx ?? try: before_import_registry = site.copy_registry() import_module('%s.adminx' % app) except: site.restore_registry(before_import_registry) if module_has_submodule(mod, 'adminx'): raise
def get_callable(lookup_view): """ Return a callable corresponding to lookup_view. * If lookup_view is already a callable, return it. * If lookup_view is a string import path that can be resolved to a callable, import that callable and return it, otherwise raise an exception (ImportError or ViewDoesNotExist). """ if callable(lookup_view): return lookup_view if not isinstance(lookup_view, six.string_types): raise ViewDoesNotExist("'%s' is not a callable or a dot-notation path" % lookup_view) mod_name, func_name = get_mod_func(lookup_view) if not func_name: # No '.' in lookup_view raise ImportError("Could not import '%s'. The path must be fully qualified." % lookup_view) try: mod = import_module(mod_name) except ImportError: parentmod, submod = get_mod_func(mod_name) if submod and not module_has_submodule(import_module(parentmod), submod): raise ViewDoesNotExist( "Could not import '%s'. Parent module %s does not exist." % (lookup_view, mod_name) ) else: raise else: try: view_func = getattr(mod, func_name) except AttributeError: raise ViewDoesNotExist( "Could not import '%s'. View does not exist in module %s." % (lookup_view, mod_name) ) else: if not callable(view_func): raise ViewDoesNotExist( "Could not import '%s.%s'. View is not callable." % (mod_name, func_name) ) return view_func
def autodiscover(): """ Auto-discover INSTALLED_APPS admin.py modules and fail silently when not present. This forces an import on them to register any admin bits they may want. """ from django.conf import settings from django.utils.importlib import import_module from django.utils.module_loading import module_has_submodule setattr(settings, 'CRISPY_TEMPLATE_PACK', 'bootstrap3') setattr(settings, 'CRISPY_CLASS_CONVERTERS', { "textinput": "textinput textInput form-control", "fileinput": "fileinput fileUpload form-control", "passwordinput": "textinput textInput form-control", }) from xadmin.views import register_builtin_views register_builtin_views(site) # load xadmin settings from XADMIN_CONF module try: xadmin_conf = getattr(settings, 'XADMIN_CONF', 'xadmin_conf.py') conf_mod = import_module(xadmin_conf) except Exception: conf_mod = None if conf_mod: for key in dir(conf_mod): setting = getattr(conf_mod, key) try: if issubclass(setting, Settings): site.register_settings(setting.__name__, setting) except Exception: pass from xadmin.plugins import register_builtin_plugins register_builtin_plugins(site) for app in settings.INSTALLED_APPS: mod = import_module(app) # Attempt to import the app's admin module. try: before_import_registry = site.copy_registry() import_module('%s.adminx' % app) except: # Reset the model registry to the state before the last import as # this import will have to reoccur on the next request and this # could raise NotRegistered and AlreadyRegistered exceptions # (see #8245). site.restore_registry(before_import_registry) # Decide whether to bubble up this error. If the app just # doesn't have an admin module, we can ignore the error # attempting to import it, otherwise we want it to bubble up. if module_has_submodule(mod, 'adminx'): raise