diff options
author | Vitiko <[email protected]> | 2022-05-13 01:10:10 -0400 |
---|---|---|
committer | Vitiko <[email protected]> | 2022-05-13 01:10:10 -0400 |
commit | 23a5ab9b0ea9cb582ea5574fd8cdd1c9169a7d85 (patch) | |
tree | e381ff34b3a72b9a0e68b6e7b0f415357447fb31 /libs | |
parent | 1dff555fc8c9c8a9be29a91b3717fcf8b07cb648 (diff) | |
download | bazarr-23a5ab9b0ea9cb582ea5574fd8cdd1c9169a7d85.tar.gz bazarr-23a5ab9b0ea9cb582ea5574fd8cdd1c9169a7d85.zip |
Improve provider configs updates
Diffstat (limited to 'libs')
-rw-r--r-- | libs/subliminal_patch/core.py | 79 |
1 files changed, 53 insertions, 26 deletions
diff --git a/libs/subliminal_patch/core.py b/libs/subliminal_patch/core.py index c5f11f75c..151eb5df1 100644 --- a/libs/subliminal_patch/core.py +++ b/libs/subliminal_patch/core.py @@ -70,15 +70,53 @@ def remove_crap_from_fn(fn): return REMOVE_CRAP_FROM_FILENAME.sub(repl, fn) +class _ProviderConfigs(dict): + def __init__(self, pool, *args, **kwargs): + super().__init__(*args, **kwargs) + self._pool = pool + + def update(self, items): + updated = set() + # Restart providers with new configs + for key, val in items.items(): + # Don't restart providers that are not enabled + if key not in self._pool.providers: + continue + + # key: provider's name; val: config dict + registered_val = self.get(key) + + if registered_val is None or registered_val == val: + continue + + updated.add(key) + + # The new dict might be a partial dict + registered_val.update(val) + + logger.debug("Config changed. Restarting provider: %s", key) + try: + provider = provider_registry[key](**registered_val) # type: ignore + provider.initialize() + except Exception as error: + self._pool.throttle_callback(key, error) + else: + self._pool.initialized_providers[key] = provider + + if updated: + logger.debug("Providers with config updates: %s", updated) + else: + logger.debug("No provider config updates") + + return super().update(items) + + class SZProviderPool(ProviderPool): def __init__(self, providers=None, provider_configs=None, blacklist=None, ban_list=None, throttle_callback=None, pre_download_hook=None, post_download_hook=None, language_hook=None): #: Name of providers to use self.providers = set(providers or []) - #: Provider configuration - self.provider_configs = provider_configs or {} - #: Initialized providers self.initialized_providers = {} @@ -101,6 +139,10 @@ class SZProviderPool(ProviderPool): if not self.throttle_callback: self.throttle_callback = lambda x, y: x + #: Provider configuration + self.provider_configs = _ProviderConfigs(self) + self.provider_configs.update(provider_configs or {}) + def update(self, providers, provider_configs, blacklist, ban_list): # Check if the pool was initialized enough hours ago self._check_lifetime() @@ -130,29 +172,8 @@ class SZProviderPool(ProviderPool): self.providers.difference_update(removed_providers) self.providers.update(list(providers)) - # Restart providers with new configs - for key, val in provider_configs.items(): - # Don't restart providers that are not enabled - if key not in self.providers: - continue - - # key: provider's name; val: config dict - old_val = self.provider_configs.get(key) - - if old_val == val: - continue - - logger.debug("Restarting provider: %s", key) - try: - provider = provider_registry[key](**val) - provider.initialize() - except Exception as error: - self.throttle_callback(key, error) - else: - self.initialized_providers[key] = provider - updated = True - - self.provider_configs = provider_configs + # self.provider_configs = provider_configs + self.provider_configs.update(provider_configs) self.blacklist = blacklist or [] self.ban_list = ban_list or {'must_contain': [], 'must_not_contain': []} @@ -540,6 +561,12 @@ class SZProviderPool(ProviderPool): return video_types + def __repr__(self): + return ( + f"{self.__class__.__name__} [{len(self.providers)} providers ({len(self.initialized_providers)} " + f"initialized; {len(self.discarded_providers)} discarded)]" + ) + class SZAsyncProviderPool(SZProviderPool): """Subclass of :class:`ProviderPool` with asynchronous support for :meth:`~ProviderPool.list_subtitles`. |