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.
This commit is contained in:
Bas Stottelaar
2014-12-14 11:58:11 +01:00
parent e1bd9eabe7
commit 30be69f08b
5 changed files with 44 additions and 29 deletions

View File

@@ -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'),

View File

@@ -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('_', ' ')

View File

@@ -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:

View File

@@ -14,12 +14,12 @@
# along with Headphones. If not, see <http://www.gnu.org/licenses/>.
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

View File

@@ -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)