def updateDirNodes(self, parent): """ This generator updates the directory nodes, based on whether they should be expandable """ for child in self.tree.iterChildren(parent): # Only directories need to be updated and since they all come first, # we can stop as soon as we find something else if self.tree.getItem(child, ROW_TYPE) != TYPE_DIR: break # Make sure it's readable directory = self.tree.getItem(child, ROW_FULLPATH) hasContent = False if os.access(directory, os.R_OK | os.X_OK): for (file, path) in tools.listDir(directory): if isdir(path) or (isfile(path) and media.isSupported(file)): hasContent = True break # Append/remove children if needed if hasContent and self.tree.getNbChildren(child) == 0: self.tree.appendRow((icons.dirMenuIcon(), '', TYPE_NONE, ''), child) elif not hasContent and self.tree.getNbChildren(child) > 0: self.tree.removeAllChildren(child) yield True if parent is not None: self.stopLoading(parent) yield False
def getDirContents(self, directory): """ Return a tuple of sorted rows (directories, playlists, mediaFiles) for the given directory """ mediaFiles = [] directories = [] for (file, path) in tools.listDir(directory): # Make directory names prettier junk = ['_'] pretty_name = file for item in junk: pretty_name = pretty_name.replace(item, ' ') if isdir(path): directories.append( (icons.dirMenuIcon(), tools.htmlEscape(pretty_name), TYPE_DIR, path)) elif isfile(path): if media.isSupported(file): mediaFiles.append( (icons.mediaFileMenuIcon(), tools.htmlEscape(pretty_name), TYPE_FILE, path)) # Individually sort each type of file by name mediaFiles.sort(key=self._filename) directories.sort(key=self._filename) return (directories, mediaFiles)
def scanPaths(dir_info, name='', tracks=None): if tracks is None: tracks = defaultdict(list) for (subname, subpath) in dir_info: if os.path.isdir(subpath): subname = name + ' / ' + subname if name else subname tracks.update(scanPaths(tools.listDir(subpath), subname, tracks)) elif isSupported(subpath): track = getTrackFromFile(subpath) tracks[name].append(track) return tracks
def preloadTracks(paths): ''' Function for preloading tracks. It is invoked when a dnd action starts and preloads the selected tracks in reverse order, so that the tracks are loaded, when the real loading function comes to them. ''' for path in reversed(paths): if os.path.isdir(path): subpaths = [path for (name, path) in tools.listDir(path)] preloadTracks(subpaths) elif isSupported(path): getTrackFromFile(path)
def getTracks(filenames): """ Same as getTracksFromFiles(), but works for any kind of filenames (files, playlists, directories) """ assert isinstance(filenames, list), 'filenames has to be a list' tracks = TrackDir(flat=True) for path in sorted(filenames): if os.path.isdir(path): dirname = tools.dirname(path) track_dict = scanPaths(tools.listDir(path), name=dirname) for name, track_list in sorted(track_dict.items()): trackdir = TrackDir(name=name) trackdir.tracks = track_list tracks.subdirs.append(trackdir) elif isSupported(path): track = getTrackFromFile(path) tracks.tracks.append(track) return tracks
def getUserCover(self, trackPath): """ Return the path to a cover file in trackPath, None if no cover found """ # Create a dictionary with candidates candidates = {} for (file, path) in tools.listDir(trackPath, True): (name, ext) = os.path.splitext(file.lower()) if ext in ACCEPTED_FILE_FORMATS: candidates[name] = path # Check each possible name using the its index in the list as its priority for name in prefs.get(__name__, 'user-cover-filenames', PREFS_DFT_USER_COVER_FILENAMES): if name in candidates: return candidates[name] if name == '*' and len(candidates) != 0: return next(iter(candidates.values())) return None