From 30be69f08b77b5d040a9f9f556ccbbb2da218eb2 Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Sun, 14 Dec 2014 11:58:11 +0100 Subject: [PATCH] Extended filtering directories to files. Only last part of path is matched. Currently, only for library scan files, because the post processor code needs cleanups before filtering files would work. --- headphones/config.py | 3 ++- headphones/helpers.py | 24 ++++++++++++++++++++++++ headphones/librarysync.py | 17 +++++------------ headphones/music_encoder.py | 6 +++--- headphones/postprocessor.py | 23 ++++++++++------------- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/headphones/config.py b/headphones/config.py index 022ee806..ebae5b62 100644 --- a/headphones/config.py +++ b/headphones/config.py @@ -93,7 +93,8 @@ _CONFIG_DEFINITIONS = { 'HTTP_ROOT': (str, 'General', '/'), 'HTTP_USERNAME': (str, 'General', ''), 'IGNORED_WORDS': (str, 'General', ''), - 'IGNORED_FOLDERS': (list, 'Advanced', ''), + 'IGNORED_FOLDERS': (list, 'Advanced', []), + 'IGNORED_FILES': (list, 'Advanced', []), 'INCLUDE_EXTRAS': (int, 'General', 0), 'INTERFACE': (str, 'General', 'default'), 'JOURNAL_MODE': (str, 'Advanced', 'wal'), diff --git a/headphones/helpers.py b/headphones/helpers.py index 57715cf1..f94d015f 100644 --- a/headphones/helpers.py +++ b/headphones/helpers.py @@ -346,6 +346,30 @@ def path_match_patterns(path, patterns): return False +def path_filter_patterns(paths, patterns, root=None): + """ + Scan for ignored paths based on glob patterns. Note that the whole path + will be matched, therefore paths should only contain the relative paths. + + The root is optional, and only used for producing meaningful debug info. + """ + + from headphones import logger + + ignored = 0 + + for path in paths[:]: + if path_match_patterns(path, patterns): + logger.debug("Path ignored by pattern: %s", + os.path.join(root or "", path)) + + ignored += 1 + paths.remove(path) + + # Return number of ignored paths + return ignored + + def extract_data(s): s = s.replace('_', ' ') diff --git a/headphones/librarysync.py b/headphones/librarysync.py index e981e5e4..d80b903a 100644 --- a/headphones/librarysync.py +++ b/headphones/librarysync.py @@ -79,24 +79,17 @@ def libraryScan(dir=None, append=False, ArtistID=None, ArtistName=None, latest_subdirectory = [] for r, d, f in helpers.walk_directory(dir): - # Scan for ignored folders. A copy of the list is taken because the - # original list is modified and list comprehensions don't work because - # of logging. - patterns = headphones.CONFIG.IGNORED_FOLDERS - - for directory in d[:]: - full_path = os.path.join(r, directory) - - if helpers.path_match_patterns(full_path, patterns): - logger.debug("Folder ignored by pattern: %s", full_path) - d.remove(directory) + # Filter paths based on config. Note that these methods work directly + # on the inputs + helpers.path_filter_patterns(d, headphones.CONFIG.IGNORED_FOLDERS, r) + helpers.path_filter_patterns(f, headphones.CONFIG.IGNORED_FILES, r) for files in f: # MEDIA_FORMATS = music file extensions, e.g. mp3, flac, etc if any(files.lower().endswith('.' + x.lower()) for x in headphones.MEDIA_FORMATS): - subdirectory = r.replace(dir, '') latest_subdirectory.append(subdirectory) + if file_count == 0 and r.replace(dir, '') != '': logger.info("[%s] Now scanning subdirectory %s" % (dir.decode(headphones.SYS_ENCODING, 'replace'), subdirectory.decode(headphones.SYS_ENCODING, 'replace'))) elif latest_subdirectory[file_count] != latest_subdirectory[file_count - 1] and file_count != 0: diff --git a/headphones/music_encoder.py b/headphones/music_encoder.py index 286a47e0..c25748f7 100644 --- a/headphones/music_encoder.py +++ b/headphones/music_encoder.py @@ -14,12 +14,12 @@ # along with Headphones. If not, see . import os -import headphones -import shutil import time +import shutil +import subprocess +import headphones import multiprocessing -import subprocess from headphones import logger from beets.mediafile import MediaFile diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index 7fdc0a62..64ac1f13 100755 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -1043,11 +1043,13 @@ def renameUnprocessedFolder(albumpath): def forcePostProcess(dir=None, expand_subfolders=True, album_dir=None): + ignored = 0 + if album_dir: folders = [album_dir.encode(headphones.SYS_ENCODING, 'replace')] - else: download_dirs = [] + if dir: download_dirs.append(dir.encode(headphones.SYS_ENCODING, 'replace')) if headphones.CONFIG.DOWNLOAD_DIR and not dir: @@ -1066,7 +1068,13 @@ def forcePostProcess(dir=None, expand_subfolders=True, album_dir=None): if not os.path.isdir(download_dir): logger.warn('Directory %s does not exist. Skipping', download_dir) continue - for folder in os.listdir(download_dir): + + # Scan for subfolders + subfolders = os.listdir(download_dir) + ignored += helpers.path_filter_patterns(subfolders, + headphones.CONFIG.IGNORED_FOLDERS, root=download_dir) + + for folder in subfolders: path_to_folder = os.path.join(download_dir, folder) if os.path.isdir(path_to_folder): @@ -1077,17 +1085,6 @@ def forcePostProcess(dir=None, expand_subfolders=True, album_dir=None): else: folders.append(path_to_folder) - # Scan for ignored folders. A copy of the list is taken because the original - # list is modified and list comprehensions don't work because of logging. - patterns = headphones.CONFIG.IGNORED_FOLDERS - ignored = 0 - - for folder in folders[:]: - if helpers.path_match_patterns(folder, patterns): - logger.debug("Folder ignored by pattern: %s", folder) - folders.remove(folder) - ignored += 1 - # Log number of folders if folders: logger.debug('Expanded post processing folders: %s', folders)