From 8221411ca20e7b3c2c90346dd0918064e7769a8a Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Fri, 1 Oct 2010 12:32:24 -0400 Subject: [PATCH] Double linked trees I have begun working on doubly-linked trees for use in collections. --- libsaria/collection/__init__.py | 9 +++++++ libsaria/collection/collection.py | 32 +++++++++++++++++++++++++ libsaria/collection/tree.py | 39 +++++++++++++++++++++++++++++++ tests/converging.py | 6 +++++ 4 files changed, 86 insertions(+) create mode 100644 tests/converging.py diff --git a/libsaria/collection/__init__.py b/libsaria/collection/__init__.py index 00a8cb26..84f917e8 100644 --- a/libsaria/collection/__init__.py +++ b/libsaria/collection/__init__.py @@ -16,6 +16,8 @@ QUEUE = 2 import collection library = collection.Collection() +library2 = collection.Collection2() + cur_lib_id = -1 def new_source(path, bg=True): @@ -25,6 +27,13 @@ def new_source(path, bg=True): return 0 return call("NEWSOURCE", library.scan, path) +def new_source2(path, bg=True): + global library2 + path = expand(path) + if not exists(path): + return 0 + return call("NEWSOURCE2", library2.scan, path) + def walk_library(): global library for track in library: diff --git a/libsaria/collection/collection.py b/libsaria/collection/collection.py index b46866ae..4f9c10eb 100644 --- a/libsaria/collection/collection.py +++ b/libsaria/collection/collection.py @@ -98,4 +98,36 @@ class Collection: #print e +class Collection2: + def __init__(self): + self.tree = None + def save(self): + from libsaria import data + data.save((self.fs_tree), "DLTree_test", "") + + def scan(self, path): + print "Collection2 scanning %s" % path + self.reset() + self.update(path) + self.save() + self.fs_tree.disp() + + def reset(self): + from tree import DLTree + self.fs_tree = DLTree() + + def update(self, path): + sep = libsaria.path.sep + fs = self.fs_tree + splitext = libsaria.path.splitext + badfiles = set([".jpg", ".db", ".ape"]) + for root,dirs,files in libsaria.path.walk(path): + root = root.strip(sep) + split_root = root.split(sep) + for file in files: + ext = splitext(file)[1] + if ext in badfiles: + continue + fs.insert(split_root + [file]) + #print split_root, file diff --git a/libsaria/collection/tree.py b/libsaria/collection/tree.py index 7f8d72ae..e93a6c11 100644 --- a/libsaria/collection/tree.py +++ b/libsaria/collection/tree.py @@ -59,3 +59,42 @@ class Tree(dict): node = Tree() self[tag] = node node.insert(tag_list[1:], tags, audio, filepath) + + +class DLTree: + def __init__(self, parent=None): + self.parent = parent + self.children = dict() + + def disp(self, level=0): + space = " " * level + + for key,value in self.children.iteritems(): + if key.__class__ == str: + print space, key + value.disp(level+1) + + #if len(self.children) == 0: + # print self.get_path() + + def get_path(self, child=None): + if self.parent == None: + return [self.children[child]] + cmp = self.children.get(child, None) + child = self + path = self.parent.get_path(child) + if cmp != None: + path.append(cmp) + return path + + def insert(self, path): + cmp = path[0] + child = None + try: + child = self.children[cmp] + except KeyError: + child = DLTree(self) + self.children[cmp] = child + self.children[child] = cmp + if len(path) > 1: + child.insert(path[1:]) diff --git a/tests/converging.py b/tests/converging.py new file mode 100644 index 00000000..96114d0e --- /dev/null +++ b/tests/converging.py @@ -0,0 +1,6 @@ +# Bryan Schumaker (10/1/2010) + +from libsaria import collection + + +collection.new_source2("~/Music/")