Example #1
0
def autodiscover():
    for app_name in settings.INSTALLED_APPS:
        app = import_module(app_name)
        if module_has_submodule(app, 'serializers'):
            import_module(app_name + '.serializers')
    for app_name in settings.INSTALLED_APPS:
        app = import_module(app_name)
        if module_has_submodule(app, 'views'):
            import_module(app_name + '.views')
 def test_has_sumbodule_with_dotted_path(self):
     """Nested module existence can be tested."""
     test_module = import_module('utils_tests.test_module')
     # A grandchild that exists.
     self.assertIs(module_has_submodule(test_module, 'child_module.grandchild_module'), True)
     # A grandchild that doesn't exist.
     self.assertIs(module_has_submodule(test_module, 'child_module.no_such_module'), False)
     # A grandchild whose parent doesn't exist.
     self.assertIs(module_has_submodule(test_module, 'no_such_module.grandchild_module'), False)
     # A grandchild whose parent is not a package.
     self.assertIs(module_has_submodule(test_module, 'good_module.no_such_module'), False)
Example #3
0
def autodiscover():
    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        if module_has_submodule(mod, 'models'):
            if not module_has_submodule(mod, 'admin'):
                models = import_module('%s.models' % app)
                for model in get_models(models):
                    try:
                        admin.site.register(model)
                    except AlreadyRegistered:
                        pass
            else:
                import_module('%s.admin' % app)
Example #4
0
def autodiscover_models(given_app=None, given_model=None):
    """ Autodiscover `fakers` modules and `ModelFaker` subclasses
        `given_app` can be given to restrict autodiscovering
        `given_model` can be given to fake only one model
    """

    if given_model and not given_app:
        raise ImproperlyConfigured("If model is given, app must be given too ")

    # Autodiscover fakers
    apps = given_app and [given_app] or settings.INSTALLED_APPS
    for app in apps:
        mod = import_module(app)
        # Attempt to import the app's fakers module.
        if module_has_submodule(mod, 'fakers'):
            import_module('%s.fakers' % app)

    models = ModelFaker.__subclasses__()
    if given_app:
        module_fakers = getattr(sys.modules.get(given_app), 'fakers')
        models = [m for m in models
                  if m.__module__.startswith(module_fakers.__name__)]
        if given_model:
            models = [getattr(module_fakers, given_model)]

    return models
