File: treemodel.py

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (340 lines) | stat: -rw-r--r-- 10,292 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""
@package core.treemodel

@brief tree structure model (used for menu, search tree)

Classes:
 - treemodel::TreeModel
 - treemodel::DictNode
 - treemodel::ModuleNode

(C) 2013-2020 by the GRASS Development Team

This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.

@author Anna Petrasova <kratochanna gmail.com>
"""

import copy

from grass.script.utils import naturally_sort


class TreeModel:
    """Class represents a tree structure with hidden root.

    TreeModel is used together with TreeView class to display results in GUI.
    The functionality is not complete, only needed methods are implemented.
    If needed, the functionality can be extended.

    >>> tree = TreeModel(DictNode)
    >>> root = tree.root
    >>> n1 = tree.AppendNode(parent=root, data={"label": "node1"})
    >>> n2 = tree.AppendNode(parent=root, data={"label": "node2"})
    >>> n11 = tree.AppendNode(parent=n1, data={"label": "node11", "xxx": 1})
    >>> n111 = tree.AppendNode(parent=n11, data={"label": "node111", "xxx": 4})
    >>> n12 = tree.AppendNode(parent=n1, data={"label": "node12", "xxx": 2})
    >>> n21 = tree.AppendNode(parent=n2, data={"label": "node21", "xxx": 1})
    >>> [node.label for node in tree.SearchNodes(key='xxx', value=1)]
    ['node11', 'node21']
    >>> [node.label for node in tree.SearchNodes(key='xxx', value=5)]
    []
    >>> tree.GetIndexOfNode(n111)
    [0, 0, 0]
    >>> tree.GetNodeByIndex((0,1)).label
    'node12'
    >>> print(tree)
    node1
      * label : node1
      node11
        * label : node11
        * xxx : 1
        node111
          * label : node111
          * xxx : 4
      node12
        * label : node12
        * xxx : 2
    node2
      * label : node2
      node21
        * label : node21
        * xxx : 1

    """

    def __init__(self, nodeClass):
        """Constructor creates root node.

        :param nodeClass: class which is used for creating nodes
        """
        self._root = nodeClass()
        self.nodeClass = nodeClass

    @property
    def root(self):
        return self._root

    def AppendNode(self, parent, **kwargs):
        """Create node and append it to parent node.

        :param parent: parent node of the new node

        :return: new node
        """
        node = self.nodeClass(**kwargs)
        # useful for debugging deleting nodes
        # weakref.finalize(node, print, "Deleted node {}".format(label))
        parent.children.append(node)
        # weakref doesn't work out of the box when deepcopying this class
        # node.parent = weakref.proxy(parent)
        node.parent = parent
        return node

    def SearchNodes(self, parent=None, **kwargs):
        """Search nodes according to specified attributes."""
        nodes = []
        parent = parent if parent else self.root
        self._searchNodes(node=parent, foundNodes=nodes, **kwargs)
        return nodes

    def _searchNodes(self, node, foundNodes, **kwargs):
        """Helper method for searching nodes."""
        if node.match(**kwargs):
            foundNodes.append(node)
        for child in node.children:
            self._searchNodes(node=child, foundNodes=foundNodes, **kwargs)

    def GetNodeByIndex(self, index):
        """Method used for communication between view (VirtualTree) and model.

        :param index: index of node, as defined in VirtualTree doc
                      (e.g. root ~ [], second node of a first node ~ [0, 1])
        """
        if len(index) == 0:
            return self.root
        return self._getNode(self.root, index)

    def GetIndexOfNode(self, node):
        """Method used for communication between view (VirtualTree) and model."""
        index = []
        return tuple(self._getIndex(node, index))

    def _getIndex(self, node, index):
        if node.parent:
            index.insert(0, node.parent.children.index(node))
            return self._getIndex(node.parent, index)
        return index

    def GetChildrenByIndex(self, index):
        """Method used for communication between view (VirtualTree) and model."""
        if len(index) == 0:
            return self.root.children
        node = self._getNode(self.root, index)
        return node.children

    def _getNode(self, node, index):
        if len(index) == 1:
            return node.children[index[0]]
        else:
            return self._getNode(node.children[index[0]], index[1:])

    def RemoveNode(self, node):
        """Removes node. If node is root, removes root's children, root is kept."""
        if node.parent:
            node.parent.children.remove(node)
        else:
            # node is root
            del node.children[:]

    def SortChildren(self, node):
        """Sorts children with 'natural sort' based on label."""
        if node.children:
            naturally_sort(node.children, key=lambda node: node.label)

    def Filtered(self, **kwargs):
        """Filters model based on parameters in kwargs
        that are passed to node's match function.
        Copies tree and returns a filtered copy."""

        def _filter(node):
            if node.children:
                to_remove = []
                for child in node.children:
                    match = _filter(child)
                    if not match:
                        to_remove.append(child)
                for child in reversed(to_remove):
                    fmodel.RemoveNode(child)
                if node.children:
                    return True
            return node.match(**kwargs)

        fmodel = copy.deepcopy(self)
        _filter(fmodel.root)

        return fmodel

    def GetLeafCount(self, node):
        """Returns the number of leaves in a node."""
        if node.children:
            count = 0
            for child in node.children:
                count += self.GetLeafCount(child)
            return count
        return 1

    def __str__(self):
        """Print tree."""
        text = []
        for child in self.root.children:
            child.nprint(text)
        return "\n".join(text)


class DictNode:
    """Node which has data in a form of dictionary."""

    def __init__(self, data=None):
        """Create node.

        :param data: data as dictionary or None
        """
        if not data:
            self.data = {"label": ""}
        else:
            self.data = data
        self._children = []
        self.parent = None

    @property
    def label(self):
        return self.data["label"]

    @property
    def children(self):
        return self._children

    def nprint(self, text, indent=0):
        text.append(indent * " " + self.label)
        if self.data:
            for key, value in self.data.items():
                text.append(
                    "%(indent)s* %(key)s : %(value)s"
                    % {"indent": (indent + 2) * " ", "key": key, "value": value}
                )

        if self.children:
            for child in self.children:
                child.nprint(text, indent + 2)

    def match(self, key, value):
        """Method used for searching according to given parameters.

        :param value: dictionary value to be matched
        :param key: data dictionary key
        """
        if key in self.data and self.data[key] == value:
            return True
        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 str 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'
        :return bool: True if an entry matching given parameters was found
        """
        if not kwargs:
            return False

        if method == "exact":
            return self._match_exact(**kwargs)
        elif method == "filtering":
            return self._match_filtering(**kwargs)

    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."""

    def __init__(self, label=None, data=None):
        super().__init__(data=data)
        self._label = label if label else ""
        if not data:
            self.data = {}

    @property
    def label(self):
        return self._label

    def match(self, key, value, case_sensitive=False):
        """Method used for searching according to command,
        keywords or description."""
        if not self.data:
            return False
        if isinstance(key, str):
            keys = [key]
        else:
            keys = key

        for key in keys:
            if key not in ("command", "keywords", "description"):
                return False
            try:
                text = self.data[key]
            except KeyError:
                continue
            if not text:
                continue
            if case_sensitive:
                # start supported but unused, so testing last
                if value in text or value == "*":
                    return True
            else:
                # this works fully only for English and requires accents
                # to be exact match (even Python 3 casefold() does not help)
                if value.lower() in text.lower() or value == "*":
                    return True
        return False


def main():
    import doctest

    doctest.testmod()


if __name__ == "__main__":
    main()