def _build_nodes_inner_for_one_menu(nodes, menu_class_name):
 done_nodes = {}
 final_nodes = []
 list_total_length = len(nodes)
 while nodes:
  should_add_to_final_list = True
  node = nodes.pop(0)
  node._counter = (getattr(node, '_counter', 0) + 1)
  if (not node.namespace):
   node.namespace = menu_class_name
  if (node.namespace not in done_nodes):
   done_nodes[node.namespace] = {}
  if (node.parent_id in done_nodes[node.namespace]):
   if (not node.parent_namespace):
    node.parent_namespace = menu_class_name
   parent = done_nodes[node.namespace][node.parent_id]
   parent.children.append(node)
   node.parent = parent
  elif node.parent_id:
   if (node._counter < list_total_length):
    nodes.append(node)
   should_add_to_final_list = False
  if should_add_to_final_list:
   final_nodes.append(node)
   done_nodes[node.namespace][node.id] = node
 return final_nodes

def _build_nodes_inner_for_one_menu ( nodes , menu_class_name ) : done_nodes = { } final_nodes = [ ] list_total_length = len ( nodes ) while nodes : should_add_to_final_list = True node = nodes . pop ( 0 ) node . _counter = ( getattr ( node , '_counter' , 0 ) + 1 ) if ( not node . namespace ) : node . namespace = menu_class_name if ( node . namespace not in done_nodes ) : done_nodes [ node . namespace ] = { } if ( node . parent_id in done_nodes [ node . namespace ] ) : if ( not node . parent_namespace ) : node . parent_namespace = menu_class_name parent = done_nodes [ node . namespace ] [ node . parent_id ] parent . children . append ( node ) node . parent = parent elif node . parent_id : if ( node . _counter < list_total_length ) : nodes . append ( node ) should_add_to_final_list = False if should_add_to_final_list : final_nodes . append ( node ) done_nodes [ node . namespace ] [ node . id ] = node return final_nodes
this is an easier to test "inner loop" building the menu tree structure for one menu .