import gobject import os import sys import thread from song import Song from cline import CLine from duration import Duration from library import Library from playlist import Playlist from songInfo import SongInfo #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() self.library = Library(self.commands.printLines) self.plist = Playlist(self.commands.printLines) self.plist.insert(self.library.nonBanned()) self.song = None # If we were given a song as input, check that it exists and begin playback if len(argv) > 0: split = argv[0].split(self.library.data.path) if len(split) > 0: index = self.library.has(split[len(split)-1]) #if index != -1: #info = self.library.data.files[index] self.plist.queueSong(index) if index==-1: file = os.path.expanduser(argv[0]) if os.path.exists(file): info = SongInfo() info.filename = file self.song = Song(info,self.next,self.commands.printLines) self.next("") # Start main loop as a thread so we can get bus calls and use command line gobject.threads_init() mainloop = gobject.MainLoop() thread.start_new_thread(mainloop.run,()) # Register commands for use in command line def registerCmnds(self): self.commands.register("quit",self.quit,"Exit ocarina") self.commands.register("exit",self.quit,"Exit ocarina") self.commands.register("play",self.play,"Play current song") self.commands.register("pause",self.pause,"Pause current song") 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") self.commands.register("next",self.next,"Advance to the next song") self.commands.register("random",self.random,"Toggle shuffle") # Quit program def quit(self,unused): self.commands.quit() self.library.dump() print "Quitting..." sys.exit(0) # Begin playback def play(self,unused): if self.song == None: return self.song.play() # Pause music def pause(self,unused): if self.song == None: return self.song.pause() # Show running time info def time(self,unused): if self.song == None: return cur = self.song.curTime() tot = self.song.info.length self.commands.printLine(cur.toStr()+" / "+tot.toStr()) # Show detailed song info def info(self,unused): # Return if no song found if self.song == None: return # Return if no tags found if self.song.info.tags == None: self.commands.printLine("Could not find any tags") return for tag in self.song.info.tags.keys(): self.commands.printLine(tag+": "+str(self.song.info.tags[tag])) self.commands.printLine(self.song.info.filename) # Show basic song info def this(self,unused): # Return if no song found if self.song == None: return # Return if no tags found if self.song.info.tags == None: self.commands.printLine("Could not find any tags") return fields = ["title","artist","track-number","track-count","album"] for field in fields: if (field in self.song.info.tags.keys()) == True: self.commands.printLine(field+": "+str(self.song.info.tags[field])) def scanLib(self,dir): if dir == "": self.commands.printLine("Please include a library directory") return self.library.scan(dir) def next(self,unused): if self.song != None: self.song.close() index = self.plist.next() if index > -1: self.song = None info = self.library.data.files[index] self.song = Song(info,self.next,self.commands.printLines) if index > -2: self.song.play() def random(self,unused): self.plist.random = not self.plist.random if __name__=='__main__':main(sys.argv[1:])