Can select songs from library

git-svn-id: file:///home/anna/Desktop/ocarina-legacy/mithos/ocarina@32 1daee41c-8060-4895-b1f0-2197c00d777a
This commit is contained in:
bjschuma 2009-06-28 18:05:17 +00:00
parent 25b7ee99bd
commit f605aa3bbd
5 changed files with 142 additions and 107 deletions

View File

@ -3,6 +3,8 @@ import pygtk
pygtk.require('2.0') pygtk.require('2.0')
import gtk import gtk
from menuItem import MenuItem
#class LibView(gtk.ScrolledWindow): #class LibView(gtk.ScrolledWindow):
class LibView(gtk.VBox): class LibView(gtk.VBox):
@ -17,6 +19,7 @@ class LibView(gtk.VBox):
self.pbar = gtk.ProgressBar() self.pbar = gtk.ProgressBar()
self.pack_start(self.win) self.pack_start(self.win)
self.pack_start(self.pbar,False,False,0) self.pack_start(self.pbar,False,False,0)
self.makeRCMenu()
self.update() self.update()
self.win.show() self.win.show()
@ -31,14 +34,17 @@ class LibView(gtk.VBox):
self.treeview.remove_column(self.col) self.treeview.remove_column(self.col)
self.pbar.pulse() self.pbar.pulse()
self.pbar.set_text("Found "+str(self.library.count)+" files.") self.pbar.set_text("Found "+str(self.library.count)+" files.")
tree = gtk.TreeStore(str) tree = gtk.TreeStore(str,int)
for artist in self.library.artAlb.keys(): for artist in self.library.artAlb.keys():
ariter = tree.append(None,[artist]) ariter = tree.append(None,[artist.title(),-1])
for album in self.library.artAlb[artist]: for album in self.library.artAlb[artist]:
aliter = tree.append(ariter,[album]) aliter = tree.append(ariter,[album.title(),-1])
for track in self.library.albTrk[album]: for track in self.library.albTrk[(artist,album)]:
tree.append(aliter,[track]) #if rval == False:
# print artist,album,self.library.files[track].title
tree.append(aliter,[self.library.files[track].title,self.library.files[track].id])
self.treeview = gtk.TreeView(tree) self.treeview = gtk.TreeView(tree)
self.treeview.connect("button_release_event",self.clicked)
self.col = gtk.TreeViewColumn('Library') self.col = gtk.TreeViewColumn('Library')
self.treeview.append_column(self.col) self.treeview.append_column(self.col)
cell = gtk.CellRendererText() cell = gtk.CellRendererText()
@ -47,12 +53,46 @@ class LibView(gtk.VBox):
self.col.set_sort_column_id(0) self.col.set_sort_column_id(0)
self.treeview.set_rules_hint(True) self.treeview.set_rules_hint(True)
self.treeview.show() self.treeview.show()
self.selection = self.treeview.get_selection()
self.selection.set_mode(gtk.SELECTION_MULTIPLE)
self.win.add(self.treeview) self.win.add(self.treeview)
if rval==False: if rval==False:
self.pbar.hide() self.pbar.hide()
return rval return rval
# Begin updating the library
def updates(self): def updates(self):
self.pbar.show() self.pbar.show()
gobject.timeout_add(500,self.update) gobject.timeout_add(1000,self.update)
# Right click menu
def makeRCMenu(self):
self.rcmenu = gtk.Menu()
add = MenuItem("Add to Playlist",self.populatePlaylist,None,None,None)
self.rcmenu.append(add)
# Show the right click menu on a right click
def clicked(self,widget,data):
if data.button == 3:
self.rcmenu.popup(None,None,None,data.button,data.time)
def populatePlaylist(self,widgit,func,data):
(model,pathlist) = self.selection.get_selected_rows()
for path in pathlist:
iter = model.get_iter(path)
self.popHelper(model,iter,model.iter_n_children(iter))
def popHelper(self,model,iter,count):
if count == 0:
print model[iter][0],count
return
child = model.iter_children(iter)
for i in range(count):
self.popHelper(model,child,model.iter_n_children(child))
child = model.iter_next(child)

View File

