From adc0a4a6b0f2797dcce1a7f902b81f5010ffb5fe Mon Sep 17 00:00:00 2001 From: Adam Rodger Date: Sun, 16 Jan 2011 15:01:19 +0000 Subject: [PATCH] [#49] Added functionality to skip files based upon regex. Unit tests pass --- tvnamer/config_defaults.py | 4 ++++ tvnamer/main.py | 1 + tvnamer/utils.py | 30 ++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/tvnamer/config_defaults.py b/tvnamer/config_defaults.py index 257907a..7923e4f 100644 --- a/tvnamer/config_defaults.py +++ b/tvnamer/config_defaults.py @@ -34,6 +34,10 @@ # No leading dot, for example: ['avi', 'mkv', 'mp4'] 'valid_extensions': [], + # When non-empty, filter out filenames that match these regexps + # e.g. [".*sample.*"] + 'filename_blacklist': [], + # Force Windows safe filenames (always True on Windows) 'windows_safe_filenames': False, diff --git a/tvnamer/main.py b/tvnamer/main.py index f2a6e7e..fe37007 100644 --- a/tvnamer/main.py +++ b/tvnamer/main.py @@ -219,6 +219,7 @@ def findFiles(paths): cur = FileFinder( cfile, with_extension = Config['valid_extensions'], + filename_blacklist = Config["filename_blacklist"], recursive = Config['recursive']) try: diff --git a/tvnamer/utils.py b/tvnamer/utils.py index 8c37209..d34cedb 100644 --- a/tvnamer/utils.py +++ b/tvnamer/utils.py @@ -103,21 +103,30 @@ class FileFinder(object): The with_extension argument is a list of valid extensions, without leading spaces. If an empty list (or None) is supplied, no extension checking is performed. + + The filename_blacklist argument is a list of regexp strings to match against + the filename (minus the extension). If a match is found, the file is skipped + (e.g. for filtering out "sample" files). If [] or None is supplied, no + filtering is done """ - def __init__(self, path, with_extension = None, recursive = False): + def __init__(self, path, with_extension = None, filename_blacklist = None, recursive = False): self.path = path if with_extension is None: self.with_extension = [] else: self.with_extension = with_extension + if filename_blacklist is None: + self.with_blacklist = [] + else: + self.with_blacklist = filename_blacklist self.recursive = recursive def findFiles(self): """Returns list of files found at path """ if os.path.isfile(self.path): - if self._checkExtension(self.path): + if self._checkExtension(self.path) and not self._blacklistedFilename(self.path): return [os.path.abspath(self.path)] else: return [] @@ -127,6 +136,8 @@ def findFiles(self): raise InvalidPath("%s is not a valid file/directory" % self.path) def _checkExtension(self, fname): + """Checks if the file extension is blacklisted in valid_extensions + """ if len(self.with_extension) == 0: return True @@ -138,6 +149,19 @@ def _checkExtension(self, fname): else: return False + def _blacklistedFilename(self, fname): + """Checks if the filename (excl. ext) matches filename_blacklist + """ + if len(self.with_blacklist) == 0: + return False + + fname, _ = os.path.splitext(fname) + for fblacklist in self.with_blacklist: + if re.match(fblacklist, fname): + return True + else: + return False + def _findFilesInPath(self, startpath): """Finds files from startpath, could be called recursively """ @@ -149,6 +173,8 @@ def _findFilesInPath(self, startpath): for subf in os.listdir(unicode(startpath)): if not self._checkExtension(subf): continue + if self._blacklistedFilename(subf): + continue newpath = os.path.join(startpath, subf) newpath = os.path.abspath(newpath) if os.path.isfile(newpath):