Skip to content

Commit

Permalink
[#49] Added functionality to skip files based upon regex. Unit tests …
Browse files Browse the repository at this point in the history
…pass
  • Loading branch information
adamrodger committed Jan 16, 2011
1 parent 8256cfe commit adc0a4a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions tvnamer/config_defaults.py
Expand Up @@ -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,

Expand Down
1 change: 1 addition & 0 deletions tvnamer/main.py
Expand Up @@ -219,6 +219,7 @@ def findFiles(paths):
cur = FileFinder(
cfile,
with_extension = Config['valid_extensions'],
filename_blacklist = Config["filename_blacklist"],
recursive = Config['recursive'])

try:
Expand Down
30 changes: 28 additions & 2 deletions tvnamer/utils.py
Expand Up @@ -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 []
Expand All @@ -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

Expand All @@ -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
"""
Expand All @@ -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):
Expand Down

0 comments on commit adc0a4a

Please sign in to comment.