home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1489 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  5.9 KB  |  197 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 sys
  8. import cPickle
  9. import os
  10. from code import InteractiveInterpreter
  11. from Queue import Queue, Empty
  12. from threading import Thread
  13. from binascii import unhexlify
  14. from multiprocessing.connection import Client
  15. from repr import repr as safe_repr
  16. from calibre.utils.pyconsole import preferred_encoding, isbytestring, POLL_TIMEOUT
  17.  
  18. def tounicode(raw):
  19.     if isbytestring(raw):
  20.         
  21.         try:
  22.             raw = raw.decode(preferred_encoding, 'replace')
  23.         except:
  24.             raw = safe_repr(raw)
  25.  
  26.         if isbytestring(raw):
  27.             
  28.             try:
  29.                 raw.decode('utf-8', 'replace')
  30.             raw = u'Undecodable bytestring'
  31.  
  32.         
  33.         return raw
  34.  
  35.  
  36. class DummyFile(object):
  37.     
  38.     def __init__(self, what, out_queue):
  39.         self.closed = False
  40.         self.name = 'console'
  41.         self.softspace = 0
  42.         self.what = what
  43.         self.out_queue = out_queue
  44.  
  45.     
  46.     def flush(self):
  47.         pass
  48.  
  49.     
  50.     def close(self):
  51.         pass
  52.  
  53.     
  54.     def write(self, raw):
  55.         self.out_queue.put((self.what, tounicode(raw)))
  56.  
  57.  
  58.  
  59. class Comm(Thread):
  60.     
  61.     def __init__(self, conn, out_queue, in_queue):
  62.         Thread.__init__(self)
  63.         self.daemon = True
  64.         self.conn = conn
  65.         self.out_queue = out_queue
  66.         self.in_queue = in_queue
  67.         self.keep_going = True
  68.  
  69.     
  70.     def run(self):
  71.         while self.keep_going:
  72.             
  73.             try:
  74.                 self.communicate()
  75.             continue
  76.             except KeyboardInterrupt:
  77.                 continue
  78.                 except EOFError:
  79.                     continue
  80.                 
  81.                 None<EXCEPTION MATCH>EOFError
  82.             return None
  83.  
  84.  
  85.     
  86.     def communicate(self):
  87.         if self.conn.poll(POLL_TIMEOUT):
  88.             
  89.             try:
  90.                 obj = self.conn.recv()
  91.             except:
  92.                 pass
  93.  
  94.             self.in_queue.put(obj)
  95.         
  96.         
  97.         try:
  98.             obj = self.out_queue.get_nowait()
  99.         except Empty:
  100.             pass
  101.  
  102.         
  103.         try:
  104.             self.conn.send(obj)
  105.         except:
  106.             raise EOFError('interpreter failed to send')
  107.  
  108.  
  109.  
  110.  
  111. class Interpreter(InteractiveInterpreter):
  112.     
  113.     def __init__(self, queue, local = { }):
  114.         if '__name__' not in local:
  115.             local['__name__'] = '__console__'
  116.         
  117.         if '__doc__' not in local:
  118.             local['__doc__'] = None
  119.         
  120.         self.out_queue = queue
  121.         sys.stdout = DummyFile('stdout', queue)
  122.         sys.stderr = DummyFile('sdterr', queue)
  123.         InteractiveInterpreter.__init__(self, locals = local)
  124.  
  125.     
  126.     def showtraceback(self, *args, **kwargs):
  127.         self.is_syntax_error = False
  128.         InteractiveInterpreter.showtraceback(self, *args, **kwargs)
  129.  
  130.     
  131.     def showsyntaxerror(self, *args, **kwargs):
  132.         self.is_syntax_error = True
  133.         InteractiveInterpreter.showsyntaxerror(self, *args, **kwargs)
  134.  
  135.     
  136.     def write(self, raw):
  137.         what = None if self.is_syntax_error else 'traceback'
  138.         self.out_queue.put((what, tounicode(raw)))
  139.  
  140.  
  141.  
  142. def connect():
  143.     os.chdir(os.environ['ORIGWD'])
  144.     address = cPickle.loads(unhexlify(os.environ['CALIBRE_WORKER_ADDRESS']))
  145.     key = unhexlify(os.environ['CALIBRE_WORKER_KEY'])
  146.     return Client(address, authkey = key)
  147.  
  148.  
  149. def main():
  150.     out_queue = Queue()
  151.     in_queue = Queue()
  152.     conn = connect()
  153.     comm = Comm(conn, out_queue, in_queue)
  154.     comm.start()
  155.     interpreter = Interpreter(out_queue)
  156.     ret = 0
  157.     while True:
  158.         
  159.         try:
  160.             
  161.             try:
  162.                 (cmd, data) = in_queue.get(1)
  163.             except Empty:
  164.                 pass
  165.  
  166.             if cmd == 'quit':
  167.                 ret = data
  168.                 comm.keep_going = False
  169.                 comm.join()
  170.                 break
  171.             elif cmd == 'run':
  172.                 if not comm.is_alive():
  173.                     ret = 1
  174.                     break
  175.                 
  176.                 ret = False
  177.                 
  178.                 try:
  179.                     ret = interpreter.runsource(data)
  180.                 except KeyboardInterrupt:
  181.                     pass
  182.                 except SystemExit:
  183.                     out_queue.put(('stderr', 'SystemExit ignored\n'))
  184.  
  185.                 out_queue.put(('done', ret))
  186.         continue
  187.         except KeyboardInterrupt:
  188.             continue
  189.         
  190.  
  191.         None<EXCEPTION MATCH>KeyboardInterrupt
  192.     return ret
  193.  
  194. if __name__ == '__main__':
  195.     main()
  196.  
  197.