def wrap_param_elements(tree): """ Find all the <param> nodes and make sure they are wrapped by <params> nodes. """ # grab all the param nodes param_nodes = [] param_vistor = functools.partial(get_param_nodes, param_nodes) tree.visit(param_vistor) for node in param_nodes: parent = node.parent params_node = None if node.parent.name == "params": continue # try to find a params node on the parent for sibling in parent.children: if sibling.name == "params": params_node = sibling break # no params node, so add one if params_node is None: params_node = ParametersNode(parent, "params", {}) parent.add_child(params_node) # now move the node under the params node node.parent = params_node params_node.add_child(node) parent.remove_child(node)
def setup_node_path(node): """ Prep this node to exist outside it's hierarchy. We'll need to give it it's full path, and make sure it has references to the path params. """ current = node path = [] params = [] while current: # handle the path if 'path' in current.attributes: path.insert(0, current.attributes['path'].strip('/')) # grab any params that exist for child in current.children: if child.name == "param": params.insert(0, child) # if the current node has a parent that is a resource too, keep going if current.parent.name == "resource": current = current.parent else: current = None # setup the path for this node node.attributes['full_path'] = '/' + '/'.join(path) if len(params) > 0: params_node = ParametersNode(node, 'params', {}) node.children.insert(0, params_node) for param in params: clone = param.clone() params_node.add_child(clone) clone.parent = params_node