<s> import time <EOL> import heapq <EOL> import select <EOL> import fcntl <EOL> import os <EOL> from functools import wraps <EOL> from weakref import WeakKeyDictionary <EOL> from urwid . util import is_mouse_event <EOL> from urwid . compat import PYTHON3 <EOL> from urwid . command_map import command_map , REDRAW_SCREEN <EOL> from urwid . wimp import PopUpTarget <EOL> from urwid import signals <EOL> from urwid . display_common import INPUT_DESCRIPTORS_CHANGED <EOL> PIPE_BUFFER_READ_SIZE = <NUM_LIT> <EOL> class ExitMainLoop ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class MainLoop ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , widget , palette = ( ) , screen = None , <EOL> handle_mouse = True , input_filter = None , unhandled_input = None , <EOL> event_loop = None , pop_ups = False ) : <EOL> self . _widget = widget <EOL> self . handle_mouse = handle_mouse <EOL> self . pop_ups = pop_ups <EOL> if not screen : <EOL> from urwid import raw_display <EOL> screen = raw_display . Screen ( ) <EOL> if palette : <EOL> screen . register_palette ( palette ) <EOL> self . screen = screen <EOL> self . screen_size = None <EOL> self . _unhandled_input = unhandled_input <EOL> self . _input_filter = input_filter <EOL> if not hasattr ( screen , '<STR_LIT>' <EOL> ) and event_loop is not None : <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( screen , ) ) <EOL> if event_loop is None : <EOL> event_loop = SelectEventLoop ( ) <EOL> self . event_loop = event_loop <EOL> self . _input_timeout = None <EOL> self . _watch_pipes = { } <EOL> def _set_widget ( self , widget ) : <EOL> self . _widget = widget <EOL> if self . pop_ups : <EOL> self . _topmost_widget . original_widget = self . _widget <EOL> else : <EOL> self . _topmost_widget = self . _widget <EOL> widget = property ( lambda self : self . _widget , _set_widget , doc = <EOL> """<STR_LIT>""" ) <EOL> def _set_pop_ups ( self , pop_ups ) : <EOL> self . _pop_ups = pop_ups <EOL> if pop_ups : <EOL> self . _topmost_widget = PopUpTarget ( self . _widget ) <EOL> else : <EOL> self . _topmost_widget = self . _widget <EOL> pop_ups = property ( lambda self : self . _pop_ups , _set_pop_ups ) <EOL> def set_alarm_in ( self , sec , callback , user_data = None ) : <EOL> """<STR_LIT>""" <EOL> def cb ( ) : <EOL> callback ( self , user_data ) <EOL> return self . event_loop . alarm ( sec , cb ) <EOL> def set_alarm_at ( self , tm , callback , user_data = None ) : <EOL> """<STR_LIT>""" <EOL> def cb ( ) : <EOL> callback ( self , user_data ) <EOL> return self . event_loop . alarm ( tm - time . time ( ) , cb ) <EOL> def remove_alarm ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> return self . event_loop . remove_alarm ( handle ) <EOL> def watch_pipe ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> pipe_rd , pipe_wr = os . pipe ( ) <EOL> fcntl . fcntl ( pipe_rd , fcntl . F_SETFL , os . O_NONBLOCK ) <EOL> watch_handle = None <EOL> def cb ( ) : <EOL> data = os . read ( pipe_rd , PIPE_BUFFER_READ_SIZE ) <EOL> rval = callback ( data ) <EOL> if rval is False : <EOL> self . event_loop . remove_watch_file ( watch_handle ) <EOL> os . close ( pipe_rd ) <EOL> watch_handle = self . event_loop . watch_file ( pipe_rd , cb ) <EOL> self . _watch_pipes [ pipe_wr ] = ( watch_handle , pipe_rd ) <EOL> return pipe_wr <EOL> def remove_watch_pipe ( self , write_fd ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> watch_handle , pipe_rd = self . _watch_pipes . pop ( write_fd ) <EOL> except KeyError : <EOL> return False <EOL> if not self . event_loop . remove_watch_file ( watch_handle ) : <EOL> return False <EOL> os . close ( pipe_rd ) <EOL> return True <EOL> def watch_file ( self , fd , callback ) : <EOL> """<STR_LIT>""" <EOL> return self . event_loop . watch_file ( fd , callback ) <EOL> def remove_watch_file ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> return self . event_loop . remove_watch_file ( handle ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . screen . started : <EOL> self . _run ( ) <EOL> else : <EOL> self . screen . run_wrapper ( self . _run ) <EOL> except ExitMainLoop : <EOL> pass <EOL> def _test_run ( self ) : <EOL> """<STR_LIT>""" <EOL> def _run ( self ) : <EOL> if self . handle_mouse : <EOL> self . screen . set_mouse_tracking ( ) <EOL> if not hasattr ( self . screen , '<STR_LIT>' ) : <EOL> return self . _run_screen_event_loop ( ) <EOL> self . draw_screen ( ) <EOL> fd_handles = [ ] <EOL> def reset_input_descriptors ( only_remove = False ) : <EOL> for handle in fd_handles : <EOL> self . event_loop . remove_watch_file ( handle ) <EOL> if only_remove : <EOL> del fd_handles [ : ] <EOL> else : <EOL> fd_handles [ : ] = [ <EOL> self . event_loop . watch_file ( fd , self . _update ) <EOL> for fd in self . screen . get_input_descriptors ( ) ] <EOL> if not fd_handles and self . _input_timeout is not None : <EOL> self . event_loop . remove_alarm ( self . _input_timeout ) <EOL> try : <EOL> signals . connect_signal ( self . screen , INPUT_DESCRIPTORS_CHANGED , <EOL> reset_input_descriptors ) <EOL> except NameError : <EOL> pass <EOL> reset_input_descriptors ( ) <EOL> idle_handle = self . event_loop . enter_idle ( self . entering_idle ) <EOL> self . event_loop . run ( ) <EOL> self . event_loop . remove_enter_idle ( idle_handle ) <EOL> reset_input_descriptors ( True ) <EOL> signals . disconnect_signal ( self . screen , INPUT_DESCRIPTORS_CHANGED , <EOL> reset_input_descriptors ) <EOL> def _update ( self , timeout = False ) : <EOL> """<STR_LIT>""" <EOL> if self . _input_timeout is not None and not timeout : <EOL> self . event_loop . remove_alarm ( self . _input_timeout ) <EOL> self . _input_timeout = None <EOL> max_wait , keys , raw = self . screen . get_input_nonblocking ( ) <EOL> if max_wait is not None : <EOL> self . _input_timeout = self . event_loop . alarm ( max_wait , <EOL> lambda : self . _update ( timeout = True ) ) <EOL> keys = self . input_filter ( keys , raw ) <EOL> if keys : <EOL> self . process_input ( keys ) <EOL> if '<STR_LIT>' in keys : <EOL> self . screen_size = None <EOL> def _run_screen_event_loop ( self ) : <EOL> """<STR_LIT>""" <EOL> next_alarm = None <EOL> while True : <EOL> self . draw_screen ( ) <EOL> if not next_alarm and self . event_loop . _alarms : <EOL> next_alarm = heapq . heappop ( self . event_loop . _alarms ) <EOL> keys = None <EOL> while not keys : <EOL> if next_alarm : <EOL> sec = max ( <NUM_LIT:0> , next_alarm [ <NUM_LIT:0> ] - time . time ( ) ) <EOL> self . screen . set_input_timeouts ( sec ) <EOL> else : <EOL> self . screen . set_input_timeouts ( None ) <EOL> keys , raw = self . screen . get_input ( True ) <EOL> if not keys and next_alarm : <EOL> sec = next_alarm [ <NUM_LIT:0> ] - time . time ( ) <EOL> if sec <= <NUM_LIT:0> : <EOL> break <EOL> keys = self . input_filter ( keys , raw ) <EOL> if keys : <EOL> self . process_input ( keys ) <EOL> while next_alarm : <EOL> sec = next_alarm [ <NUM_LIT:0> ] - time . time ( ) <EOL> if sec > <NUM_LIT:0> : <EOL> break <EOL> tm , callback = next_alarm <EOL> callback ( ) <EOL> if self . event_loop . _alarms : <EOL> next_alarm = heapq . heappop ( self . event_loop . _alarms ) <EOL> else : <EOL> next_alarm = None <EOL> if '<STR_LIT>' in keys : <EOL> self . screen_size = None <EOL> def _test_run_screen_event_loop ( self ) : <EOL> """<STR_LIT>""" <EOL> def process_input ( self , keys ) : <EOL> """<STR_LIT>""" <EOL> if not self . screen_size : <EOL> self . screen_size = self . screen . get_cols_rows ( ) <EOL> something_handled = False <EOL> for k in keys : <EOL> if k == '<STR_LIT>' : <EOL> continue <EOL> if is_mouse_event ( k ) : <EOL> event , button , col , row = k <EOL> if self . _topmost_widget . mouse_event ( self . screen_size , <EOL> event , button , col , row , focus = True ) : <EOL> k = None <EOL> elif self . _topmost_widget . selectable ( ) : <EOL> k = self . _topmost_widget . keypress ( self . screen_size , k ) <EOL> if k : <EOL> if command_map [ k ] == REDRAW_SCREEN : <EOL> self . screen . clear ( ) <EOL> something_handled = True <EOL> else : <EOL> something_handled |= bool ( self . unhandled_input ( k ) ) <EOL> else : <EOL> something_handled = True <EOL> return something_handled <EOL> def _test_process_input ( self ) : <EOL> """<STR_LIT>""" <EOL> def input_filter ( self , keys , raw ) : <EOL> """<STR_LIT>""" <EOL> if self . _input_filter : <EOL> return self . _input_filter ( keys , raw ) <EOL> return keys <EOL> def unhandled_input ( self , input ) : <EOL> """<STR_LIT>""" <EOL> if self . _unhandled_input : <EOL> return self . _unhandled_input ( input ) <EOL> def entering_idle ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . screen . started : <EOL> self . draw_screen ( ) <EOL> def draw_screen ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . screen_size : <EOL> self . screen_size = self . screen . get_cols_rows ( ) <EOL> canvas = self . _topmost_widget . render ( self . screen_size , focus = True ) <EOL> self . screen . draw_screen ( self . screen_size , canvas ) <EOL> class SelectEventLoop ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _alarms = [ ] <EOL> self . _watch_files = { } <EOL> self . _idle_handle = <NUM_LIT:0> <EOL> self . _idle_callbacks = { } <EOL> def alarm ( self , seconds , callback ) : <EOL> """<STR_LIT>""" <EOL> tm = time . time ( ) + seconds <EOL> heapq . heappush ( self . _alarms , ( tm , callback ) ) <EOL> return ( tm , callback ) <EOL> def remove_alarm ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _alarms . remove ( handle ) <EOL> heapq . heapify ( self . _alarms ) <EOL> return True <EOL> except ValueError : <EOL> return False <EOL> def watch_file ( self , fd , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _watch_files [ fd ] = callback <EOL> return fd <EOL> def remove_watch_file ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> if handle in self . _watch_files : <EOL> del self . _watch_files [ handle ] <EOL> return True <EOL> return False <EOL> def enter_idle ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _idle_handle += <NUM_LIT:1> <EOL> self . _idle_callbacks [ self . _idle_handle ] = callback <EOL> return self . _idle_handle <EOL> def remove_enter_idle ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . _idle_callbacks [ handle ] <EOL> except KeyError : <EOL> return False <EOL> return True <EOL> def _entering_idle ( self ) : <EOL> """<STR_LIT>""" <EOL> for callback in self . _idle_callbacks . values ( ) : <EOL> callback ( ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _did_something = True <EOL> while True : <EOL> try : <EOL> self . _loop ( ) <EOL> except select . error , e : <EOL> if e . args [ <NUM_LIT:0> ] != <NUM_LIT:4> : <EOL> raise <EOL> except ExitMainLoop : <EOL> pass <EOL> def _loop ( self ) : <EOL> """<STR_LIT>""" <EOL> fds = self . _watch_files . keys ( ) <EOL> if self . _alarms or self . _did_something : <EOL> if self . _alarms : <EOL> tm = self . _alarms [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> timeout = max ( <NUM_LIT:0> , tm - time . time ( ) ) <EOL> if self . _did_something and ( not self . _alarms or <EOL> ( self . _alarms and timeout > <NUM_LIT:0> ) ) : <EOL> timeout = <NUM_LIT:0> <EOL> tm = '<STR_LIT>' <EOL> ready , w , err = select . select ( fds , [ ] , fds , timeout ) <EOL> else : <EOL> tm = None <EOL> ready , w , err = select . select ( fds , [ ] , fds ) <EOL> if not ready : <EOL> if tm == '<STR_LIT>' : <EOL> self . _entering_idle ( ) <EOL> self . _did_something = False <EOL> elif tm is not None : <EOL> tm , alarm_callback = self . _alarms . pop ( <NUM_LIT:0> ) <EOL> alarm_callback ( ) <EOL> self . _did_something = True <EOL> for fd in ready : <EOL> self . _watch_files [ fd ] ( ) <EOL> self . _did_something = True <EOL> class GLibEventLoop ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> from gi . repository import GLib <EOL> self . GLib = GLib <EOL> self . _alarms = [ ] <EOL> self . _watch_files = { } <EOL> self . _idle_handle = <NUM_LIT:0> <EOL> self . _glib_idle_enabled = False <EOL> self . _idle_callbacks = { } <EOL> self . _loop = GLib . MainLoop ( ) <EOL> self . _exc_info = None <EOL> self . _enable_glib_idle ( ) <EOL> def alarm ( self , seconds , callback ) : <EOL> """<STR_LIT>""" <EOL> @ self . handle_exit <EOL> def ret_false ( ) : <EOL> callback ( ) <EOL> self . _enable_glib_idle ( ) <EOL> return False <EOL> fd = self . GLib . timeout_add ( int ( seconds * <NUM_LIT:1000> ) , ret_false ) <EOL> self . _alarms . append ( fd ) <EOL> return ( fd , callback ) <EOL> def remove_alarm ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _alarms . remove ( handle [ <NUM_LIT:0> ] ) <EOL> self . GLib . source_remove ( handle [ <NUM_LIT:0> ] ) <EOL> return True <EOL> except ValueError : <EOL> return False <EOL> def watch_file ( self , fd , callback ) : <EOL> """<STR_LIT>""" <EOL> @ self . handle_exit <EOL> def io_callback ( source , cb_condition ) : <EOL> callback ( ) <EOL> self . _enable_glib_idle ( ) <EOL> return True <EOL> self . _watch_files [ fd ] = self . GLib . io_add_watch ( fd , self . GLib . IO_IN , io_callback ) <EOL> return fd <EOL> def remove_watch_file ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> if handle in self . _watch_files : <EOL> self . GLib . source_remove ( self . _watch_files [ handle ] ) <EOL> del self . _watch_files [ handle ] <EOL> return True <EOL> return False <EOL> def enter_idle ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _idle_handle += <NUM_LIT:1> <EOL> self . _idle_callbacks [ self . _idle_handle ] = callback <EOL> return self . _idle_handle <EOL> def _enable_glib_idle ( self ) : <EOL> if self . _glib_idle_enabled : <EOL> return <EOL> self . GLib . idle_add ( self . _glib_idle_callback ) <EOL> self . _glib_idle_enabled = True <EOL> def _glib_idle_callback ( self ) : <EOL> for callback in self . _idle_callbacks . values ( ) : <EOL> callback ( ) <EOL> self . _glib_idle_enabled = False <EOL> return False <EOL> def remove_enter_idle ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . _idle_callbacks [ handle ] <EOL> except KeyError : <EOL> return False <EOL> return True <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _loop . run ( ) <EOL> finally : <EOL> if self . _loop . is_running ( ) : <EOL> self . _loop . quit ( ) <EOL> if self . _exc_info : <EOL> exc_info = self . _exc_info <EOL> self . _exc_info = None <EOL> raise exc_info [ <NUM_LIT:0> ] , exc_info [ <NUM_LIT:1> ] , exc_info [ <NUM_LIT:2> ] <EOL> def handle_exit ( self , f ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( * args , ** kargs ) : <EOL> try : <EOL> return f ( * args , ** kargs ) <EOL> except ExitMainLoop : <EOL> self . _loop . quit ( ) <EOL> except : <EOL> import sys <EOL> self . _exc_info = sys . exc_info ( ) <EOL> if self . _loop . is_running ( ) : <EOL> self . _loop . quit ( ) <EOL> return False <EOL> return wrapper <EOL> class TornadoEventLoop ( object ) : <EOL> """<STR_LIT>""" <EOL> _ioloop_registry = WeakKeyDictionary ( ) <EOL> _max_idle_handle = <NUM_LIT:0> <EOL> class PollProxy ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , poll_obj , idle_map ) : <EOL> self . __poll_obj = poll_obj <EOL> self . __idle_map = idle_map <EOL> self . _idle_done = False <EOL> self . _prev_timeout = <NUM_LIT:0> <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . __poll_obj , name ) <EOL> def poll ( self , timeout ) : <EOL> if timeout > self . _prev_timeout : <EOL> self . _idle_done = False <EOL> self . _prev_timeout = timeout <EOL> start = time . time ( ) <EOL> events = self . __poll_obj . poll ( <NUM_LIT:0> ) <EOL> if events : <EOL> self . _idle_done = False <EOL> return events <EOL> if not self . _idle_done : <EOL> for callback in self . __idle_map . values ( ) : <EOL> callback ( ) <EOL> self . _idle_done = True <EOL> timeout = max ( <NUM_LIT:0> , min ( timeout , timeout + start - time . time ( ) ) ) <EOL> events = self . __poll_obj . poll ( timeout ) <EOL> if events : <EOL> self . _idle_done = False <EOL> return events <EOL> @ classmethod <EOL> def _patch_poll_impl ( cls , ioloop ) : <EOL> """<STR_LIT>""" <EOL> if ioloop in cls . _ioloop_registry : <EOL> return <EOL> cls . _ioloop_registry [ ioloop ] = idle_map = { } <EOL> ioloop . _impl = cls . PollProxy ( ioloop . _impl , idle_map ) <EOL> def __init__ ( self , ioloop = None ) : <EOL> if not ioloop : <EOL> from tornado . ioloop import IOLoop <EOL> ioloop = IOLoop . instance ( ) <EOL> self . _ioloop = ioloop <EOL> self . _patch_poll_impl ( ioloop ) <EOL> self . _pending_alarms = { } <EOL> self . _watch_handles = { } <EOL> self . _max_watch_handle = <NUM_LIT:0> <EOL> self . _exception = None <EOL> def alarm ( self , secs , callback ) : <EOL> ioloop = self . _ioloop <EOL> def wrapped ( ) : <EOL> try : <EOL> del self . _pending_alarms [ handle ] <EOL> except KeyError : <EOL> pass <EOL> self . handle_exit ( callback ) ( ) <EOL> handle = ioloop . add_timeout ( ioloop . time ( ) + secs , wrapped ) <EOL> self . _pending_alarms [ handle ] = <NUM_LIT:1> <EOL> return handle <EOL> def remove_alarm ( self , handle ) : <EOL> self . _ioloop . remove_timeout ( handle ) <EOL> try : <EOL> del self . _pending_alarms [ handle ] <EOL> except KeyError : <EOL> return False <EOL> else : <EOL> return True <EOL> def watch_file ( self , fd , callback ) : <EOL> from tornado . ioloop import IOLoop <EOL> handler = lambda fd , events : self . handle_exit ( callback ) ( ) <EOL> self . _ioloop . add_handler ( fd , handler , IOLoop . READ ) <EOL> self . _max_watch_handle += <NUM_LIT:1> <EOL> handle = self . _max_watch_handle <EOL> self . _watch_handles [ handle ] = fd <EOL> return handle <EOL> def remove_watch_file ( self , handle ) : <EOL> fd = self . _watch_handles . pop ( handle , None ) <EOL> if fd is None : <EOL> return False <EOL> else : <EOL> self . _ioloop . remove_handler ( fd ) <EOL> return True <EOL> def enter_idle ( self , callback ) : <EOL> self . _max_idle_handle += <NUM_LIT:1> <EOL> handle = self . _max_idle_handle <EOL> idle_map = self . _ioloop_registry [ self . _ioloop ] <EOL> idle_map [ handle ] = callback <EOL> return handle <EOL> def remove_enter_idle ( self , handle ) : <EOL> idle_map = self . _ioloop_registry [ self . _ioloop ] <EOL> cb = idle_map . pop ( handle , None ) <EOL> return cb is not None <EOL> def handle_exit ( self , func ) : <EOL> @ wraps ( func ) <EOL> def wrapper ( * args , ** kw ) : <EOL> try : <EOL> return func ( * args , ** kw ) <EOL> except ExitMainLoop : <EOL> self . _ioloop . stop ( ) <EOL> except Exception as exc : <EOL> self . _exception = exc <EOL> self . _ioloop . stop ( ) <EOL> return False <EOL> return wrapper <EOL> def run ( self ) : <EOL> self . _ioloop . start ( ) <EOL> if self . _exception : <EOL> exc , self . _exception = self . _exception , None <EOL> raise exc <EOL> try : <EOL> from twisted . internet . abstract import FileDescriptor <EOL> except ImportError : <EOL> FileDescriptor = object <EOL> class TwistedInputDescriptor ( FileDescriptor ) : <EOL> def __init__ ( self , reactor , fd , cb ) : <EOL> self . _fileno = fd <EOL> self . cb = cb <EOL> FileDescriptor . __init__ ( self , reactor ) <EOL> def fileno ( self ) : <EOL> return self . _fileno <EOL> def doRead ( self ) : <EOL> return self . cb ( ) <EOL> class TwistedEventLoop ( object ) : <EOL> """<STR_LIT>""" <EOL> _idle_emulation_delay = <NUM_LIT:1.0> / <NUM_LIT> <EOL> def __init__ ( self , reactor = None , manage_reactor = True ) : <EOL> """<STR_LIT>""" <EOL> if reactor is None : <EOL> import twisted . internet . reactor <EOL> reactor = twisted . internet . reactor <EOL> self . reactor = reactor <EOL> self . _alarms = [ ] <EOL> self . _watch_files = { } <EOL> self . _idle_handle = <NUM_LIT:0> <EOL> self . _twisted_idle_enabled = False <EOL> self . _idle_callbacks = { } <EOL> self . _exc_info = None <EOL> self . manage_reactor = manage_reactor <EOL> self . _enable_twisted_idle ( ) <EOL> def alarm ( self , seconds , callback ) : <EOL> """<STR_LIT>""" <EOL> handle = self . reactor . callLater ( seconds , self . handle_exit ( callback ) ) <EOL> return handle <EOL> def remove_alarm ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> from twisted . internet . error import AlreadyCancelled , AlreadyCalled <EOL> try : <EOL> handle . cancel ( ) <EOL> return True <EOL> except AlreadyCancelled : <EOL> return False <EOL> except AlreadyCalled : <EOL> return False <EOL> def watch_file ( self , fd , callback ) : <EOL> """<STR_LIT>""" <EOL> ind = TwistedInputDescriptor ( self . reactor , fd , <EOL> self . handle_exit ( callback ) ) <EOL> self . _watch_files [ fd ] = ind <EOL> self . reactor . addReader ( ind ) <EOL> return fd <EOL> def remove_watch_file ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> if handle in self . _watch_files : <EOL> self . reactor . removeReader ( self . _watch_files [ handle ] ) <EOL> del self . _watch_files [ handle ] <EOL> return True <EOL> return False <EOL> def enter_idle ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _idle_handle += <NUM_LIT:1> <EOL> self . _idle_callbacks [ self . _idle_handle ] = callback <EOL> return self . _idle_handle <EOL> def _enable_twisted_idle ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _twisted_idle_enabled : <EOL> return <EOL> self . reactor . callLater ( self . _idle_emulation_delay , <EOL> self . handle_exit ( self . _twisted_idle_callback , enable_idle = False ) ) <EOL> self . _twisted_idle_enabled = True <EOL> def _twisted_idle_callback ( self ) : <EOL> for callback in self . _idle_callbacks . values ( ) : <EOL> callback ( ) <EOL> self . _twisted_idle_enabled = False <EOL> def remove_enter_idle ( self , handle ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . _idle_callbacks [ handle ] <EOL> except KeyError : <EOL> return False <EOL> return True <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . manage_reactor : <EOL> return <EOL> self . reactor . run ( ) <EOL> if self . _exc_info : <EOL> exc_info = self . _exc_info <EOL> self . _exc_info = None <EOL> raise exc_info [ <NUM_LIT:0> ] , exc_info [ <NUM_LIT:1> ] , exc_info [ <NUM_LIT:2> ] <EOL> def handle_exit ( self , f , enable_idle = True ) : <EOL> """<STR_LIT>""" <EOL> def wrapper ( * args , ** kargs ) : <EOL> rval = None <EOL> try : <EOL> rval = f ( * args , ** kargs ) <EOL> except ExitMainLoop : <EOL> if self . manage_reactor : <EOL> self . reactor . stop ( ) <EOL> except : <EOL> import sys <EOL> print sys . exc_info ( ) <EOL> self . _exc_info = sys . exc_info ( ) <EOL> if self . manage_reactor : <EOL> self . reactor . crash ( ) <EOL> if enable_idle : <EOL> self . _enable_twisted_idle ( ) <EOL> return rval <EOL> return wrapper <EOL> def _refl ( name , rval = None , exit = False ) : <EOL> """<STR_LIT>""" <EOL> class Reflect ( object ) : <EOL> def __init__ ( self , name , rval = None ) : <EOL> self . _name = name <EOL> self . _rval = rval <EOL> def __call__ ( self , * argl , ** argd ) : <EOL> args = "<STR_LIT:U+002CU+0020>" . join ( [ repr ( a ) for a in argl ] ) <EOL> if args and argd : <EOL> args = args + "<STR_LIT:U+002CU+0020>" <EOL> args = args + "<STR_LIT:U+002CU+0020>" . join ( [ k + "<STR_LIT:=>" + repr ( v ) for k , v in argd . items ( ) ] ) <EOL> print self . _name + "<STR_LIT:(>" + args + "<STR_LIT:)>" <EOL> if exit : <EOL> raise ExitMainLoop ( ) <EOL> return self . _rval <EOL> def __getattr__ ( self , attr ) : <EOL> if attr . endswith ( "<STR_LIT>" ) : <EOL> raise AttributeError ( ) <EOL> if hasattr ( self , attr + "<STR_LIT>" ) : <EOL> return Reflect ( self . _name + "<STR_LIT:.>" + attr , getattr ( self , attr + "<STR_LIT>" ) ) <EOL> return Reflect ( self . _name + "<STR_LIT:.>" + attr ) <EOL> return Reflect ( name ) <EOL> def _test ( ) : <EOL> import doctest <EOL> doctest . testmod ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) </s>
