File: loader.py

package info (click to toggle)
linkchecker 10.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: python: 13,154; makefile: 134; sh: 71; xml: 36; sql: 20; javascript: 19; php: 2
file content (110 lines) | stat: -rw-r--r-- 3,546 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright (C) 2012-2014 Bastian Kleineidam
"""
Functions to load plugin modules.

Example usage::

    modules = loader.get_package_modules('plugins')
    plugins = loader.get_plugins(modules, PluginClass)
"""
import os
import importlib.util
import pkgutil

from .fileutil import is_writable_by_others


def check_writable_by_others(filename):
    """Check if file is writable by others on POSIX systems.
    On non-POSIX systems the check is ignored."""
    if os.name != 'posix':
        # XXX on non-posix systems other bits are relevant
        return
    return is_writable_by_others(filename)


def get_package_modules(packagename, packagepath):
    """Find all valid modules in the given package which must be a folder
    in the same directory as this loader.py module. A valid module has
    a .py extension, and is importable.

    @return: all loaded valid modules
    @rtype: iterator of module
    """
    for mod in pkgutil.iter_modules(packagepath):
        if not mod.ispkg:
            try:
                name = f"..{packagename}.{mod.name}"
                yield importlib.import_module(name, __name__)
            except ImportError as msg:
                print(_("WARN: could not load module %s: %s") % (mod.name, msg))


def get_folder_modules(folder, parentpackage):
    """."""
    if check_writable_by_others(folder):
        print("ERROR: refuse to load modules from world writable folder %r" % folder)
        return
    for filename in get_importable_files(folder):
        fullname = os.path.join(folder, filename)
        modname = parentpackage + "." + filename[:-3]
        try:
            spec = importlib.util.spec_from_file_location(modname, fullname)
            module = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(module)
            yield module
        except ImportError as msg:
            print(_("WARN: could not load file %s: %s") % (fullname, msg))


def get_importable_files(folder):
    """Find all module files in the given folder that end with '.py' and
    don't start with an underscore.

    @return: module names
    @rtype: iterator of string
    """
    for fname in os.listdir(folder):
        if fname.endswith('.py') and not fname.startswith('_'):
            fullname = os.path.join(folder, fname)
            if check_writable_by_others(fullname):
                print(
                    "ERROR: refuse to load module from world writable file %r"
                    % fullname
                )
            else:
                yield fname


def get_plugins(modules, classes):
    """Find all given (sub-)classes in all modules.

    @param modules: the modules to search
    @type modules: iterator of modules
    @return: found classes
    @rtype: iterator of class objects
    """
    for module in modules:
        yield from get_module_plugins(module, classes)


def get_module_plugins(module, classes):
    """Return all subclasses of a class in the module.
    If the module defines __all__, only those entries will be searched,
    otherwise all objects not starting with '_' will be searched.
    """
    try:
        names = module.__all__
    except AttributeError:
        names = [x for x in vars(module) if not x.startswith('_')]
    for name in names:
        try:
            obj = getattr(module, name)
        except AttributeError:
            continue
        try:
            for classobj in classes:
                if issubclass(obj, classobj):
                    yield obj
        except TypeError:
            continue