home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1486 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  4.8 KB  |  140 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. import cPickle
  9. import signal
  10. import time
  11. from Queue import Queue, Empty
  12. from multiprocessing.connection import Listener, arbitrary_address
  13. from binascii import hexlify
  14. from PyQt4.Qt import QThread, pyqtSignal
  15. from calibre.utils.pyconsole import Process, iswindows, POLL_TIMEOUT
  16.  
  17. class Controller(QThread):
  18.     show_error = pyqtSignal(object, object, object)
  19.     write_output = pyqtSignal(object, object, object)
  20.     interpreter_done = pyqtSignal(object, object)
  21.     interpreter_died = pyqtSignal(object, object)
  22.     
  23.     def __init__(self, parent):
  24.         QThread.__init__(self, parent)
  25.         self.keep_going = True
  26.         self.current_command = None
  27.         self.out_queue = Queue()
  28.         self.address = None(arbitrary_address if iswindows else 'AF_UNIX')
  29.         self.auth_key = os.urandom(32)
  30.         if iswindows and self.address[1] == ':':
  31.             self.address = self.address[2:]
  32.         
  33.         self.listener = Listener(address = self.address, authkey = self.auth_key, backlog = 4)
  34.         self.env = {
  35.             'CALIBRE_LAUNCH_INTERPRETER': '1',
  36.             'CALIBRE_WORKER_ADDRESS': hexlify(cPickle.dumps(self.listener.address, -1)),
  37.             'CALIBRE_WORKER_KEY': hexlify(self.auth_key) }
  38.         self.process = Process(self.env)
  39.         self.output_file_buf = self.process(redirect_output = False)
  40.         self.conn = self.listener.accept()
  41.         self.start()
  42.  
  43.     
  44.     def run(self):
  45.         while self.keep_going and self.is_alive:
  46.             
  47.             try:
  48.                 self.communicate()
  49.             continue
  50.             except KeyboardInterrupt:
  51.                 continue
  52.                 except EOFError:
  53.                     break
  54.                     continue
  55.                 
  56.                 None<EXCEPTION MATCH>EOFError
  57.             try:
  58.                 self.listener.close()
  59.             except:
  60.                 None<EXCEPTION MATCH>KeyboardInterrupt
  61.  
  62.             return None
  63.  
  64.     
  65.     def communicate(self):
  66.         if self.conn.poll(POLL_TIMEOUT):
  67.             self.dispatch_incoming_message(self.conn.recv())
  68.         
  69.         
  70.         try:
  71.             obj = self.out_queue.get_nowait()
  72.         except Empty:
  73.             pass
  74.  
  75.         
  76.         try:
  77.             self.conn.send(obj)
  78.         except:
  79.             raise EOFError('controller failed to send')
  80.  
  81.  
  82.     
  83.     def dispatch_incoming_message(self, obj):
  84.         
  85.         try:
  86.             (cmd, data) = obj
  87.         except:
  88.             print 'Controller received invalid message'
  89.             print repr(obj)
  90.             return None
  91.  
  92.         if cmd in ('stdout', 'stderr'):
  93.             self.write_output.emit(data, cmd, self)
  94.         elif cmd == 'syntaxerror':
  95.             self.show_error.emit(True, data, self)
  96.         elif cmd == 'traceback':
  97.             self.show_error.emit(False, data, self)
  98.         elif cmd == 'done':
  99.             self.current_command = None
  100.             self.interpreter_done.emit(self, data)
  101.         
  102.  
  103.     
  104.     def runsource(self, cmd):
  105.         self.current_command = cmd
  106.         self.out_queue.put(('run', cmd))
  107.  
  108.     
  109.     def __nonzero__(self):
  110.         return self.process.is_alive
  111.  
  112.     
  113.     def returncode(self):
  114.         return self.process.returncode
  115.  
  116.     returncode = property(returncode)
  117.     
  118.     def interrupt(self):
  119.         if hasattr(signal, 'SIGINT'):
  120.             os.kill(self.process.pid, signal.SIGINT)
  121.         elif hasattr(signal, 'CTRL_C_EVENT'):
  122.             os.kill(self.process.pid, signal.CTRL_C_EVENT)
  123.         
  124.  
  125.     
  126.     def is_alive(self):
  127.         return self.process.is_alive
  128.  
  129.     is_alive = property(is_alive)
  130.     
  131.     def kill(self):
  132.         self.out_queue.put(('quit', 0))
  133.         t = 0
  134.         while self.is_alive and t < 10:
  135.             time.sleep(0.1)
  136.         self.process.kill()
  137.         self.keep_going = False
  138.  
  139.  
  140.