Example #1
0
    def init_event(plugin='plugins.events'):
        logging.debug('loading event plugin "%s"', plugin)
        path_name = None

        from importlib.util import find_spec as importlib_find

        path_name = importlib_find(plugin)
        try:
            path_name = path_name.submodule_search_locations[0]
        except TypeError:
            path_name = path_name.origin

        module_list = [plugin]
        if not path_name.endswith('.py'):
            module_list = glob('{}/[!_]*.py'.format(path_name))
            module_list = [
                '.'.join((plugin, os.path.split(f)[-1][:-3]))
                for f in module_list
            ]

        for module in module_list:
            try:
                import_module(module)
            except:
                logger.exception('Failed to import %s', module)
Example #2
0
    def _load_plugins(self, plugin):
        logger.info('loading plugin "%s"', plugin)
        path_name = None

        if PY2:
            import imp

            for mod in plugin.split('.'):
                if path_name is not None:
                    path_name = [path_name]
                _, path_name, _ = imp.find_module(mod, path_name)
        else:
            from importlib.util import find_spec as importlib_find

            path_name = importlib_find(plugin)
            try:
                path_name = path_name.submodule_search_locations[0]
            except TypeError:
                path_name = path_name.origin

        module_list = [plugin]
        if not path_name.endswith('.py'):
            module_list = glob('{}/[!_]*.py'.format(path_name))
            module_list = [
                '.'.join((plugin, os.path.split(f)[-1][:-3]))
                for f in module_list
            ]
        for module in module_list:
            try:
                import_module(module)
            except:
                # TODO Better exception handling
                logger.exception('Failed to import %s', module)
Example #3
0
    def _load_plugins(self, plugin):
        logger.info('loading plugin "%s"', plugin)
        path_name = None

        if PY2:
            import imp

            for mod in plugin.split('.'):
                if path_name is not None:
                    path_name = [path_name]
                _, path_name, _ = imp.find_module(mod, path_name)
        else:
            from importlib.util import find_spec as importlib_find

            path_name = importlib_find(plugin)
            try:
                path_name = path_name.submodule_search_locations[0]
            except TypeError:
                path_name = path_name.origin

        module_list = [plugin]
        if not path_name.endswith('.py'):
            module_list = glob('{}/[!_]*.py'.format(path_name))
            module_list = ['.'.join((plugin, os.path.split(f)[-1][:-3])) for f
                           in module_list]
        for module in module_list:
            try:
                import_module(module)
            except:
                # TODO Better exception handling
                logger.exception('Failed to import %s', module)
def module_has_submodule(package, module_name):
    """See if 'module' is in 'package'."""
    """
      package:  app 模块 实例  
      module_name = "models"
    """
    print("package.__path__==", package.__path__)
    try:
        package_name = package.__name__  # 例如: 'django.contrib.auth'  值为 settings.INSTALLED_APPS 中 所配置的值
        # 此 module 的 url 地址,
        # 例如: ['D:\\SystemInstallFiles\\python_msi\\pythonVirtual_Env\\liujiang_Env\\lib\\site-packages\\django\\contrib\\admin']
        package_path = package.__path__
    except AttributeError:
        # package isn't a package.
        return False

    full_module_name = package_name + '.' + module_name  # 'django.contrib.auth.models'

    try:
        return importlib_find(full_module_name, package_path) is not None
    except (ImportError, AttributeError):
        # When module_name is an invalid dotted path, Python raises ImportError
        # (or ModuleNotFoundError in Python 3.6+). AttributeError may be raised
        # if the penultimate part of the path is not a package.
        # (https://bugs.python.org/issue30436)
        return False
Example #5
0
    def module_has_submodule(package, module_name):
        """See if 'module' is in 'package'."""
        try:
            package_name = package.__name__
            package_path = package.__path__
        except AttributeError:
            # package isn't a package.
            return False

        full_module_name = package_name + '.' + module_name
        return importlib_find(full_module_name, package_path) is not None
Example #6
0
    def module_has_submodule(package, module_name):
        """See if 'module' is in 'package'."""
        try:
            package_name = package.__name__
            package_path = package.__path__
        except AttributeError:
            # package isn't a package.
            return False

        full_module_name = package_name + '.' + module_name
        return importlib_find(full_module_name, package_path) is not None
