1 """GUI support for the IPython ZeroMQ kernel - GTK toolkit support. 2 """ 3 # ----------------------------------------------------------------------------- 4 # Copyright (C) 2010-2011 The IPython Development Team 5 # 6 # Distributed under the terms of the BSD License. The full license is in 7 # the file LICENSE, distributed as part of this software. 8 # ----------------------------------------------------------------------------- 9 10 # ----------------------------------------------------------------------------- 11 # Imports 12 # ----------------------------------------------------------------------------- 13 # stdlib 14 import sys 15 import warnings 16 17 # Third-party 18 import gobject 19 import gtk 20 21 warnings.warn( 22 "The Gtk3 event loop for ipykernel is deprecated", category=DeprecationWarning, stacklevel=2 23 ) 24 25 # ----------------------------------------------------------------------------- 26 # Classes and functions 27 # ----------------------------------------------------------------------------- 28 29 30 class GTKEmbed: 31 """A class to embed a kernel into the GTK main event loop.""" 32 33 def __init__(self, kernel): 34 """Initialize the embed.""" 35 self.kernel = kernel 36 # These two will later store the real gtk functions when we hijack them 37 self.gtk_main = None 38 self.gtk_main_quit = None 39 40 def start(self): 41 """Starts the GTK main event loop and sets our kernel startup routine.""" 42 # Register our function to initiate the kernel and start gtk 43 gobject.idle_add(self._wire_kernel) 44 gtk.main() 45 46 def _wire_kernel(self): 47 """Initializes the kernel inside GTK. 48 49 This is meant to run only once at startup, so it does its job and 50 returns False to ensure it doesn't get run again by GTK. 51 """ 52 self.gtk_main, self.gtk_main_quit = self._hijack_gtk() 53 gobject.timeout_add(int(1000 * self.kernel._poll_interval), self.iterate_kernel) 54 return False 55 56 def iterate_kernel(self): 57 """Run one iteration of the kernel and return True. 58 59 GTK timer functions must return True to be called again, so we make the 60 call to :meth:`do_one_iteration` and then return True for GTK. 61 """ 62 self.kernel.do_one_iteration() 63 return True 64 65 def stop(self): 66 """Stop the embed.""" 67 # FIXME: this one isn't getting called because we have no reliable 68 # kernel shutdown. We need to fix that: once the kernel has a 69 # shutdown mechanism, it can call this. 70 if self.gtk_main_quit: 71 self.gtk_main_quit() 72 sys.exit() 73 74 def _hijack_gtk(self): 75 """Hijack a few key functions in GTK for IPython integration. 76 77 Modifies pyGTK's main and main_quit with a dummy so user code does not 78 block IPython. This allows us to use %run to run arbitrary pygtk 79 scripts from a long-lived IPython session, and when they attempt to 80 start or stop 81 82 Returns 83 ------- 84 The original functions that have been hijacked: 85 - gtk.main 86 - gtk.main_quit 87 """ 88 89 def dummy(*args, **kw): 90 """No-op.""" 91 92 # save and trap main and main_quit from gtk 93 orig_main, gtk.main = gtk.main, dummy 94 orig_main_quit, gtk.main_quit = gtk.main_quit, dummy 95 return orig_main, orig_main_quit 96