Rename new settings talker methods. Move parse_settings for talkers to earlier and only pass talkers own settings.
This commit is contained in:
parent
0f10e6e848
commit
6a650514fa
@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from collections.abc import Mapping
|
||||
|
||||
import settngs
|
||||
|
||||
@ -9,9 +10,9 @@ from comictalker.talkerbase import ComicTalker
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def register_talker_settings(parser: settngs.Manager, plugins: dict[str, ComicTalker]) -> None:
|
||||
def register_talker_settings(parser: settngs.Manager, plugins: Mapping[str, ComicTalker]) -> None:
|
||||
for talker_name, talker in plugins.items():
|
||||
try:
|
||||
parser.add_group(talker_name, talker.comic_settings, False)
|
||||
parser.add_group(talker_name, talker.register_settings, False)
|
||||
except Exception:
|
||||
logger.exception("Failed to register settings for %s", talker_name)
|
||||
|
@ -21,7 +21,7 @@ import logging.handlers
|
||||
import platform
|
||||
import signal
|
||||
import sys
|
||||
from typing import Any
|
||||
from collections.abc import Mapping
|
||||
|
||||
import settngs
|
||||
|
||||
@ -30,6 +30,7 @@ from comicapi import utils
|
||||
from comictaggerlib import cli, ctoptions
|
||||
from comictaggerlib.ctversion import version
|
||||
from comictaggerlib.log import setup_logging
|
||||
from comictalker.talkerbase import ComicTalker
|
||||
|
||||
if sys.version_info < (3, 10):
|
||||
import importlib_metadata
|
||||
@ -66,7 +67,7 @@ class App:
|
||||
self.options = settngs.Config({}, {})
|
||||
self.initial_arg_parser = ctoptions.initial_cmd_line_parser()
|
||||
self.config_load_success = False
|
||||
self.talker_plugins: dict[str, Any] = {}
|
||||
self.talker_plugins: Mapping[str, ComicTalker] = {}
|
||||
|
||||
def run(self) -> None:
|
||||
opts = self.initialize()
|
||||
@ -74,7 +75,6 @@ class App:
|
||||
self.talker_plugins = ct_api.get_talkers(version, opts.config.user_cache_dir)
|
||||
self.register_options()
|
||||
self.parse_options(opts.config)
|
||||
self.initialize_talkers()
|
||||
|
||||
self.main()
|
||||
|
||||
@ -97,6 +97,7 @@ class App:
|
||||
self.options, self.config_load_success = self.manager.parse_config(
|
||||
config_paths.user_config_dir / "settings.json"
|
||||
)
|
||||
self.initialize_talkers()
|
||||
self.options = self.manager.get_namespace(self.options)
|
||||
|
||||
self.options = ctoptions.validate_commandline_options(self.options, self.manager)
|
||||
@ -119,7 +120,7 @@ class App:
|
||||
# Apply talker settings from config file
|
||||
try:
|
||||
for talker_name, talker in self.talker_plugins.items():
|
||||
ct_api.set_talker_settings(talker, self.options[0])
|
||||
ct_api.set_talker_settings(talker, self.options[0][talker_name])
|
||||
except Exception as e:
|
||||
# Remove talker as we failed to apply the settings
|
||||
del self.talker_plugins[e.source] # type: ignore[attr-defined]
|
||||
@ -156,7 +157,6 @@ class App:
|
||||
talker_api = self.talker_plugins[self.options[0].talkers_source]
|
||||
except Exception as e:
|
||||
logger.exception(f"Unable to load talker {self.options[0].talkers_source}. Error: {str(e)}")
|
||||
talker_api = None
|
||||
# TODO error True can be changed to False after the talker settings menu generation is in
|
||||
error = (f"Unable to load talker {self.options[0].talkers_source}. Error: {str(e)}", True)
|
||||
|
||||
|
@ -15,9 +15,9 @@
|
||||
# limitations under the License.
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import pathlib
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
import comictalker.talkers.comicvine
|
||||
@ -26,9 +26,9 @@ from comictalker.talkerbase import ComicTalker, TalkerError
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def set_talker_settings(talker: ComicTalker, settings: argparse.Namespace) -> None:
|
||||
def set_talker_settings(talker: ComicTalker, settings: dict[str, Any]) -> None:
|
||||
try:
|
||||
talker.set_settings(settings)
|
||||
talker.parse_settings(settings)
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
f"Failed to set talker settings for {talker.source_details.name}, will use defaults. Error: {str(e)}",
|
||||
@ -36,10 +36,11 @@ def set_talker_settings(talker: ComicTalker, settings: argparse.Namespace) -> No
|
||||
raise TalkerError(source=talker.source_details.name, code=4, desc="Could not apply talker settings")
|
||||
|
||||
|
||||
def get_talkers(version: str, cache: pathlib.Path) -> dict[str, Any]:
|
||||
def get_talkers(version: str, cache: pathlib.Path) -> Mapping[str, ComicTalker]:
|
||||
"""Returns all comic talker instances"""
|
||||
# TODO separate PR will bring talkers in via entry points. TalkerError etc. source will then be a var
|
||||
talkers = {}
|
||||
|
||||
for talker in [comictalker.talkers.comicvine.ComicVineTalker]:
|
||||
try:
|
||||
obj = talker(version, cache)
|
||||
|
@ -13,10 +13,9 @@
|
||||
# limitations under the License.
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import pathlib
|
||||
from typing import Callable
|
||||
from typing import Any, Callable
|
||||
|
||||
import settngs
|
||||
|
||||
@ -148,11 +147,12 @@ class ComicTalker:
|
||||
self.api_key: str = ""
|
||||
self.api_url: str = ""
|
||||
|
||||
def comic_settings(self, parser: settngs.Manager) -> None:
|
||||
"""Talker settings."""
|
||||
def register_settings(self, parser: settngs.Manager) -> None:
|
||||
"""Allows registering settings using the settngs package with an argparse like interface"""
|
||||
|
||||
def set_settings(self, settings: argparse.Namespace) -> None:
|
||||
"""Apply talker settings from config to object."""
|
||||
def parse_settings(self, settings: dict[str, Any]) -> None:
|
||||
"""settings is a dictionary of options defined in register_settings.
|
||||
It is only guaranteed that the settings defined in register_settings will be present."""
|
||||
|
||||
def check_api_key(self, key: str, url: str) -> bool:
|
||||
"""
|
||||
|
@ -192,8 +192,7 @@ class ComicVineTalker(ComicTalker):
|
||||
# NOTE: This was hardcoded before which is why it isn't in settings
|
||||
self.wait_for_rate_limit_time: int = 20
|
||||
|
||||
def comic_settings(self, parser: settngs.Manager) -> None:
|
||||
"""Register settings"""
|
||||
def register_settings(self, parser: settngs.Manager) -> None:
|
||||
parser.add_setting("--cv-use-series-start-as-volume", default=False, action=argparse.BooleanOptionalAction)
|
||||
parser.add_setting("--cv-wait-on-ratelimit", default=False, action=argparse.BooleanOptionalAction)
|
||||
parser.add_setting(
|
||||
@ -211,24 +210,18 @@ class ComicVineTalker(ComicTalker):
|
||||
help="Use the given Comic Vine URL.",
|
||||
)
|
||||
|
||||
def set_settings(self, settings: argparse.Namespace) -> None:
|
||||
"""Set settings."""
|
||||
if settings.comicvine_cv_remove_html_tables:
|
||||
self.remove_html_tables = bool(settings.comicvine_cv_remove_html_tables)
|
||||
if settings.comicvine_cv_use_series_start_as_volume:
|
||||
self.use_series_start_as_volume = settings.comicvine_cv_use_series_start_as_volume
|
||||
if settings.comicvine_cv_api_key:
|
||||
self.api_key = settings.comicvine_cv_api_key
|
||||
if settings.comicvine_cv_url:
|
||||
try:
|
||||
tmp_url = urlsplit(settings.comicvine_cv_url)
|
||||
# joinurl only works properly if there is a trailing slash
|
||||
if tmp_url.path and tmp_url.path[-1] != "/":
|
||||
tmp_url = tmp_url._replace(path=tmp_url.path + "/")
|
||||
def parse_settings(self, settings: dict[str, Any]) -> None:
|
||||
self.remove_html_tables = settings["cv_remove_html_tables"]
|
||||
self.use_series_start_as_volume = settings["cv_use_series_start_as_volume"]
|
||||
if settings["cv_api_key"]:
|
||||
self.api_key = settings["cv_api_key"]
|
||||
if settings["cv_url"]:
|
||||
tmp_url = urlsplit(settings["cv_url"])
|
||||
# joinurl only works properly if there is a trailing slash
|
||||
if tmp_url.path and tmp_url.path[-1] != "/":
|
||||
tmp_url = tmp_url._replace(path=tmp_url.path + "/")
|
||||
|
||||
self.api_url = tmp_url.geturl()
|
||||
except Exception:
|
||||
logger.exception("Failed to parse new talker URL for %s, will use default", self.source_name)
|
||||
self.api_url = tmp_url.geturl()
|
||||
|
||||
def check_api_key(self, key: str, url: str) -> bool:
|
||||
if not url:
|
||||
|
Loading…
Reference in New Issue
Block a user