File: pySimpleConsole.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 (95 lines) | stat: -rwxr-xr-x 2,263 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /usr/bin/python
#
#  pySimpleConsole.py
#  pythonDebugConsole
#
#  Created by Jens Ayton on 2007-11-29.
#  Copyright (c) 2007 Jens Ayton. All rights reserved.
#


"""
A simple implementation of the Oolite JavaScript debug console interface.
"""

__author__	= "Jens Ayton <jens@ayton.se>"
__version__	= "1.0"


from ooliteConsoleServer import *
from twisted.internet.protocol import Factory
from twisted.internet import stdio, reactor
from OoliteDebugCLIProtocol import OoliteDebugCLIProtocol


cliHandler = None


class SimpleConsoleDelegate:
	__active = False
	
	def __init__(self, protocol):
		self.protocol = protocol
		self.identityString = "pySimpleConsole"
	
	def __del__(self):
		if self.__active: self.protocol.factory.activeCount -= 1
		if cliHandler.inputReceiver is self:  cliHandler.inputReceiver = None
	
	def acceptConnection(self):
		return self.protocol.factory.activeCount < 1
	
	def connectionOpened(self, ooliteVersionString):
		print "Opened connection to Oolite version", ooliteVersionString
		self.protocol.factory.activeCount += 1
		self.__active = True
		cliHandler.inputReceiver = self
	
	def connectionClosed(self, message):
		if message != None and len(message) > 0:
			print "Connection closed with message:", message
		else:
			print "Connection closed with no message."
		print ""
		if self.__active:
			self.protocol.factory.activeCount -= 1
			self.__active = False
	
	def writeToConsole(self, message, colorKey, emphasisRanges):
		print "  " + message
	
	def clearConsole(self):
		pass
		
	def showConsole(self):
		pass
	
	def receiveUserInput(self, string):
		self.protocol.sendCommand(string)
	
	def closeConnection(self, message):
		self.protocol.closeConnection(message)


def getInputReceiver():
	return currentInputReceiver


# Set up console server protocol
factory = Factory()
factory.delegateClass = SimpleConsoleDelegate
factory.activeCount = 0
factory.protocol = OoliteDebugConsoleProtocol

# Set up command line I/O protocol
cliHandler = OoliteDebugCLIProtocol()
cliHandler.getInputReceiver = getInputReceiver
stdio.StandardIO(cliHandler)

print "Python Oolite debug console"
print "Type /quit to quit."
print "Waiting for connection..."
print ""

reactor.listenTCP(defaultOoliteConsolePort, factory)
reactor.run()