From 7ad74e6dca0e6d24e5e57dec9f3529305b1ecda8 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Tue, 9 Nov 2010 13:52:02 -0500 Subject: [PATCH] FSTree() FSTree() is a tree representing paths in a filesystem. It has functions for inserting paths and returning the paths it is storing. A path is a UNIX path: "/home/bjschuma" is the path to my home directory. --- libsaria/trees.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/libsaria/trees.py b/libsaria/trees.py index a93e8aa9..aca83017 100644 --- a/libsaria/trees.py +++ b/libsaria/trees.py @@ -1,5 +1,7 @@ # Bryan Schumaker (11/06/2010) +from libsaria.path import sep + class Tree(dict): def __init__(self, value = None): #self.children = dict() @@ -24,7 +26,6 @@ class Tree(dict): for res in child.walk(): yield [key] + res - def insert(self, path, values=[]): path_part = path[0] value = path_part @@ -38,3 +39,22 @@ class Tree(dict): 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) +