-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.py
74 lines (57 loc) · 1.96 KB
/
modules.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
# With insirpation fro: https://github.com/opensourcehacker/sevabot
from __future__ import absolute_import, division
import os
import imp
import settings
modules = {}
class Module:
def __init__(self, skype, name, path):
# TODO: Scan the file for its regex trigger, which should be defined as "trigger"
self.skype = skype
self.path = path
self.name = name
self.module = imp.load_source(self.name, self.path)
self.triggers = self.module.triggers
@staticmethod
def isValid(path):
"""
Simple validity checker.
TODO: Check if there are any python errors in module before loading it
"""
return path.endswith(".py")
def run(self, arg):
self.module.main(arg)
# def init(self, skype):
# """
# (Re)load Python code and get access to exported class instance.
# Bound stateful handler to a Skype instance.
# """
# # http://docs.python.org/2/library/imp.html#imp.load_module
# self.module = imp.load_source(self.name, self.path)
# #self.handler = module.sevabot_handler
# #self.handler.init(skype)
def loadModules():
"""
Scan all modules folders for executable scripts.
"""
unloadModules()
for folder in settings.MODULE_PATHS:
folder = os.path.abspath(folder)
for f in os.listdir(folder):
fpath = os.path.join(folder, f)
# Remove file extension
body, ext = os.path.splitext(f)
module = loadModule(body, fpath)
if module:
modules[body] = module
if not len(modules.keys()):
raise RuntimeError("No modules found in: %s" % settings.MODULE_PATHS)
return modules.keys()
def loadModule(name, path):
# At the moment, only support python programs as modules
if Module.isValid(path):
return Module(None, name, path)
else:
return None
def unloadModules():
pass