@ -4,55 +4,38 @@ import tagpy
import cPickle as pickle import cPickle as pickle
from libdata import LibData from libdata import LibData
from songInfo import SongInfo from songInfo import SongInfo
import thread
class Library: class Library:
#def __init__(self,prnt): #def __init__(self,prnt):
def __init__(self): def __init__(self):
#self.prnt = prnt
#self.data = LibData()
self.goodTypes = ["ogg","mp3"]#,"wma"] self.goodTypes = ["ogg","mp3"]#,"wma"]
self.files = [] self.reset()
self.count = 0
self.path = ""
self.scanning = False self.scanning = False
self.artAlb = dict()
self.albTrk = dict()
# Build up directory if library save
#self.save = os.path.expanduser("~")
#self.save = os.path.join(self.save,".ocarina")
#self.save = os.path.join(self.save,"library.pickle")
# Load existing library
#if os.path.exists(self.save):
#self.prnt(["Library found, loading..."])
# print "Library found, loading..."
# p = pickle.Unpickler(open(self.save))
# self.data = p.load()
# Begin a scan on dir # Begin a scan on dir
def scan(self,thread,dir): def scan(self,thread,dir):
#self.data = LibData()
#self.data.path = os.path.expanduser(dir)
self.scanning = True self.scanning = True
self.reset()
self.path = os.path.expanduser(dir)
if os.path.exists(self.path) == False:
#print "Directory not found: %s" % dir
return
#print "Scanning: "+self.path
self.traverse("")
self.scanning = False
#print "Found %s files!" % str(self.count)
def reset(self):
self.files = [] self.files = []
self.count = 0 self.count = 0
self.artAlb = dict() self.artAlb = dict()
self.albTrk = dict() self.albTrk = dict()
self.path = ""
self.path = os.path.expanduser(dir)
if os.path.exists(self.path) == False:
#self.prnt(["Directory not found: "+dir])
print "Directory not found: %s" % dir
return
print "Scanning: "+self.path
#self.scanning = True
self.traverse("")
self.scanning = False
#num = len(self.data.files)
#self.prnt(["Found "+str(num)+" files!"])
print "Found %s files!" % str(self.count)
#self.dump()
# Traverse directorys # Traverse directorys
@ -71,11 +54,14 @@ class Library:
tSplit = entry.rsplit('.') tSplit = entry.rsplit('.')
type = tSplit[len(tSplit)-1].lower() type = tSplit[len(tSplit)-1].lower()
if (type in self.goodTypes) == True: if (type in self.goodTypes) == True:
self.add(self.hash(joined),joined) self.count += 1
thread.start_new_thread(self.add,(self.hash(joined),joined,self.count-1))
#self.add(self.hash(joined),joined)
# Hash a file and return list of words # Hash a file and return list of words
def hash(self,file): def hash(self,file):
return
file = file.lower() file = file.lower()
# Only keep letters and numbers # Only keep letters and numbers
words = re.sub('[^a-z0-9]',' ',file).split() words = re.sub('[^a-z0-9]',' ',file).split()
@ -83,12 +69,12 @@ class Library:
# Add song to library # Add song to library
def add(self,words,file): def add(self,words,file,index):
index = len(self.files) self.files+=[SongInfo()]
info = SongInfo() info = self.files[index]
info.filename = os.path.join(self.path,file) info.filename = os.path.join(self.path,file)
self.files += [info.filename]
info.count = 0 info.count = 0
info.id = index
split = info.filename.rsplit(os.sep) split = info.filename.rsplit(os.sep)
max = 3 max = 3
if len(split) < 3: if len(split) < 3:
@ -100,32 +86,6 @@ class Library:
info.album = split[len(split)-2] info.album = split[len(split)-2]
else: else:
info.artist = split[len(split)-3] info.artist = split[len(split)-3]
self.files+=[info]
if (info.artist in self.artAlb.keys()) == False:
self.artAlb[info.artist] = [info.album]
elif (info.album in self.artAlb[info.artist]) == False:
self.artAlb[info.artist] += [info.album]
if (info.album in self.albTrk.keys()) == False:
self.albTrk[info.album] = [info.title]
#elif (info.title in self.albTrk[self.info.album]) == False:
else:
self.albTrk[info.album] += [info.title]
self.count += 1
#print self.count
return
#use later for length
#a = f.audioProperties()
#info.length = a.length
#print file
#print info.artist,",", info.title,",", info.album
#print s1,s2,s3
f = tagpy.FileRef(info.filename) f = tagpy.FileRef(info.filename)
t = f.tag() t = f.tag()
@ -137,16 +97,22 @@ class Library:
info.artist = t.artist info.artist = t.artist
a = f.audioProperties() a = f.audioProperties()
info.length = a.length info.duration = a.length
info.id = len(self.data.files) info.fixTime()
self.data.files+=[info] artist = info.artist.lower()
print info.id album = info.album.lower()
for word in words:
if (word in self.data.map.keys()) == True:
self.data.map[word]+=[index] if (artist in self.artAlb.keys()) == False:
else: self.artAlb[artist] = [album]
self.data.map[word] = [index] elif (album in self.artAlb[artist]) == False:
self.artAlb[artist] += [album]
if ((artist,album) in self.albTrk.keys()) == False:
self.albTrk[(artist,album)] = [index]
else:
self.albTrk[(artist,album)] += [index]
# Dump to file # Dump to file

