-
Notifications
You must be signed in to change notification settings - Fork 2
/
OoliteDebugCLIProtocol.py
61 lines (50 loc) · 1.53 KB
/
OoliteDebugCLIProtocol.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
#
# OoliteDebugCLIProtocol.py
# pythonDebugConsole
#
# Created by Jens Ayton on 2007-12-29.
# Copyright (c) 2007 Jens Ayton. All rights reserved.
#
from twisted.internet import stdio, reactor
from twisted.protocols import basic
# import sys
from sys import version_info as version_info
from sys import stderr as stderr
import logging
cmdLogger = logging.getLogger('DebugConsole.CLIProtocol')
class OoliteDebugCLIProtocol(basic.LineReceiver):
delimiter = "\n" if version_info[0] == 2 else b"\n"
inputReceiver = None
def connectionMade(self):
pass
def lineReceived(self, bsline):
if not bsline: return
try:
if isinstance(bsline, bytes):
line = bsline.decode('ascii')
else:
line = bsline
if line[0] == "/":
self.__internalCommand(line)
elif self.inputReceiver:
self.inputReceiver.receiveUserInput(bsline)
else:
cmdLogger.warning("No client connected.")
except:
cmdLogger.exception("Exception in input handler.")
def __internalCommand(self, line):
parts = line[1:].split()
command = parts[0]
args = parts[1:]
argMsg = str.join(" ", args)
# cmdLogger.debug('Internal command "' + command + '" with arguments "' + argMsg + '".')
if command == "quit":
reactor.stop()
elif command == "close":
# Note: I don't recommend using the /close command, as it crashes Oolite.
if self.inputReceiver:
self.inputReceiver.closeConnection(argMsg)
else:
cmdLogger.warning("No client connected.")
else:
cmdLogger.error("Unknown console command: " + line, file=stderr)