From f38e2a1cf1f70b122ca050a7644a68ddf0d1dea8 Mon Sep 17 00:00:00 2001 From: "Michael \"Irish\" O'Neill" Date: Mon, 4 May 2020 00:58:49 -0400 Subject: [PATCH] Rename `filter_filename` to `sanitize_filename` (#77) Rename 'filter_filename` to `sanitize_filename` and add a `system_type` parameter to change which OS type it will sanitize the filename for. By default, this is set to `os.name` and supports `posix` and `nt`. Streamline the behavior of `filter_series`. This function now returns the given string in title case unless a custom name is found. Update list of supported Python versions to a minimum of Python 3.6 (which is required for f-strings). Bump version to `2.1.3`. Resolves #72. --- nielsen/__init__.py | 2 +- nielsen/api.py | 28 ++++++++++++---------------- setup.py | 6 ++---- test.py | 4 ++-- 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/nielsen/__init__.py b/nielsen/__init__.py index 41c4be6..3d59023 100644 --- a/nielsen/__init__.py +++ b/nielsen/__init__.py @@ -4,11 +4,11 @@ ''' from nielsen.api import ( - filter_filename, filter_series, get_file_info, organize_file, process_file, + sanitize_filename, ) from nielsen.config import ( diff --git a/nielsen/api.py b/nielsen/api.py index dadcd90..56d2e5d 100644 --- a/nielsen/api.py +++ b/nielsen/api.py @@ -126,17 +126,9 @@ def filter_series(series): Its Always Sunny In Philadelphia = It's Always Sunny in Philadelphia Marvel's Agents of S.H.I.E.L.D. = Agents of S.H.I.E.L.D. Mr Robot = Mr. Robot + If no filter is found, return the original string in title case. ''' - if CONFIG.has_option('Filters', series): - # Return configured name if found - return CONFIG.get('Filters', series) - - if series.islower(): - # Use title case if everything is lowercase - return series.title() - - # Return original input if nothing to do - return series + return CONFIG.get('Filters', series, fallback=series.title()) def process_file(filename): @@ -161,7 +153,7 @@ def process_file(filename): # Define a format for the renamed file form = "{series} -{season}.{episode}- {title}.{extension}" # Generate the new filename - name = filter_filename(form.format(**info)) + name = sanitize_filename(form.format(**info)) logging.info("Rename to: '%s'", name) # Create a PurePath object to reference the renamed file to check for @@ -186,12 +178,16 @@ def process_file(filename): return file -def filter_filename(filename): - '''Replace invalid characters in a filename with hyphens. The set of - invalid characters is determined by the operating system.''' - if os_name == 'posix': +def sanitize_filename(filename, system_type=os_name): + '''Replace characters which are invalid for a file basename in the string + `filename` with hyphens. The set of invalid characters is determined by the + operating system. For example, an episode title of `AC/DC` would cause the + operating system to treat everything before the forward-slash as another + directory. This function returns the same string as provided, but with an + episode title token of `AC-DC`.''' + if system_type == 'posix': invalid_chars = re.compile('[/\0]') - elif os_name == 'nt': + elif system_type == 'nt': invalid_chars = re.compile('[/\\?%*:|"<>]') else: logging.warning('OS not recognized: %s', os_name) diff --git a/setup.py b/setup.py index 68d6fa1..3276fb3 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name='Nielsen', - version='2.1.1', + version='2.1.3', author="Michael 'Irish' O'Neill", author_email="irish.dot@gmail.com", url='https://github.com/IrishPrime/nielsen/', @@ -20,11 +20,9 @@ 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', 'Operating System :: Microsoft :: Windows', 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python :: 3.3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', 'Programming Language :: Python', 'Topic :: Desktop Environment :: File Managers', 'Topic :: Multimedia :: Video', diff --git a/test.py b/test.py index 44f4fe7..4243110 100755 --- a/test.py +++ b/test.py @@ -240,10 +240,10 @@ def test_filter_series(self): self.assertEqual(nielsen.filter_series("The Flash 2014"), "The Flash") - def test_filter_filename(self): + def test_sanitize_filename(self): '''Test removing invalid characters from filenames.''' # Invalid characters for POSIX systems - self.assertEqual(nielsen.filter_filename( + self.assertEqual(nielsen.sanitize_filename( 'Brooklyn Nine-Nine -02.20- AC/DC.mp4'), 'Brooklyn Nine-Nine -02.20- AC-DC.mp4')