def autodiscover():
    """
    Auto-discover INSTALLED_APPS ophandler.py modules and fail silently when
    not present. This forces an import on them to register any handler 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

    handler_module_name = settings.OBJECT_PERMISSION_HANDLER_MODULE_NAME

    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        # Attempt to import the app's object permission handler module.
        try:
            before_import_registry = copy.copy(site._registry)
            import_module('%s.%s' % (app, handler_module_name))
        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
            site._registry = before_import_registry

            # Decide wheter to bubble up this error. If the app just
            # doesn't have an object permission module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, handler_module_name):
                raise
def autodiscover():
    """
    Iterate over django.apps.get_app_configs() and discover
    versatileimagefield.py modules.
    """
    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():
        # Attempt to import the app's module.

        try:
            before_import_sizedimage_registry = copy.copy(
                versatileimagefield_registry._sizedimage_registry
            )
            before_import_filter_registry = copy.copy(
                versatileimagefield_registry._filter_registry
            )
            import_module('%s.versatileimagefield' % app_config.name)
        except:
            # Reset the versatileimagefield_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 django ticket #8245).
            versatileimagefield_registry._sizedimage_registry = \
                before_import_sizedimage_registry
            versatileimagefield_registry._filter_registry = \
                before_import_filter_registry

            # Decide whether to bubble up this error. If the app just
            # doesn't have the module in question, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(app_config.module, 'versatileimagefield'):
                raise
Example #7
0
    def get_app_modules(self, apps):
        """return array of imported leonardo modules for apps
        """
        modules = getattr(self, "_modules", [])

        if not modules:
            from django.utils.module_loading import module_has_submodule

            # Try importing a modules from the module package
            package_string = '.'.join(['leonardo', 'module'])

            for app in apps:
                exc = '...'
                try:
                    # check if is not full app
                    _app = import_module(app)
                except Exception as e:
                    _app = False
                    exc = e

                if module_has_submodule(
                        import_module(package_string), app) or _app:
                    if _app:
                        mod = _app
                    else:
                        mod = import_module('.{0}'.format(app), package_string)
                    if mod:
                        modules.append(mod)
                        continue

                warnings.warn('%s was skipped because %s ' % (app, exc))

            self._modules = modules
        return self._modules
Example #8
0
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)
        try:
            if func_name != "":
                lookup_view = getattr(import_module(mod_name), 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)
                )
        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
    return lookup_view
Example #9
0
def autodiscover():
    """
    Auto-discover INSTALLED_APPS report.py modules and fail silently when
    not present. Borrowed form django.contrib.admin
    """

    from django.utils.importlib import import_module
    from django.utils.module_loading import module_has_submodule

    global reports

    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        # Attempt to import the app's admin module.
        try:
            before_import_registry = copy.copy(reports)
            import_module('%s.reports' % 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).
            reports = 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, 'reports'):
                raise
Example #10
0
def get_notifications(user):
	notifications = {}
	for app in settings.INSTALLED_APPS:
		mod = import_module(app)
		
		try:
			submod = import_module('%s.notifications' % app)
		except:
			if module_has_submodule(mod, 'notifications'):
				raise
			
			continue
		
		for name in dir(submod):
			attr = getattr(submod, name)
			if isinstance(attr, NotificationTemplate):
				if attr.staff_only and not user.is_staff:
					continue
				
				if isinstance(attr.perms, (list, tuple)):
					if any(attr.perms) and not user.has_perms(attr.perms):
						continue
				
				notifications[(app, name)] = attr.label or name.replace('_', ' ').capitalize()
	
	return notifications.items()
def autodiscover():
    """
    Auto-discover INSTALLED_APPS backends.py modules that inherit from
    GenericSocialUserBackend and fail silently when not present.
    This forces an import on them to register any backend classes.
    """
    from copy import copy

    from django.conf import settings
    from django.contrib.admin.sites import site
    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 email module.
        try:
            before_import_registry = copy(site._registry)
            import_module('%s.backends' % app)
        except Exception, exc:
            # 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

            # backends exists but import failed, raise the exception.
            if module_has_submodule(mod, 'backends'):
                raise Exception(
                    'Failed to import {0}.backends with error: {1}.'.format(
                        app, exc))
Example #12
0
    def _init_admin_site(self, extension):
        """Creates and initializes an admin site for an extension.

        This creates the admin site and imports the extensions admin
        module to register the models.

        The url patterns for the admin site are generated in
        _install_admin_urls().
        """
        extension.admin_site = AdminSite(extension.info.app_name)

        # Import the extension's admin module.
        try:
            admin_module_name = '%s.admin' % extension.info.app_name
            if admin_module_name in sys.modules:
                # If the extension has been loaded previously and
                # we are re-enabling it, we must reload the module.
                # Just importing again will not cause the ModelAdmins
                # to be registered.
                reload(sys.modules[admin_module_name])
            else:
                import_module(admin_module_name)
        except ImportError:
            mod = import_module(extension.info.app_name)

            # 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 ImportError(
                    "Importing admin module for extension %s failed"
                    % extension.info.app_name)
def _autodiscover(registry):
    """See documentation for autodiscover (without the underscore)"""
    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(registry)
            import_module('%s.autocomplete_light_registry' % 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).
            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, 'autocomplete_light_registry'):
                raise
Example #14
0
def get_migration_status(**options):
    # type: (**Any) -> str
    verbosity = options.get('verbosity', 1)

    for app_config in apps.get_app_configs():
        if module_has_submodule(app_config.module, "management"):
            import_module('.management', app_config.name)

    app_labels = [options['app_label']] if options.get('app_label') else None
    db = options.get('database', DEFAULT_DB_ALIAS)
    out = StringIO()
    call_command(
        'showmigrations',
        '--list',
        app_labels=app_labels,
        database=db,
        no_color=options.get('no_color', False),
        settings=options.get('settings', os.environ['DJANGO_SETTINGS_MODULE']),
        stdout=out,
        traceback=options.get('traceback', True),
        verbosity=verbosity,
    )
    connections.close_all()
    out.seek(0)
    output = out.read()
    return re.sub('\x1b\[(1|0)m', '', output)
Example #15
0
    def _urls(self):
        """Constructs the URLconf for Horizon from registered Dashboards."""
        urlpatterns = self._get_default_urlpatterns()
        self._autodiscover()

        # Discover each dashboard's panels.
        for dash in self._registry.values():
            dash._autodiscover()

        # Load the plugin-based panel configuration
        self._load_panel_customization()

        # Allow for override modules
        if self._conf.get("customization_module", None):
            customization_module = self._conf["customization_module"]
            bits = customization_module.split('.')
            mod_name = bits.pop()
            package = '.'.join(bits)
            mod = import_module(package)
            try:
                before_import_registry = copy.copy(self._registry)
                import_module('%s.%s' % (package, mod_name))
            except Exception:
                self._registry = before_import_registry
                if module_has_submodule(mod, mod_name):
                    raise

        # Compile the dynamic urlconf.
        for dash in self._registry.values():
            urlpatterns += patterns('',
                                    url(r'^%s/' % dash.slug,
                                        include(dash._decorated_urls)))

        # Return the three arguments to django.conf.urls.include
        return urlpatterns, self.namespace, self.slug
Example #16
0
	def load_app(self, app_name, can_postpone=True):
		"""
		Loads the app with the provided fully qualified name, and returns the
		model module.
		"""
		self.handled[app_name] = None
		self.nesting_level += 1
		app_module = import_module(app_name)
		try:
			cron = import_module('.crons', app_name)
		except ImportError:
			self.nesting_level -= 1
			if not module_has_submodule(app_module, 'crons'):
				return None
			else:
				if can_postpone:
					self.postponed.append(app_name)
					return None
				else:
					raise

		self.nesting_level -= 1
		if cron not in self.app_store:
			self.app_store[cron] = len(self.app_store)
			self.app_labels[self._label_for(cron)] = cron
		return cron
Example #17
0
    def load_app(self, app_name, can_postpone=False):
        """
        Loads the app with the provided fully qualified name, and returns the
        model module.
        """
        self.handled.add(app_name)
        self.nesting_level += 1
        app_module = import_module(app_name)
        try:
            models = import_module('%s.%s' % (app_name, MODELS_MODULE_NAME))
        except ImportError:
            self.nesting_level -= 1
            # If the app doesn't have a models module, we can just ignore the
            # ImportError and return no models for it.
            if not module_has_submodule(app_module, MODELS_MODULE_NAME):
                return None
            # But if the app does have a models module, we need to figure out
            # whether to suppress or propagate the error. If can_postpone is
            # True then it may be that the package is still being imported by
            # Python and the models module isn't available yet. So we add the
            # app to the postponed list and we'll try it again after all the
            # recursion has finished (in populate). If can_postpone is False
            # then it's time to raise the ImportError.
            else:
                if can_postpone:
                    self.postponed.append(app_name)
                    return None
                else:
                    raise

        self.nesting_level -= 1
        if models not in self.app_store:
            self.app_store[models] = len(self.app_store)
            self.app_labels[self._label_for(models)] = models
        return models
Example #18
0
def register_inner():
    """
    copy from django.contrib.admin.autodiscover
    """

    import copy
    from django.utils.importlib import import_module
    from django.utils.module_loading import module_has_submodule

    for app in ADMIN_INNER_APPS:
        mod = import_module(app)
        # Attempt to import the app's admin module.
        try:
            before_import_registry = copy.copy(admin.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).
            admin.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
Example #19
0
    def test_loader(self):
        "Normal module existence can be tested"
        test_module = import_module('regressiontests.utils.test_module')

        # An importable child
        self.failUnless(module_has_submodule(test_module, 'good_module'))
        mod = import_module('regressiontests.utils.test_module.good_module')
        self.assertEqual(mod.content, 'Good Module')

        # A child that exists, but will generate an import error if loaded
        self.failUnless(module_has_submodule(test_module, 'bad_module'))
        self.assertRaises(ImportError, import_module, 'regressiontests.utils.test_module.bad_module')

        # A child that doesn't exist
        self.failIf(module_has_submodule(test_module, 'no_such_module'))
        self.assertRaises(ImportError, import_module, 'regressiontests.utils.test_module.no_such_module')
Example #20
0
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
Example #21
0
    def load_app(self, app_name, can_postpone=True):
        """
		Loads the app with the provided fully qualified name, and returns the
		model module.
		"""
        self.handled[app_name] = None
        self.nesting_level += 1
        app_module = import_module(app_name)
        try:
            router = import_module(".routers", app_name)
        except ImportError:
            self.nesting_level -= 1
            if not module_has_submodule(app_module, "routers"):
                return None
            else:
                if can_postpone:
                    self.postponed.append(app_name)
                    return None
                else:
                    raise

        self.nesting_level -= 1
        if router not in self.app_store:
            self.app_store[router] = len(self.app_store)
            self.app_labels[self._label_for(router)] = router
        return router
Example #22
0
def initialize():
    logger.info("Initializing Kermit WebUI environment")
    """
    Auto-discover INSTALLED_APPS with MODULES_TO_IMPORT modules.
    """

    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        if hasattr(mod, "initialize"):
            try:
                mod.initialize()
            except:
                logger.error("ERROR initializing module %s! %s" % (app, sys.exc_info))
        else:
            logger.debug("App %s does not have initialization method" % app)

        for current_module in MODULES_TO_IMPORT:
            try:
                before_import_registry = copy.copy(kermit_modules._registry)
                import_module("%s.%s" % (app, current_module))
            except:
                kermit_modules._registry = before_import_registry
                if module_has_submodule(mod, current_module):
                    logger.error("Module %s found but there is an error importing it")
                    logger.error(sys.exc_info())
                    continue
Example #23
0
def autodiscover(module_name=None):
    """
    Auto-discover INSTALLED_APPS permissions.py modules and fail silently when
    not present. This forces an import on them to register any permissions 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
    from permission.handlers import registry

    module_name = module_name or settings.PERMISSION_MODULE_NAME

    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        # Attempt to import the app's permissions module
        try:
            before_import_registry = copy.copy(registry._registry)
            import_module('%s.%s' % (app, module_name))
        except:
            # Reset the model registry to the state before tha last import as
            # this import will have to reoccur on the next request and this
            # could raise NotRegistered and AlreadyRegistered exceptions
            # (see #8254)
            registry._registry = before_import_registry

            # Decide whether to bubble up this error. If the app just
            # doesn't have an permissions module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, module_name):
                raise
