Created
October 16, 2012 22:34
-
-
Save sporsh/da0c2b165eb6f827f848 to your computer and use it in GitHub Desktop.
twisted interactive shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Thread, Event | |
class Backend(object): | |
reactor_started = Event() | |
def start(self, timeout=None): | |
print "Starting backend..." | |
self._thread = Thread(target=self._start_reactor) | |
self._thread.start() | |
self.reactor_started.wait(timeout) | |
self.started() | |
def stop(self, reason='COOL', timeout=None): | |
print "Stopping backend (%s)..." % reason | |
self._stop_reactor() | |
def _start_reactor(self): | |
try: | |
print "Starting reactor..." | |
from twisted.internet import reactor | |
def reactor_started(): | |
print "Reactor started." | |
self.reactor_started.set() | |
reactor.callWhenRunning(reactor_started) | |
reactor.run(installSignalHandlers=False) | |
except Exception as e: | |
self._reactor_stopped(e) | |
finally: | |
self._reactor_stopped() | |
def started(self): | |
print "Backend started." | |
def _stop_reactor(self): | |
try: | |
print "Stopping reactor..." | |
from twisted.internet import reactor | |
reactor.callFromThread(reactor.stop) | |
except Exception as e: | |
self._reactor_stopped(e) | |
def _reactor_stopped(self, reason='Stopped cleanly'): | |
print "Reactor stopped (%s)." % reason |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Frontend(object): | |
def __init__(self, console=None): | |
console = console or 'default' | |
self.shell = self._get_shell(console) | |
def run(self, backend, auto_stop_backend=True): | |
try: | |
print "Starting interactive console..." | |
self.shell(banner="HELLO?", locals_={'backend': backend}) | |
print "interactive console closed." | |
except Exception as e: | |
print "interactive console crashed:", e | |
finally: | |
if auto_stop_backend: | |
backend.stop('Frontend closed') | |
def _get_shell(self, name): | |
return getattr(self, '_run_shell_%s' % name.upper()) | |
def _run_shell_DEFAULT(self, banner, locals_): | |
import code | |
return code.interact(banner=banner, local=locals_) | |
def _run_shell_BPYTHON(self, banner, locals_): | |
import bpython | |
return bpython.embed(locals_=locals_, banner=banner) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from backend import Backend | |
from frontend import Frontend | |
if __name__ == '__main__': | |
backend = Backend() | |
backend.start() | |
console = None | |
try: | |
import sys | |
console = sys.argv[1] | |
except: | |
pass | |
frontend = Frontend(console) | |
frontend.run(backend) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment