From fb06e4364fa5635233ccf70210db161525142fc1 Mon Sep 17 00:00:00 2001 From: DYefremov Date: Wed, 27 Jan 2021 16:46:37 +0300 Subject: [PATCH] added saving of bouquet file names --- app/eparser/ecommons.py | 3 ++- app/eparser/enigma/bouquets.py | 31 ++++++++++++++++--------- app/ui/main_app_window.py | 41 ++++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/app/eparser/ecommons.py b/app/eparser/ecommons.py index 09f8a0bc..2eebfe9d 100644 --- a/app/eparser/ecommons.py +++ b/app/eparser/ecommons.py @@ -17,7 +17,8 @@ class BqServiceType(Enum): ALT = "ALT" # Service with alternatives -Bouquet = namedtuple("Bouquet", ["name", "type", "services", "locked", "hidden"]) +Bouquet = namedtuple("Bouquet", ["name", "type", "services", "locked", "hidden", "file"]) +Bouquet.__new__.__defaults__ = (None, BqServiceType.DEFAULT, [], None, None, None) # For Python3 < 3.7 Bouquets = namedtuple("Bouquets", ["name", "type", "bouquets"]) BouquetService = namedtuple("BouquetService", ["name", "type", "data", "num"]) diff --git a/app/eparser/enigma/bouquets.py b/app/eparser/enigma/bouquets.py index df37968e..3ef5de9d 100644 --- a/app/eparser/enigma/bouquets.py +++ b/app/eparser/enigma/bouquets.py @@ -29,7 +29,7 @@ class BouquetsWriter: self._force_bq_names = force_bq_names self._marker_index = 1 self._space_index = 0 - self._alt_index = 0 + self._alt_names = set() def write(self): line = [] @@ -38,11 +38,21 @@ class BouquetsWriter: for bqs in self._bouquets: line.clear() line.append("#NAME {}\n".format(bqs.name)) + bq_file_names = {b.file for b in bqs.bouquets} + count = 1 + + for bq in bqs.bouquets: + bq_name = bq.file + if not bq_name: + if self._force_bq_names: + bq_name = re.sub(pattern, "_", bq.name) + else: + bq_name = "de{0:02d}".format(count) + while bq_name in bq_file_names: + count += 1 + bq_name = "de{0:02d}".format(count) + bq_file_names.add(bq_name) - for index, bq in enumerate(bqs.bouquets): - bq_name = _DEFAULT_BOUQUET_NAME - if index > 0: - bq_name = re.sub(pattern, "_", bq.name) if self._force_bq_names else "de{0:02d}".format(index) line.append(self._SERVICE.format(2 if bq.type == BqType.RADIO.value else 1, bq_name, bq.type)) self.write_bouquet(self._path + "userbouquet.{}.{}".format(bq_name, bq.type), bq.name, bq.services) @@ -68,12 +78,13 @@ class BouquetsWriter: services = srv.transponder if services: p = Path(path) + alt_name = srv.data_id + f_name = "alternatives.{}{}".format(alt_name, p.suffix) + if self._force_bq_names: alt_name = re.sub(self._ALT_PAT, "_", srv.service).lower() f_name = "alternatives.{}{}".format(alt_name, p.suffix) - else: - f_name = "alternatives.de{:02d}{}".format(self._alt_index, p.suffix) - self._alt_index += 1 + alt_path = "{}/{}".format(p.parent, f_name) bouquet.append(self._ALT.format(f_name)) self.write_bouquet(alt_path, srv.service, services) @@ -133,7 +144,7 @@ class BouquetsReader: else: real_b_names[rb_name] = 0 - bouquets[2].append(Bouquet(rb_name, bq_type, services, None, None)) + bouquets[2].append(Bouquet(rb_name, bq_type, services, None, None, b_name)) else: raise ValueError("No bouquet name found for: {}".format(line)) @@ -167,7 +178,7 @@ class BouquetsReader: if alt: alt_name, alt_type = alt.group(1), alt.group(2) alt_bq_name, alt_srvs = BouquetsReader.get_bouquet(path, alt_name, alt_type, "alternatives") - services.append(BouquetService(alt_bq_name, BqServiceType.ALT, srv.lstrip(), tuple(alt_srvs))) + services.append(BouquetService(alt_bq_name, BqServiceType.ALT, alt_name, tuple(alt_srvs))) elif srv_data[0].strip() in BouquetsReader._STREAM_TYPES or srv_data[10].startswith(("http", "rtsp")): stream_data, sep, desc = srv.partition("#DESCRIPTION") desc = desc.lstrip(":").strip() if desc else srv_data[-1].strip() diff --git a/app/ui/main_app_window.py b/app/ui/main_app_window.py index 318909aa..75db0bee 100644 --- a/app/ui/main_app_window.py +++ b/app/ui/main_app_window.py @@ -181,6 +181,9 @@ class Application(Gtk.Application): self._picons_buffer = [] self._services = {} self._bouquets = {} + self._bq_file = {} + self._alt_file = set() + self._alt_counter = 1 self._data_hash = 0 # For bouquets with different names of services in bouquet and main list self._extra_bouquets = {} @@ -1481,9 +1484,9 @@ class Application(Gtk.Application): self.append_bouquet(bq, row.iter) def append_bouquet(self, bq, parent): - name, bt_type, locked, hidden = bq.name, bq.type, bq.locked, bq.hidden - self._bouquets_model.append(parent, [name, locked, hidden, bt_type]) - bq_id = "{}:{}".format(name, bt_type) + name, bq_type, locked, hidden = bq.name, bq.type, bq.locked, bq.hidden + self._bouquets_model.append(parent, [name, locked, hidden, bq_type]) + bq_id = "{}:{}".format(name, bq_type) services = [] extra_services = {} # for services with different names in bouquet and main list agr = [None] * 7 @@ -1508,14 +1511,16 @@ class Application(Gtk.Application): self._picons.get(picon_id, None), picon_id, *agr, data_id, fav_id, None) self._services[fav_id] = srv elif s_type is BqServiceType.ALT: + self._alt_file.add("{}:{}".format(srv.data, bq_type)) srv = Service(None, None, None, srv.name, locked, None, None, s_type.name, - None, None, *agr, fav_id, fav_id, srv.num) + None, None, *agr, srv.data, fav_id, srv.num) self._services[fav_id] = srv elif srv.name: extra_services[fav_id] = srv.name services.append(fav_id) self._bouquets[bq_id] = services + self._bq_file[bq_id] = bq.file if extra_services: self._extra_bouquets[bq_id] = extra_services @@ -1574,7 +1579,10 @@ class Application(Gtk.Application): self._services.clear() self._rows_buffer.clear() self._picons.clear() + self._alt_file.clear() + self._alt_counter = 1 self._bouquets.clear() + self._bq_file.clear() self._extra_bouquets.clear() self._current_bq_name = None self._bq_name_label.set_text("") @@ -1625,7 +1633,7 @@ class Application(Gtk.Application): if profile is SettingsType.ENIGMA_2: bq_s = self.get_enigma_bq_services(bq_s, ex_s) - bq = Bouquet(bq_name, bq_type, bq_s, locked, hidden) + bq = Bouquet(bq_name, bq_type, bq_s, locked, hidden, self._bq_file.get(bq_id, None)) bqs.append(bq) if len(b_path) == 1: bouquets.append(Bouquets(*model.get(itr, Column.BQ_NAME, Column.BQ_TYPE), bqs if bqs else [])) @@ -1682,9 +1690,9 @@ class Application(Gtk.Application): if profile is SettingsType.ENIGMA_2: parent = self._bouquets_model.append(None, ["Bouquets (TV)", None, None, BqType.TV.value]) - self.append_bouquet(Bouquet("Favourites (TV)", BqType.TV.value, [], None, None), parent) + self.append_bouquet(Bouquet("Favourites (TV)", BqType.TV.value, [], None, None, "favourites"), parent) parent = self._bouquets_model.append(None, ["Bouquets (Radio)", None, None, BqType.RADIO.value]) - self.append_bouquet(Bouquet("Favourites (Radio)", BqType.RADIO.value, [], None, None), parent) + self.append_bouquet(Bouquet("Favourites (Radio)", BqType.RADIO.value, [], None, None, "favourites"), parent) elif profile is SettingsType.NEUTRINO_MP: self._bouquets_model.append(None, ["Providers", None, None, BqType.BOUQUET.value]) self._bouquets_model.append(None, ["FAV", None, None, BqType.TV.value]) @@ -2849,6 +2857,9 @@ class Application(Gtk.Application): model_name = get_base_model(model).get_name() if model_name == self.FAV_MODEL_NAME: srv_type = model.get_value(model.get_iter(paths), Column.FAV_TYPE) + if srv_type == BqServiceType.ALT.name: + return self.show_error_dialog("Operation not allowed in this context!") + if srv_type in self._marker_types: return self.on_rename(view) elif srv_type == BqServiceType.IPTV.name: @@ -2902,6 +2913,7 @@ class Application(Gtk.Application): model.set_value(itr, 0, response) old_bq_name = "{}:{}".format(bq_name, bq_type) self._bouquets[bq] = self._bouquets.pop(old_bq_name) + self._bq_file[bq] = self._bq_file.pop(old_bq_name, None) self._current_bq_name = response self._bq_name_label.set_text(self._current_bq_name) self._bq_selected = bq @@ -3052,7 +3064,8 @@ class Application(Gtk.Application): return row = model[paths][:] - if row[Column.FAV_TYPE] in {BqServiceType.MARKER.name, BqServiceType.SPACE.name, BqServiceType.ALT.name}: + s_types = {BqServiceType.MARKER.name, BqServiceType.SPACE.name, BqServiceType.ALT.name, BqServiceType.IPTV.name} + if row[Column.FAV_TYPE] in s_types: self.show_error_dialog("Operation not allowed in this context!") return @@ -3061,7 +3074,16 @@ class Application(Gtk.Application): if not srv or not bq: return + bq_name, sep, bq_type = self._bq_selected.partition(":") fav_id = srv.fav_id + + key = "de{:02d}:{}".format(self._alt_counter, bq_type) + # Generating file name for alternative + while key in self._alt_file: + self._alt_counter += 1 + key = "de{:02d}:{}".format(self._alt_counter, bq_type) + + alt_name = "de{:02d}".format(self._alt_counter) alt_id = "alternatives_{}_{}".format(self._bq_selected, fav_id) if alt_id in bq: self.show_error_dialog("A similar service is already in this list!") @@ -3070,7 +3092,7 @@ class Application(Gtk.Application): dt, it = BqServiceType.DEFAULT, BqServiceType.IPTV bq_srv = BouquetService(None, dt if srv.service_type != it.name else it, fav_id, 0) s_type = BqServiceType.ALT.name - a_srv = srv._replace(service_type=s_type, pos=None, fav_id=alt_id, data_id=alt_id, transponder=(bq_srv,)) + a_srv = srv._replace(service_type=s_type, pos=None, data_id=alt_name, fav_id=alt_id, transponder=(bq_srv,)) try: index = bq.index(fav_id) except ValueError as e: @@ -3078,6 +3100,7 @@ class Application(Gtk.Application): else: bq[index] = alt_id self._services[alt_id] = a_srv + self._alt_file.add(key) data = {Column.FAV_CODED: srv.coded, Column.FAV_SERVICE: srv.service, Column.FAV_LOCKED: srv.locked, Column.FAV_HIDE: srv.hide, Column.FAV_TYPE: s_type, Column.FAV_POS: None, Column.FAV_ID: alt_id, Column.FAV_PICON: self._picons.get(srv.picon_id, None)}