""" Author: R. Le Gac """ import locale from gluon import current class Node(object): """Node associating a leaf to a widget. """ def __init__(self, text): """Constructor of the node text name of the node appearing in the viewport """ self.text = text self.children = [] self.hidden = [] def add_child(self, text, cfg): """Add a child (leaf) to the node. text the name of the leaf cfg configuration dictionary for the associated ExtJS widget """ di = {'cfg': cfg, 'leaf': True, 'text': text} self.children.append(di) def add_children(self, leaves, func=None, hidden=[]): """Helper method to add children to the node. leaves a list of string func function to translate the leaf name into the configuration dictionary of the associated ExtJS widget hidden List of children to be hidden Leaf names are translated and sorted by alphabetic order. """ # translator T = current.T # translate and order leaves in alphabetic order # according to local setting cvt = {} for el in leaves: cvt[T(el)] = el translate_leaves = cvt.keys() translate_leaves.sort(cmp=locale.strcoll) # fill the node with its children for tr_leaf in translate_leaves: leaf = cvt[tr_leaf] if leaf in hidden: continue self.add_child(tr_leaf, func(leaf)) def get_node(self): """Return the configuration dictionary for the node. """ return {'text': self.text, 'children': self.children} def sort_children(self): """sort children according to alphabetical order. """ di = {} for child in self.children: di[child['text']] = child names = di.keys() names.sort(cmp=locale.strcoll) self.children = [] for name in names: self.children.append(di[name])