forked from maria-korosteleva/mpotrDevelopment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inp.py
48 lines (43 loc) · 1.58 KB
/
inp.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
import glib, sys, os, fcntl
class Input(object):
def __init__(self, displaier, sendMess, sendShutdown):
self.buffer = ''
self.line_callback = self.commandSwitch
self.sendMess = sendMess
self.displaier = displaier
self.sendShutdown = sendShutdown
flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
flags |= os.O_NONBLOCK
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags)
glib.io_add_watch(sys.stdin, glib.IO_IN, self.io_callback)
def io_callback(self, fd, condition):
chunk = fd.read()
for char in chunk:
if char == '\n':
self.line_callback(self.buffer)
self.buffer = ''
else:
self.buffer += char
return True
#Process chat commands
def processCommand(self, command):
self.displaier.redisplay()
self.displaier.log('command: ' + command)
global loop
c = command[1:]
if (c == "exit"):
self.sendShutdown(0)
elif (c == "restart"):
self.sendShutdown(1)
elif (c == "cat"):
self.displaier.system("\n /\\___/\\\n( o o )\n( =^= )\n( )\n( )\n( )))))))))))))))\n")
else:
self.displaier.system("Unknown command: " + c)
#Process command or send message
def commandSwitch(self, message):
global context, account, chat, purple
if (message[0] == '!'):
self.processCommand(message)
else:
self.sendMess(message)
self.displaier.redisplay()