Example #7
0
def module_has_submodule(package, module_name):
    """See if 'module' is in 'package'."""
    try:
        package_name = package.__name__
        package_path = package.__path__
    except AttributeError:
        # package isn't a package.
        return False

    full_module_name = package_name + "." + module_name
    try:
        return importlib_find(full_module_name, package_path) is not None
    except ModuleNotFoundError:
        # When module_name is an invalid dotted path, Python raises
        # ModuleNotFoundError.
        return False
Example #8
0
def module_has_submodule(package, module_name):
    """See if 'module' is in 'package'."""
    try:
        package_name = package.__name__
        package_path = package.__path__
    except AttributeError:
        # package isn't a package.
        return False

    full_module_name = package_name + '.' + module_name
    try:
        return importlib_find(full_module_name, package_path) is not None
    except (ModuleNotFoundError, AttributeError):
        # When module_name is an invalid dotted path, Python raises
        # ModuleNotFoundError. AttributeError is raised on PY36 (fixed in PY37)
        # if the penultimate part of the path is not a package.
        return False
Example #9
0
def module_has_submodule(package, module_name):
    """See if 'module' is in 'package'."""
    try:
        package_name = package.__name__
        package_path = package.__path__
    except AttributeError:
        # package isn't a package.
        return False

    full_module_name = package_name + '.' + module_name
    try:
        return importlib_find(full_module_name, package_path) is not None
    except (ModuleNotFoundError, AttributeError):
        # When module_name is an invalid dotted path, Python raises
        # ModuleNotFoundError. AttributeError is raised on PY36 (fixed in PY37)
        # if the penultimate part of the path is not a package.
        return False
Example #10
0
def module_has_submodule(package, module_name):
    """See if 'module' is in 'package'."""
    try:
        package_name = package.__name__
        package_path = package.__path__
    except AttributeError:
        # package isn't a package.
        return False

    full_module_name = package_name + '.' + module_name
    try:
        return importlib_find(full_module_name, package_path) is not None
    except (ImportError, AttributeError):
        # When module_name is an invalid dotted path, Python raises ImportError
        # (or ModuleNotFoundError in Python 3.6+). AttributeError may be raised
        # if the penultimate part of the path is not a package.
        # (https://bugs.python.org/issue30436)
        return False
Example #11
0
def autodiscover_modules(*module_names,
                         parent_modules=[],
                         find_in_apps=True,
                         not_core_apps=False):
    """
    Auto-discover named modules on module paths.
    Fails silently when not present. 
    Forces an import on the module to recover any requests.
    Very similar to django.utils.module_loading.autodiscover_modules,
    but it's not.
    
    module_names 
        module names to find
    parent_modules 
        hardcoded list of module paths to search
    find_in_apps 
        seek for modules in registered apps
    not_core_apps 
        remove 'django' paths from any given list
    return
        list of modules loaded
    """
    app_modules = []
    if (find_in_apps):
        app_modules = [a.name for a in apps.get_app_configs()]
    if (not_core_apps):
        app_modules = [p for p in app_modules if (not p.startswith('django'))]
    module_parents = [*parent_modules, *app_modules]
    r = []
    for module_parent in module_parents:
        for name in module_names:
            # Attempt to import the app's module.
            try:
                p = module_parent + '.' + name
                import_module(p)
                r.append(p)
            except Exception:
                # if the module doesn't exist, ignore the error, if
                # it does and threw an error, bubble up.
                #NB: Django's module_has_submodule is not a good test. May
                # fail as code, so module not exist.
                if (importlib_find(module_parent + '.' + name) is not None):
                    raise
    return r
Example #12
0
    def _load_plugins(plugin):
        logger.info('loading plugin "%s"', plugin)
        path_name = importlib_find(plugin)
        try:
            path_name = path_name.submodule_search_locations[0]
        except TypeError:
            path_name = path_name.origin

        module_list = [plugin]
        if not path_name.endswith('.py'):
            module_list = glob('{}/[!_]*.py'.format(path_name))
            module_list = [
                '.'.join((plugin, os.path.split(f)[-1][:-3]))
                for f in module_list
            ]
        for module in module_list:
            try:
                import_module(module)
            except Exception:  # pylint: disable=broad-except
                # TODO Better exception handling
                logger.exception('Failed to import %s', module)