aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/plugin.py
blob: f567cbb50c5195cf6aa74e7668f718d139f75af1 (plain) (blame)
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
import os
import os.path
import sys
def my_import(mod_name):
    module = __import__(mod_name)
    components = mod_name.split('.')
    for comp in components[1:]:
        module = getattr(module, comp)
    return module

def iter_plugins(prefix):
    """
    >>> "list" in [n for n,m in iter_plugins("becommands")]
    True
    >>> "plugin" in [n for n,m in iter_plugins("libbe")]
    True
    """
    modfiles = os.listdir(os.path.join(plugin_path, prefix))
    modfiles.sort()
    for modfile in modfiles:
        if modfile.endswith(".py") and modfile != "__init__.py":
            yield modfile[:-3], my_import(prefix+"."+modfile[:-3])


def get_plugin(prefix, name):
    """
    >>> get_plugin("becommands", "asdf") is None
    True
    >>> q = repr(get_plugin("becommands", "list"))
    >>> q.startswith("<module 'becommands.list' from ")
    True
    """
    dirprefix = '/'.join(prefix.split('.'))
    command_path = os.path.join(plugin_path, dirprefix, name+".py")
    if os.path.isfile(command_path):
        return my_import(prefix + "." + name)
    return None

plugin_path = sys.path[0]
while not os.path.isfile(os.path.join(plugin_path, "libbe/plugin.py")):
    plugin_path = os.path.realpath(os.path.dirname(plugin_path))
if plugin_path not in sys.path:
    sys.path.append(plugin_path)
def _test():
    import doctest
    doctest.testmod()

if __name__ == "__main__":
    _test()