61 lines
1.2 KiB
Python
61 lines
1.2 KiB
Python
# Bryan Schumaker (11/06/2010)
|
|
|
|
from libsaria.path import sep
|
|
|
|
class Tree(dict):
|
|
def __init__(self, value = None):
|
|
#self.children = dict()
|
|
self.value = value
|
|
|
|
def disp(self, level = 0):
|
|
#children = self.children
|
|
for child in self:
|
|
space = " " * level
|
|
print space, "%s (%s)" % (child, self[child].value)
|
|
self[child].disp(level + 1)
|
|
|
|
def walk(self):
|
|
keys = self.keys()
|
|
keys.sort()
|
|
|
|
for key in keys:
|
|
child = self[key]
|
|
if len(child.keys()) == 0:
|
|
yield [key]
|
|
continue
|
|
for res in child.walk():
|
|
yield [key] + res
|
|
|
|
def insert(self, path, values=[]):
|
|
path_part = path[0]
|
|
value = path_part
|
|
if len(values) > 0:
|
|
value = values[0]
|
|
values = values[1:]
|
|
child = self.get(path_part, None)
|
|
if child == None:
|
|
child = self.__class__(value)
|
|
self[path_part] = child
|
|
if len(path) > 1:
|
|
child.insert(path[1:], values)
|
|
|
|
|
|
class FSTree(Tree):
|
|
def __init__(self, value = None):
|
|
Tree.__init__(self, value)
|
|
|
|
def walk_paths(self):
|
|
for path in self.walk():
|
|
yield sep.join(path)
|
|
|
|
def insert_path(self, base, file = None):
|
|
path = base.split(sep)
|
|
self.insert_path_split(path, file)
|
|
|
|
def insert_path_split(self, base, file = None):
|
|
path = base
|
|
if file != None:
|
|
path += [file]
|
|
self.insert(path)
|
|
|