From c6bf106dd3be6990de403dfca85c2dc22011298e Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Tue, 25 Jan 2022 14:52:43 -0500 Subject: [PATCH] device: netlist: Rework source row data structure Put non-UI bits into a class, rather that stuff them into gtk columns. Simplifies adding bits here Signed-off-by: Cole Robinson --- virtManager/device/netlist.py | 76 ++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/virtManager/device/netlist.py b/virtManager/device/netlist.py index d8c9a566..55fe5c2e 100644 --- a/virtManager/device/netlist.py +++ b/virtManager/device/netlist.py @@ -12,26 +12,27 @@ from ..lib import uiutil from ..baseclass import vmmGObjectUI -NET_ROW_TYPE = 0 -NET_ROW_SOURCE = 1 -NET_ROW_LABEL = 2 -NET_ROW_SENSITIVE = 3 -NET_ROW_MANUAL = 4 +NET_ROW_LABEL = 0 +NET_ROW_SENSITIVE = 1 +NET_ROW_DATA = 2 -def _build_row(nettype, source_name, - label, is_sensitive, manual=False): - row = [] - row.insert(NET_ROW_TYPE, nettype) - row.insert(NET_ROW_SOURCE, source_name) - row.insert(NET_ROW_LABEL, label) - row.insert(NET_ROW_SENSITIVE, is_sensitive) - row.insert(NET_ROW_MANUAL, manual) - return row +class _NetRowData: + @staticmethod + def build_row(*args, **kwargs): + rowdata = _NetRowData(*args, **kwargs) + return [rowdata.label, rowdata.is_sensitive, rowdata] + + def __init__(self, nettype, source, label, is_sensitive, manual=False): + self.nettype = nettype + self.source = source + self.label = label + self.is_sensitive = is_sensitive + self.manual = manual def _build_manual_row(nettype, label): - return _build_row(nettype, None, label, True, manual=True) + return _NetRowData.build_row(nettype, None, label, True, manual=True) def _pretty_network_desc(nettype, source=None, netobj=None): @@ -87,11 +88,9 @@ class vmmNetworkList(vmmGObjectUI): def _init_ui(self): fields = [] - fields.insert(NET_ROW_TYPE, str) - fields.insert(NET_ROW_SOURCE, str) fields.insert(NET_ROW_LABEL, str) fields.insert(NET_ROW_SENSITIVE, bool) - fields.insert(NET_ROW_MANUAL, bool) + fields.insert(NET_ROW_DATA, object) model = Gtk.ListStore(*fields) combo = self.widget("net-source") @@ -118,7 +117,7 @@ class vmmNetworkList(vmmGObjectUI): if net.get_xmlobj().virtualport_type == "openvswitch": label += " (OpenVSwitch)" - row = _build_row(nettype, net.get_name(), label, True) + row = _NetRowData.build_row(nettype, net.get_name(), label, True) rows.append(row) return rows @@ -151,12 +150,12 @@ class vmmNetworkList(vmmGObjectUI): if add_usermode: nettype = virtinst.DeviceInterface.TYPE_USER label = _pretty_network_desc(nettype) - model.append(_build_row(nettype, None, label, True)) + model.append(_NetRowData.build_row(nettype, None, label, True)) defaultnetidx = None for row in sorted(vnets, key=lambda r: r[NET_ROW_LABEL]): model.append(row) - if row[NET_ROW_SOURCE] == "default": + if row[NET_ROW_DATA].source == "default": defaultnetidx = len(model) - 1 bridgeidx = _add_manual_bridge_row() @@ -228,11 +227,12 @@ class vmmNetworkList(vmmGObjectUI): combo = self.widget("net-source") def _find_row(_nettype, _source, _manual): for row in combo.get_model(): - if _nettype and row[NET_ROW_TYPE] != _nettype: + rowdata = row[NET_ROW_DATA] + if _nettype and rowdata.nettype != _nettype: continue - if _source and row[NET_ROW_SOURCE] != _source: + if _source and rowdata.source != _source: continue - if _manual and row[NET_ROW_MANUAL] != _manual: + if _manual and rowdata.manual != _manual: continue # pragma: no cover return row.iter @@ -254,7 +254,7 @@ class vmmNetworkList(vmmGObjectUI): # a label for it and stuff it in the list desc = _pretty_network_desc(nettype, source) combo.get_model().insert(0, - _build_row(nettype, source, desc, True)) + _NetRowData.build_row(nettype, source, desc, True)) return combo.get_model()[0].iter @@ -262,14 +262,15 @@ class vmmNetworkList(vmmGObjectUI): # Public APIs # ############### - def _get_network_row(self): - return uiutil.get_list_selected_row(self.widget("net-source")) + def _get_network_row_data(self): + return uiutil.get_list_selection( + self.widget("net-source"), column=NET_ROW_DATA) def get_network_selection(self): - row = self._get_network_row() - net_type = row[NET_ROW_TYPE] - net_src = row[NET_ROW_SOURCE] - net_check_manual = row[NET_ROW_MANUAL] + rowdata = self._get_network_row_data() + net_type = rowdata.nettype + net_src = rowdata.source + net_check_manual = rowdata.manual if net_check_manual: net_src = self.widget("net-manual-source").get_text() or None @@ -327,7 +328,8 @@ class vmmNetworkList(vmmGObjectUI): ignore2 = kwargs netlist = self.widget("net-source") - current_label = uiutil.get_list_selection(netlist, column=2) + current_label = uiutil.get_list_selection( + netlist, column=NET_ROW_LABEL) model = netlist.get_model() if not model: @@ -341,7 +343,7 @@ class vmmNetworkList(vmmGObjectUI): netlist.set_model(model) for row in netlist.get_model(): - if current_label and row[2] == current_label: + if current_label and row[NET_ROW_LABEL] == current_label: netlist.set_active_iter(row.iter) return @@ -351,15 +353,15 @@ class vmmNetworkList(vmmGObjectUI): def _on_net_source_changed(self, src): ignore = src self._emit_changed() - row = self._get_network_row() - if not row: + rowdata = self._get_network_row_data() + if not rowdata: return # pragma: no cover - nettype = row[NET_ROW_TYPE] + nettype = rowdata.nettype is_direct = (nettype == virtinst.DeviceInterface.TYPE_DIRECT) uiutil.set_grid_row_visible( self.widget("net-macvtap-warn-box"), is_direct) - show_bridge = row[NET_ROW_MANUAL] + show_bridge = rowdata.manual uiutil.set_grid_row_visible( self.widget("net-manual-source"), show_bridge)