Revert to path mapping from 0.8.3.4.

pull/697/head
Louis Vézina 5 years ago
parent 3ffdb2ef66
commit 78c3a94677

@ -3,138 +3,72 @@ from __future__ import absolute_import
import ast import ast
import os import os
import re import re
import types
import logging import logging
import operator
import libs
import chardet import chardet
from bs4 import UnicodeDammit from bs4 import UnicodeDammit
from collections import OrderedDict
from config import settings from config import settings
from utils import get_sonarr_platform, get_radarr_platform
def sonarr_path_mapping_regex():
global path_mapping
global sonarr_regex
global sonarr_use_path_mapping
path_mapping = ast.literal_eval(settings.general.path_mappings)
path_mapping = sorted(path_mapping, key=operator.itemgetter(0), reverse=True)
path_mapping = OrderedDict((mapping[0], mapping[1]) for mapping in path_mapping if mapping[0] != '')
if any(item for sublist in path_mapping for item in sublist):
sonarr_use_path_mapping = True
sonarr_regex = re.compile("|".join(path_mapping.keys()))
else:
sonarr_use_path_mapping = False
def sonarr_path_mapping_reverse_regex():
global sonarr_platform
global path_mapping_reverse
global sonarr_reverse_regex
global sonarr_use_path_mapping
sonarr_platform = get_sonarr_platform()
path_mapping_reverse = ast.literal_eval(settings.general.path_mappings)
path_mapping_reverse = sorted(path_mapping_reverse, key=operator.itemgetter(0), reverse=True)
path_mapping_reverse = OrderedDict((mapping[1], mapping[0]) for mapping in path_mapping_reverse if mapping[0] != '')
if any(item for sublist in path_mapping_reverse for item in sublist):
sonarr_use_path_mapping = True
sonarr_reverse_regex = re.compile("|".join(map(re.escape, path_mapping_reverse.keys())))
else:
sonarr_use_path_mapping = False
def radarr_path_mapping_regex():
global path_mapping_movie
global radarr_regex
global radarr_use_path_mapping
path_mapping_movie = ast.literal_eval(settings.general.path_mappings_movie)
path_mapping_movie = sorted(path_mapping_movie, key=operator.itemgetter(0), reverse=True)
path_mapping_movie = OrderedDict((mapping[0], mapping[1]) for mapping in path_mapping_movie if mapping[0] != '')
if any(item for sublist in path_mapping_movie for item in sublist):
radarr_use_path_mapping = True
radarr_regex = re.compile("|".join(path_mapping_movie.keys()))
else:
radarr_use_path_mapping = False
def radarr_path_mapping_reverse_regex():
global radarr_platform
global path_mapping_reverse_movie
global radarr_reverse_regex
global radarr_use_path_mapping
radarr_platform = get_sonarr_platform()
path_mapping_reverse_movie = ast.literal_eval(settings.general.path_mappings)
path_mapping_reverse_movie = sorted(path_mapping_reverse_movie, key=operator.itemgetter(0), reverse=True)
path_mapping_reverse_movie = OrderedDict((mapping[1], mapping[0]) for mapping in path_mapping_reverse_movie if mapping[0] != '')
if any(item for sublist in path_mapping_reverse_movie for item in sublist):
radarr_use_path_mapping = True
radarr_reverse_regex = re.compile("|".join(map(re.escape, path_mapping_reverse_movie.keys())))
else:
radarr_use_path_mapping = False
def path_replace(path): def path_replace(path):
if path is None or sonarr_use_path_mapping is False: if path is None:
return path return None
reverted_path = sonarr_regex.sub(lambda match: path_mapping[match.group(0)], path, count=1) for path_mapping in ast.literal_eval(settings.general.path_mappings):
if path_mapping[0] in path:
from os.path import normpath path = path.replace(path_mapping[0], path_mapping[1])
if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path):
return normpath(reverted_path) path = path.replace('/', '\\')
elif path.startswith('/'):
path = path.replace('\\', '/')
break
return path
def path_replace_reverse(path): def path_replace_reverse(path):
if path is None or sonarr_use_path_mapping is False: if path is None:
return path return None
reverted_path_temp = sonarr_reverse_regex.sub(lambda match: path_mapping_reverse[match.group(0)], path, count=1)
if sonarr_platform == 'posix': for path_mapping in ast.literal_eval(settings.general.path_mappings):
from posixpath import normpath if path_mapping[1] in path:
reverted_path = reverted_path_temp.replace('\\', '/') path = path.replace(path_mapping[1], path_mapping[0])
elif sonarr_platform == 'nt': if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path):
from ntpath import normpath path = path.replace('/', '\\')
reverted_path = reverted_path_temp.replace('/', '\\') elif path.startswith('/'):
path = path.replace('\\', '/')
return normpath(reverted_path) break
return path
def path_replace_movie(path): def path_replace_movie(path):
if path is None or radarr_use_path_mapping is False: if path is None:
return path return None
reverted_path = radarr_regex.sub(lambda match: path_mapping_movie[match.group(0)], path, count=1)
from os.path import normpath for path_mapping in ast.literal_eval(settings.general.path_mappings_movie):
if path_mapping[0] in path:
return normpath(reverted_path) path = path.replace(path_mapping[0], path_mapping[1])
if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path):
path = path.replace('/', '\\')
elif path.startswith('/'):
path = path.replace('\\', '/')
break
return path
def path_replace_reverse_movie(path): def path_replace_reverse_movie(path):
if path is None or radarr_use_path_mapping is False: if path is None:
return path return None
reverted_path_temp = radarr_reverse_regex.sub(lambda match: path_mapping_reverse_movie[match.group(0)], path, count=1)
if radarr_platform == 'posix': for path_mapping in ast.literal_eval(settings.general.path_mappings_movie):
from posixpath import normpath if path_mapping[1] in path:
reverted_path = reverted_path_temp.replace('\\', '/') path = path.replace(path_mapping[1], path_mapping[0])
elif radarr_platform == 'nt': if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path):
from ntpath import normpath path = path.replace('/', '\\')
reverted_path = reverted_path_temp.replace('/', '\\') elif path.startswith('/'):
path = path.replace('\\', '/')
return normpath(reverted_path) break
return path
def pp_replace(pp_command, episode, subtitles, language, language_code2, language_code3, forced): def pp_replace(pp_command, episode, subtitles, language, language_code2, language_code3, forced):
@ -203,9 +137,3 @@ def force_unicode(s):
except UnicodeDecodeError: except UnicodeDecodeError:
s = UnicodeDammit(s).unicode_markup s = UnicodeDammit(s).unicode_markup
return s return s
sonarr_path_mapping_regex()
sonarr_path_mapping_reverse_regex()
radarr_path_mapping_regex()
radarr_path_mapping_reverse_regex()

Loading…
Cancel
Save