This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_plugins.py
93 lines (65 loc) · 2.56 KB
/
mod_plugins.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
92
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import imp
import os
import re
import sys
import mod_logger
log = mod_logger.initlog(__name__)
RE_PLUGIN_FILE = re.compile("^([^\.]+)\.py$")
found = dict()
quorum = False
switcher = False
notifier = False
def search(plugin_dir):
global found
for subdir in os.listdir(plugin_dir):
if not os.path.isdir(os.path.join(plugin_dir, subdir)):
continue
for filename in os.listdir(os.path.join(plugin_dir, subdir)):
if not os.path.isfile(os.path.join(plugin_dir, subdir, filename)):
continue
match = RE_PLUGIN_FILE.match(filename)
if match:
plugin_type = subdir
plugin_name = match.group(1)
plugin_desc = imp.find_module(plugin_name, [os.path.join(plugin_dir, plugin_type)])
if not found.has_key(plugin_type):
found[plugin_type] = dict()
found[plugin_type][plugin_name] = plugin_desc
return found
def load(plugin_type, plugin_name):
global found
if not found:
log.fatal("Plugins detection has not been done, please call plugins.search(plugin_dir) first.\n")
return False
if found.has_key(plugin_type) and found[plugin_type].has_key(plugin_name):
return imp.load_module("%s_%s" % (plugin_type, plugin_name), *found[plugin_type][plugin_name])
return False
def loadQuorum(quorum_plugin):
global quorum
# load quorum plugin if any
if quorum_plugin:
quorum = load("quorum", quorum_plugin) or log.fatal("Failed to load quorum plugin '%s'" % (quorum_plugin))
def loadSwitcher(switcher_plugin):
global switcher
# load switcher plugin if any
if switcher_plugin:
switcher = load("switcher", switcher_plugin) or log.fatal("Failed to load switcher plugin '%s'" % (switcher_plugin))
def loadNotifier(notifier_plugin):
global notifier
# load notifier plugin if any
if notifier_plugin:
notifier = load("notifier", notifier_plugin) or log.fatal("Failed to load notifier plugin '%s'" % (notifier_plugin))
def show():
global found
if not found:
sys.stderr.write("Plugins detection has not been done, please call plugins.search(plugin_dir) first.\n")
return False
print "Found plugins :"
for plugin_type in found.keys():
print " %s :" % (plugin_type.title())
for plugin_name in found[plugin_type].keys():
print " %s -- %s" % (plugin_name, found[plugin_type][plugin_name][1])
print
return True