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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import select
  5. import socket
  6. import sys
  7. import time
  8. import os
  9. from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EBADF, ECONNABORTED, errorcode
  10.  
  11. try:
  12.     socket_map
  13. except NameError:
  14.     socket_map = { }
  15.  
  16.  
  17. def _strerror(err):
  18.     res = os.strerror(err)
  19.     if res == 'Unknown error':
  20.         res = errorcode[err]
  21.     
  22.     return res
  23.  
  24.  
  25. class ExitNow(Exception):
  26.     pass
  27.  
  28. _reraised_exceptions = (ExitNow, KeyboardInterrupt, SystemExit)
  29.  
  30. def read(obj):
  31.     
  32.     try:
  33.         obj.handle_read_event()
  34.     except _reraised_exceptions:
  35.         raise 
  36.     except:
  37.         obj.handle_error()
  38.  
  39.  
  40.  
  41. def write(obj):
  42.     
  43.     try:
  44.         obj.handle_write_event()
  45.     except _reraised_exceptions:
  46.         raise 
  47.     except:
  48.         obj.handle_error()
  49.  
  50.  
  51.  
  52. def _exception(obj):
  53.     
  54.     try:
  55.         obj.handle_expt_event()
  56.     except _reraised_exceptions:
  57.         raise 
  58.     except:
  59.         obj.handle_error()
  60.  
  61.  
  62.  
  63. def readwrite(obj, flags):
  64.     
  65.     try:
  66.         if flags & select.POLLIN:
  67.             obj.handle_read_event()
  68.         
  69.         if flags & select.POLLOUT:
  70.             obj.handle_write_event()
  71.         
  72.         if flags & (select.POLLHUP | select.POLLERR | select.POLLNVAL):
  73.             obj.handle_close()
  74.         
  75.         if flags & select.POLLPRI:
  76.             obj.handle_expt_event()
  77.     except _reraised_exceptions:
  78.         raise 
  79.     except:
  80.         obj.handle_error()
  81.  
  82.  
  83.  
  84. def poll(timeout = 0, map = None):
  85.     if map is None:
  86.         map = socket_map
  87.     
  88.     if map:
  89.         r = []
  90.         w = []
  91.         e = []
  92.         for fd, obj in map.items():
  93.             is_r = obj.readable()
  94.             is_w = obj.writable()
  95.             if is_r:
  96.                 r.append(fd)
  97.             
  98.             if is_w:
  99.                 w.append(fd)
  100.             
  101.             if is_r or is_w:
  102.                 e.append(fd)
  103.                 continue
  104.         
  105.         if r == r and w == w:
  106.             pass
  107.         elif w == e:
  108.             time.sleep(timeout)
  109.             return None
  110.         
  111.         try:
  112.             (r, w, e) = select.select(r, w, e, timeout)
  113.         except select.error:
  114.             err = None
  115.             if err.args[0] != EINTR:
  116.                 raise 
  117.             err.args[0] != EINTR
  118.             return None
  119.  
  120.         for fd in r:
  121.             obj = map.get(fd)
  122.             if obj is None:
  123.                 continue
  124.             
  125.             read(obj)
  126.         
  127.         for fd in w:
  128.             obj = map.get(fd)
  129.             if obj is None:
  130.                 continue
  131.             
  132.             write(obj)
  133.         
  134.         for fd in e:
  135.             obj = map.get(fd)
  136.             if obj is None:
  137.                 continue
  138.             
  139.             _exception(obj)
  140.         
  141.     
  142.  
  143.  
  144. def poll2(timeout = 0, map = None):
  145.     if map is None:
  146.         map = socket_map
  147.     
  148.     if timeout is not None:
  149.         timeout = int(timeout * 1000)
  150.     
  151.     pollster = select.poll()
  152.     if map:
  153.         for fd, obj in map.items():
  154.             flags = 0
  155.             if obj.readable():
  156.                 flags |= select.POLLIN | select.POLLPRI
  157.             
  158.             if obj.writable():
  159.                 flags |= select.POLLOUT
  160.             
  161.             if flags:
  162.                 flags |= select.POLLERR | select.POLLHUP | select.POLLNVAL
  163.                 pollster.register(fd, flags)
  164.                 continue
  165.         
  166.         
  167.         try:
  168.             r = pollster.poll(timeout)
  169.         except select.error:
  170.             err = None
  171.             if err.args[0] != EINTR:
  172.                 raise 
  173.             err.args[0] != EINTR
  174.             r = []
  175.  
  176.         for fd, flags in r:
  177.             obj = map.get(fd)
  178.             if obj is None:
  179.                 continue
  180.             
  181.             readwrite(obj, flags)
  182.         
  183.     
  184.  
  185. poll3 = poll2
  186.  
  187. def loop(timeout = 30, use_poll = False, map = None, count = None):
  188.     if map is None:
  189.         map = socket_map
  190.     
  191.     if use_poll and hasattr(select, 'poll'):
  192.         poll_fun = poll2
  193.     else:
  194.         poll_fun = poll
  195.     if count is None:
  196.         while map:
  197.             poll_fun(timeout, map)
  198.     else:
  199.         while map and count > 0:
  200.             poll_fun(timeout, map)
  201.             count = count - 1
  202.  
  203.  
  204. class dispatcher:
  205.     debug = False
  206.     connected = False
  207.     accepting = False
  208.     closing = False
  209.     addr = None
  210.     ignore_log_types = frozenset([
  211.         'warning'])
  212.     
  213.     def __init__(self, sock = None, map = None):
  214.         if map is None:
  215.             self._map = socket_map
  216.         else:
  217.             self._map = map
  218.         self._fileno = None
  219.         if sock:
  220.             sock.setblocking(0)
  221.             self.set_socket(sock, map)
  222.             self.connected = True
  223.             
  224.             try:
  225.                 self.addr = sock.getpeername()
  226.             except socket.error:
  227.                 err = None
  228.                 if err.args[0] == ENOTCONN:
  229.                     self.connected = False
  230.                 else:
  231.                     self.del_channel(map)
  232.                     raise 
  233.                 err.args[0] == ENOTCONN
  234.             
  235.  
  236.         None<EXCEPTION MATCH>socket.error
  237.         self.socket = None
  238.  
  239.     
  240.     def __repr__(self):
  241.         status = [
  242.             self.__class__.__module__ + '.' + self.__class__.__name__]
  243.         if self.accepting and self.addr:
  244.             status.append('listening')
  245.         elif self.connected:
  246.             status.append('connected')
  247.         
  248.         if self.addr is not None:
  249.             
  250.             try:
  251.                 status.append('%s:%d' % self.addr)
  252.             except TypeError:
  253.                 status.append(repr(self.addr))
  254.             except:
  255.                 None<EXCEPTION MATCH>TypeError
  256.             
  257.  
  258.         None<EXCEPTION MATCH>TypeError
  259.         return '<%s at %#x>' % (' '.join(status), id(self))
  260.  
  261.     
  262.     def add_channel(self, map = None):
  263.         if map is None:
  264.             map = self._map
  265.         
  266.         map[self._fileno] = self
  267.  
  268.     
  269.     def del_channel(self, map = None):
  270.         fd = self._fileno
  271.         if map is None:
  272.             map = self._map
  273.         
  274.         if fd in map:
  275.             del map[fd]
  276.         
  277.         self._fileno = None
  278.  
  279.     
  280.     def create_socket(self, family, type):
  281.         self.family_and_type = (family, type)
  282.         sock = socket.socket(family, type)
  283.         sock.setblocking(0)
  284.         self.set_socket(sock)
  285.  
  286.     
  287.     def set_socket(self, sock, map = None):
  288.         self.socket = sock
  289.         self._fileno = sock.fileno()
  290.         self.add_channel(map)
  291.  
  292.     
  293.     def set_reuse_addr(self):
  294.         
  295.         try:
  296.             self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) | 1)
  297.         except socket.error:
  298.             pass
  299.  
  300.  
  301.     
  302.     def readable(self):
  303.         return True
  304.  
  305.     
  306.     def writable(self):
  307.         return True
  308.  
  309.     
  310.     def listen(self, num):
  311.         self.accepting = True
  312.         if os.name == 'nt' and num > 5:
  313.             num = 5
  314.         
  315.         return self.socket.listen(num)
  316.  
  317.     
  318.     def bind(self, addr):
  319.         self.addr = addr
  320.         return self.socket.bind(addr)
  321.  
  322.     
  323.     def connect(self, address):
  324.         self.connected = False
  325.         err = self.socket.connect_ex(address)
  326.         if err in (EINPROGRESS, EALREADY, EWOULDBLOCK):
  327.             return None
  328.         if err in (0, EISCONN):
  329.             self.addr = address
  330.             self.handle_connect_event()
  331.         else:
  332.             raise socket.error(err, errorcode[err])
  333.         return err in (EINPROGRESS, EALREADY, EWOULDBLOCK)
  334.  
  335.     
  336.     def accept(self):
  337.         
  338.         try:
  339.             (conn, addr) = self.socket.accept()
  340.             return (conn, addr)
  341.         except socket.error:
  342.             why = None
  343.             if why.args[0] == EWOULDBLOCK:
  344.                 pass
  345.             else:
  346.                 raise 
  347.             why.args[0] == EWOULDBLOCK
  348.  
  349.  
  350.     
  351.     def send(self, data):
  352.         
  353.         try:
  354.             result = self.socket.send(data)
  355.             return result
  356.         except socket.error:
  357.             why = None
  358.             if why.args[0] == EWOULDBLOCK:
  359.                 return 0
  360.             if why.args[0] in (ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED):
  361.                 self.handle_close()
  362.                 return 0
  363.             raise 
  364.         except:
  365.             why.args[0] in (ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED)
  366.  
  367.  
  368.     
  369.     def recv(self, buffer_size):
  370.         
  371.         try:
  372.             data = self.socket.recv(buffer_size)
  373.             if not data:
  374.                 self.handle_close()
  375.                 return ''
  376.             return data
  377.         except socket.error:
  378.             why = None
  379.             if why.args[0] in [
  380.                 ECONNRESET,
  381.                 ENOTCONN,
  382.                 ESHUTDOWN,
  383.                 ECONNABORTED]:
  384.                 self.handle_close()
  385.                 return ''
  386.             raise 
  387.         except:
  388.             why.args[0] in [
  389.                 ECONNRESET,
  390.                 ENOTCONN,
  391.                 ESHUTDOWN,
  392.                 ECONNABORTED]
  393.  
  394.  
  395.     
  396.     def close(self):
  397.         self.connected = False
  398.         self.accepting = False
  399.         self.del_channel()
  400.         
  401.         try:
  402.             self.socket.close()
  403.         except socket.error:
  404.             why = None
  405.             if why.args[0] not in (ENOTCONN, EBADF):
  406.                 raise 
  407.             why.args[0] not in (ENOTCONN, EBADF)
  408.  
  409.  
  410.     
  411.     def __getattr__(self, attr):
  412.         return getattr(self.socket, attr)
  413.  
  414.     
  415.     def log(self, message):
  416.         sys.stderr.write('log: %s\n' % str(message))
  417.  
  418.     
  419.     def log_info(self, message, type = 'info'):
  420.         if type not in self.ignore_log_types:
  421.             print '%s: %s' % (type, message)
  422.         
  423.  
  424.     
  425.     def handle_read_event(self):
  426.         if self.accepting:
  427.             self.handle_accept()
  428.         elif not self.connected:
  429.             self.handle_connect_event()
  430.             self.handle_read()
  431.         else:
  432.             self.handle_read()
  433.  
  434.     
  435.     def handle_connect_event(self):
  436.         self.connected = True
  437.         self.handle_connect()
  438.  
  439.     
  440.     def handle_write_event(self):
  441.         if self.accepting:
  442.             return None
  443.         if not self.connected:
  444.             err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
  445.             if err != 0:
  446.                 raise socket.error(err, _strerror(err))
  447.             err != 0
  448.             self.handle_connect_event()
  449.         
  450.         self.handle_write()
  451.  
  452.     
  453.     def handle_expt_event(self):
  454.         err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
  455.         if err != 0:
  456.             self.handle_close()
  457.         else:
  458.             self.handle_expt()
  459.  
  460.     
  461.     def handle_error(self):
  462.         (nil, t, v, tbinfo) = compact_traceback()
  463.         
  464.         try:
  465.             self_repr = repr(self)
  466.         except:
  467.             self_repr = '<__repr__(self) failed for object at %0x>' % id(self)
  468.  
  469.         self.log_info('uncaptured python exception, closing channel %s (%s:%s %s)' % (self_repr, t, v, tbinfo), 'error')
  470.         self.handle_close()
  471.  
  472.     
  473.     def handle_expt(self):
  474.         self.log_info('unhandled incoming priority event', 'warning')
  475.  
  476.     
  477.     def handle_read(self):
  478.         self.log_info('unhandled read event', 'warning')
  479.  
  480.     
  481.     def handle_write(self):
  482.         self.log_info('unhandled write event', 'warning')
  483.  
  484.     
  485.     def handle_connect(self):
  486.         self.log_info('unhandled connect event', 'warning')
  487.  
  488.     
  489.     def handle_accept(self):
  490.         self.log_info('unhandled accept event', 'warning')
  491.  
  492.     
  493.     def handle_close(self):
  494.         self.log_info('unhandled close event', 'warning')
  495.         self.close()
  496.  
  497.  
  498.  
  499. class dispatcher_with_send(dispatcher):
  500.     
  501.     def __init__(self, sock = None, map = None):
  502.         dispatcher.__init__(self, sock, map)
  503.         self.out_buffer = ''
  504.  
  505.     
  506.     def initiate_send(self):
  507.         num_sent = 0
  508.         num_sent = dispatcher.send(self, self.out_buffer[:512])
  509.         self.out_buffer = self.out_buffer[num_sent:]
  510.  
  511.     
  512.     def handle_write(self):
  513.         self.initiate_send()
  514.  
  515.     
  516.     def writable(self):
  517.         if not not (self.connected):
  518.             pass
  519.         return len(self.out_buffer)
  520.  
  521.     
  522.     def send(self, data):
  523.         if self.debug:
  524.             self.log_info('sending %s' % repr(data))
  525.         
  526.         self.out_buffer = self.out_buffer + data
  527.         self.initiate_send()
  528.  
  529.  
  530.  
  531. def compact_traceback():
  532.     (t, v, tb) = sys.exc_info()
  533.     tbinfo = []
  534.     if not tb:
  535.         raise AssertionError('traceback does not exist')
  536.     tb
  537.     while tb:
  538.         tbinfo.append((tb.tb_frame.f_code.co_filename, tb.tb_frame.f_code.co_name, str(tb.tb_lineno)))
  539.         tb = tb.tb_next
  540.     del tb
  541.     (file, function, line) = tbinfo[-1]
  542.     info = []([ '[%s|%s|%s]' % x for x in tbinfo ])
  543.     return ((file, function, line), t, v, info)
  544.  
  545.  
  546. def close_all(map = None, ignore_all = False):
  547.     if map is None:
  548.         map = socket_map
  549.     
  550.     for x in map.values():
  551.         
  552.         try:
  553.             x.close()
  554.         continue
  555.         except OSError:
  556.             x = None
  557.             if x.args[0] == EBADF:
  558.                 pass
  559.             elif not ignore_all:
  560.                 raise 
  561.             
  562.             x.args[0] == EBADF
  563.             except _reraised_exceptions:
  564.                 raise 
  565.                 continue
  566.                 if not ignore_all:
  567.                     raise 
  568.                 ignore_all
  569.                 continue
  570.             
  571.         return None
  572.  
  573.  
  574. if os.name == 'posix':
  575.     import fcntl
  576.     
  577.     class file_wrapper:
  578.         
  579.         def __init__(self, fd):
  580.             self.fd = os.dup(fd)
  581.  
  582.         
  583.         def recv(self, *args):
  584.             return os.read(self.fd, *args)
  585.  
  586.         
  587.         def send(self, *args):
  588.             return os.write(self.fd, *args)
  589.  
  590.         read = recv
  591.         write = send
  592.         
  593.         def close(self):
  594.             os.close(self.fd)
  595.  
  596.         
  597.         def fileno(self):
  598.             return self.fd
  599.  
  600.  
  601.     
  602.     class file_dispatcher(dispatcher):
  603.         
  604.         def __init__(self, fd, map = None):
  605.             dispatcher.__init__(self, None, map)
  606.             self.connected = True
  607.             
  608.             try:
  609.                 fd = fd.fileno()
  610.             except AttributeError:
  611.                 pass
  612.  
  613.             self.set_file(fd)
  614.             flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
  615.             flags = flags | os.O_NONBLOCK
  616.             fcntl.fcntl(fd, fcntl.F_SETFL, flags)
  617.  
  618.         
  619.         def set_file(self, fd):
  620.             self.socket = file_wrapper(fd)
  621.             self._fileno = self.socket.fileno()
  622.             self.add_channel()
  623.  
  624.  
  625.  
  626.