-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserva.py
More file actions
60 lines (51 loc) · 1.75 KB
/
serva.py
File metadata and controls
60 lines (51 loc) · 1.75 KB
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
import os
import inspect
from glob import glob
from importlib.machinery import SourceFileLoader
from biblib import biblib
from configobj import ConfigObj
from validate import Validator
from plugin_template import IRCPlugin
class Serva(object):
def __init__(self):
self.config = None
self.bot = None
self.plugins = {}
self.load()
def load(self):
config = ConfigObj("serva.ini", configspec="config_spec.ini")
self.config = config
config.validate(Validator(), copy=True)
config.write()
self.bot = biblib.Bot((config["hostname"], config["port"]), config["name"], usessl=config["ssl"])
self.bot.events.Connected += self.connected
self.load_all_plugins()
self.bot.connect()
def load_all_plugins(self):
if not os.path.exists("plugins"):
os.mkdir("plugins")
for module in glob("plugins/*.py"):
print(module)
print(self.load_plugin(module))
def load_plugin(self, module):
name = os.path.splitext(os.path.basename(module))[0]
if name == "__init__":
return False
loader = SourceFileLoader("plugins." + name, module)
try:
plugin = loader.load_module()
except ImportError:
self.log_error()
return False
for i, cls in inspect.getmembers(plugin, inspect.isclass):
if issubclass(cls, IRCPlugin) and cls is not IRCPlugin:
print(cls)
self.plugins[name] = cls(self)
self.plugins[name].load()
return True
return False
def connected(self):
for channel in self.config["channels"]:
self.bot.join(channel)
if __name__ == "__main__":
Serva()