<s> """<STR_LIT>""" <EOL> try : <EOL> import ctypes <EOL> except ImportError : <EOL> ctypes = None <EOL> except SystemError : <EOL> ctypes = None <EOL> import os <EOL> import sys <EOL> from distutils . version import LooseVersion as V <EOL> GUI_WX = '<STR_LIT>' <EOL> GUI_QT = '<STR_LIT>' <EOL> GUI_QT4 = '<STR_LIT>' <EOL> GUI_GTK = '<STR_LIT>' <EOL> GUI_TK = '<STR_LIT>' <EOL> GUI_OSX = '<STR_LIT>' <EOL> GUI_PYGLET = '<STR_LIT>' <EOL> GUI_GTK3 = '<STR_LIT>' <EOL> GUI_NONE = '<STR_LIT:none>' <EOL> def _stdin_ready_posix ( ) : <EOL> """<STR_LIT>""" <EOL> infds , outfds , erfds = select . select ( [ sys . stdin ] , [ ] , [ ] , <NUM_LIT:0> ) <EOL> return bool ( infds ) <EOL> def _stdin_ready_nt ( ) : <EOL> """<STR_LIT>""" <EOL> return msvcrt . kbhit ( ) <EOL> def _stdin_ready_other ( ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def _ignore_CTRL_C_posix ( ) : <EOL> """<STR_LIT>""" <EOL> signal . signal ( signal . SIGINT , signal . SIG_IGN ) <EOL> def _allow_CTRL_C_posix ( ) : <EOL> """<STR_LIT>""" <EOL> signal . signal ( signal . SIGINT , signal . default_int_handler ) <EOL> def _ignore_CTRL_C_other ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _allow_CTRL_C_other ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> if os . name == '<STR_LIT>' : <EOL> import select <EOL> import signal <EOL> stdin_ready = _stdin_ready_posix <EOL> ignore_CTRL_C = _ignore_CTRL_C_posix <EOL> allow_CTRL_C = _allow_CTRL_C_posix <EOL> elif os . name == '<STR_LIT>' : <EOL> import msvcrt <EOL> stdin_ready = _stdin_ready_nt <EOL> ignore_CTRL_C = _ignore_CTRL_C_other <EOL> allow_CTRL_C = _allow_CTRL_C_other <EOL> else : <EOL> stdin_ready = _stdin_ready_other <EOL> ignore_CTRL_C = _ignore_CTRL_C_other <EOL> allow_CTRL_C = _allow_CTRL_C_other <EOL> class InputHookManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> if ctypes is None : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> self . PYFUNC = ctypes . PYFUNCTYPE ( ctypes . c_int ) <EOL> self . guihooks = { } <EOL> self . aliases = { } <EOL> self . apps = { } <EOL> self . _reset ( ) <EOL> def _reset ( self ) : <EOL> self . _callback_pyfunctype = None <EOL> self . _callback = None <EOL> self . _installed = False <EOL> self . _current_gui = None <EOL> def get_pyos_inputhook ( self ) : <EOL> """<STR_LIT>""" <EOL> return ctypes . c_void_p . in_dll ( ctypes . pythonapi , "<STR_LIT>" ) <EOL> def get_pyos_inputhook_as_func ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . PYFUNC . in_dll ( ctypes . pythonapi , "<STR_LIT>" ) <EOL> def set_inputhook ( self , callback ) : <EOL> """<STR_LIT>""" <EOL> ignore_CTRL_C ( ) <EOL> self . _callback = callback <EOL> self . _callback_pyfunctype = self . PYFUNC ( callback ) <EOL> pyos_inputhook_ptr = self . get_pyos_inputhook ( ) <EOL> original = self . get_pyos_inputhook_as_func ( ) <EOL> pyos_inputhook_ptr . value = ctypes . cast ( self . _callback_pyfunctype , ctypes . c_void_p ) . value <EOL> self . _installed = True <EOL> return original <EOL> def clear_inputhook ( self , app = None ) : <EOL> """<STR_LIT>""" <EOL> pyos_inputhook_ptr = self . get_pyos_inputhook ( ) <EOL> original = self . get_pyos_inputhook_as_func ( ) <EOL> pyos_inputhook_ptr . value = ctypes . c_void_p ( None ) . value <EOL> allow_CTRL_C ( ) <EOL> self . _reset ( ) <EOL> return original <EOL> def clear_app_refs ( self , gui = None ) : <EOL> """<STR_LIT>""" <EOL> if gui is None : <EOL> self . apps = { } <EOL> elif gui in self . apps : <EOL> del self . apps [ gui ] <EOL> def register ( self , toolkitname , * aliases ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( cls ) : <EOL> inst = cls ( self ) <EOL> self . guihooks [ toolkitname ] = inst <EOL> for a in aliases : <EOL> self . aliases [ a ] = toolkitname <EOL> return cls <EOL> return decorator <EOL> def current_gui ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _current_gui <EOL> def enable_gui ( self , gui = None , app = None ) : <EOL> """<STR_LIT>""" <EOL> if gui in ( None , GUI_NONE ) : <EOL> return self . disable_gui ( ) <EOL> if gui in self . aliases : <EOL> return self . enable_gui ( self . aliases [ gui ] , app ) <EOL> try : <EOL> gui_hook = self . guihooks [ gui ] <EOL> except KeyError : <EOL> e = "<STR_LIT>" <EOL> raise ValueError ( e . format ( gui , '<STR_LIT:U+002CU+0020>' . join ( self . guihooks ) ) ) <EOL> self . _current_gui = gui <EOL> app = gui_hook . enable ( app ) <EOL> if app is not None : <EOL> app . _in_event_loop = True <EOL> self . apps [ gui ] = app <EOL> return app <EOL> def disable_gui ( self ) : <EOL> """<STR_LIT>""" <EOL> gui = self . _current_gui <EOL> if gui in self . apps : <EOL> self . apps [ gui ] . _in_event_loop = False <EOL> return self . clear_inputhook ( ) <EOL> class InputHookBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , manager ) : <EOL> self . manager = manager <EOL> def disable ( self ) : <EOL> pass <EOL> inputhook_manager = InputHookManager ( ) <EOL> @ inputhook_manager . register ( '<STR_LIT>' ) <EOL> class NullInputHook ( InputHookBase ) : <EOL> """<STR_LIT>""" <EOL> def enable ( self , app = None ) : <EOL> pass <EOL> clear_inputhook = inputhook_manager . clear_inputhook <EOL> set_inputhook = inputhook_manager . set_inputhook <EOL> current_gui = inputhook_manager . current_gui <EOL> clear_app_refs = inputhook_manager . clear_app_refs <EOL> enable_gui = inputhook_manager . enable_gui <EOL> disable_gui = inputhook_manager . disable_gui <EOL> register = inputhook_manager . register <EOL> guis = inputhook_manager . guihooks </s>
