Refactor naming utils to utils.naming

This commit is contained in:
Labrys of Knossos 2019-01-05 17:22:40 -05:00
commit f042e014b1
2 changed files with 53 additions and 32 deletions

View file

@ -20,6 +20,7 @@ from core import extractor, logger
from core.utils import shutil_custom
from core.utils.download_info import get_download_info, update_download_info_status
from core.utils.links import copy_link, replace_links
from core.utils.naming import clean_file_name, is_sample, sanitize_name
from core.utils.network import find_download, test_connection, wake_on_lan, wake_up
from core.utils.parsers import (
parse_args,
@ -51,32 +52,6 @@ requests.packages.urllib3.disable_warnings()
shutil_custom.monkey_patch()
def sanitize_name(name):
"""
>>> sanitize_name('a/b/c')
'a-b-c'
>>> sanitize_name('abc')
'abc'
>>> sanitize_name('a"b')
'ab'
>>> sanitize_name('.a.b..')
'a.b'
"""
# remove bad chars from the filename
name = re.sub(r'[\\/*]', '-', name)
name = re.sub(r'[:\'<>|?]', '', name)
# remove leading/trailing periods and spaces
name = name.strip(' .')
try:
name = name.encode(core.SYS_ENCODING)
except Exception:
pass
return name
def category_search(input_directory, input_name, input_category, root, categories):
tordir = False
@ -188,12 +163,6 @@ def is_min_size(input_name, min_size):
return True
def is_sample(input_name):
# Ignore 'sample' in files
if re.search('(^|[\\W_])sample\\d*[\\W_]', input_name.lower()):
return True
def flatten(output_destination):
logger.info('FLATTEN: Flattening directory: {0}'.format(output_destination))
for outputFile in list_media_files(output_destination):

52
core/utils/naming.py Normal file
View file

@ -0,0 +1,52 @@
import re
import core
def sanitize_name(name):
"""
>>> sanitize_name('a/b/c')
'a-b-c'
>>> sanitize_name('abc')
'abc'
>>> sanitize_name('a"b')
'ab'
>>> sanitize_name('.a.b..')
'a.b'
"""
# remove bad chars from the filename
name = re.sub(r'[\\/*]', '-', name)
name = re.sub(r'[:\'<>|?]', '', name)
# remove leading/trailing periods and spaces
name = name.strip(' .')
try:
name = name.encode(core.SYS_ENCODING)
except Exception:
pass
return name
def clean_file_name(filename):
"""Cleans up nzb name by removing any . and _
characters, along with any trailing hyphens.
Is basically equivalent to replacing all _ and . with a
space, but handles decimal numbers in string, for example:
"""
filename = re.sub(r'(\D)\.(?!\s)(\D)', r'\1 \2', filename)
filename = re.sub(r'(\d)\.(\d{4})', r'\1 \2', filename) # if it ends in a year then don't keep the dot
filename = re.sub(r'(\D)\.(?!\s)', r'\1 ', filename)
filename = re.sub(r'\.(?!\s)(\D)', r' \1', filename)
filename = filename.replace('_', ' ')
filename = re.sub('-$', '', filename)
filename = re.sub(r'^\[.*]', '', filename)
return filename.strip()
def is_sample(input_name):
# Ignore 'sample' in files
if re.search('(^|[\\W_])sample\\d*[\\W_]', input_name.lower()):
return True