-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.py
71 lines (58 loc) · 1.6 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
from multiprocessing import Process, Pipe
from threading import Thread
# Plugin process side main entry point.
def launch(pluginName, pipe):
import api
module = __import__("plugins." + pluginName)
api.register("sendline", pipe.send)
while True:
line = pipe.recv()
print("Remote read:" + line)
api.emitEvent("line", line)
# Main process side plugin wrapper
class PluginRunner:
def __init__(self, plugin):
self.name = plugin
self.proc = None
self.running = False
self.local_pipe, self.remote_pipe = Pipe()
def getConnection(self):
return self.local_pipe
def start(self):
assert not self.running, "Already running."
self.running = True
self.thread = Thread(target=self.run)
self.thread.start()
def restart(self):
self.proc.terminate()
def stop(self):
assert self.running, "Running"
self.running = False
self.proc.terminate()
self.thread.join()
self.remote_pipe.close()
self.local_pipe.close()
def run(self):
while self.running:
self.proc = Process(target=launch, args=('repeat', self.remote_pipe))
self.proc.start()
print("Waiting on proc to end")
self.proc.join()
# Main process example.
if __name__=="__main__":
plugin = PluginRunner("repeat")
pipe = plugin.getConnection()
def reader():
try:
while True:
print("Main read:" + pipe.recv())
except:
print("Main reader ended.")
Thread(target = reader).start()
plugin.start()
import time
time.sleep(1)
plugin.restart()
pipe.send(":Nick!user@host PRIVMSG #chan :!repeat test")
time.sleep(.1)
plugin.stop()