-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginInterface.py
180 lines (149 loc) · 4.28 KB
/
PluginInterface.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
import imp,threading,os,cPickle
def loadPlugin(fileName, pipes, args):
pluginPath,name = os.path.split(fileName)
className = os.path.splitext(name)[0]
os.chdir(pluginPath)
pluginModule = __import__(className)
pluginClass = getattr(pluginModule, className)
if pipes[0].recv():
pluginInstance = pluginClass(pipes, args)
pluginInstance.run()
class PluginInterface(object):
def __init__(self, pipes):
self.inPipe = pipes[0]
self.outPipe = pipes[1]
self.queue = []
self.hasWork = threading.Condition()
self.workerThread = threading.Thread(target=self.__work)
self.__running = False
self.__questioner = None
def initialize(self, args):
pass
@staticmethod
def getDependencies(args):
return []
@staticmethod
def getFunctions(args):
return []
def run(self):
self.__running = True
self.workerThread.deamon = True
self.workerThread.start()
while self.__running:
try:
task = self.inPipe.recv()
self.hasWork.acquire()
self.queue.append(task)
self.hasWork.notify()
self.hasWork.release()
except KeyboardInterrupt:
self.__stop()
except Exception as e:
print(self, e)
def shutDown(self):
pass
def __stop(self):
self.__running = False
self.outPipe.send(Stop())
self.shutDown()
def __outPipeSend(self, content):
self.outPipe.send(content)
def signalEvent(self, eventName, *args):
self.__outPipeSend(Event(eventName, args))
def callMethod(self, name, *args):
self.__outPipeSend(Method(name, args))
def callFunction(self, name, *args):
#Clear any previous answers if there are some
while self.outPipe.poll():
self.outPipe.recv()
self.__outPipeSend(Function(name, args))
result = self.outPipe.recv()
return result.getValue()
def log(self, *args):
string = ''
for i in args:
string += ' ' + str(i)
self.callMethod(('Logger', 'log'), self.__class__.__name__ + ': ' + string)
def questioner(self):
return self.__questioner
def _getTarget(self, name):
"""
\brief Get the target of the call
\return The target function
"""
return getattr(self, name)
def __work(self):
while self.__running:
try:
self.hasWork.acquire()
while len(self.queue) < 1:
self.hasWork.wait()
job = self.queue[0]
del self.queue[0]
self.hasWork.release()
if isinstance(job, Stop):
self.__running = False
elif isinstance(job, Method):
try:
method = self._getTarget(job.name)
except AttributeError:
print('Unknown method ' + self.__class__.__name__ + '.'
+ job.name + str(job.getArgs()))
self.__questioner = str(job.questioner)
try:
method(*job.getArgs())
except TypeError:
print('Used wrong arguments in method call '
+ self.__class__.__name__ + '.' + str(job.name) + str(job.getArgs())
+ ' from ' + str(job.questioner))
raise
except:
print('Exception in method call '
+ self.__class__.__name__ + '.' + str(job.name) + str(job.getArgs())
+ ' from ' + str(job.questioner))
raise
elif isinstance(job, Event):
pass
elif isinstance(job, Function):
try:
function = self._getTarget(job.name)
except AttributeError:
print('Unknown function ' + self.__class__.__name__ + '.'
+ str(job.name) + str(job.getArgs()))
self.__questioner = job.questioner
try:
value = function(*job.getArgs())
except TypeError:
print('Used wrong arguments in function call '
+ self.__class__.__name__ + '.' + str(job.name) + str(job.getArgs())
+ ' from ' + job.questioner)
raise
except:
print('Exception in function call '
+ self.__class__.__name__ + '.' + str(job.name) + str(job.getArgs())
+ ' from ' + job.questioner)
raise
self.inPipe.send(Result(job, value))
except KeyboardInterrupt:
self.__stop()
class Function:
def __init__(self, name, args, questioner = None):
self.name = name
self.args = cPickle.dumps(args)
self.questioner = questioner
def getArgs(self):
return cPickle.loads(self.args)
class Method(Function):
pass
class Event(Function):
pass
class Result:
def __init__(self, function, result):
self.function = function
self.value = cPickle.dumps(result)
def getValue(self):
return cPickle.loads(self.value)
class Stop:
pass
class Error(Exception):
pass