Skip to content

Commit

Permalink
Rename filter_filename to sanitize_filename (#77)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
IrishPrime committed May 4, 2020
1 parent 9fc80fa commit f38e2a1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion nielsen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
28 changes: 12 additions & 16 deletions nielsen/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand All @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit f38e2a1

Please sign in to comment.