From ead1f31eb1a46a0f3092886de8363c65988ea124 Mon Sep 17 00:00:00 2001 From: Halali Date: Fri, 14 Dec 2018 23:18:52 +0100 Subject: [PATCH] Preparation for new Config code --- bazarr/config.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ bazarr/helper.py | 64 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 bazarr/config.py create mode 100644 bazarr/helper.py diff --git a/bazarr/config.py b/bazarr/config.py new file mode 100644 index 000000000..b39689d6a --- /dev/null +++ b/bazarr/config.py @@ -0,0 +1,78 @@ +# coding=utf-8 +import os +import configparser + +from get_argv import config_dir + + +class _ConfigSection(object): + """Hold settings for a section of the configuration file.""" + + def __getattr__(self, attr): + if attr in self.__dict__: + return self.__dict__[attr] + else: + raise AttributeError('No "%s" setting in section.' % attr) + + def __getitem__(self, item): + return getattr(self, item) + + def __repr__(self): + return str(tuple(self.__dict__.keys())) + + +def read(config_file): + """Read settings from file and return a dot notation object.""" + config = configparser.ConfigParser() + config.read(unicode(config_file)) + + dotini = _ConfigSection() + + for section in config.sections(): + + s = _ConfigSection() + for key, value in config.items(section): + if value == "True": + value = True + elif value == "False": + value = False + setattr(s, key, value) + + setattr(dotini, section, s) + + return dotini + + +settings = read(os.path.join(os.path.join(config_dir, 'config', 'config.ini'))) + +# sonarr url +if settings.sonarr.ssl: + protocol_sonarr = "https" +else: + protocol_sonarr = "http" + +if settings.sonarr.base_url == '': + settings.sonarr.base_url = "/" +if not settings.sonarr.base_url.startswith("/"): + settings.sonarr.base_url = "/" + settings.sonarr.base_url +if settings.sonarr.base_url.endswith("/"): + settings.sonarr.base_url = settings.sonarr.base_url[:-1] + +url_sonarr = protocol_sonarr + "://" + settings.sonarr.ip + ":" + settings.sonarr.port + settings.sonarr.base_url +url_sonarr_short = protocol_sonarr + "://" + settings.sonarr.ip + ":" + settings.sonarr.port + +# radarr url +if settings.radarr.ssl: + protocol_radarr = "https" +else: + protocol_radarr = "http" + +if settings.radarr.base_url == '': + settings.radarr.base_url = "/" +if not settings.radarr.base_url.startswith("/"): + settings.radarr.base_url = "/" + settings.radarr.base_url +if settings.radarr.base_url.endswith("/"): + settings.radarr.base_url = settings.radarr.base_url[:-1] + +url_radarr = protocol_radarr + "://" + settings.radarr.ip + ":" + settings.radarr.port + settings.radarr.base_url +url_radarr_short = protocol_radarr + "://" + settings.radarr.ip + ":" + settings.radarr.port diff --git a/bazarr/helper.py b/bazarr/helper.py new file mode 100644 index 000000000..f0c6babbc --- /dev/null +++ b/bazarr/helper.py @@ -0,0 +1,64 @@ +# coding=utf-8 +import os +import re + +from config import settings + + +def path_replace(path): + for path_mapping in 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): + for path_mapping in 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): + for path_mapping in 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): + for path_mapping in 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): + pp_command = pp_command.replace('{{directory}}', os.path.dirname(episode)) + pp_command = pp_command.replace('{{episode}}', episode) + pp_command = pp_command.replace('{{episode_name}}', os.path.splitext(os.path.basename(episode))[0]) + pp_command = pp_command.replace('{{subtitles}}', subtitles) + pp_command = pp_command.replace('{{subtitles_language}}', language) + pp_command = pp_command.replace('{{subtitles_language_code2}}', language_code2) + pp_command = pp_command.replace('{{subtitles_language_code3}}', language_code3) + return pp_command