Beispiel #1
0
    def test_missing_module(self):
        with self.assertRaises(ImportError):
            modules.import_module('asdfg')

        module = modules.import_module(
            'asdfg',
            ignore_missing=True,
        )
        self.assertIsNone(module)
Beispiel #2
0
def autodiscover():
    """
    Auto-discover INSTALLED_APPS registry.py modules and fail silently when
    not present. This forces an import on them to register any entries they
    may want.
    """
    from yepes.apps import apps
    from yepes.utils.modules import import_module
    for app_config in apps.get_app_configs():
        module_path = '.'.join((app_config.name, 'registry'))
        import_module(module_path, ignore_missing=True)
Beispiel #3
0
    def test_invalid_syntax(self):
        with self.assertRaises(SyntaxError):
            modules.import_module('utils.invalid_syntax')

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            module = modules.import_module(
                'utils.invalid_syntax',
                ignore_internal_errors=True,
            )
            self.assertEqual(len(w), 1)
            self.assertTrue(issubclass(w[0].category, SyntaxWarning))
            self.assertIn('invalid syntax', str(w[0].message))
            self.assertIsNone(module)
Beispiel #4
0
def get_class(self, module_name, class_name):
    """
    Dynamically imports a single class from the given ``module_name``.

    This is very similar to ``AppConfig.get_model()`` method but this
    method is more general though as it can load any class from the
    matching app, not just a model.

    Raises LookupError if the app does not contain the specified module
    or if the requested class cannot be found in the module.

    """
    module_path = self.module.__name__
    if module_name:
        module_path = '.'.join((module_path, module_name))

    module = import_module(module_path, ignore_missing=True)
    if module is None:
        msg = "Module '{0}.{1}' could not be found."
        raise LookupError(msg.format(self.label, module_name))

    try:
        return getattr(module, class_name)
    except AttributeError:
        msg = "Class '{0}' could not be found in '{1}.{2}'."
        raise LookupError(msg.format(class_name, self.label, module_name))
Beispiel #5
0
def _import_backend(path):
    module_path, class_name = path.rsplit('.', 1)
    module = import_module(module_path)
    class_ = getattr(module, class_name, None)
    if class_ is None:
        msg = "Class '{0}' could not be found in '{1}'."
        raise LookupError(msg.format(class_name, module_path))
    else:
        return class_
Beispiel #6
0
 def test_valid_module(self):
     module = modules.import_module('cgi')
     import cgi
     self.assertEqual(module, cgi)
     self.assertIs(module, cgi)
Beispiel #7
0
 def import_plan(self, path):
     module_path, class_name = path.rsplit('.', 1)
     module = import_module(module_path, ignore_internal_errors=True)
     return getattr(module, class_name, None)