Example #24
0
    def ready(self):
        # if ready was already called
        if hasattr(self, 'bmf_config'):  # pragma: no cover
            return True

        self.bmf_config = apps.get_app_config(self.bmf_label)

        if not hasattr(self.bmf_config, 'site'):  # pragma: no cover
            raise ImproperlyConfigured(
                "Can not find a site attribute in %(cls)s. "
                "Please import the BMF-Framework before you "
                "import any BMF-Modules in your INSTALLED_APPS." % {
                    'cls': self.bmf_config.__class__.__name__
                }
            )

        # autodiscover bmf modules ============================================
        if module_has_submodule(self.module, "bmf_module"):  # pragma: no branch

            # load instructions of bmf_module.py
            import_module('%s.%s' % (self.name, "bmf_module"))

            # see if model needs a number_cycle
            for model in [m for m in self.models.values() if hasattr(m, '_bmfmeta') and m._bmfmeta.number_cycle]:
                self.bmf_config.site.register_numbercycle(model)

        logger.debug('App "%s" (%s) is ready' % (
            self.verbose_name,
            self.label,
        ))
Example #25
0
def autodiscover():
    """
    Auto-discover INSTALLED_APPS mockup.py modules and fail silently when
    not present. This forces an import on them to register any mockup bits they
    may want.
    """

    global _registry

    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        # Attempt to import the app's mockup module.
        try:
            before_import_registry = copy.copy(_registry)
            import_module('%s.mockup' % 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).
            _registry = before_import_registry

            # Decide whether to bubble up this error. If the app just
            # doesn't have an mockup module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, 'mockup'):
                raise
