From 6e24e305db1077ca3c399be422319d7b2a7e520d Mon Sep 17 00:00:00 2001 From: Sander Sweers Date: Wed, 25 Dec 2024 19:39:25 +0100 Subject: [PATCH 1/2] Lists: Improve naming --- blueman/gui/DeviceList.py | 12 ++-- blueman/gui/GenericList.py | 75 ++++++++++++------------ blueman/gui/manager/ManagerDeviceList.py | 20 +++---- 3 files changed, 54 insertions(+), 53 deletions(-) diff --git a/blueman/gui/DeviceList.py b/blueman/gui/DeviceList.py index 857cb6683..84364d314 100644 --- a/blueman/gui/DeviceList.py +++ b/blueman/gui/DeviceList.py @@ -323,14 +323,14 @@ def do_cache(self, tree_iter: Gtk.TreeIter, kwargs: dict[str, Any]) -> None: self.path_to_row[object_path] = Gtk.TreeRowReference.new(self.liststore, self.liststore.get_path(tree_iter)) - def append(self, **columns: object) -> Gtk.TreeIter: - tree_iter = super().append(**columns) - self.do_cache(tree_iter, columns) + def append(self, **list_columns: object) -> Gtk.TreeIter: + tree_iter = super().append(**list_columns) + self.do_cache(tree_iter, list_columns) return tree_iter - def prepend(self, **columns: object) -> Gtk.TreeIter: - tree_iter = super().prepend(**columns) - self.do_cache(tree_iter, columns) + def prepend(self, **list_columns: object) -> Gtk.TreeIter: + tree_iter = super().prepend(**list_columns) + self.do_cache(tree_iter, list_columns) return tree_iter def set(self, tree_iter: Gtk.TreeIter, **kwargs: object) -> None: diff --git a/blueman/gui/GenericList.py b/blueman/gui/GenericList.py index f4d0ce225..7df0d394a 100644 --- a/blueman/gui/GenericList.py +++ b/blueman/gui/GenericList.py @@ -21,46 +21,47 @@ class ListDataDict(_ListDataDictBase, total=False): # noinspection PyAttributeOutsideInit class GenericList(Gtk.TreeView): - def __init__(self, data: Iterable[ListDataDict], headers_visible: bool = True, visible: bool = False) -> None: + def __init__(self, rowdata: Iterable[ListDataDict], headers_visible: bool = True, visible: bool = False) -> None: super().__init__(headers_visible=headers_visible, visible=visible) self.set_name("GenericList") self.selection = self.get_selection() - self._load(data) + self._load(rowdata) def _load(self, data: Iterable[ListDataDict]) -> None: - self.ids: dict[str, int] = {} - self.columns: dict[str, Gtk.TreeViewColumn] = {} + # Mapping of internal rowdata id to the ListStore column number and TreeViewColumn + self.list_col_order: dict[str, int] = {} + self.view_columns: dict[str, Gtk.TreeViewColumn] = {} - types = [row["type"] for row in data] + list_col_types = [row["type"] for row in data] - self.liststore = Gtk.ListStore(*types) + self.liststore = Gtk.ListStore(*list_col_types) self.filter = self.liststore.filter_new() self.set_model(self.filter) - for i, row in enumerate(data): - self.ids[row["id"]] = i + for list_col_num, row in enumerate(data): + self.list_col_order[row["id"]] = list_col_num if "renderer" not in row: continue - column = Gtk.TreeViewColumn() - column.pack_start(row["renderer"], True) - column.set_attributes(row["renderer"], **row["render_attrs"]) + view_column = Gtk.TreeViewColumn() + view_column.pack_start(row["renderer"], True) + view_column.set_attributes(row["renderer"], **row["render_attrs"]) if "view_props" in row: - column.set_properties(**row["view_props"]) + view_column.set_properties(**row["view_props"]) if "celldata_func" in row: func, user_data = row["celldata_func"] - column.set_cell_data_func(row["renderer"], func, user_data) + view_column.set_cell_data_func(row["renderer"], func, user_data) - self.columns[row["id"]] = column - self.append_column(column) + self.view_columns[row["id"]] = view_column + self.append_column(view_column) def selected(self) -> Gtk.TreeIter | None: - model, tree_iter = self.selection.get_selected() + list_model, tree_iter = self.selection.get_selected() if tree_iter is not None: - tree_iter = model.convert_iter_to_child_iter(tree_iter) + tree_iter = list_model.convert_iter_to_child_iter(tree_iter) return tree_iter def delete(self, tree_iter: Gtk.TreeIter) -> bool: @@ -70,41 +71,41 @@ def delete(self, tree_iter: Gtk.TreeIter) -> bool: else: return False - def _add(self, **columns: object) -> Collection[object]: + def _add(self, **list_columns: object) -> Collection[object]: items: dict[int, object] = {} - for k, v in self.ids.items(): - items[v] = None + for col_id, list_col_num in self.list_col_order.items(): + items[list_col_num] = None - for k, val in columns.items(): - if k in self.ids: - items[self.ids[k]] = val + for col_id, col_value in list_columns.items(): + if col_id in self.list_col_order: + items[self.list_col_order[col_id]] = col_value else: - raise Exception(f"Invalid key {k}") + raise Exception(f"Invalid key {col_id}") return items.values() - def append(self, **columns: object) -> Gtk.TreeIter: - vals = self._add(**columns) + def append(self, **list_columns: object) -> Gtk.TreeIter: + vals = self._add(**list_columns) return self.liststore.append(vals) - def prepend(self, **columns: object) -> Gtk.TreeIter: - vals = self._add(**columns) + def prepend(self, **list_columns: object) -> Gtk.TreeIter: + vals = self._add(**list_columns) return self.liststore.prepend(vals) - def set(self, tree_iter: Gtk.TreeIter, **cols: object) -> None: - for k, v in cols.items(): - self.liststore.set(tree_iter, self.ids[k], v) + def set(self, tree_iter: Gtk.TreeIter, **list_columns: object) -> None: + for col_id, col_value in list_columns.items(): + self.liststore.set(tree_iter, self.list_col_order[col_id], col_value) def get(self, tree_iter: Gtk.TreeIter, *items: str) -> dict[str, Any]: - row_data = {} + data = {} if not items: - columns = [(name, self.ids[name]) for name in self.ids] + columns = [(col_id, self.list_col_order[col_id]) for col_id in self.list_col_order] else: - columns = [(name, self.ids[name]) for name in items if name in self.ids] + columns = [(col_id, self.list_col_order[col_id]) for col_id in items if col_id in self.list_col_order] - for name, colid in columns: - row_data[name] = self.liststore.get_value(tree_iter, colid) - return row_data + for col_id, list_col_num in columns: + data[col_id] = self.liststore.get_value(tree_iter, list_col_num) + return data def get_iter(self, path: Gtk.TreePath | None) -> Gtk.TreeIter | None: if path is None: diff --git a/blueman/gui/manager/ManagerDeviceList.py b/blueman/gui/manager/ManagerDeviceList.py index ac825ce68..b231ba82e 100644 --- a/blueman/gui/manager/ManagerDeviceList.py +++ b/blueman/gui/manager/ManagerDeviceList.py @@ -124,10 +124,10 @@ def _on_settings_changed(self, settings: Gio.Settings, key: str) -> None: else: sort_type = Gtk.SortType.DESCENDING - column_id = self.ids.get(sort_by) + list_col_num = self.list_col_order.get(sort_by) - if column_id: - self.liststore.set_sort_column_id(column_id, sort_type) + if list_col_num: + self.liststore.set_sort_column_id(list_col_num, sort_type) def on_icon_theme_changed(self, _icon_them: Gtk.IconTheme) -> None: for row in self.liststore: @@ -560,7 +560,7 @@ def tooltip_query(self, _tw: Gtk.Widget, x: int, y: int, _kb: bool, tooltip: Gtk self.tooltip_col = path[1] return False - if path[1] == self.columns["device_surface"]: + if path[1] == self.view_columns["device_surface"]: tree_iter = self.get_iter(path[0]) assert tree_iter is not None @@ -585,9 +585,9 @@ def tooltip_query(self, _tw: Gtk.Widget, x: int, y: int, _kb: bool, tooltip: Gtk self.tooltip_col = path[1] return True - elif path[1] == self.columns["battery_pb"] \ - or path[1] == self.columns["tpl_pb"] \ - or path[1] == self.columns["rssi_pb"]: + elif path[1] == self.view_columns["battery_pb"] \ + or path[1] == self.view_columns["tpl_pb"] \ + or path[1] == self.view_columns["rssi_pb"]: tree_iter = self.get_iter(path[0]) assert tree_iter is not None @@ -602,7 +602,7 @@ def tooltip_query(self, _tw: Gtk.Widget, x: int, y: int, _kb: bool, tooltip: Gtk tpl = self.get(tree_iter, "tpl")["tpl"] if battery != 0: - if path[1] == self.columns["battery_pb"]: + if path[1] == self.view_columns["battery_pb"]: lines.append(f"Battery: {int(battery)}%") else: lines.append(f"Battery: {int(battery)}%") @@ -619,7 +619,7 @@ def tooltip_query(self, _tw: Gtk.Widget, x: int, y: int, _kb: bool, tooltip: Gtk else: rssi_state = _("Too much") - if path[1] == self.columns["rssi_pb"]: + if path[1] == self.view_columns["rssi_pb"]: lines.append(_("Received Signal Strength: %(rssi)u%% (%(rssi_state)s)") % {"rssi": rssi, "rssi_state": rssi_state}) else: @@ -638,7 +638,7 @@ def tooltip_query(self, _tw: Gtk.Widget, x: int, y: int, _kb: bool, tooltip: Gtk else: tpl_state = _("Very High") - if path[1] == self.columns["tpl_pb"]: + if path[1] == self.view_columns["tpl_pb"]: lines.append(_("Transmit Power Level: %(tpl)u%% (%(tpl_state)s)") % {"tpl": tpl, "tpl_state": tpl_state}) else: From b4929d56ff33418a22a2d43bdc76db5c6092c330 Mon Sep 17 00:00:00 2001 From: Sander Sweers Date: Thu, 26 Dec 2024 01:09:56 +0100 Subject: [PATCH 2/2] GenericList: Replace Exception with Keyerror --- blueman/gui/GenericList.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blueman/gui/GenericList.py b/blueman/gui/GenericList.py index 7df0d394a..4125189db 100644 --- a/blueman/gui/GenericList.py +++ b/blueman/gui/GenericList.py @@ -80,7 +80,7 @@ def _add(self, **list_columns: object) -> Collection[object]: if col_id in self.list_col_order: items[self.list_col_order[col_id]] = col_value else: - raise Exception(f"Invalid key {col_id}") + raise KeyError(f"Invalid key {col_id}") return items.values()