From a735b3db1375a1fd869e8a011f682e47092cee98 Mon Sep 17 00:00:00 2001 From: DYefremov Date: Mon, 18 Oct 2021 19:36:19 +0300 Subject: [PATCH] minor picons loading optimization --- app/commons.py | 14 ++++++++++++++ app/ui/main.py | 15 ++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/commons.py b/app/commons.py index c0b9e63c..35624385 100644 --- a/app/commons.py +++ b/app/commons.py @@ -1,4 +1,5 @@ import logging +from collections import defaultdict from functools import wraps from threading import Thread, Timer @@ -77,5 +78,18 @@ def run_with_delay(timeout=5): return run_with +class DefaultDict(defaultdict): + """ Extended to support functions with params as default factory. """ + + def __missing__(self, key): + if self.default_factory: + value = self[key] = self.default_factory(key) + return value + return super().__missing__(key) + + def get(self, key, default=None): + return self[key] + + if __name__ == "__main__": pass diff --git a/app/ui/main.py b/app/ui/main.py index 7ece0313..4359b0d1 100644 --- a/app/ui/main.py +++ b/app/ui/main.py @@ -36,7 +36,7 @@ from urllib.parse import urlparse, unquote from gi.repository import GLib, Gio, GObject -from app.commons import run_idle, log, run_task, run_with_delay, init_logger +from app.commons import run_idle, log, run_task, run_with_delay, init_logger, DefaultDict from app.connections import (HttpAPI, download_data, DownloadType, upload_data, test_http, TestException, HttpApiException, STC_XML_FILE) from app.eparser import get_blacklist, write_blacklist, write_bouquet @@ -215,7 +215,7 @@ class Application(Gtk.Application): self._filter_cache = {} # For bouquets with different names of services in bouquet and main list self._extra_bouquets = {} - self._picons = {} + self._picons = DefaultDict(self.get_picon) self._blacklist = set() self._current_bq_name = None self._bq_selected = "" # Current selected bouquet @@ -681,10 +681,14 @@ class Application(Gtk.Application): paned.set_position(width * 0.5) self._fav_paned.set_position(width * 0.27) - @run_idle + @run_task def update_picons_size(self): self._picons_size = self._settings.list_picon_size update_picons_data(self._settings.profile_picons_path, self._picons, self._picons_size) + self.update_picons_pixbufs() + + @run_idle + def update_picons_pixbufs(self): self._fav_model.foreach(lambda m, p, itr: m.set_value(itr, Column.FAV_PICON, self._picons.get( self._services.get(m.get_value(itr, Column.FAV_ID)).picon_id, None))) self._services_model.foreach(lambda m, p, itr: m.set_value(itr, Column.SRV_PICON, self._picons.get( @@ -1695,8 +1699,6 @@ class Application(Gtk.Application): yield True services = get_services(data_path, prf, self.get_format_version() if prf is SettingsType.ENIGMA_2 else 0) yield True - update_picons_data(self._settings.profile_picons_path, self._picons, self._picons_size) - yield True except FileNotFoundError as e: msg = get_message("Please, download files from receiver or setup your path for read data!") self.show_error_message(getattr(e, "message", str(e)) + "\n\n" + msg) @@ -3268,6 +3270,9 @@ class Application(Gtk.Application): update_picons_data(self._settings.profile_picons_path, self._picons, self._picons_size) append_picons(self._picons, self._services_model) + def get_picon(self, p_id): + return get_picon_pixbuf(f"{self._settings.profile_picons_path}{p_id}", self._settings.list_picon_size) + def on_assign_picon(self, view, src_path=None, dst_path=None): return assign_picons(self.get_target_view(view), self._services_view, self._fav_view, self._main_window, self._picons, self._settings, self._services, src_path, dst_path)