Skip to content

Commit

Permalink
new class DictFilterNode created due to duplication issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lindakladivova committed Apr 19, 2024
1 parent d7346cf commit ec5f01e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 64 deletions.
46 changes: 46 additions & 0 deletions gui/wxpython/core/treemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
34 changes: 2 additions & 32 deletions gui/wxpython/datacatalog/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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.
Expand Down
34 changes: 2 additions & 32 deletions gui/wxpython/history/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand All @@ -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.
Expand Down

0 comments on commit ec5f01e

Please sign in to comment.