More work with collections based on doubly linked trees

This commit is contained in:
Bryan Schumaker 2010-10-03 19:00:06 -04:00
parent 31b470e597
commit ab29b2cb2b
5 changed files with 61 additions and 32 deletions

View File

@ -100,7 +100,7 @@ class TrackRecord:
self.length = 0
self.count = 0
self.year = 0
self.like = None
#self.like = None
self.fs = None
self.tags = None
@ -110,11 +110,19 @@ class Collection2:
def __init__(self):
self.fs_tree = None
self.tag_tree = None
self.records = None
self.next_record = 0
def save(self, file):
libsaria.data.save(
[self.fs_tree, self.tag_tree, self.records, self.next_record],
file, "")
def reset(self):
from tree import DLTree
self.fs_tree = DLTree()
self.tag_tree = DLTree()
self.records = dict()
def disp(self):
pass
@ -122,3 +130,28 @@ class Collection2:
#print
#print
#self.tag_tree.disp()
def insert_allocate(self, components, ref):
t = ref.tag()
artist = str(t.artist)
album = str(t.album)
title = str(t.title)
audio = ref.audioProperties()
if artist == "":
artist = "Unknown Artist"
if album == "":
album = "Unknown Album"
if title == "":
title = "Unknown Title"
record = TrackRecord(t.year, audio.length)
id = self.next_record
self.next_record += 1
fs = self.fs_tree.insert(components + [id])
tags = self.tag_tree.insert([artist, album, title, id])
record.fs = fs
record.tags = tags
self.records[id] = record

View File

@ -10,43 +10,23 @@ walk = libsaria.path.walk
join = libsaria.path.join
splitext = libsaria.path.splitext
Record = collection.TrackRecord
class Library(collection.Collection2):
def __init__(self):
collection.Collection2.__init__(self)
self.badfiles = set()
pass
def save(self):
libsaria.data.save((self.fs_tree, self.tag_tree), "DLTree_test", "")
#def save(self):
#libsaria.data.save((self.fs_tree, self.tag_tree), "DLTree_test", "")
# libsaria.data.save(self, "DLTree_test", "")
def scan(self, path):
print "Library scanning %s" % path
self.reset()
self.update(path)
self.save()
self.save("DLTree_test")
self.disp()
def insert(self, components, ref):
t = ref.tag()
artist = str(t.artist)
album = str(t.album)
title = str(t.title)
audio = ref.audioProperties()
if artist == "":
artist = "Unknown Artist"
if album == "":
album = "Unknown Album"
if title == "":
title = "Unknown Title"
record = Record(t.year, audio.length)
fs = self.fs_tree.insert(components + [record])
tags = self.tag_tree.insert([artist, album, title, record])
record.fs = fs
record.tags = tags
def update(self, path):
badfiles = self.badfiles
FileRef = libsaria.collection.FileRef
@ -65,7 +45,9 @@ class Library(collection.Collection2):
badfiles.add(ext)
continue
try:
self.insert(split_root + [file], ref)
except Exception, e:
#print e
self.insert_allocate(split_root + [file], ref)
except UnicodeEncodeError:
pass
#except Exception, e:
# print e
# pass

View File

@ -64,13 +64,21 @@ class Tree(dict):
class DLTree:
def __init__(self, parent=None):
self.parent = parent
self.children = dict()
self.children = None
def disp(self, level=0):
space = " " * level
children = self.children
for key,value in self.children.iteritems():
if key.__class__ == str:
if children == None:
return
keys = children.keys()
keys.sort()
for key in keys:
if key.__class__ == str or key.__class__ == int:
value = children[key]
print space, key
value.disp(level+1)
@ -92,6 +100,11 @@ class DLTree:
child = None
try:
child = self.children[cmp]
except TypeError:
self.children = dict()
child = DLTree(self)
self.children[cmp] = child
self.children[child] = cmp
except KeyError:
child = DLTree(self)
self.children[cmp] = child

View File

@ -2,6 +2,7 @@
from libsaria import path
import cPickle as pickle
#import pickle
plugin = None
PROTO = pickle.HIGHEST_PROTOCOL