From 78c3a9467739f54a3d05f47277bf7111512363b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Wed, 4 Dec 2019 19:42:44 -0500 Subject: [PATCH] Revert to path mapping from 0.8.3.4. --- bazarr/helper.py | 160 +++++++++++++---------------------------------- 1 file changed, 44 insertions(+), 116 deletions(-) diff --git a/bazarr/helper.py b/bazarr/helper.py index a16645127..6f0e09ab0 100644 --- a/bazarr/helper.py +++ b/bazarr/helper.py @@ -3,138 +3,72 @@ from __future__ import absolute_import import ast import os import re -import types import logging -import operator - -import libs import chardet from bs4 import UnicodeDammit -from collections import OrderedDict 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): - if path is None or sonarr_use_path_mapping is False: - return path + if path is None: + return None - reverted_path = sonarr_regex.sub(lambda match: path_mapping[match.group(0)], path, count=1) - - from os.path import normpath - - return normpath(reverted_path) + for path_mapping in ast.literal_eval(settings.general.path_mappings): + if path_mapping[0] in 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(path): - if path is None or sonarr_use_path_mapping is False: - return path - - reverted_path_temp = sonarr_reverse_regex.sub(lambda match: path_mapping_reverse[match.group(0)], path, count=1) + if path is None: + return None - if sonarr_platform == 'posix': - from posixpath import normpath - reverted_path = reverted_path_temp.replace('\\', '/') - elif sonarr_platform == 'nt': - from ntpath import normpath - reverted_path = reverted_path_temp.replace('/', '\\') - - return normpath(reverted_path) + for path_mapping in ast.literal_eval(settings.general.path_mappings): + if path_mapping[1] in path: + path = path.replace(path_mapping[1], path_mapping[0]) + 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_movie(path): - if path is None or radarr_use_path_mapping is False: - return path - - reverted_path = radarr_regex.sub(lambda match: path_mapping_movie[match.group(0)], path, count=1) + if path is None: + return None - from os.path import normpath - - return normpath(reverted_path) + for path_mapping in ast.literal_eval(settings.general.path_mappings_movie): + if path_mapping[0] in 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): - if path is None or radarr_use_path_mapping is False: - return path - - reverted_path_temp = radarr_reverse_regex.sub(lambda match: path_mapping_reverse_movie[match.group(0)], path, count=1) + if path is None: + return None - if radarr_platform == 'posix': - from posixpath import normpath - reverted_path = reverted_path_temp.replace('\\', '/') - elif radarr_platform == 'nt': - from ntpath import normpath - reverted_path = reverted_path_temp.replace('/', '\\') - - return normpath(reverted_path) + for path_mapping in ast.literal_eval(settings.general.path_mappings_movie): + if path_mapping[1] in path: + path = path.replace(path_mapping[1], path_mapping[0]) + if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path): + path = path.replace('/', '\\') + elif path.startswith('/'): + path = path.replace('\\', '/') + break + return path def pp_replace(pp_command, episode, subtitles, language, language_code2, language_code3, forced): @@ -203,9 +137,3 @@ def force_unicode(s): except UnicodeDecodeError: s = UnicodeDammit(s).unicode_markup return s - - -sonarr_path_mapping_regex() -sonarr_path_mapping_reverse_regex() -radarr_path_mapping_regex() -radarr_path_mapping_reverse_regex()