File: OoliteDebugCLIProtocol.py

package info (click to toggle)
oolite 1.77.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,264 kB
  • ctags: 5,362
  • sloc: objc: 132,090; ansic: 10,457; python: 2,225; sh: 1,325; makefile: 332; perl: 259; xml: 125; php: 5
file content (51 lines) | stat: -rw-r--r-- 1,224 bytes parent folder | download
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
#
#  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


class OoliteDebugCLIProtocol(basic.LineReceiver):
	delimiter = "\n"
	inputReceiver = None
	
	
	def connectionMade(self):
		pass
	
	
	def lineReceived(self, line):
		if not line:  return
		
		try:
			if line[0] == "/":  self.__internalCommand(line)
			elif self.inputReceiver:
				self.inputReceiver.receiveUserInput(line)
			else:
				print "No client connected."
		except:
			print >> sys.stderr, "Exception in input handler."
	
	
	def __internalCommand(self, line):
		parts = line[1:].split()
		command = parts[0]
		args = parts[1:]
		argMsg = str.join(" ", args)
		
		# print '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:  print "No client connected."
		else:
			print >> sys.stderr, "Unknown console command: " + line