def getFileInfo(self, name, pathname): try: size = os.path.getsize(pathname) info = metadata.detect(pathname) if info: # Sometimes the duration (time) or the bitrate of the file is unknown if info["time"] is None or info["bitrate"] is None: fileinfo = (name, size, None, None) else: bitrateinfo = (int(info["bitrate"]), int(info["vbr"])) fileinfo = (name, size, bitrateinfo, int(info["time"])) else: fileinfo = (name, size, None, None) return fileinfo except Exception as errtuple: message = _("Scanning File Error: %(error)s Path: %(path)s") % { 'error': errtuple, 'path': pathname } self.logMessage(message) displayTraceback(sys.exc_info()[2])
def getFileInfo(self, name, pathname): try: audio = None size = os.stat(pathname).st_size if size > 0: try: audio = taglib.File(pathname) except IOError: pass if audio is not None: bitrateinfo = ( int(audio.bitrate), int(False) ) # Second argument used to be VBR (variable bitrate) fileinfo = (name, size, bitrateinfo, int(audio.length)) else: fileinfo = (name, size, None, None) return fileinfo except Exception as errtuple: message = _("Error while scanning file %(path)s: %(error)s") % { 'path': pathname, 'error': errtuple } self.logMessage(message) displayTraceback(sys.exc_info()[2])
def SetDirectory(self, directory): self.selected_folder = directory self.FileStore.clear() self.files.clear() found_dir = False for d, f in self.shares: if d == directory: found_dir = True files = f break if not found_dir: return for file in files: # DecodedFilename, HSize, Bitrate, HLength, Size, Length, RawFilename rl = 0 try: size = int(file[2]) except ValueError: size = 0 f = [self.decode(file[1]), Humanize(size)] if file[3] == "": f += ["", ""] else: # file[4] is for file types such as 'mp3' attrs = file[4] if attrs != [] and type(attrs) is list: if len(attrs) >= 3: br = str(attrs[0]) if attrs[2]: br = br + " (vbr)" try: rl = int(attrs[1]) except ValueError: rl = 0 l = "%i:%02i" % (rl / 60, rl % 60) f += [br, l] else: f += ["", ""] else: f += ["", ""] f += [int(size), rl, file[1]] try: self.files[f[0]] = self.FileStore.append(f) except Exception as error: displayTraceback()
def SetDirectory(self, directory): self.selected_folder = directory self.FileStore.clear() self.files.clear() found_dir = False for d, f in self.shares: if d == directory: found_dir = True files = f break if not found_dir: return for file in files: # DecodedFilename, HSize, Bitrate, HLength, Size, Length, RawFilename rl = 0 try: size = int(file[2]) except ValueError: size = 0 f = [self.decode(file[1]), Humanize(size)] if file[3] == "": f += ["", ""] else: # file[4] is for file types such as 'mp3' attrs = file[4] if attrs != [] and type(attrs) is list: if len(attrs) >= 3: br = str(attrs[0]) if attrs[2]: br = br + " (vbr)" try: rl = int(attrs[1]) except ValueError: rl = 0 l = "%i:%02i" % (rl / 60, rl % 60) f += [br, l] else: f += ["", ""] else: f += ["", ""] f += [long(size), rl, file[1]] try: self.files[f[0]] = self.FileStore.append(f) except Exception, error: displayTraceback()
def SetDirectory(self, path): self.selected_folder = path self.FileStore.clear() self.files.clear() node = self.DirStore.on_get_iter(path) if node == None or node == (0,0): return files = self.DirStore.GetData(node) for file in files: # DecodedFilename, HSize, Bitrate, HLength, Size, Length, RawFilename rl = 0 try: size = int(file[2]) except ValueError: size = 0 f = [self.decode(file[1]), Humanize(size)] if file[3] == "": f += ["", ""] else: #file[4] is for file types such as 'mp3' attrs = file[4] if attrs != [] and type(attrs) is list: if len(attrs) >= 3: br = str(attrs[0]) if len(attrs) > 2 and attrs[2]: br = br + _(" (vbr)") try: rl = int(attrs[1]) except ValueError: rl = 0 l = "%i:%02i" % (rl / 60, rl % 60) f += [br, l] else: f += ["", ""] else: f += ["", ""] f += [long(size), rl, file[1]] try: self.files[f[0]] = self.FileStore.append(f) except Exception, error: displayTraceback()
def SetDirectory(self, directory): self.selected_folder = directory self.FileStore.clear() self.files.clear() found_dir = False for d, f in self.shares: if d == directory: found_dir = True files = f break if not found_dir: return for file in files: # Filename, HSize, Bitrate, HLength, Size, Length, RawFilename rl = 0 try: size = int(file[2]) # Some clients send incorrect file sizes if size < 0 or size > maxsize: size = 0 except ValueError: size = 0 f = [file[1], HumanSize(size)] h_bitrate, bitrate, h_length = GetResultBitrateLength( size, file[4]) f += [h_bitrate, h_length] f += [int(size), rl, file[1]] try: self.files[f[0]] = self.FileStore.append(f) except Exception: displayTraceback()
def getFilesListUnicode(self, mtimes, oldmtimes, oldlist, yieldcall=None, progress=None, rebuild=False): """ Get a list of files with their filelength, bitrate and track length in seconds """ list = {} count = 0 for directory in mtimes: directory = os.path.expanduser(directory) virtualdir = self.real2virtual(directory) count += 1 if progress: percent = float(count) / len(mtimes) if percent <= 1.0: gobject.idle_add(progress.set_fraction, percent) # force Unicode for reading from disk u_directory = "%s" % directory str_directory = str(directory) # noqa: F841 if self.hiddenCheck({'dir': directory}): continue if directory in oldmtimes and directory not in oldlist: # Partial information, happened with unicode paths that N+ couldn't handle properly del oldmtimes[directory] if not rebuild and directory in oldmtimes: if mtimes[directory] == oldmtimes[directory]: if os.path.exists(directory): try: list[virtualdir] = oldlist[virtualdir] continue except KeyError: log.addwarning( _("Inconsistent cache for '%(vdir)s', rebuilding '%(dir)s'" ) % { 'vdir': virtualdir, 'dir': directory }) else: log.adddebug( _("Dropping missing directory %(dir)s") % {'dir': directory}) continue list[virtualdir] = [] try: contents = os.listdir(u_directory) except OSError as errtuple: print(str(errtuple)) self.logMessage(str(errtuple)) continue contents.sort() for filename in contents: if self.hiddenCheck({'dir': directory, 'file': filename}): continue path = os.path.join(directory, filename) s_path = str(path) ppath = str(path) s_filename = str(filename) try: # try to force Unicode for reading from disk isfile = os.path.isfile(ppath) except OSError as errtuple: message = _("Scanning Error: %(error)s Path: %(path)s") % { 'error': errtuple, 'path': path } print(str(message)) self.logMessage(message) displayTraceback(sys.exc_info()[2]) continue else: if isfile: # Get the metadata of the file via mutagen data = self.getFileInfoUnicode(s_filename, s_path) if data is not None: list[virtualdir].append(data) if yieldcall is not None: yieldcall() return list
def getDirsMtimesUnicode(self, dirs, yieldcall=None): list = {} for directory in dirs: directory = os.path.expanduser(directory.replace("//", "/")) u_directory = "%s" % directory str_directory = str(directory) if self.hiddenCheck({'dir': directory}): continue try: contents = dircache.listdir(u_directory) mtime = os.path.getmtime(u_directory) except OSError as errtuple: message = _( "Scanning Directory Error: %(error)s Path: %(path)s") % { 'error': errtuple, 'path': u_directory } print(str(message)) self.logMessage(message) displayTraceback(sys.exc_info()[2]) continue contents.sort() list[str_directory] = mtime for filename in contents: path = os.path.join(directory, filename) # force Unicode for reading from disk in win32 u_path = "%s" % path s_path = str(path) try: isdir = os.path.isdir(u_path) except OSError as errtuple: message = _("Scanning Error: %(error)s Path: %(path)s") % { 'error': errtuple, 'path': u_path } print(str(message)) self.logMessage(message) continue try: mtime = os.path.getmtime(u_path) except OSError as errtuple: # noqa: F841 try: mtime = os.path.getmtime(s_path) except OSError as errtuple: message = _( "Scanning Error: %(error)s Path: %(path)s") % { 'error': errtuple, 'path': u_path } print(str(message)) self.logMessage(message) continue else: if isdir: list[s_path] = mtime dircontents = self.getDirsMtimesUnicode([path]) for k in dircontents: list[k] = dircontents[k] if yieldcall is not None: yieldcall() return list
def getDirsMtimes(self, dirs, yieldcall=None): list = {} for directory in dirs: directory = os.path.expanduser(directory.replace("//", "/")) if self.hiddenCheck({'dir': directory}): continue try: contents = dircache.listdir(directory) mtime = os.path.getmtime(directory) except OSError as errtuple: message = _( "Scanning Directory Error: %(error)s Path: %(path)s") % { 'error': errtuple, 'path': directory } print(str(message)) self.logMessage(message) displayTraceback(sys.exc_info()[2]) continue contents.sort() list[directory] = mtime for filename in contents: path = os.path.join(directory, filename) try: isdir = os.path.isdir(path) except OSError as errtuple: message = _("Scanning Error: %(error)s Path: %(path)s") % { 'error': errtuple, 'path': path } print(str(message)) self.logMessage(message) continue try: mtime = os.path.getmtime(path) except OSError as errtuple: islink = False try: islink = os.path.islink(path) except OSError as errtuple2: print(errtuple2) if islink: message = _( "Scanning Error: Broken link to directory: \"%(link)s\" from Path: \"%(path)s\". Repair or remove this link." ) % { 'link': os.readlink(path), 'path': path } else: message = _( "Scanning Error: %(error)s Path: %(path)s") % { 'error': errtuple, 'path': path } print(str(message)) self.logMessage(message) continue else: if isdir: list[path] = mtime dircontents = self.getDirsMtimes([path]) for k in dircontents: list[k] = dircontents[k] if yieldcall is not None: yieldcall() return list