Example #26
0
    def collect_indexes(self):
        indexes = []

        for app in settings.INSTALLED_APPS:
            mod = importlib.import_module(app)

            try:
                search_index_module = importlib.import_module("%s.search_indexes" % app)
            except ImportError:
                if module_has_submodule(mod, "search_indexes"):
                    raise

                continue

            for item_name, item in inspect.getmembers(search_index_module, inspect.isclass):
                if getattr(item, "haystack_use_for_indexing", False) and getattr(item, "get_model", None):
                    # We've got an index. Check if we should be ignoring it.
                    class_path = "%s.search_indexes.%s" % (app, item_name)

                    if class_path in self.excluded_indexes or self.excluded_indexes_ids.get(item_name) == id(item):
                        self.excluded_indexes_ids[str(item_name)] = id(item)
                        continue

                    indexes.append(item())

        return indexes
    def collect_indexes(self):
        indexes = []

        for app in settings.INSTALLED_APPS:
            try:
                mod = importlib.import_module(app)
            except ImportError:
                warnings.warn('Installed app %s is not an importable Python module and will be ignored' % app)
                continue

            try:
                search_index_module = importlib.import_module("%s.search_indexes" % app)
            except ImportError:
                if module_has_submodule(mod, 'search_indexes'):
                    raise

                continue

            for item_name, item in inspect.getmembers(search_index_module, inspect.isclass):
                if getattr(item, 'haystack_use_for_indexing', False) and getattr(item, 'get_model', None):
                    # We've got an index. Check if we should be ignoring it.
                    class_path = "%s.search_indexes.%s" % (app, item_name)

                    if class_path in self.excluded_indexes or self.excluded_indexes_ids.get(item_name) == id(item):
                        self.excluded_indexes_ids[str(item_name)] = id(item)
                        continue

                    indexes.append(item())

        return indexes
Example #28
0
def initialize():
    logger.info("Initializing Plugins environment")
    """
    Auto-discover INSTALLED_MODULES with MODULES_TO_IMPORT modules.
    """

    installed_plugins = utils.installed_plugins_list()
    logger.info("Installed Plugins: %s" % installed_plugins)
    for plugin in installed_plugins:
        mod = import_module("%s.%s" % (BASE_PLUGIN_PATH, plugin))
        for current_module in MODULES_TO_IMPORT:
            try:
                before_import_registry = copy.copy(plugins._registry)
                import_module('%s.%s.%s' % (BASE_PLUGIN_PATH, plugin, current_module))
            except:
                plugins._registry = before_import_registry
                if module_has_submodule(mod, current_module):
                    logger.error("Module %s found but there is an error importing it")
                    logger.error(sys.exc_info())
                    continue
                
        if CHECK_WIDGETS:
            try:
                imp.find_module("widgets", mod.__path__)
                registry.inject_widget("%s.%s" % (BASE_PLUGIN_PATH, plugin))
            except ImportError:
                logger.debug ("Plugin %s does not provides widgets" % plugin)
