Docker-in-Docker (DinD) capabilities of public runners deactivated. More info

navtree.py 3.21 KB
Newer Older
legac's avatar
legac committed
1
""" the ``Node`` class to build navigation tree.
2

3
"""
4
import json
5
import locale
6 7


8
from .directsvc import JSONEncoder
9
from gluon import current
10

11

12
class Node(object):
13
    """The *node* is the key element of the navigation tree.
14
    It contains children (leaves) which are associated to widgets.
legac's avatar
legac committed
15
    The widget will be display on the panel of the *Viewport*.
16

17 18 19 20
    Args:
        text (str): name of the node appearing in the viewport
            navigation tree.

LE GAC Renaud's avatar
LE GAC Renaud committed
21
    Attributes:
22 23 24
        text (str): name of the node
        children (list): list of children
        hidden (list): list of children to be hidden
LE GAC Renaud's avatar
LE GAC Renaud committed
25

26
    """
Renaud Le Gac's avatar
Renaud Le Gac committed
27
    def __init__(self, text):
28

Renaud Le Gac's avatar
Renaud Le Gac committed
29 30 31 32 33 34
        self.text = text
        self.children = []
        self.hidden = []

    def add_child(self, text, cfg):
        """Add a child (leaf) to the node.
35

36
        Args:
37 38 39 40 41
            text (str):
                the name of the leaf

            cfg (dict):
                the Ext JS configuration options defining
42
                the widget associated to the leaf
43

Renaud Le Gac's avatar
Renaud Le Gac committed
44
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
45 46 47
        di = {"cfg": json.dumps(cfg, cls=JSONEncoder),
              "leaf": True,
              "text": text}
48

Renaud Le Gac's avatar
Renaud Le Gac committed
49 50 51
        self.children.append(di)

    def add_children(self, leaves, func=None, hidden=[]):
52
        """Add children (leaves) to the node.
Renaud Le Gac's avatar
Renaud Le Gac committed
53

54
        Args:
55
            leaves (list): names of the leaves
56

57
            func (function):
58
                function mapping the leaf name to
59 60
                the Ext JS configuration options dictionary.
                The latter defines the associated widget.
61

62
            hidden (list): list of children to be ignored.
63

LE GAC Renaud's avatar
LE GAC Renaud committed
64
        Note:
legac's avatar
legac committed
65 66
            * Leaf names are translated in the current language
            * Leaf names are sorted in alphabetic order
67

Renaud Le Gac's avatar
Renaud Le Gac committed
68
        """
69
        # translator
70
        T = current.T
71 72

        # translate and order leaves in alphabetic order
73 74 75
        # according to local setting
        cvt = {}
        for el in leaves:
76 77
            # NOTE: str is required to force the translation when lazyT is on
            cvt[str(T(el))] = el
78

79
        translate_leaves = list(cvt.keys())
80
        translate_leaves.sort()
81

82
        # fill the node with its children
83
        for tr_leaf in translate_leaves:
84
            leaf = cvt[tr_leaf]
85
            if leaf in hidden:
86
                continue
87

Renaud Le Gac's avatar
Renaud Le Gac committed
88
            self.add_child(tr_leaf, func(leaf))
89

90
    def get_node(self):
91
        """Get the dictionary defining the node.
92

LE GAC Renaud's avatar
LE GAC Renaud committed
93 94
        Returns:
            dict:
95 96 97 98 99 100 101 102
                * **text** (str): name of the node
                * **children** (list): list of leaves. Each element is
                  a dictionary with three keys:

                      * **cfg** (dict): configuration for the widgets
                        associated to the leaf.
                      * **leave** (bool): always ``True``.
                      * **text** (str): name of the leaf
103

104
        """
LE GAC Renaud's avatar
LE GAC Renaud committed
105
        return {"text": self.text, "children": self.children}
106 107 108

    def sort_children(self):
        """sort children according to alphabetical order.
109

110 111 112
        """
        di = {}
        for child in self.children:
LE GAC Renaud's avatar
LE GAC Renaud committed
113
            di[child["text"]] = child
114

115
        names = list(di.keys())
116
        names.sort(cmp=locale.strcoll)
117

118 119 120
        self.children = []
        for name in names:
            self.children.append(di[name])