def test_cancel(self): def func(): assert 0 def callback(result): assert 0 cancel = Cancellable() cancel.cancel() call_async(func, cancel, callback) Gtk.main_iteration() run_gtk_loop()
def test_cancel(self): def func(): assert 0 def callback(result): assert 0 cancel = Cancellable() cancel.cancel() call_async(func, cancel, callback) Gtk.main_iteration() while Gtk.events_pending(): Gtk.main_iteration()
def __init__(self, url, width=-1, height=-1): """ Args: url (str): an HTTP URL width (int): a width to reserve for the image or -1 height (int): a height to reserve for the image or -1 """ super().__init__() self._cancel = Cancellable() call_async(self._fetch_image, self._cancel, self._finished, (url, )) self.connect("destroy", self._on_destroy) self.set_size_request(width, height) self.set_from_icon_name("image-loading", Gtk.IconSize.BUTTON)
def test_main(self): cancel = Cancellable() data = [] def func(): data.append(threading.current_thread().name) def callback(result): data.append(threading.current_thread().name) call_async(func, cancel, callback) Gtk.main_iteration() while Gtk.events_pending(): Gtk.main_iteration() call_async_background(func, cancel, callback) Gtk.main_iteration() while Gtk.events_pending(): Gtk.main_iteration() main_name = threading.current_thread().name self.assertEqual(len(data), 4) self.assertNotEqual(data[0], main_name) self.assertEqual(data[1], main_name) self.assertNotEqual(data[2], main_name) self.assertEqual(data[3], main_name)
def test_search(self): manager = CoverManager(use_built_in=False) handler = manager.plugin_handler for source in dummy_sources: handler.plugin_handle(source) handler.plugin_enable(source) source.cls.cover_call = False source.cls.fetch_call = False song = AudioFile({ "~filename": os.path.join("/tmp/asong.ogg"), "album": "Abbey Road", "artist": "The Beatles" }) songs = [song] results = [] def done(manager, provider, result): self.failUnless(result, msg="Shouldn't succeed with no results") results.append(result) def finished(manager, songs): print("Finished!") manager.connect('covers-found', done) manager.search_cover(Cancellable(), songs) manager.connect('searches-complete', finished) run_loop() self.failUnlessEqual(len(results), 1)
class WebImage(Gtk.Image): """A Gtk.Image which loads the image over HTTP in the background and displays it when available. """ def __init__(self, url, width=-1, height=-1): """ Args: url (str): an HTTP URL width (int): a width to reserve for the image or -1 height (int): a height to reserve for the image or -1 """ super().__init__() self._cancel = Cancellable() call_async(self._fetch_image, self._cancel, self._finished, (url,)) self.connect("destroy", self._on_destroy) self.set_size_request(width, height) self.set_from_icon_name("image-loading", Gtk.IconSize.BUTTON) def _on_destroy(self, *args): self._cancel.cancel() def _fetch_image(self, url): try: data = urlopen(url).read() except Exception as e: print_w("Couldn't read web image from %s (%s)" % (url, e)) return None try: loader = GdkPixbuf.PixbufLoader() except GLib.GError as e: print_w("Couldn't create GdkPixbuf (%s)" % e) else: loader.write(data) loader.close() print_d("Got web image from %s" % url) return loader.get_pixbuf() def _finished(self, pixbuf): if pixbuf is None: self.set_from_icon_name("image-missing", Gtk.IconSize.BUTTON) else: self.set_from_pixbuf(pixbuf)
class WebImage(Gtk.Image): """A Gtk.Image which loads the image over HTTP in the background and displays it when available. """ def __init__(self, url, width=-1, height=-1): """ Args: url (str): an HTTP URL width (int): a width to reserve for the image or -1 height (int): a height to reserve for the image or -1 """ super(WebImage, self).__init__() self._cancel = Cancellable() call_async(self._fetch_image, self._cancel, self._finished, (url,)) self.connect("destroy", self._on_destroy) self.set_size_request(width, height) self.set_from_icon_name("image-loading", Gtk.IconSize.BUTTON) def _on_destroy(self, *args): self._cancel.cancel() def _fetch_image(self, url): try: data = urlopen(url).read() except Exception as e: print_w("Couldn't read web image from %s (%s)" % (url, e)) return None try: loader = GdkPixbuf.PixbufLoader() except GLib.GError as e: print_w("Couldn't create GdkPixbuf (%s)" % e) else: loader.write(data) loader.close() print_d("Got web image from %s" % url) return loader.get_pixbuf() def _finished(self, pixbuf): if pixbuf is None: self.set_from_icon_name("image-missing", Gtk.IconSize.BUTTON) else: self.set_from_pixbuf(pixbuf)
def plugin_on_changed(self, songs): ratings_dict = { song(ATTR_BRAINZ): int(song(ATTR_RATING) * 100) for song in songs if ATTR_BRAINZ in song and ATTR_RATING in song } if len(ratings_dict): call_async_background( musicbrainzngs.submit_ratings, Cancellable(), callback=lambda *args: (), kwargs=dict(recording_ratings=ratings_dict, ), )
def run(self): def do_fetch_versions(): try: return fetch_versions(BUILD_TYPE) except UpdateError: print_exc() return None cancel = Cancellable() self.connect("response", self._on_response, cancel) call_async(do_fetch_versions, cancel, self._on_result) return super().run()
def __init__(self, url, width=-1, height=-1): """ Args: url (str): an HTTP URL width (int): a width to reserve for the image or -1 height (int): a height to reserve for the image or -1 """ super(WebImage, self).__init__() self._cancel = Cancellable() call_async(self._fetch_image, self._cancel, self._finished, (url,)) self.connect("destroy", self._on_destroy) self.set_size_request(width, height) self.set_from_icon_name("image-loading", Gtk.IconSize.BUTTON)
def PluginPreferences(self, parent): table = Gtk.Table(n_rows=3, n_columns=3) table.set_col_spacings(6) table.set_row_spacings(6) label = Gtk.Label(label=_("_Port:"), use_underline=True) label.set_alignment(0.0, 0.5) table.attach(label, 0, 1, 1, 2, xoptions=Gtk.AttachOptions.FILL | Gtk.AttachOptions.SHRINK) entry = UndoEntry() entry.set_text(str(get_port_num())) def validate_port(entry, text, *args): try: int(text) except ValueError: entry.stop_emission("insert-text") entry.connect("insert-text", validate_port) def port_activate(entry, *args): try: port_num = int(entry.get_text()) except ValueError as e: print_w(e) else: if get_port_num() != port_num: set_port_num(port_num) self._refresh() entry.connect_after("activate", port_activate) entry.connect_after("focus-out-event", port_activate) table.attach(entry, 1, 2, 1, 2) port_revert = Gtk.Button() port_revert.add(Gtk.Image.new_from_icon_name( Icons.DOCUMENT_REVERT, Gtk.IconSize.MENU)) def port_revert_cb(button, entry): entry.set_text(str(DEFAULT_PORT)) entry.emit("activate") port_revert.connect("clicked", port_revert_cb, entry) table.attach( port_revert, 2, 3, 1, 2, xoptions=Gtk.AttachOptions.SHRINK) label = Gtk.Label(label=_("Local _IP:"), use_underline=True) label.set_alignment(0.0, 0.5) table.attach(label, 0, 1, 0, 1, xoptions=Gtk.AttachOptions.FILL | Gtk.AttachOptions.SHRINK) label = Gtk.Label(label=_("P_assword:"), use_underline=True) label.set_alignment(0.0, 0.5) table.attach(label, 0, 1, 2, 3, xoptions=Gtk.AttachOptions.FILL | Gtk.AttachOptions.SHRINK) entry = UndoEntry() entry.set_text(self.config_get("password")) entry.connect('changed', self.config_entry_changed, "password") table.attach(entry, 1, 3, 2, 3) label = Gtk.Label() label.set_padding(6, 6) label.set_alignment(0.0, 0.5) label.set_selectable(True) label.set_label("...") table.attach(label, 1, 3, 0, 1) cancel = Cancellable() label.connect("destroy", lambda *x: cancel.cancel()) call_async(fetch_local_ip, cancel, label.set_label) box = Gtk.VBox(spacing=12) clients = Gtk.Label() clients.set_padding(6, 6) clients.set_markup(u"""\ \u2022 <a href="https://play.google.com/store/apps/details?id=com.\ namelessdev.mpdroid">MPDroid</a> (Android) \u2022 <a href="https://play.google.com/store/apps/details?id=org.\ gateshipone.malp">M.A.L.P.</a> (Android) """) clients.set_alignment(0, 0) box.pack_start( qltk.Frame(_("Connection"), child=table), False, True, 0) box.pack_start( qltk.Frame(_("Tested Clients"), child=clients), True, True, 0) return box