-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
222 lines (186 loc) · 7.37 KB
/
plugin.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import common
app = common.app # global NMControl app object
import threading
import os
from optparse import OptionGroup
try:
from ConfigParser import SafeConfigParser # Python 2.X
except ImportError:
from configparser import SafeConfigParser # Python 3+
import inspect
log = common.get_logger(__name__)
def public(func):
func.rpc_public = True
return func
class PluginThread(threading.Thread):
daemon = True
name = None
mode = None
desc = None
running = False
threadRunning = False
options = {}
helps = {}
depends = {}
services = {}
#version = None
def __init__(self, mode = 'plugin'):
if self.name is None:
raise Exception(str(self.__class__) + " : name not defined")
self.mode = mode
self.nameconf = self.mode + '-' + self.name + '.conf'
self.nameclass = self.mode + self.name.capitalize()
self.parser = app['parser']
self.conf = {}
self._loadconfig()
threading.Thread.__init__(self)
def start(self):
if self.threadRunning: return
self.threadRunning = True
threading.Thread.start(self)
def run(self):
if self.running: return
self.start2()
def start2(self, arg = []):
log.debug("Plugin %s parent starting" %(self.name))
self.running = True
# start depends
if len(self.depends) > 0:
if 'plugins' in self.depends:
for dep in self.depends['plugins']:
try:
app['plugins'][dep].start()
except KeyError:
raise Exception(str(self.name) + " 'depends': plugin not found: " + dep)
if 'services' in self.depends:
for dep in self.depends['services']:
try:
app['services'][dep].start()
except KeyError:
raise Exception(str(self.name) + " 'depends': service not found: " + dep)
return self.pStart()
def pStart(self, arg = []):
log.debug("Plugin %s parent start" %(self.name))
return True
def stop(self, arg = []):
if not self.running: return
log.debug("Plugin %s parent stopping" %(self.name))
self.running = False
return self.pStop()
def pStop(self, arg = []):
log.debug("Plugin %s parent stop" %(self.name))
log.info("Plugin %s stopped" %(self.name))
return True
def status(self, arg = []):
return self.pStatus()
def pStatus(self, arg = []):
if self.running:
return "Plugin " + self.name + " running"
def reload(self, arg = []):
log.debug("Plugin %s parent reloading" %(self.name))
return self.pReload()
def pReload(self, arg = []):
log.debug("Plugin %s parent reload" %(self.name))
self.loadconfig()
return True
def restart(self, arg = []):
log.debug("Plugin %s parent restarting" %(self.name))
return self.pRestart()
def pRestart(self, arg = []):
log.debug("Plugin %s parent restart" %(self.name))
self.stop()
self.start2()
return True
def help(self, *arg):
return self.pHelp(*arg)
def pHelp(self, *args):
if len(args) > 0 and type(args[0]) != list:
method = args[0]
if method in self.helps:
help = self.helps[method][3]
help += '\n' + method + ' ' + self.helps[method][2]
return help
methods = self._getPluginMethods()
help = '* Available commands for plugin ' + self.name + ' :'
for method in methods:
if method in self.helps:
method = method + ' ' + self.helps[method][2]
help += '\n' + method
return help
def _getPluginMethods(self):
parents = []
parentmethods = inspect.getmembers(threading.Thread)
for (method, value) in parentmethods:
parents.append(method)
methods = []
allmethods = inspect.getmembers(self, predicate=inspect.ismethod)
for (method, value) in allmethods:
if method[0] == '_':
continue
if method[0] == 'p' and method[1].upper() == method[1]:
continue
if method in parents:
continue
if method == 'start2': method = 'start'
if method in self.helps:
method = method + ' ' + self.helps[method][2]
methods.append(method)
methods.sort()
return methods
def _loadconfig(self, arg = []):
# manage services
for service, value in self.services.iteritems():
if self.name not in app['services'][service].services:
app['services'][service].services = {}
app['services'][service].services[self.name] = value
# add command line args to the program options + build default configuration data
defaultConf = '[' + self.name + ']\n'
group = OptionGroup(app['parser'], self.name.capitalize() + " Options", self.desc)
for option in self.options:
value = self.options[option]
if len(value) == 3:
help = value[0] + " " + value[2] + ""
defaultConf += '; ' + value[0] + ' - choices: ' + value[2] + '\n'
else:
help = value[0]
defaultConf += '; ' + value[0] + '\n'
defaultConf += ';' + option + '=' + str(value[1]) + '\n\n'
if option != 'start':
if self.name == 'main':
group.add_option('--' + option, '--' + self.name + '.' + option, type='str', help=help, metavar=str(value[1]))
else:
group.add_option('--' + self.name + '.' + option, type='str', help=help, metavar=str(value[1]))
self.conf[option] = value[1]
app['parser'].add_option_group(group)
# create default config if none
userConfFile = app['path']['conf'] + self.nameconf
if not os.path.exists(userConfFile):
if not os.path.exists(app['path']['conf']): # TODO: Note that with Python 3.2+ we can compress this to os.makedirs(app['path']['conf'], exist_ok=True), see http://stackoverflow.com/a/5032238
os.makedirs(app['path']['conf'])
fp = open(userConfFile, 'w')
fp.write(defaultConf)
fp.close()
# read user config
fileconf = SafeConfigParser()
fileconf.read(userConfFile)
# set user config
for option in fileconf.options(self.name):
self.conf[option] = fileconf.get(self.name, option)
self.pLoadconfig()
def pLoadconfig(self, arg = []):
pass
# call a plugin method
def _rpc(self, method, *args, **kwargs): #, module = None):
#if module is not None and (type(module) == str or type(module) == unicode):
# self = __import__(module)
#elif module is not None:
# self = module
func = getattr(self, method)
if "api_user" not in kwargs:
api_user = "public"
else:
api_user = kwargs["api_user"]
log.debug(method, "public function?", hasattr(func, "rpc_public"))
if api_user != "admin" and not hasattr(func, "rpc_public"):
raise Exception('Method "' + method + '" not allowed')
return func(*args)