diff options
author | jerous <jerous@gmail.com> | 2008-08-22 01:39:36 +0200 |
---|---|---|
committer | jerous <jerous@gmail.com> | 2008-08-22 01:41:23 +0200 |
commit | 59cccbf6289a63c06a51662aeecf9af6168804bb (patch) | |
tree | aebeedb520f07301b012e783724bce015c4e58cd /jmpd.py | |
parent | f51b1696adcbaffa484f0eaa0abdd407c412e58e (diff) |
create library
saving and loading of library (with unicode)
Diffstat (limited to 'jmpd.py')
-rw-r--r-- | jmpd.py | 191 |
1 files changed, 191 insertions, 0 deletions
@@ -0,0 +1,191 @@ +import socket +from thread import start_new_thread +import codecs +import os + +from mutagen.easyid3 import EasyID3 + +from clSettings import Settings + +MUSIC_DIRECTORY="music_directory" +ENCODING="UTF-8" + +class server: + ssocket=None + + def start(self, type=socket.SOCK_STREAM): + self.ssocket=socket.socket(socket.AF_INET, type) + start_new_thread(self._start_server, ()) + + def _start_server(self): + self.ssocket.bind(('localhost', 6600)) + self.ssocket.listen(5) + self.ssocket.settimeout(None) + + while True: + csock,caddr=self.ssocket.accept() + start_new_thread(self.handle, (csock, caddr)) + + def read(file): + return file.readline().strip() + def write(file, line): + file.write(line) + + def handle(self, csocket, caddr): + print("Incoming connection from ".caddr) + file=csocket.makefile('rw', 0) + self.read(file) + +def _getName(line): + return line[0:line.find(': ')] +def _getValue(line): + return line[line.find(': ')+2:] + +tags=['title', 'artist', 'album', 'tracknumber', 'genre'] +class jmpd: + _lib=None # library + _settings=None + + def __init__(self): + self._lib=[] + self._settings=Settings("mpdconf") + self.readLib() + print self._lib + + def writeLib(self): + all_tags=tags + encoding=ENCODING + tags.append('filename') + + fout=codecs.open(self._settings.get("db_file"), mode='w', encoding=encoding) + fout.write("# jmpd\n") + fout.write("begin info\n") + fout.write("encoding: %s\n" % (encoding)) + fout.write("jmpd: 0.1\n") + fout.write("end info\n") + fout.write("\n") + + fout.write("begin library\n") + cur_path="" + for entry in self._lib: + if entry['path']!=cur_path: + fout.write("set path: %s\n" % entry['path']) + cur_path=entry['path'] + + fout.write("begin file\n") + for tag in all_tags: + if tag in entry: + fout.write("%s: %s\n" % (tag, entry[tag])) + fout.write("end file\n") + + fout.write("end library\n") + fout.close() + + def readLib(self): + """Restore the library from file""" + db_file=self._settings.get("db_file") + encoding=ENCODING + if os.path.exists(db_file)==False: + self._lib=[] + return + + # we first open the file in ASCII-mode, and read on till the real encoding + # then we reopen it in that encoding + fin=open(db_file, 'r') + fin.close() + fin=codecs.open(db_file, mode='r', encoding=encoding) + + self._readLine(fin) # begin info + encoding=_getValue(self._readLine(fin)) + version=_getValue(self._readLine(fin)) + if version!="0.1": + raise Exception("Invalid database file") + self._readNext(fin, 'end info') + + self._readNext(fin, 'begin library') + cur_path="" + while True: + line=self._readLine(fin) + if line=="end library": + break + if _getName(line)=="set path: ": + cur_path=_getValue(line) + elif line=="begin file": + self._readFile(fin, cur_path, encoding) + + fin.close() + + def _readFile(self, fin, cur_path, encoding): + """Read the data for a file""" + entry={'filepath': cur_path} + line="" + while line!="end file": + line=self._readLine(fin) + if line.find(': ')>0: + entry[_getName(line)]=_getValue(line) + + self._lib.append(entry) + def _readEnd(self, fin, section): + """Read the next line containing an end section""" + line="" + while line!="end %s"%(section): + line=self._readLine(fin) + return line + def _readBegin(self, fin, section): + """Read the next line containing a begin section""" + line="" + while line!="begin %s"%(section): + line=self._readLine(fin) + return line + + def _readNext(self, fin, find): + line="" + while line!=find: + line=self._readLine(fin) + return line + def _readLine(self, fin): + """Reads the next line having characters and not being a commented line""" + ret="\n" + try: + while ret=="\n" or ret[0]=='#': + ret=fin.readline() + except: + pass + if len(ret)==0: + raise Exception("EOF") + return ret.strip() + + + def update(self): + """Look for songs in the music_directory""" + self._lib=[] + for dir in self._settings.get(MUSIC_DIRECTORY).split(":"): + self._update_rec(dir) + + self.writeLib() + + def _update_rec(self, path): + subdirs=[] + for filename in os.listdir(path): + filepath="%s/%s"%(path,filename) + if os.path.isdir(filepath): + subdirs.append(filepath) + if filename[-4:]==".mp3": + audio=EasyID3(filepath) + entry = {'path': path.decode(ENCODING) + , 'filename': filename.decode(ENCODING) + } + for tag in tags: + try: + value=str(audio[tag]) + entry[tag]=value[3:-2] + except: + entry[tag]='' + self._lib.append(entry) + + for subdir in subdirs: + self._update_rec(subdir) + +cl=jmpd() +cl.update() +print cl._lib |