This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.py
91 lines (68 loc) · 2.6 KB
/
module.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#
# 2011, Kristofer Hallin ([email protected])
#
# Mermaid, IRC bot written by Kristofer Hallin
#
import os
import sys
import log
import imp
from os.path import exists
class Module(object):
def __init__(self):
sys.path.append("plugins/")
self.log = log.Logger("module")
self.modules = {}
self.actions = {
".load": self.module_load,
".unload": self.module_unload,
".reload": self.module_reload,
}
self.modules_autoload("plugins/")
def action_fallback(self, nickname, str):
return
def module_loaded(self, name):
if not name in self.modules:
self.log.debug("Module " + name + " not loaded")
return False
self.log.debug("Module " + name + " is loaded")
return True
def module_add(self, module_name):
name = os.path.splitext(os.path.basename(module_name))[0]
self.modules[name] = imp.load_module(name, *imp.find_module(name, ["plugins/"]))
self.modules[name].__register__(self.actions)
def module_del(self, name):
self.modules[name].__unregister__(self.actions)
del self.modules[name]
def module_load(self, nickname, name):
if os.path.isfile("plugins/" + name + ".py") == True:
self.log.debug("Module " + name + ".py not found in directory")
return "Module " + name + " not found"
if self.module_loaded(name):
return "Module " + name + " already loaded"
self.module_add(name)
self.log.debug("Module " + name + " added")
return "Module " + name + " loaded"
def module_unload(self, nickname, name):
if self.module_loaded(name) == False:
return "Module " + name + " not loaded"
self.module_del(name)
return "Module " + name + " unloaded"
def module_reload(self, nickname, name):
if self.module_loaded(name) == False:
return "Module " + name + " not loaded"
self.module_unload(nickname, name);
return "Module " + name + " reloaded"
def modules_autoload(self, path):
modules = [file for file in os.listdir(path) if file.lower().endswith(".py")]
for module in modules:
module = os.path.splitext(module)[0]
self.log.debug("Loading module " + module)
self.module_load("None", "plugins/" + module)
if __name__ == '__main__':
m = Module()
# m.module_load("test", "nextep")
# m.module_reload("test", "nextep")
# m.module_unload("test", "nextep")
# m.modules_autoload("plugins/")