Example #29
0
    def setUpClass(cls):
        super(TestModelsLoaderMixin, cls).setUpClass()

        cls._tests_loader_models_mod = None

        if not cls.tests_app:
            cls.tests_app = cls.__module__

        tests_module = import_module(cls.tests_app)

        if not module_has_submodule(tests_module, 'models'):
            # Set up a 'models' module, containing any models local to the
            # module that this TestCase is in.
            models_mod_name = '%s.models' % cls.tests_app
            models_mod = imp.new_module(models_mod_name)

            # Django needs a value here. Doesn't matter what it is.
            models_mod.__file__ = ''

            # Transfer all the models over into this new module.
            test_module = sys.modules[cls.__module__]

            for key, value in six.iteritems(test_module.__dict__):
                if inspect.isclass(value) and issubclass(value, Model):
                    models_mod.__dict__[key] = value

            cls._tests_loader_models_mod = models_mod
Example #30
0
def autodiscover(glean_noun='gleaners'):
    """Like admin's autodiscover.

    gleaners is the name of the module containing connectors.

    """
    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 gleaners module.
        try:
            before_import_register = copy.copy(registry._register)
            registry.register(app, glean_noun)
        except Exception, e:
            print e
            log.error(e)
            registry._register = before_import_register

            # Decide whether to bubble up this error. If the app just
            # doesn't have a gleaners module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, glean_noun):
                raise e
Example #31
0
def autodiscover():
    """
    Autodiscover function from django.contrib.admin
    """

    for app in settings.INSTALLED_APPS:
        mod = import_module(app)

        try:
            before_import_registry = copy.copy(site._registry)
            import_module('%s.admin' % app)
        except:
            site._registry = before_import_registry
            if module_has_submodule(mod, 'admin'):
                raise
Example #32
0
def collect_consts():
    consts_dict = {}

    for app in apps.get_app_configs():
        if not module_has_submodule(app.module, 'consts'):
            continue

        consts = get_consts(import_module('%s.consts' % app.name))

        if not consts:
            continue

        consts_dict[app.label] = consts

    return consts_dict
Example #33
0
    def import_modelresources(self):
        """
        If a plugin has a modelresource.py, import it

        (This exists when a plugin knows how to import-export itself)
        """
        if (
            module_has_submodule(self.module, MODELRESOURCE_MODULE_NAME)
            and self.name != "pulpcore.app"
        ):
            modelrsrc_module_name = "{name}.{module}".format(
                name=self.name, module=MODELRESOURCE_MODULE_NAME
            )
            self.modelresource_module = import_module(modelrsrc_module_name)
            self.exportable_classes = self.modelresource_module.IMPORT_ORDER
Example #34
0
def import_app_sitetree_module(app):
    """Imports sitetree module from a given app.

    :param str|unicode app: Application name
    :return: module|None
    """
    module_name = settings.APP_MODULE_NAME
    module = import_module(app)
    try:
        sub_module = import_module('%s.%s' % (app, module_name))
        return sub_module
    except ImportError:
        if module_has_submodule(module, module_name):
            raise
        return None
Example #35
0
    def _return_module(self, module_name):
        if module_has_submodule(sys.modules[self.module_path], module_name):
            models_module_name = "%s.%s" % (self.module_path, module_name)
            try:
                return import_module(models_module_name)
            except Exception as e:
                logging.warn(
                    "Tried to import module {module_name} from {plugin} but an error was raised"
                    .format(
                        plugin=self.module_path,
                        module_name=module_name,
                    ))
                logging.exception(e)

        return None
Example #36
0
def autodiscover():
    REPORTING_SOURCE_FILE = getattr(settings, 'REPORTING_SOURCE_FILE',
                                    'reports')
    modules = []
    for app in settings.INSTALLED_APPS:
        mod = import_module(app)

        try:
            module = import_module('%s.%s' % (app, REPORTING_SOURCE_FILE))
        except:
            if module_has_submodule(mod, REPORTING_SOURCE_FILE):
                raise
        else:
            modules.append(module)
    return modules
Example #37
0
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)
Example #38
0
def discover_gooddata_modules():
    gd_classes = {}
    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        try:
            gd_module = import_module('%s.gooddata' % app)
            for (cls_name, cls) in inspect.getmembers(gd_module, inspect.isclass):
                if not issubclass(cls, Dataset):
                    continue
                app_cls_name = '.'.join((app.split('.')[-1], cls_name))
                gd_classes[app_cls_name] = cls
        except:
            if module_has_submodule(mod, 'gooddata'):
                raise
    return gd_classes
Example #39
0
    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.

        # 由于apps.all_models是DefaultDict类型,当访问该字典中不存在的Key时,其将自动创建一个空键值
        # 因此,当访问self.apps.all_models[self.label]时,如果不存在,则自动创建一个空的字典项
        # 并将该字典项赋值给self.models属性
        self.models = self.apps.all_models[self.label]

        # Model模块的名称必须是'Models'
        if module_has_submodule(self.module, MODELS_MODULE_NAME):
            models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME)
            # 导入Model模块后,创建Model类时(注意不是实例),会调用元类django.db.base.ModelBase.__new__()方法
            # 在该方法中,会调用apps.register_model()方法,完成对apps.all_models属性的赋值
            self.models_module = import_module(models_module_name)
