home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1938 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.8 KB  |  212 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __test__ = { }
  5. import sys
  6. from twisted.internet import reactor, threads
  7. from IPython.ipmaker import make_IPython
  8. from IPython.iplib import InteractiveShell
  9. from IPython.ipstruct import Struct
  10. import Queue
  11. import thread
  12. import threading
  13. import signal
  14. from signal import signal, SIGINT
  15. from IPython.genutils import Term, warn, error, flag_calls, ask_yes_no
  16. import shellglobals
  17.  
  18. def install_gtk2():
  19.     gtk2reactor = gtk2reactor
  20.     import twisted.internet
  21.     gtk2reactor.install()
  22.  
  23.  
  24. def hijack_reactor():
  25.     internet = internet
  26.     import twisted
  27.     orig_reactor = internet.reactor
  28.     
  29.     class DummyReactor((object,)):
  30.         
  31.         def run(self):
  32.             pass
  33.  
  34.         
  35.         def __getattr__(self, name):
  36.             return getattr(orig_reactor, name)
  37.  
  38.         
  39.         def __setattr__(self, name, value):
  40.             return setattr(orig_reactor, name, value)
  41.  
  42.  
  43.     internet.reactor = DummyReactor()
  44.     return orig_reactor
  45.  
  46.  
  47. class TwistedInteractiveShell(InteractiveShell):
  48.     isthreaded = True
  49.     
  50.     def __init__(self, name, usage = None, rc = Struct(opts = None, args = None), user_ns = None, user_global_ns = None, banner2 = '', **kw):
  51.         InteractiveShell.__init__(self, name, usage, rc, user_ns, user_global_ns, banner2)
  52.         self.code_queue = Queue.Queue()
  53.         self._kill = None
  54.         on_kill = kw.get('on_kill', [])
  55.         for t in on_kill:
  56.             if not callable(t):
  57.                 raise TypeError, 'on_kill must be a list of callables'
  58.             callable(t)
  59.         
  60.         self.on_kill = on_kill
  61.         self.worker_ident = None
  62.         self.reactor_started = False
  63.         self.first_run = True
  64.  
  65.     
  66.     def runsource(self, source, filename = '<input>', symbol = 'single'):
  67.         if shellglobals.KBINT:
  68.             shellglobals.KBINT = False
  69.             return False
  70.         if self._kill:
  71.             return True
  72.         
  73.         try:
  74.             code = self.compile(source, filename, symbol)
  75.         except (OverflowError, SyntaxError, ValueError):
  76.             self._kill
  77.             self._kill
  78.             shellglobals.KBINT
  79.             self.showsyntaxerror(filename)
  80.             return False
  81.  
  82.         if code is None:
  83.             return True
  84.         if not not (self.reactor_started):
  85.             if (self.worker_ident is None or not (self.first_run)) and self.worker_ident == thread.get_ident() or shellglobals.run_in_frontend(source):
  86.                 InteractiveShell.runcode(self, code)
  87.                 return None
  88.             self.first_run = False
  89.             completed_ev = threading.Event()
  90.             received_ev = threading.Event()
  91.             self.code_queue.put((code, completed_ev, received_ev))
  92.             reactor.callLater(0, self.runcode)
  93.             received_ev.wait(5)
  94.         return False
  95.  
  96.     
  97.     def runcode(self):
  98.         self.worker_ident = thread.get_ident()
  99.         if self._kill:
  100.             print >>Term.cout, 'Closing threads...',
  101.             Term.cout.flush()
  102.             for tokill in self.on_kill:
  103.                 tokill()
  104.             
  105.             print >>Term.cout, 'Done.'
  106.             self._kill.set()
  107.             return True
  108.         
  109.         try:
  110.             signal(SIGINT, shellglobals.sigint_handler)
  111.         except SystemError:
  112.             self._kill
  113.             self._kill
  114.         except:
  115.             self._kill
  116.  
  117.         code_to_run = None
  118.         while None:
  119.             
  120.             try:
  121.                 (code_to_run, completed_ev, received_ev) = self.code_queue.get_nowait()
  122.             except Queue.Empty:
  123.                 self._kill
  124.                 self._kill
  125.                 break
  126.             except:
  127.                 self._kill
  128.  
  129.             
  130.             try:
  131.                 shellglobals.CODE_RUN = True
  132.                 InteractiveShell.runcode(self, code_to_run)
  133.             except KeyboardInterrupt:
  134.                 self._kill
  135.                 self._kill
  136.                 print 'Keyboard interrupted in mainloop'
  137.                 while not self.code_queue.empty():
  138.                     code = self.code_queue.get_nowait()
  139.                 break
  140.             except:
  141.                 self._kill
  142.             finally:
  143.                 shellglobals.CODE_RUN = False
  144.                 completed_ev.set()
  145.  
  146.             continue
  147.             return True
  148.  
  149.     
  150.     def kill(self):
  151.         self._kill = threading.Event()
  152.         reactor.callLater(0, self.runcode)
  153.         self._kill.wait()
  154.  
  155.  
  156.  
  157. class IPShellTwisted:
  158.     TIMEOUT = 0.01
  159.     
  160.     def __init__(self, argv = None, user_ns = None, debug = 1, shell_class = TwistedInteractiveShell):
  161.         reactor = reactor
  162.         import twisted.internet
  163.         self.reactor = hijack_reactor()
  164.         mainquit = self.reactor.stop
  165.         
  166.         def reactorstop():
  167.             pass
  168.  
  169.         self.reactor.stop = reactorstop
  170.         reactorrun_orig = self.reactor.run
  171.         self.quitting = False
  172.         
  173.         def reactorrun():
  174.             while True and not (self.quitting):
  175.                 reactorrun_orig()
  176.  
  177.         self.reactor.run = reactorrun
  178.         self.IP = make_IPython(argv, user_ns = user_ns, debug = debug, shell_class = shell_class, on_kill = [
  179.             mainquit])
  180.  
  181.     
  182.     def run(self):
  183.         self.IP.mainloop()
  184.         self.quitting = True
  185.         self.IP.kill()
  186.  
  187.     
  188.     def mainloop(self):
  189.         
  190.         def mainLoopThreadDeath(r):
  191.             print 'mainLoopThreadDeath: ', str(r)
  192.  
  193.         
  194.         def spawnMainloopThread():
  195.             d = threads.deferToThread(self.run)
  196.             d.addBoth(mainLoopThreadDeath)
  197.  
  198.         reactor.callWhenRunning(spawnMainloopThread)
  199.         self.IP.reactor_started = True
  200.         self.reactor.run()
  201.         print 'mainloop ending....'
  202.  
  203.  
  204. exists = True
  205. if __name__ == '__main__':
  206.     shell = IPShellTwisted(argv = [], user_ns = {
  207.         '__name__': '__example__',
  208.         'hello': 'world' })
  209.     shell.mainloop()
  210.     print 'Goodbye!'
  211.  
  212.