<s> """<STR_LIT>""" <EOL> import ctypes <EOL> import sys <EOL> import warnings <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> class InputHookManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . PYFUNC = ctypes . PYFUNCTYPE ( ctypes . c_int ) <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> 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> 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 enable_wx ( self , app = None ) : <EOL> """<STR_LIT>""" <EOL> from IPython . lib . inputhookwx import inputhook_wx <EOL> self . set_inputhook ( inputhook_wx ) <EOL> self . _current_gui = GUI_WX <EOL> import wx <EOL> if app is None : <EOL> app = wx . GetApp ( ) <EOL> if app is None : <EOL> app = wx . App ( redirect = False , clearSigInt = False ) <EOL> app . _in_event_loop = True <EOL> self . _apps [ GUI_WX ] = app <EOL> return app <EOL> def disable_wx ( self ) : <EOL> """<STR_LIT>""" <EOL> if GUI_WX in self . _apps : <EOL> self . _apps [ GUI_WX ] . _in_event_loop = False <EOL> self . clear_inputhook ( ) <EOL> def enable_qt4 ( self , app = None ) : <EOL> """<STR_LIT>""" <EOL> from IPython . external . qt_for_kernel import QtCore , QtGui <EOL> if '<STR_LIT>' in sys . modules : <EOL> warnings . warn ( """<STR_LIT>""" , <EOL> RuntimeWarning ) <EOL> try : <EOL> QtCore . pyqtRestoreInputHook ( ) <EOL> except AttributeError : <EOL> pass <EOL> self . _current_gui = GUI_QT4 <EOL> if app is None : <EOL> app = QtCore . QCoreApplication . instance ( ) <EOL> if app is None : <EOL> app = QtGui . QApplication ( [ "<STR_LIT:U+0020>" ] ) <EOL> app . _in_event_loop = True <EOL> self . _apps [ GUI_QT4 ] = app <EOL> return app <EOL> def disable_qt4 ( self ) : <EOL> """<STR_LIT>""" <EOL> if GUI_QT4 in self . _apps : <EOL> self . _apps [ GUI_QT4 ] . _in_event_loop = False <EOL> self . clear_inputhook ( ) <EOL> def enable_gtk ( self , app = None ) : <EOL> """<STR_LIT>""" <EOL> import gtk <EOL> try : <EOL> gtk . set_interactive ( True ) <EOL> self . _current_gui = GUI_GTK <EOL> except AttributeError : <EOL> from IPython . lib . inputhookgtk import inputhook_gtk <EOL> self . set_inputhook ( inputhook_gtk ) <EOL> self . _current_gui = GUI_GTK <EOL> def disable_gtk ( self ) : <EOL> """<STR_LIT>""" <EOL> self . clear_inputhook ( ) <EOL> def enable_tk ( self , app = None ) : <EOL> """<STR_LIT>""" <EOL> self . _current_gui = GUI_TK <EOL> if app is None : <EOL> import tkinter <EOL> app = tkinter . Tk ( ) <EOL> app . withdraw ( ) <EOL> self . _apps [ GUI_TK ] = app <EOL> return app <EOL> def disable_tk ( self ) : <EOL> """<STR_LIT>""" <EOL> self . clear_inputhook ( ) <EOL> def current_gui ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _current_gui <EOL> inputhook_manager = InputHookManager ( ) <EOL> enable_wx = inputhook_manager . enable_wx <EOL> disable_wx = inputhook_manager . disable_wx <EOL> enable_qt4 = inputhook_manager . enable_qt4 <EOL> disable_qt4 = inputhook_manager . disable_qt4 <EOL> enable_gtk = inputhook_manager . enable_gtk <EOL> disable_gtk = inputhook_manager . disable_gtk <EOL> enable_tk = inputhook_manager . enable_tk <EOL> disable_tk = inputhook_manager . disable_tk <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> def enable_gui ( gui = None , app = None ) : <EOL> """<STR_LIT>""" <EOL> guis = { None : clear_inputhook , <EOL> GUI_OSX : lambda app = False : None , <EOL> GUI_TK : enable_tk , <EOL> GUI_GTK : enable_gtk , <EOL> GUI_WX : enable_wx , <EOL> GUI_QT : enable_qt4 , <EOL> GUI_QT4 : enable_qt4 } <EOL> try : <EOL> gui_hook = guis [ gui ] <EOL> except KeyError : <EOL> e = "<STR_LIT>" % ( gui , list ( guis . keys ( ) ) ) <EOL> raise ValueError ( e ) <EOL> return gui_hook ( app ) </s>
