-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpySimpleConsole.py
97 lines (72 loc) · 2.33 KB
/
pySimpleConsole.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
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
96
97
#! /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 <[email protected]>"
__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):
# newer versions of twisted will send subsequent message(s) of class Failure
if not isinstance(message, bytes):
return
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()