Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stdio.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- test-case-name: twisted.conch.test.test_manhole -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Asynchronous local terminal input handling
  6. @author: Jp Calderone
  7. """
  8. import os
  9. import sys
  10. import termios
  11. import tty
  12. from twisted.conch.insults.insults import ServerProtocol
  13. from twisted.conch.manhole import ColoredManhole
  14. from twisted.internet import defer, protocol, reactor, stdio
  15. from twisted.python import failure, log, reflect
  16. class UnexpectedOutputError(Exception):
  17. pass
  18. class TerminalProcessProtocol(protocol.ProcessProtocol):
  19. def __init__(self, proto):
  20. self.proto = proto
  21. self.onConnection = defer.Deferred()
  22. def connectionMade(self):
  23. self.proto.makeConnection(self)
  24. self.onConnection.callback(None)
  25. self.onConnection = None
  26. def write(self, data):
  27. """
  28. Write to the terminal.
  29. @param data: Data to write.
  30. @type data: L{bytes}
  31. """
  32. self.transport.write(data)
  33. def outReceived(self, data):
  34. """
  35. Receive data from the terminal.
  36. @param data: Data received.
  37. @type data: L{bytes}
  38. """
  39. self.proto.dataReceived(data)
  40. def errReceived(self, data):
  41. """
  42. Report an error.
  43. @param data: Data to include in L{Failure}.
  44. @type data: L{bytes}
  45. """
  46. self.transport.loseConnection()
  47. if self.proto is not None:
  48. self.proto.connectionLost(failure.Failure(UnexpectedOutputError(data)))
  49. self.proto = None
  50. def childConnectionLost(self, childFD):
  51. if self.proto is not None:
  52. self.proto.childConnectionLost(childFD)
  53. def processEnded(self, reason):
  54. if self.proto is not None:
  55. self.proto.connectionLost(reason)
  56. self.proto = None
  57. class ConsoleManhole(ColoredManhole):
  58. """
  59. A manhole protocol specifically for use with L{stdio.StandardIO}.
  60. """
  61. def connectionLost(self, reason):
  62. """
  63. When the connection is lost, there is nothing more to do. Stop the
  64. reactor so that the process can exit.
  65. """
  66. reactor.stop()
  67. def runWithProtocol(klass):
  68. fd = sys.__stdin__.fileno()
  69. oldSettings = termios.tcgetattr(fd)
  70. tty.setraw(fd)
  71. try:
  72. stdio.StandardIO(ServerProtocol(klass))
  73. reactor.run()
  74. finally:
  75. termios.tcsetattr(fd, termios.TCSANOW, oldSettings)
  76. os.write(fd, b"\r\x1bc\r")
  77. def main(argv=None):
  78. log.startLogging(open("child.log", "w"))
  79. if argv is None:
  80. argv = sys.argv[1:]
  81. if argv:
  82. klass = reflect.namedClass(argv[0])
  83. else:
  84. klass = ConsoleManhole
  85. runWithProtocol(klass)
  86. if __name__ == "__main__":
  87. main()