View File

@ -54,19 +54,19 @@ class Song():
self.getNext(None,None) self.getNext(None,None)
#if self.quit != None: #if self.quit != None:
# self.quit("") # self.quit("")
elif t == gst.MESSAGE_TAG: #elif t == gst.MESSAGE_TAG:
tags = message.parse_tag() # tags = message.parse_tag()
for tag in tags.keys(): # for tag in tags.keys():
self.info.tags[tag] = tags[tag] # self.info.tags[tag] = tags[tag]
if tag=="title": # if tag=="title":
self.info.title = tags[tag] # self.info.title = tags[tag]
elif tag=="album": # elif tag=="album":
self.info.album = tags[tag] # self.info.album = tags[tag]
elif tag=="artist": # elif tag=="artist":
self.info.artist = tags[tag] # self.info.artist = tags[tag]
self.setInfo(tags) # self.setInfo(tags)
#self.taglist = message.parse_tag() #self.taglist = message.parse_tag()
return # return
# Change state to "playing" # Change state to "playing"
@ -95,15 +95,15 @@ class Song():
# Find the duration of the pipeline # Find the duration of the pipeline
def duration(self): #def duration(self):
try: # try:
self.info.length = Duration() # self.info.length = Duration()
length = self.player.query_duration(self.time_format,None)[0] # length = self.player.query_duration(self.time_format,None)[0]
self.total = length # self.total = length
self.info.length.setTime(length) # self.info.length.setTime(length)
return True # return True
except: #except:
return False # return False
#self.length.disp(self.prnt) #self.length.disp(self.prnt)

View File

@ -4,11 +4,34 @@ class SongInfo:
def __init__(self): def __init__(self):
self.id = 0 self.id = 0
self.filename = "" self.filename = ""
#self.tags = dict()
self.count = 0 self.count = 0
#self.banned = False # Length is a string, duration is an int
self.length = None self.length = ""
self.duration = None self.duration = 0
self.title = "" self.title = ""
self.album = "" self.album = ""
self.artist = "" self.artist = ""
def fixTime(self):
time = self.duration
# Find hour
if time >= 3600:
hour = time/3600
time = time - (self.hour * 3600)
if hour > 0:
self.length=str(hour)+":"
# Find minute
if time >= 60:
min = time/60
time = time - (min * 60)
if min < 10:
self.length+="0"
self.length+=str(min)+":"
else:
self.length+="00:"
# Remainder is seconds
sec = time
if sec < 10:
self.length+="0"
self.length+=str(sec)

View File

@ -273,8 +273,8 @@ class Window(gtk.Window):
# This is the dropdown selections # This is the dropdown selections
# Make a new library option # Make a new library option
newLib = MenuItem("New Library",self.selectDir,"ScanLib",self.data.library.scan,None) newLib = MenuItem("New Library",self.selectDir,"ScanLib",self.data.library.scan,None)
#update = MenuItem("Update",self.libview.update,"Update",None,None) delete = MenuItem("Delete Library",self.deleteLib,"Delete",None,None)
library = MenuItem("Library",None,None,None,[newLib]) library = MenuItem("Library",None,None,None,[newLib,delete])
bar.append(library) bar.append(library)
# Replace first 'None' with after track functions # Replace first 'None' with after track functions
@ -287,6 +287,11 @@ class Window(gtk.Window):
self.mainLayout.pack_start(bar,False,False,0) self.mainLayout.pack_start(bar,False,False,0)
def deleteLib(self,widgit,data,other=None):
self.data.library.reset()
self.libview.update()
# Used to select a directory # Used to select a directory
def selectDir(self,widgit,data,func): def selectDir(self,widgit,data,func):
dirsel = gtk.FileChooserDialog(None,action=gtk.FILE_CHOOSER_ACTION_OPEN,buttons =(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) dirsel = gtk.FileChooserDialog(None,action=gtk.FILE_CHOOSER_ACTION_OPEN,buttons =(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
@ -332,6 +337,7 @@ class Window(gtk.Window):
trackview.set_rules_hint(True) trackview.set_rules_hint(True)
trackview.show() trackview.show()
trackscroll = gtk.ScrolledWindow() trackscroll = gtk.ScrolledWindow()
trackscroll.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
trackscroll.add(trackview) trackscroll.add(trackview)
trackscroll.show() trackscroll.show()
self.divider.add2(trackscroll) self.divider.add2(trackscroll)