-
Notifications
You must be signed in to change notification settings - Fork 29
/
__init__.py
68 lines (52 loc) · 2.07 KB
/
__init__.py
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
import asyncio
import importlib
import os
import warnings
import logging
import time
import inspect
async def init(bot):
logging.basicConfig(level=logging.WARNING)
logging.getLogger('asyncio').setLevel(logging.ERROR)
plugins = [
# Dynamically import
importlib.import_module(f'.', f'{__name__}.{file[:-3]}')
# All the files in the current directory
for file in os.listdir(os.path.dirname(__file__))
# If they start with a letter and are Python files
if file[0].isalpha() and file.endswith('.py')
]
# Keep a mapping of module name to module for easy access inside the plugins
modules = {m.__name__.split('.')[-1]: m for m in plugins}
# All kwargs provided to get_init_args are those that plugins may access
to_init = (get_init_coro(plugin, bot=bot, modules=modules) for plugin in plugins)
# Plugins may not have a valid init so those need to be filtered out
await asyncio.gather(*(filter(None, to_init)))
def get_init_coro(plugin, **kwargs):
p_init = getattr(plugin, 'init', None)
if not callable(p_init):
return
result_kwargs = {}
sig = inspect.signature(p_init)
for param in sig.parameters:
if param in kwargs:
result_kwargs[param] = kwargs[param]
else:
logging.error('Plugin %s has unknown init parameter %s', plugin.__name__, param.__name__)
return
return _init_plugin(plugin, result_kwargs)
async def _init_plugin(plugin, kwargs):
try:
logging.warning(f'Loading plugin {plugin.__name__}…')
start = time.time()
ret = await plugin.init(**kwargs)
took = time.time() - start
logging.warning(f'Loaded plugin {plugin.__name__} (took {took:.2f}s)')
except Exception:
logging.exception(f'Failed to load plugin {plugin}')
else:
# Plugins may return a task that should not just be lost.
if asyncio.iscoroutinefunction(ret):
await ret
async def start_plugins(bot, plugins):
await asyncio.gather(*(_init_plugin(bot, plugin) for plugin in plugins))