-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·62 lines (52 loc) · 1.67 KB
/
console.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cmd
import sys
from multiprocessing import Process
from os.path import abspath, dirname
sys.path.append(abspath(dirname(abspath(__file__)) + '../../../'))
from core.brain.main import Brain, worker
from core.config.settings import PEOPLE
import zmq
class Shell(cmd.Cmd):
"""Simple command processor example."""
prompt = 'smarty-bot > '
def do_prompt(self, line):
"Change the interactive prompt"
self.prompt = line + '> '
def default(self, req):
"""docstring for default"""
context = zmq.Context()
request = {
'request': req,
'from': 'jabber',
'cmd_path': req.split(),
'cmd_args': req,
'sender': PEOPLE['admin']['email'],
'uuid': ''
}
b = Brain()
response = b.react_on(request)
##check workers activity
w = response.get('worker', None)
if w:
w['addr'] = 'ipc:///tmp/smarty-brain-worker-'
p = Process(target=worker, kwargs=w)
p.start()
w['addr'] = 'ipc:///tmp/smarty-brain-worker-%d' % p.pid
ws = context.socket(zmq.REQ)
ws.connect(w['addr'])
ws.send_json({'cmd': 'run'})
response = ws.recv_json()
if response:
print(response['text'])
#cont = response.get('continue', '')
ws.send_json({'cmd': 'terminate'})
ws.close()
p.terminate()
else:
print(response['text'])
def do_EOF(self, line):
return True
if __name__ == '__main__':
Shell().cmdloop()