def playList_Load_M3U(filepath, library): """ load an M3U playlist, the file paths in the the meu file will be converted to a list of Song objects filepath - path to the file to load library - a list of songs already in memory """ dt = DateTime() dt.timer_start() if not os.path.exists(filepath): return [] try: rf = codecs.open(filepath, "r", encoding='utf-8') except: print "cannot open m3u file:\n %s" % filepath return [] bsearch = SongPath_Lookup(library) playlist = [] line = rf.readline() count = 0 while line: try: line = line.strip() if line[0] == '#': line = rf.readline() # load the next line ( a file path?) continue song = bsearch.find(line) #path_to_song(line,library) if song != None: playlist.append(song) except Exception as e: print "\n\n\nerror\n\n\n" print e finally: line = rf.readline() count += 1 rf.close() dt.timer_end() print "Loaded %d songs from m3u container in %s" % ( len(playlist), DateTime.formatTimeDeltams(dt.timedelta)) return playlist
def playList_Save_M3U(filepath, data, typ=0): driveList = systemDriveList() wf = codecs.open(filepath, "w", encoding='utf-8') dt = DateTime() dt.timer_start() for x in range(len(data)): path = data[x][EnumSong.PATH] if typ > 0: #alternate save formats remove the drive path = stripDriveFromPath(driveList, path) wf.write("%s\n" % (unicode(path))) wf.close() dt.timer_end() print "Saved %d songs to m3u container in %s" % ( len(data), DateTime.formatTimeDeltams(dt.timedelta))
def musicSave_LIBZ(filepath, songList, typ=1, block_size=128): """ save a new file, but first compress it using LZMA. file is saved as a binary file. HEADER: several 8 byte frames in a 4:4 byte pattern 4 bytes describing the value in format 'LXXX' 4 byte integer the header ends when the first string 'SIZE' is found Frame 8 byte header, followed by SIZE bytes corresponding to X LZMA compressed song representations. Frame Header: 4 bytes - the word 'SIZE' 4 bytes - unsigned integer, size of frame, excluding frame header Frame Body: SIZE bytes compressed using pyLZMA.compress() each frame will compress HEADERS: LVER - VERSION :outlines if any changes to reading will be needed (future proofing) LTYP - TYPE : bitwise or combination of save settings : 1 - no compression : 2 - remove drie list from start of path ( multi os mode ) LBLK - BLOCK SIZE : maximum number of songs per block, after decompression LCNT - COUNT : count of all songs saved to the file LFMT - SNG FORMAT : number of lines per song record Based off of the following two docstrings. there is no reason to store the parameters used for saving. compress(string, dictionary=23, fastBytes=128, literalContextBits=3, literalPosBits=0, posBits=2, algorithm=2, eos=1, multithreading=1, matchfinder='bt4') Compress the data in string using the given parameters, returning a string containing the compressed data. decompress(data[, maxlength]) Decompress the data, returning a string containing the decompressed data. If the string has been compressed without an EOS marker, you must provide the maximum length as keyword parameter. decompress(data, bufsize[, maxlength]) Decompress the data using an initial output buffer of size bufsize. If the string has been compressed without an EOS marker, you must provide the maximum length as keyword parameter. """ dt = DateTime() dt.timer_start() with open(filepath, "wb") as FILE: FILE.write(struct.pack("4sI", "LVER", 1)) # FILE.write(struct.pack("4sI", "LTYP", typ | 1)) # FILE.write(struct.pack("4sI", "LBLK", block_size)) # number of songs in each block FILE.write(struct.pack("4sI", "LCNT", len(songList))) # FILE.write(struct.pack("4sI", "LFMT", Song.repr_length())) LIBZ_write_songList(FILE, songList, typ, block_size) dt.timer_end() print "Saved %d songs to libz container in %s" % ( len(songList), DateTime.formatTimeDeltams(dt.timedelta))
def musicLoad_LIBZ(filepath): """ load the specified .libz file and return an array of songs. #todo : read 8 bytes # read in : LVERABCD # if A== 0x3D == '=' then file is ascii format # when saving a non binary version save version as '=001' or 3D 30 30 31 # increment the version count for new ascii file formats # for binary formats: # LVERABCD, where A,B,C,D are a little endian 32 bit integer with A=0x00 """ srclib = fileGetName(filepath) if not os.path.exists(filepath): return [] R = [] drivelist = [] cnt = 0 dt = DateTime() dt.timer_start() with open(filepath, "rb") as FILE: # ################################## # read the header header = {} bin = FILE.read(8) key, val = struct.unpack("4sI", bin) while key != "SIZE" and bin: header[key] = val bin = FILE.read(8) if bin: key, val = struct.unpack("4sI", bin) # ################################## # process the header dictionary typ = header.get("LTYP", 0) # needed for restoring file paths blk = header.get("LBLK", 128) # not really needed fmt = header.get( "LFMT", Song.repr_length()) # needed to restore each song record. cnt = header.get("LCNT", 0) if typ & 2 == 2: drivelist = systemDriveList() # ################################## # now read the data from the file. bin = FILE.read(val) while bin: if typ & 1 == 0 and pylzma != None: #compression is only used when typ&1 == 0. bin = pylzma.decompress(bin) R += LIBZ_process_block(bin, typ, fmt, drivelist, srclib) bin = FILE.read(8) if bin: key, size = struct.unpack("4sI", bin) bin = FILE.read(size) # read val bytes from the frame dt.timer_end() print "Loaded %d/%d songs from libz container in %s" % ( len(R), cnt, DateTime.formatTimeDeltams(dt.timedelta)) return R
def musicSave_LIBZ(filepath,songList,typ=1,block_size=128): """ save a new file, but first compress it using LZMA. file is saved as a binary file. HEADER: several 8 byte frames in a 4:4 byte pattern 4 bytes describing the value in format 'LXXX' 4 byte integer the header ends when the first string 'SIZE' is found Frame 8 byte header, followed by SIZE bytes corresponding to X LZMA compressed song representations. Frame Header: 4 bytes - the word 'SIZE' 4 bytes - unsigned integer, size of frame, excluding frame header Frame Body: SIZE bytes compressed using pyLZMA.compress() each frame will compress HEADERS: LVER - VERSION :outlines if any changes to reading will be needed (future proofing) LTYP - TYPE : bitwise or combination of save settings : 1 - no compression : 2 - remove drie list from start of path ( multi os mode ) LBLK - BLOCK SIZE : maximum number of songs per block, after decompression LCNT - COUNT : count of all songs saved to the file LFMT - SNG FORMAT : number of lines per song record Based off of the following two docstrings. there is no reason to store the parameters used for saving. compress(string, dictionary=23, fastBytes=128, literalContextBits=3, literalPosBits=0, posBits=2, algorithm=2, eos=1, multithreading=1, matchfinder='bt4') Compress the data in string using the given parameters, returning a string containing the compressed data. decompress(data[, maxlength]) Decompress the data, returning a string containing the decompressed data. If the string has been compressed without an EOS marker, you must provide the maximum length as keyword parameter. decompress(data, bufsize[, maxlength]) Decompress the data using an initial output buffer of size bufsize. If the string has been compressed without an EOS marker, you must provide the maximum length as keyword parameter. """ dt = DateTime(); dt.timer_start() with open(filepath,"wb") as FILE: FILE.write( struct.pack("4sI","LVER",1) ); # FILE.write( struct.pack("4sI","LTYP",typ|1) ); # FILE.write( struct.pack("4sI","LBLK",block_size) ); # number of songs in each block FILE.write( struct.pack("4sI","LCNT",len(songList)) ); # FILE.write( struct.pack("4sI","LFMT",Song.repr_length() ) ); LIBZ_write_songList(FILE,songList,typ,block_size) dt.timer_end(); print "Saved %d songs to libz container in %s"%( len(songList), DateTime.formatTimeDeltams(dt.timedelta))
def musicLoad_LIBZ(filepath): """ load the specified .libz file and return an array of songs. #todo : read 8 bytes # read in : LVERABCD # if A== 0x3D == '=' then file is ascii format # when saving a non binary version save version as '=001' or 3D 30 30 31 # increment the version count for new ascii file formats # for binary formats: # LVERABCD, where A,B,C,D are a little endian 32 bit integer with A=0x00 """ srclib = fileGetName(filepath) if not os.path.exists(filepath): return []; R=[]; drivelist = []; cnt = 0 dt = DateTime() dt.timer_start(); with open(filepath,"rb") as FILE: # ################################## # read the header header={}; bin = FILE.read(8); key,val = struct.unpack("4sI",bin) while key != "SIZE" and bin: header[key] = val; bin = FILE.read(8); if bin: key,val = struct.unpack("4sI",bin) # ################################## # process the header dictionary typ = header.get("LTYP",0) # needed for restoring file paths blk = header.get("LBLK",128) # not really needed fmt = header.get("LFMT",Song.repr_length()) # needed to restore each song record. cnt = header.get("LCNT",0) if typ&2 == 2: drivelist=systemDriveList(); # ################################## # now read the data from the file. bin = FILE.read(val); while bin: if typ&1 == 0 and pylzma != None: #compression is only used when typ&1 == 0. bin = pylzma.decompress(bin); R += LIBZ_process_block( bin , typ, fmt, drivelist,srclib ); bin = FILE.read(8); if bin: key,size = struct.unpack("4sI",bin) bin = FILE.read(size); # read val bytes from the frame dt.timer_end(); print "Loaded %d/%d songs from libz container in %s"%(len(R),cnt,DateTime.formatTimeDeltams(dt.timedelta)) return R;
def _cpy_files(self): dt = DateTime(); r = len(self.listc) - 1 if r < 0: return True self.parent.emit(SIGNAL("SYNC_SET_RANGE"),self.parent,0,r) self.parent.emit(SIGNAL("SYNC_SET_VALUE"),self.parent,0) # ########################################################### # Copy Songs self.index = 0 rTime = 0 byteAvg = 0 time_remaining=0 stime = None dt.timer_start(); while self.alive and self.index < len(self.listc): free = driveGetFreeSpace(self.dir) MBfree = free[2] if MBfree < 75: self.alive = False print " *** Out Of Free Space. Error." break; createDirStructure(self.listc[self.index][1]) # ---------------------------------------------- s = "Copying %d/%d - %s - %d MB free" p = (self.index,len(self.listc),DateTime.formatTimeDeltams(time_remaining),MBfree) self.parent.emit(SIGNAL("SYNC_SET_TEXT"),self.parent,s%p) # ---------------------------------------------- bytes=0 try: src = self.listc[self.index][0] dst = self.listc[self.index][1] self.parent.debug("src:%s"%src); self.parent.debug("dst:%s"%dst); if os.path.exists(src) == False: self.parent.debug("Cannot Find Source File"); elif os.path.exists(dst) == False: #e32.file_copy(self.listc[self.index][0],self.listc[self.index][1]) bytes = os.path.getsize(self.listc[self.index][0]) #MpTest.fcopy(src,dst) PYCOPY (src, dst) #copy(self.listc[self.index][0],self.listc[self.index][1]) except: self.parent.debug( "*** ERROR: %s"%self.listc[self.index][1] ) # get the end time right before the next update dt.timer_end(); try: if bytes > 0: delta = dt.usdelta #print delta, float(delta)/bytes, bytes/delta delta = float(delta)/float(bytes) if rTime > 0: rTime = (rTime*9 + delta)/10 else: rTime = delta if byteAvg > 0: byteAvg = (byteAvg*9 + bytes)/10 else: byteAvg = bytes time_remaining = byteAvg*rTime #average microseconds per song time_remaining *= (r-self.index) # times the total songs remaining time_remaining /= 1000 # to milliseconds except: pass finally: dt.timer_start(); self.parent.emit(SIGNAL("UPDATE_SYNC_DIALOG"),self.parent,self.index) # get the next start time immediatley after updating. self.index += 1 self.parent.emit(SIGNAL("SYNC_SET_TEXT"),self.parent,"Copying - Done") return;