def test_copy_named_item(self): service = CopyService() service.init(Application) ef = self.element_factory diagram = ef.create(UML.Diagram) c = diagram.create(items.ClassItem, subject=ef.create(UML.Class)) c.subject.name = "Name" from gi.repository import GLib self.assertEqual(0, GLib.main_depth()) diagram.canvas.update_now() i = list(diagram.canvas.get_all_items()) self.assertEqual(1, len(i), i) self.assertEqual("Name", i[0]._name.text) service.copy([c]) assert diagram.canvas.get_all_items() == [c] service.paste(diagram) i = diagram.canvas.get_all_items() self.assertEqual(2, len(i), i) diagram.canvas.update_now() self.assertEqual("Name", i[0]._name.text) self.assertEqual("Name", i[1]._name.text)
def wrapper(*args, **kwargs): global getattr, setattr, delattr # execute directly if we're not in the main loop if GLib.main_depth() == 0: return func(*args, **kwargs) elif not self.single: def async_wrapper(*aargs): if DEBUG_ASYNC: print("async:", func, args, kwargs) func(*args, **kwargs) self.source(async_wrapper).attach() else: # Idle handlers should be registered per instance holder = args[0] try: if getattr(holder, async_id): return except AttributeError as e: def async_wrapper(*aargs): if DEBUG_ASYNC: print("async:", func, args, kwargs) try: func(*args, **kwargs) finally: delattr(holder, async_id) return False setattr(holder, async_id, self.source(async_wrapper).attach())
def dialog_run(run_fn) -> int: import gi gi.require_version('Gtk', '3.0') from gi.repository import GLib, Gtk log("dialog_run(%s) is_main_thread=%s, main_level=%i", run_fn, is_main_thread(), Gtk.main_level()) if is_main_thread() or Gtk.main_level() == 0: return run_fn() log("dialog_run(%s) main_depth=%s", run_fn, GLib.main_depth()) #do a little dance if we're not running in the main thread: #block this thread and wait for the main thread to run the dialog from threading import Event e = Event() code = [] def main_thread_run(): log("main_thread_run() calling %s", run_fn) try: code.append(run_fn()) finally: e.set() GLib.idle_add(main_thread_run) log("dialog_run(%s) waiting for main thread to run", run_fn) e.wait() log("dialog_run(%s) code=%s", run_fn, code) return code[0]
def wrapper(*args, **kwargs): global getattr, setattr, delattr # execute directly if we're not in the main loop. if GLib.main_depth() == 0: return func(*args, **kwargs) elif not self.single: def async_wrapper(*aargs): if DEBUG_ASYNC: print("async:", func, args, kwargs) func(*args, **kwargs) source(async_wrapper).attach() else: # Idle handlers should be registered per instance holder = args[0] try: if getattr(holder, async_id): return except AttributeError as e: def async_wrapper(*aargs): if DEBUG_ASYNC: print("async:", func, args, kwargs) try: func(*args, **kwargs) finally: delattr(holder, async_id) return False setattr(holder, async_id, source(async_wrapper).attach())
def exit(self): """Exit our gtk application and kill gtk main if we have to""" if self.main_loop < GLib.main_depth(): # Quit Gtk loop if we started one. logging.debug("Quit '%s' Main Loop." % ( self._primary and self._primary.name or 'program')) Gtk.main_quit() # You have to return in order for the loop to exit return 0
def __init__(self, **kwargs): self._loaded = {} self._inital = {} self._primary = None self.main_loop = GLib.main_depth() self.opt = Options(self, **kwargs) # Now start dishing out initalisation self.init_gui() # Start up a gtk main loop when requested if self.opt.start_loop: self.run_mainloop()
def get_uptime_for_profile(self, profile): """Ask and return the uptime of an active profile. Step 1, Issue an async request for new data. Step 2, Return immediately with the cached value. Note: On error the cache is purged. Supports synchronous mode in the absence of an event loop. """ def rh(retval): self._uptime_cache[profile] = retval def eh(exception): try: del self._uptime_cache[profile] except KeyError: pass try: del self._interface_cache[profile] except KeyError: pass try: interface = self._interface_cache[profile] except KeyError: try: p = dbus.SessionBus().get_object(PGlobs.dbus_bus_basename + \ "." + profile, self.obj_path) interface = dbus.Interface(p, self.interface_name) except dbus.exceptions.DBusException as e: eh(e) return self._uptime_cache.default_factory() self._interface_cache[profile] = interface if GLib.main_depth(): # asynchronous: more CPU efficient but requires event loop interface.get_uptime(reply_handler=rh, error_handler=eh) return self._uptime_cache[profile] else: # synchronous return interface.get_uptime()
def test_main_depth(self): self.assertEqual(GLib.main_depth(), 0)
def test_in_main_context(): assert GLib.main_depth() == 1