From 8f322fc34345966318fc34e2b9d8876efcf27581 Mon Sep 17 00:00:00 2001 From: bjschuma Date: Sun, 31 May 2009 20:26:02 +0000 Subject: [PATCH] Made library class, store using pickle git-svn-id: file:///home/anna/Desktop/ocarina-legacy/mithos/ocarina@4 1daee41c-8060-4895-b1f0-2197c00d777a --- trunk/cline.py | 16 +++++++++-- trunk/libdata.py | 7 +++++ trunk/library.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ trunk/ocarina.py | 39 +++++++++++++++----------- trunk/song.py | 11 +++++--- 5 files changed, 123 insertions(+), 23 deletions(-) create mode 100644 trunk/libdata.py create mode 100644 trunk/library.py diff --git a/trunk/cline.py b/trunk/cline.py index 524c3606..1e5fe958 100644 --- a/trunk/cline.py +++ b/trunk/cline.py @@ -64,11 +64,17 @@ class CLine(threading.Thread): # Enter key pressed # Returns true if we keep going def enter(self): - self.input = self.input.strip().lower() + args = self.input.strip().split(' ',1) + self.input = args[0].lower() + input = "" + if len(args) > 1: + input = args[1].strip() + #self.printLine(self.input+"\t"+input) + #self.input = self.input.strip().lower() if self.input=="": return elif self.input in self.cmnds.keys(): - self.cmnds[self.input][0]() + self.cmnds[self.input][0](input) self.advanceLine() self.prompt() self.input = "" @@ -120,7 +126,11 @@ class CLine(threading.Thread): (y,x) = self.stdscr.getyx() self.advanceLine() (y,x) = self.stdscr.getyx() - self.stdscr.addstr(line) + if len(line) > (self.maxSize[1]-1): + self.stdscr.addstr(line[0:(self.maxSize[1]-1)]) + self.printLine(line[self.maxSize[1]-1:len(line)]) + else: + self.stdscr.addstr(line) #self.stdscr.move(y+1,x) self.stdscr.refresh() diff --git a/trunk/libdata.py b/trunk/libdata.py new file mode 100644 index 00000000..f9e3c865 --- /dev/null +++ b/trunk/libdata.py @@ -0,0 +1,7 @@ + +# This class is used to store all library data +class LibData: + def __init__(self): + self.path = "" + self.files = [] + self.map = dict() diff --git a/trunk/library.py b/trunk/library.py new file mode 100644 index 00000000..9b8ecb60 --- /dev/null +++ b/trunk/library.py @@ -0,0 +1,73 @@ +import os +import re +import cPickle as pickle +from libdata import LibData +#from song import Song + +class Library(): + def __init__(self,prnt): + #def __init__(self): + self.prnt = prnt + self.data = LibData() + self.goodTypes = ["ogg","mp3","wma"] + # 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..."]) + p = pickle.Unpickler(open(self.save)) + self.data = p.load() + + + # Begin a scan on dir + def scan(self,dir): + self.data = LibData() + self.data.path = os.path.expanduser(dir) + self.prnt(["Scanning: "+self.data.path]) + self.traverse("") + num = len(self.data.files) + self.prnt(["Found "+str(num)+" files!"]) + self.dump() + + + # Traverse directorys + def traverse(self,dir): + # List and sort contents + contents = os.listdir(os.path.join(self.data.path,dir)) + contents.sort() + for entry in contents: + joined = os.path.join(dir,entry) + full = os.path.join(self.data.path,joined) + # Call traverse on directorys + if os.path.isdir(full): + self.traverse(joined) + # Add music to library + else: + tSplit = entry.rsplit('.') + type = tSplit[len(tSplit)-1].lower() + if (type in self.goodTypes) == True: + self.hash(joined) + + + # Hash a file and add to library + def hash(self,file): + index = len(self.data.files) + self.data.files+=[file] + file = file.lower() + # Only keep letters and numbers + words = re.sub('[^a-z0-9]',' ',file).split() + for word in words: + if (word in self.data.map.keys()) == True: + self.data.map[word]+=[index] + else: + self.data.map[word] = [index] + + + # Dump to file + def dump(self): + out = open(self.save,'w') + p = pickle.Pickler(out,2) + p.dump(self.data) + out.close() diff --git a/trunk/ocarina.py b/trunk/ocarina.py index bef013c5..dee666c6 100644 --- a/trunk/ocarina.py +++ b/trunk/ocarina.py @@ -5,20 +5,21 @@ import thread from song import Song from cline import CLine from duration import Duration +from library import Library #import cmnds class main: def __init__(self,argv): if len(argv) == 0: print "python ocarina.py /path/to/song/" + return self.commands = CLine() self.registerCmnds() - lines = ["test","testing","final test"] - self.commands.printLines(lines) - self.song = Song(argv[0],self.quit,self.commands.printLines) - #self.song.play() + self.song.play() + + self.library = Library(self.commands.printLines) # Start main loop as a thread so we can get bus calls and use command line gobject.threads_init() @@ -26,13 +27,6 @@ class main: thread.start_new_thread(mainloop.run,()) - # Quit program - def quit(self): - self.commands.quit() - print "Quitting..." - sys.exit(0) - - # Register commands for use in command line def registerCmnds(self): self.commands.register("quit",self.quit,"Exit ocarina") @@ -42,24 +36,31 @@ class main: self.commands.register("time",self.time,"Display running time") self.commands.register("info",self.info,"Display detailed info about current song") self.commands.register("this",self.this,"Display basic info about current song") + self.commands.register("lib",self.scanLib,"Create a library based on the directory passed in") + # Quit program + def quit(self,unused): + self.commands.quit() + self.library.dump() + print "Quitting..." + sys.exit(0) # Begin playback - def play(self): + def play(self,unused): self.song.play() # Pause music - def pause(self): + def pause(self,unused): self.song.pause() # Show running time info - def time(self): + def time(self,unused): cur = self.song.curTime() tot = self.song.length self.commands.printLine(cur.toStr()+" / "+tot.toStr()) # Show detailed song info - def info(self): + def info(self,unused): # Return if no tags found if self.song.taglist == None: self.commands.printLine("Could not find any tags") @@ -68,7 +69,7 @@ class main: self.commands.printLine(tag+": "+str(self.song.taglist[tag])) # Show basic song info - def this(self): + def this(self,unused): # Return if no tags found if self.song.taglist == None: self.commands.printLine("Could not find any tags") @@ -79,4 +80,10 @@ class main: self.commands.printLine(field+": "+str(self.song.taglist[field])) + def scanLib(self,dir): + if dir == "": + self.commands.printLine("Please include a library directory") + return + self.library.scan(dir) + if __name__=='__main__':main(sys.argv[1:]) diff --git a/trunk/song.py b/trunk/song.py index 95ee83d3..908c404b 100644 --- a/trunk/song.py +++ b/trunk/song.py @@ -35,13 +35,16 @@ class Song(): t = message.type if t == gst.MESSAGE_EOS: #print "End of stream" - self.prnt("End of stream") - self.quit() + self.prnt(["End of stream"]) + if self.quit != None: + self.quit("") elif t == gst.MESSAGE_ERROR: err, debug = message.parse_error() - self.prnt("Error: %s" % err,debug) + if self.prnt != None: + self.prnt(["Error: "+ str(err) + " " +str(debug)]) #print "Error: %s" % err, debug - self.quit() + if self.quit != None: + self.quit("") elif t == gst.MESSAGE_TAG: self.taglist = message.parse_tag() return