6 Copyright (C) 2009-2014 8 RT-Synthesis Research Group 9 Intelligent Systems Research Institute, 10 National Institute of Advanced Industrial Science and Technology (AIST), 13 Licensed under the Eclipse Public License -v 1.0 (EPL) 14 http://www.opensource.org/licenses/eclipse-1.0.txt 16 Object representing a generic node in the tree. 30 '''Base class for a node in the tree. 32 Do not create this class directly. Create objects using a suitable child 36 def __init__(self, name=None, parent=None, children=None, filter=[],
37 dynamic=
False, *args, **kwargs):
40 @param name Name of this node (i.e. its entry in the path). 41 @param parent The parent node of this node, if any. 42 @param children If the list of children is already known, put it here. 43 @param filter A list of paths to filter by. 44 @param dynamic Enable dynamic features such as observers on this node 45 and any children it creates. 48 super(TreeNode, self).
__init__(*args, **kwargs)
62 '''Get this node as a string.''' 64 indent =
''.rjust(self.
depth)
65 result =
'{0}{1}, {2}\n'.format(indent, self.
_name, self.
_children)
71 '''Add a callback to this node. 73 Callbacks are called when the specified event occurs. The available 74 events depends on the specific node type. Args should be a value to 75 pass to the callback when it is called. The callback should be of the 78 def callback(node, value, cb_args): 80 where node will be the node that called the function, value is the 81 relevant information for the event, and cb_args are the arguments you 82 registered with the callback. 85 if event
not in self.
_cbs:
87 self.
_cbs[event] = [(cb, args)]
90 '''Get a child node of this node, or this node, based on a path. 92 @param path A list of path elements pointing to a node in the tree. 93 For example, ['/', 'localhost', 'dir.host']. The first 94 element in this path should be this node's name. 95 @return The node pointed to by @ref path, or None if the path does not 96 point to a node in the tree below this node. 100 if path[0] == self.
_name:
111 '''Check if a path exists below this node. 113 @param path A list of path elements pointing to a node in the tree. 114 For example, ['/', 'localhost', 'dir.host']. The first 115 element in this path should be this node's name. 116 @return True if the path points to a node in the tree below this node, 117 or this node itself (for paths one element long). False 122 if path[0] == self.
_name:
133 '''Is @ref other_node a child of this node?''' 138 '''Is @ref other_node the parent of this note?''' 139 return other_node == self.
parent 141 def iterate(self, func, args=None, filter=[]):
142 '''Call a function on this node, and recursively all its children. 144 This is a depth-first iteration. 146 @param func The function to call. Its declaration must be 147 'def blag(node, args)', where 'node' is the current node 148 in the iteration and args is the value of @ref args. 149 @param args Extra arguments to pass to the function at each iteration. 150 Pass multiple arguments in as a tuple. 151 @param filter A list of filters to apply before calling func for each 152 node in the iteration. If the filter is not True, 153 @ref func will not be called for that node. Each filter 154 entry should be a string, representing one of the is_* 155 properties (is_component, etc), or a function object. 156 @return The results of the calls to @ref func in a list. 162 filters_passed =
True 165 if not eval(
'self.' + f):
166 filters_passed =
False 170 filters_passed =
False 173 result = [func(self, args)]
175 result = [func(self, args)]
181 '''Remove a callback from this node. 183 The callback is removed from the specified event. 185 @param cb The callback function to remove. 188 if event
not in self.
_cbs:
190 c = [(x[0], x[1])
for x
in self.
_cbs[event]]
193 self.
_cbs[event].remove(c[0])
197 '''The child nodes of this node (if any).''' 199 return self._children.values()
203 '''A list of the names of the child nodes of this node (if any).''' 205 return self._children.keys()
209 '''The depth of this node in the tree. 211 The root node is depth 0. 222 '''Get and change the dynamic setting of this node.''' 238 '''The full path of this node.''' 241 return self._parent.full_path + [self.
_name]
247 '''The full path of this node as a string.''' 250 if self._parent._name ==
'/':
251 return self._parent.full_path_str + self.
_name 253 return self._parent.full_path_str +
'/' + self.
_name 259 '''Is this node a component?''' 264 '''Is this node a directory?''' 266 if self.
_name ==
'/':
272 '''Is this node a manager?''' 277 '''Is this node a name server (specialisation of directory nodes)?''' 282 '''Is this node unknown?''' 287 '''Is this node a zombie?''' 292 '''The name of this node.''' 298 '''The name server of the node (i.e. its top-most parent below /).''' 303 elif self._parent.name ==
'/':
306 return self._parent.nameserver
310 '''The ORB used to access this object. 312 This property's value will be None if no object above this object is a 317 if self._parent.name ==
'/':
319 return self._parent.orb
323 '''This node's parent, or None if no parent.''' 339 self._parent.remove_child(self)
344 '''The name of this node's parent or an empty string if no parent.''' 347 return self._parent.name
353 '''The root node of the tree this node is in.''' 356 return self._parent.root
363 self.
_children[new_child._name] = new_child
366 if event
not in self.
_cbs:
368 for (cb, args)
in self.
_cbs[event]:
369 cb(self, value, args)
def _enable_dynamic(self, enable=True)
def add_callback(self, event, cb, args=None)
def is_parent(self, other_node)
def rem_callback(self, event, cb)
def iterate(self, func, args=None, filter=[])
def _remove_all_children(self)
def remove_child(self, child)
def __init__(self, name=None, parent=None, children=None, filter=[], dynamic=False, args, kwargs)
def _add_child(self, new_child)
def is_child(self, other_node)
def _call_cb(self, event, value)
def _set_events(self, events)