-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_handler.py
73 lines (49 loc) · 1.93 KB
/
plugin_handler.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
69
70
71
72
73
#! /usr/bin/python3
import importlib
import os
import sys
class plugins_class:
def __init__(self, ghbot_instance, directory, name_prefix):
print(self, ghbot_instance)
self.ghbot = ghbot_instance
self.directory = directory
self.name_prefix = name_prefix
self.plugins = dict()
self.load_modules()
def load_modules(self):
which = []
for filename in os.listdir(self.directory):
name_only = filename.rstrip('.py')
if filename[0:len(self.name_prefix)] == self.name_prefix and not name_only in self.plugins:
full_name = f'{self.directory}.{name_only}'
self.plugins[name_only] = importlib.import_module(full_name)
which.append(name_only)
return which
# returns True if any plugin processed the command
def process(self, nick, parameters):
for name in self.plugins:
try:
if self.plugins[name].process(self.ghbot, nick, parameters):
return True
except Exception as e:
print(f'while invoking local plugin {name}: "{e}" at line number: {e.__traceback__.tb_lineno}')
return False
def list_plugins(self):
return [name for name in self.plugins]
def get_commandos(self, name):
return self.plugins[name].get_commandos()
def reload_module(self, name):
ok = False
full_name = f'{self.directory}.{name}'
for k, v in list(sys.modules.items()):
if full_name in k:
importlib.reload(v)
ok = True
return ok
if __name__ == "__main__":
plugin_subdir = 'plugins' # relative path!!
plugins = plugins_class(None, plugin_subdir, 'ghb_')
print(plugins.list_plugins())
plugins.process('test', '!door_open')
print(plugins.get_commandos('ghb_door'))
print(plugins.reload_module('ghb_door'))