diff --git a/gui/wxpython/core/treemodel.py b/gui/wxpython/core/treemodel.py index 1c1fc2deee9..7bd3805a122 100644 --- a/gui/wxpython/core/treemodel.py +++ b/gui/wxpython/core/treemodel.py @@ -239,6 +239,52 @@ def match(self, key, value): return False +class DictFilterNode(DictNode): + """Node which has data in a form of dictionary and can be filtered.""" + + def __init__(self, data=None): + super().__init__(data=data) + + def match(self, method="exact", **kwargs): + """Method used for searching according to given parameters. + + :param method: 'exact' for exact match or 'filtering' for filtering by type/name + :param kwargs key-value to be matched, filtering method uses 'type' and 'name' + """ + if not kwargs: + return False + + if method == "exact": + return self._match_exact(**kwargs) + elif method == "filtering": + return self._match_filtering(**kwargs) + else: + return False + + def _match_exact(self, **kwargs): + """Match method for exact matching.""" + for key, value in kwargs.items(): + if not (key in self.data and self.data[key] == value): + return False + return True + + def _match_filtering(self, **kwargs): + """Match method for filtering.""" + if ( + "type" in kwargs + and "type" in self.data + and kwargs["type"] != self.data["type"] + ): + return False + if ( + "name" in kwargs + and "name" in self.data + and not kwargs["name"].search(self.data["name"]) + ): + return False + return True + + class ModuleNode(DictNode): """Node representing module.""" diff --git a/gui/wxpython/datacatalog/tree.py b/gui/wxpython/datacatalog/tree.py index 65ef518fce8..fb017f41039 100644 --- a/gui/wxpython/datacatalog/tree.py +++ b/gui/wxpython/datacatalog/tree.py @@ -37,7 +37,7 @@ ) from gui_core.dialogs import TextEntryDialog from core.giface import StandaloneGrassInterface -from core.treemodel import TreeModel, DictNode +from core.treemodel import TreeModel, DictFilterNode from gui_core.treeview import TreeView from gui_core.wrap import Menu from datacatalog.dialogs import CatalogReprojectionDialog @@ -170,7 +170,7 @@ def OnOK(self, event): self.EndModal(wx.ID_OK) -class DataCatalogNode(DictNode): +class DataCatalogNode(DictFilterNode): """Node representing item in datacatalog.""" def __init__(self, data=None): @@ -196,36 +196,6 @@ def label(self): return _("{name}").format(**data) - def match(self, method="exact", **kwargs): - """Method used for searching according to given parameters. - - :param method: 'exact' for exact match or 'filtering' for filtering by type/name - :param kwargs key-value to be matched, filtering method uses 'type' and 'name' - where 'name' is compiled regex - """ - if not kwargs: - return False - - if method == "exact": - for key, value in kwargs.items(): - if not (key in self.data and self.data[key] == value): - return False - return True - # for filtering - if ( - "type" in kwargs - and "type" in self.data - and kwargs["type"] != self.data["type"] - ): - return False - if ( - "name" in kwargs - and "name" in self.data - and not kwargs["name"].search(self.data["name"]) - ): - return False - return True - class DataCatalogTree(TreeView): """Tree structure visualizing and managing grass database. diff --git a/gui/wxpython/history/tree.py b/gui/wxpython/history/tree.py index 682ddada0a9..8e580a38496 100644 --- a/gui/wxpython/history/tree.py +++ b/gui/wxpython/history/tree.py @@ -32,7 +32,7 @@ split, ) from gui_core.forms import GUI -from core.treemodel import TreeModel, DictNode +from core.treemodel import TreeModel, DictFilterNode from gui_core.treeview import CTreeView from gui_core.wrap import Menu @@ -41,7 +41,7 @@ from grass.grassdb import history -class HistoryBrowserNode(DictNode): +class HistoryBrowserNode(DictFilterNode): """Node representing item in history browser.""" def __init__(self, data=None): @@ -63,36 +63,6 @@ def label(self): else: return self.data["name"] - def match(self, method="exact", **kwargs): - """Method used for searching according to given parameters. - - :param method: 'exact' for exact match or 'filtering' for filtering by type/name - :param kwargs key-value to be matched, filtering method uses 'type' and 'name' - """ - if not kwargs: - return False - - if method == "exact": - for key, value in kwargs.items(): - if not (key in self.data and self.data[key] == value): - return False - return True - - # for filtering - if ( - "type" in kwargs - and "type" in self.data - and kwargs["type"] != self.data["type"] - ): - return False - if ( - "name" in kwargs - and "name" in self.data - and not kwargs["name"].search(self.data["name"]) - ): - return False - return True - class HistoryBrowserTree(CTreeView): """Tree structure visualizing and managing history of executed commands.