home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __test__ = { }
- import sys
- from twisted.internet import reactor, threads
- from IPython.ipmaker import make_IPython
- from IPython.iplib import InteractiveShell
- from IPython.ipstruct import Struct
- import Queue
- import thread
- import threading
- import signal
- from signal import signal, SIGINT
- from IPython.genutils import Term, warn, error, flag_calls, ask_yes_no
- import shellglobals
-
- def install_gtk2():
- gtk2reactor = gtk2reactor
- import twisted.internet
- gtk2reactor.install()
-
-
- def hijack_reactor():
- internet = internet
- import twisted
- orig_reactor = internet.reactor
-
- class DummyReactor((object,)):
-
- def run(self):
- pass
-
-
- def __getattr__(self, name):
- return getattr(orig_reactor, name)
-
-
- def __setattr__(self, name, value):
- return setattr(orig_reactor, name, value)
-
-
- internet.reactor = DummyReactor()
- return orig_reactor
-
-
- class TwistedInteractiveShell(InteractiveShell):
- isthreaded = True
-
- def __init__(self, name, usage = None, rc = Struct(opts = None, args = None), user_ns = None, user_global_ns = None, banner2 = '', **kw):
- InteractiveShell.__init__(self, name, usage, rc, user_ns, user_global_ns, banner2)
- self.code_queue = Queue.Queue()
- self._kill = None
- on_kill = kw.get('on_kill', [])
- for t in on_kill:
- if not callable(t):
- raise TypeError, 'on_kill must be a list of callables'
- callable(t)
-
- self.on_kill = on_kill
- self.worker_ident = None
- self.reactor_started = False
- self.first_run = True
-
-
- def runsource(self, source, filename = '<input>', symbol = 'single'):
- if shellglobals.KBINT:
- shellglobals.KBINT = False
- return False
- if self._kill:
- return True
-
- try:
- code = self.compile(source, filename, symbol)
- except (OverflowError, SyntaxError, ValueError):
- self._kill
- self._kill
- shellglobals.KBINT
- self.showsyntaxerror(filename)
- return False
-
- if code is None:
- return True
- if not not (self.reactor_started):
- if (self.worker_ident is None or not (self.first_run)) and self.worker_ident == thread.get_ident() or shellglobals.run_in_frontend(source):
- InteractiveShell.runcode(self, code)
- return None
- self.first_run = False
- completed_ev = threading.Event()
- received_ev = threading.Event()
- self.code_queue.put((code, completed_ev, received_ev))
- reactor.callLater(0, self.runcode)
- received_ev.wait(5)
- return False
-
-
- def runcode(self):
- self.worker_ident = thread.get_ident()
- if self._kill:
- print >>Term.cout, 'Closing threads...',
- Term.cout.flush()
- for tokill in self.on_kill:
- tokill()
-
- print >>Term.cout, 'Done.'
- self._kill.set()
- return True
-
- try:
- signal(SIGINT, shellglobals.sigint_handler)
- except SystemError:
- self._kill
- self._kill
- except:
- self._kill
-
- code_to_run = None
- while None:
-
- try:
- (code_to_run, completed_ev, received_ev) = self.code_queue.get_nowait()
- except Queue.Empty:
- self._kill
- self._kill
- break
- except:
- self._kill
-
-
- try:
- shellglobals.CODE_RUN = True
- InteractiveShell.runcode(self, code_to_run)
- except KeyboardInterrupt:
- self._kill
- self._kill
- print 'Keyboard interrupted in mainloop'
- while not self.code_queue.empty():
- code = self.code_queue.get_nowait()
- break
- except:
- self._kill
- finally:
- shellglobals.CODE_RUN = False
- completed_ev.set()
-
- continue
- return True
-
-
- def kill(self):
- self._kill = threading.Event()
- reactor.callLater(0, self.runcode)
- self._kill.wait()
-
-
-
- class IPShellTwisted:
- TIMEOUT = 0.01
-
- def __init__(self, argv = None, user_ns = None, debug = 1, shell_class = TwistedInteractiveShell):
- reactor = reactor
- import twisted.internet
- self.reactor = hijack_reactor()
- mainquit = self.reactor.stop
-
- def reactorstop():
- pass
-
- self.reactor.stop = reactorstop
- reactorrun_orig = self.reactor.run
- self.quitting = False
-
- def reactorrun():
- while True and not (self.quitting):
- reactorrun_orig()
-
- self.reactor.run = reactorrun
- self.IP = make_IPython(argv, user_ns = user_ns, debug = debug, shell_class = shell_class, on_kill = [
- mainquit])
-
-
- def run(self):
- self.IP.mainloop()
- self.quitting = True
- self.IP.kill()
-
-
- def mainloop(self):
-
- def mainLoopThreadDeath(r):
- print 'mainLoopThreadDeath: ', str(r)
-
-
- def spawnMainloopThread():
- d = threads.deferToThread(self.run)
- d.addBoth(mainLoopThreadDeath)
-
- reactor.callWhenRunning(spawnMainloopThread)
- self.IP.reactor_started = True
- self.reactor.run()
- print 'mainloop ending....'
-
-
- exists = True
- if __name__ == '__main__':
- shell = IPShellTwisted(argv = [], user_ns = {
- '__name__': '__example__',
- 'hello': 'world' })
- shell.mainloop()
- print 'Goodbye!'
-
-