Example #40
0
    def test_loader(self):
        "Normal module existence can be tested"
        test_module = import_module('regressiontests.utils.test_module')

        # An importable child
        self.assertTrue(module_has_submodule(test_module, 'good_module'))
        mod = import_module('regressiontests.utils.test_module.good_module')
        self.assertEqual(mod.content, 'Good Module')

        # A child that exists, but will generate an import error if loaded
        self.assertTrue(module_has_submodule(test_module, 'bad_module'))
        self.assertRaises(ImportError, import_module,
                          'regressiontests.utils.test_module.bad_module')

        # A child that doesn't exist
        self.assertFalse(module_has_submodule(test_module, 'no_such_module'))
        self.assertRaises(ImportError, import_module,
                          'regressiontests.utils.test_module.no_such_module')

        # Don't be confused by caching of import misses
        import types  # causes attempted import of regressiontests.utils.types
        self.assertFalse(
            module_has_submodule(sys.modules['regressiontests.utils'],
                                 'types'))
Example #41
0
    def discover_tasks_modules(self):
        ignored_modules = set(getattr(settings, "DRAMATIQ_IGNORED_MODULES",
                                      []))
        app_configs = (c for c in apps.get_app_configs()
                       if module_has_submodule(c.module, "tasks"))
        tasks_modules = ["django_periodiq.setup"]  # Broker module is first
        for conf in app_configs:
            module = conf.name + ".tasks"
            if module in ignored_modules:
                self.stdout.write(" * Ignored tasks module: %r" % module)
            else:
                self.stdout.write(" * Discovered tasks module: %r" % module)
                tasks_modules.append(module)

        return tasks_modules
Example #42
0
def _autodiscover(recipes):
    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)
        try:
            before_import_recipes = copy.copy(recipes)
            import_module('%s.badgify_recipes' % app)
        except:
            recipes = before_import_recipes
            if module_has_submodule(mod, 'badgify_recipes'):
                raise
Example #43
0
    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
Example #44
0
def autodiscover(*args):
    ''' Discover submodules in Django apps that would otherwise be
        ignored (listeners, configforms, etc)

        Usage: autodiscover('configforms'[, 'listeners'[, ...]])
    '''
    from django.conf import settings
    from django.utils.importlib import import_module
    from django.utils.module_loading import module_has_submodule

    for submod in args:
        for app in settings.INSTALLED_APPS:
            mod = import_module(app)
            if module_has_submodule(mod, submod):
                import_module('%s.%s' % (app, submod))
Example #45
0
def autodiscover(site):
    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)
        try:
            before_import_registry = copy.copy(site._registry)
            import_module('%s.admin' % app)
        except:
            site._registry = before_import_registry
            if module_has_submodule(mod, 'admin'):
                raise
Example #46
0
def autodiscover():
    """
    Auto-discover INSTALLED_APPS badges.py modules and fail silently when
    not present.
    """
    from django.utils.importlib import import_module
    for app in django_settings.INSTALLED_APPS:
        mod = import_module(app)
        try:
            badges_mod = import_module('%s.badges' % app)
            if hasattr(badges_mod, 'register_signals'):
                badges_mod.register_signals()
        except ImportError:
            if module_has_submodule(mod, 'badges'):
                raise
def autodiscover():
    '''
    Auto-discover INSTALLED_APPS translation.py modules and fail silently when
    not present. This forces an import on them to register.
    Also import explicit modules.
    '''
    import os
    import sys
    import copy
    from django.utils.module_loading import module_has_submodule
    from modeltrans.translator import translator

    from importlib import import_module
    from django.conf import settings
    from django.apps import apps
    mods = [(app_config.name, app_config.module)
            for app_config in apps.get_app_configs()]

    for (app, mod) in mods:
        # Attempt to import the app's translation module.
        module = '%s.translation' % app
        before_import_registry = copy.copy(translator._registry)
        try:
            import_module(module)
        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
            translator._registry = before_import_registry

            # Decide whether to bubble up this error. If the app just
            # doesn't have an translation module, we can ignore the error
            # attempting to import it, otherwise we want it to bubble up.
            if module_has_submodule(mod, 'translation'):
                raise

    # In debug mode, print a list of registered models and pid to stdout.
    # Note: Differing model order is fine, we don't rely on a particular
    # order, as far as base classes are registered before subclasses.
    if settings.DEBUG:
        try:
            if sys.argv[1] in ('runserver', 'runserver_plus'):
                models = translator.get_registered_models()
                names = ', '.join(m.__name__ for m in models)
                print('modeltrans: Registered %d models for translation'
                      ' (%s) [pid: %d].' % (len(models), names, os.getpid()))
        except IndexError:
            pass
Example #48
0
    def _autodiscover(self):
        """Discovers panels to register from the current dashboard module."""
        if getattr(self, "_autodiscover_complete", False):
            return

        panels_to_discover = []
        panel_groups = []
        # If we have a flat iterable of panel names, wrap it again so
        # we have a consistent structure for the next step.
        if all([isinstance(i, six.string_types) for i in self.panels]):
            self.panels = [self.panels]

        # Now iterate our panel sets.
        default_created = False
        for panel_set in self.panels:
            # Instantiate PanelGroup classes.
            if not isinstance(panel_set, collections.Iterable) and \
                    issubclass(panel_set, PanelGroup):
                panel_group = panel_set(self)
            # Check for nested tuples, and convert them to PanelGroups
            elif not isinstance(panel_set, PanelGroup):
                panel_group = PanelGroup(self, panels=panel_set)

            # Put our results into their appropriate places
            panels_to_discover.extend(panel_group.panels)
            panel_groups.append((panel_group.slug, panel_group))
            if panel_group.slug == DEFAULT_PANEL_GROUP:
                default_created = True

        # Plugin panels can be added to a default panel group. Make sure such a
        # default group exists.
        if not default_created:
            default_group = PanelGroup(self)
            panel_groups.insert(0, (default_group.slug, default_group))
        self._panel_groups = collections.OrderedDict(panel_groups)

        # Do the actual discovery
        package = '.'.join(self.__module__.split('.')[:-1])
        mod = import_module(package)
        for panel in panels_to_discover:
            try:
                before_import_registry = copy.copy(self._registry)
                import_module('.%s.panel' % panel, package)
            except Exception:
                self._registry = before_import_registry
                if module_has_submodule(mod, panel):
                    raise
        self._autodiscover_complete = True
Example #49
0
    def handle(self, fixture, **options):
        available_fixtures = {}
        for app in settings.INSTALLED_APPS:
            try:
                fixture_gen = import_module(".fixture_gen", app)
            except ImportError:
                if module_has_submodule(import_module(app), "fixture_gen"):
                   raise
                continue
            for obj in fixture_gen.__dict__.values():
                if getattr(obj, "__fixture_gen__", False):
                    available_fixtures[(app.rsplit(".", 1)[-1], obj.__name__)] = obj
        app_label, fixture_name = fixture.rsplit(".", 1)
        try:
            fixture = available_fixtures[(app_label, fixture_name)]
        except KeyError:
            available = ", ".join(
                "%s.%s" % (app_label, fixture_name)
                for app_label, fixture_name in available_fixtures
            )
            raise CommandError("Fixture generator '%s' not found, available "
                "choices: %s" % (fixture, available))

        requirements, models = linearize_requirements(available_fixtures, fixture)

        settings.DATABASES[FIXTURE_DATABASE] = {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": ":memory:",
        }
        old_routers = router.routers
        router.routers = [FixtureRouter(models)]
        try:
            # migrate_all=True is for south, Django just absorbs it
            call_command("syncdb", database=FIXTURE_DATABASE, verbosity=0,
                interactive=False, migrate_all=True)
            for fixture_func in requirements:
                fixture_func()
            call_command("dumpdata",
                *["%s.%s" % (m._meta.app_label, m._meta.object_name) for m in models],
                **dict(options, verbosity=0, database=FIXTURE_DATABASE)
            )
        finally:
            del settings.DATABASES[FIXTURE_DATABASE]
            if isinstance(connections._connections, dict):
                del connections._connections[FIXTURE_DATABASE]
            else:
                delattr(connections._connections, FIXTURE_DATABASE)
            router.routers = old_routers
def collect_indexes_with_debugging(original_function, self):
    from django.conf import settings
    print "collect_indexes: settings = %s" % settings
    print "collect_indexes: INSTALLED_APPS = %s" % settings.INSTALLED_APPS
    # import pdb; pdb.set_trace()

    from haystack import connections
    from django.utils.module_loading import module_has_submodule
    import inspect

    try:
        from django.utils import importlib
    except ImportError:
        from haystack.utils import importlib

    for app in settings.INSTALLED_APPS:
        print "collect_indexes: trying %s" % app
        mod = importlib.import_module(app)

        try:
            search_index_module = importlib.import_module("%s.search_indexes" %
                                                          app)
        except ImportError:
            if module_has_submodule(mod, 'search_indexes'):
                raise

            continue

        for item_name, item in inspect.getmembers(search_index_module,
                                                  inspect.isclass):
            print "collect_indexes: %s: %s" % (
                item_name, getattr(item, 'haystack_use_for_indexing', False))
            if getattr(item, 'haystack_use_for_indexing', False):
                # We've got an index. Check if we should be ignoring it.
                class_path = "%s.search_indexes.%s" % (app, item_name)

                print "excluded_index %s = %s" % (class_path, class_path
                                                  in self.excluded_indexes)
                print "excluded_indexes_id %s = %s" % (
                    str(item_name), self.excluded_indexes_ids.get(item_name)
                    == id(item))

                if class_path in self.excluded_indexes or self.excluded_indexes_ids.get(
                        item_name) == id(item):
                    self.excluded_indexes_ids[str(item_name)] = id(item)
                    continue

    return original_function(self)
