115 lines
2.5 KiB
Python
115 lines
2.5 KiB
Python
# Bryan Schumaker (8/8/2010)
|
|
|
|
import libsaria
|
|
|
|
import datetime
|
|
timedelta = datetime.timedelta
|
|
|
|
tag = None
|
|
audio = None
|
|
ins_table = None
|
|
ins_index = None
|
|
ins_tree = None
|
|
|
|
class TrackRecord:
|
|
def __init__(self, year, length, lenstr):
|
|
self.length = length
|
|
self.lenstr = lenstr
|
|
self.count = 0
|
|
self.year = year
|
|
#self.like = None
|
|
|
|
self.fs = None
|
|
self.tags = None
|
|
|
|
|
|
class Collection:
|
|
def __init__(self, file):
|
|
self.file = file
|
|
self.load()
|
|
|
|
def save(self):
|
|
libsaria.data.save(
|
|
(self.fs_tree, self.tag_tree, self.index, self.records,
|
|
self.next_record, self.size),
|
|
self.file, "")
|
|
|
|
def load(self):
|
|
objects = libsaria.data.load(self.file, "")
|
|
if objects == None or len(objects) != 6:
|
|
self.reset()
|
|
return
|
|
(self.fs_tree, self.tag_tree, self.index, self.records,
|
|
self.next_record, self.size) = objects
|
|
|
|
def reset(self):
|
|
from tree import DLTree
|
|
from index import Index
|
|
self.fs_tree = DLTree()
|
|
self.tag_tree = DLTree()
|
|
self.index = Index()
|
|
self.records = dict()
|
|
self.next_record = 0
|
|
self.size = 0
|
|
|
|
def disp(self):
|
|
pass
|
|
#self.fs_tree.disp()
|
|
#print
|
|
#print
|
|
#self.tag_tree.disp()
|
|
|
|
def walk_tags(self):
|
|
for tag in self.tag_tree.walk_forwards():
|
|
rec = self.records[tag[3]]
|
|
yield tag + [rec.year] + [rec.lenstr]
|
|
|
|
def find_id(self, file):
|
|
stripped = file.strip(libsaria.path.sep)
|
|
split = stripped.split(libsaria.path.sep)
|
|
print self.fs_tree.walk_path(split)
|
|
|
|
def get_attr(self, id, attr):
|
|
rec = self.records[id]
|
|
if attr == "filepath":
|
|
cmp = rec.fs.walk_backwards()
|
|
return libsaria.path.sep.join([""] + cmp)
|
|
if attr == "playcount":
|
|
return rec.count
|
|
|
|
tags = rec.tags.walk_backwards()
|
|
if attr == "artist":
|
|
return tags[0]
|
|
if attr == "album":
|
|
return tags[1]
|
|
if attr == "title":
|
|
return tags[2]
|
|
|
|
def insert_allocate(self, components, ref):
|
|
t = ref.tag()
|
|
artist = t.artist
|
|
album = t.album
|
|
title = t.title
|
|
audio = ref.audioProperties()
|
|
if artist == "":
|
|
artist = u"Unknown Artist"
|
|
if album == "":
|
|
album = u"Unknown Album"
|
|
if title == "":
|
|
title = u"Unknown Title"
|
|
|
|
len = audio.length
|
|
record = TrackRecord(t.year, len, "%s" % timedelta(seconds=len))
|
|
|
|
id = self.next_record
|
|
self.next_record += 1
|
|
|
|
fs = self.fs_tree.insert(components + [id])
|
|
tags = self.tag_tree.insert([artist, album, title, id])
|
|
self.index.insert([album, artist, title], id)
|
|
|
|
record.fs = fs
|
|
record.tags = tags
|
|
self.records[id] = record
|
|
self.size += 1
|