def deep_first_search(vpath):
root = Node(vpath)
node = root
while True:
objs = list_directory(node.name)
for obj in objs:
child = Node(node.name + obj['objname'] + '/', node)
node.set_child(child)
first_child = node.first_child
if first_child:
node = first_child
else:
while not node.has_next_sibling:
if node is root:
break
if node is root:
first_child = node.first_child
if first_child:
node = first_child
continue
else:
break
tmp = node
node = node.next_sibling(node)
node.remove_sibling(tmp)
class Node(object):
"""docstring for Node"""
def __init__(self, name, parent=None):
super(Node, self).__init__()
self.__name = name
self.__parent = parent
self.__child_nodes = []
@property
def has_next_sibling(self):
try:
if self.__parent != None:
return len(self.__parent.__child_nodes) > 1
else:
return False
except (IndexError, AttributeError):
return False
@property
def name(self):
return self.__name
@property
def parent(self):
return self.__parent
@property
def first_child(self):
try:
return self.__child_nodes[0]
except (IndexError, AttributeError):
return None
def next_sibling(self, node):
try:
if self.__parent != None:
i = self.__parent.__child_nodes.index(node)
return self.__parent.__child_nodes[i+1]
else:
return None
except (IndexError, AttributeError):
return None
def set_child(self, node):
self.__child_nodes.append(node)
def remove_child(self, node):
try:
self.__child_nodes.remove(node)
except Exception, e:
raise e
def remove_sibling(self, node):
try:
self.__parent.__child_nodes.remove(node)
except Exception, e:
raise e
No comments:
Post a Comment