Example #51
0
def get_callable(lookup_view, can_fail=False):
    """
    将函数的字符串版本转换成一个可调用的函数, 主要是从各个 app 中搜索相关的 view

    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
Example #52
0
    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.
        #
        # 这里是一个shadow-copy,
        # 即: self.apps.all_models 发生变化后, self.models 也会跟着发生变化.
        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)

            # 这里负责导入model,
            # ModelBase会回调register.register_model,
            # 将 model 写入回到 self.apps.all_modles 中,
            # 从而更新到 self.models 中.
            self.models_module = import_module(models_module_name)
Example #53
0
 def _get_default_urlpatterns(self):
     package_string = '.'.join(self.__module__.split('.')[:-1])
     if getattr(self, 'urls', None):
         try:
             mod = import_module('.%s' % self.urls, package_string)
         except ImportError:
             mod = import_module(self.urls)
         urlpatterns = mod.urlpatterns
     else:
         # Try importing a urls.py from the dashboard package
         if module_has_submodule(import_module(package_string), 'urls'):
             urls_mod = import_module('.urls', package_string)
             urlpatterns = urls_mod.urlpatterns
         else:
             urlpatterns = []
     return urlpatterns
Example #54
0
def autodiscover():
    """
    Taken from ``django.contrib.admin.autodiscover`` and used to run
    any calls to the ``processor_for`` decorator.
    """
    global LOADED
    if LOADED:
        return
    LOADED = True
    for app in settings.INSTALLED_APPS:
        module = import_module(app)
        try:
            import_module("%s.page_processors" % app)
        except:
            if module_has_submodule(module, "page_processors"):
                raise
Example #55
0
    def import_models(self, all_models=None):
        if all_models == None:
            self.models = self.apps.all_models[self.label]
        else:
            self.models = all_models

        if module_has_submodule(self.module, MODELS_MODULE_NAME):
            for _mod in self.models:
                mod = self.models[_mod]
                # if type(mod) != str and mod._meta.app_label == self.name:
                #    return
            models_module_name = "%s.%s" % (self.name, MODELS_MODULE_NAME)
            try:
                self.models_module = import_module(models_module_name)
            except:
                self.models_module = None
Example #56
0
def load_caches():
    import_module('.caches', 'flash')

    FLASH_APPS = flash_settings.FLASH_APPS
    if isfunction(FLASH_APPS):
        FLASH_APPS = FLASH_APPS()

    for app_name in FLASH_APPS:
        app_module = import_module(app_name)
        try:
            module = import_module('.caches', app_name)
        except ImportError:
            if module_has_submodule(app_module, 'caches'):
                print('Import error in %s/caches.py:' % app_name)
                raise
    import flash.contenttypes_caches
Example #57
0
def update_badges(overwrite=False):
    from django.utils.importlib import import_module

    for app in settings.INSTALLED_APPS:
        mod = import_module(app)
        try:
            badges_mod = import_module('%s.badges' % app)
            fixture_label = '%s_badges' % app.replace('.', '_')
            call_command('loaddata', fixture_label, verbosity=1)
            if hasattr(badges_mod, 'badges'):
                badger.utils.update_badges(badges_mod.badges, overwrite)
            if hasattr(badges_mod, 'update_badges'):
                badges_mod.update_badges(overwrite)
        except ImportError:
            if module_has_submodule(mod, 'badges'):
                raise
    def discover_tasks_modules(self):
        ignored_modules = set(getattr(settings, "DRAMATIQ_IGNORED_MODULES",
                                      []))
        app_configs = (c for c in apps.get_app_configs()
                       if module_has_submodule(c.module, "tasks"))
        tasks_modules = ["django_dramatiq.setup"]

        def is_ignored_module(module_name):
            if not ignored_modules:
                return False

            if module_name in ignored_modules:
                return True

            name_parts = module_name.split(".")

            for c in range(1, len(name_parts)):
                part_name = ".".join(name_parts[:c]) + ".*"
                if part_name in ignored_modules:
                    return True

            return False

        for conf in app_configs:
            module = conf.name + ".tasks"

            if is_ignored_module(module):
                self.stdout.write(" * Ignored tasks module: %r" % module)
                continue

            imported_module = importlib.import_module(module)
            if not self._is_package(imported_module):
                self.stdout.write(" * Discovered tasks module: %r" % module)
                tasks_modules.append(module)
            else:
                submodules = self._get_submodules(imported_module)

                for submodule in submodules:
                    if is_ignored_module(submodule):
                        self.stdout.write(" * Ignored tasks module: %r" %
                                          submodule)
                    else:
                        self.stdout.write(" * Discovered tasks module: %r" %
                                          submodule)
                        tasks_modules.append(submodule)

        return tasks_modules
Example #59
0
def autodiscover():
    # 为 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)
        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
Example #60
0
def autodiscover():
    from importlib import import_module
    from django.conf import settings
    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 api module.
        try:
            import_module('%s.publishers' % app)
        except:

            # 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, 'publishers'):
                raise