Consolidate plugin code
This commit is contained in:
parent
ee426e6473
commit
d987a811e3
@ -3,8 +3,10 @@ from __future__ import annotations
|
||||
from comictaggerlib.ctoptions.cmdline import initial_cmd_line_parser, register_commandline, validate_commandline_options
|
||||
from comictaggerlib.ctoptions.file import register_settings, validate_settings
|
||||
from comictaggerlib.ctoptions.plugin import register_plugin_settings, validate_plugin_settings
|
||||
from comictaggerlib.ctoptions.talker_plugins import register_talker_settings
|
||||
from comictaggerlib.ctoptions.types import ComicTaggerPaths
|
||||
from comictalker.talkerbase import ComicTalker
|
||||
|
||||
talker_plugins: dict[str, ComicTalker] = {}
|
||||
|
||||
__all__ = [
|
||||
"initial_cmd_line_parser",
|
||||
|
@ -6,6 +6,7 @@ import os
|
||||
import settngs
|
||||
|
||||
import comicapi.comicarchive
|
||||
import comictaggerlib.ctoptions
|
||||
|
||||
logger = logging.getLogger("comictagger")
|
||||
|
||||
@ -22,12 +23,20 @@ def archiver(manager: settngs.Manager) -> None:
|
||||
exe_registered.add(archiver.exe)
|
||||
|
||||
|
||||
def validate_plugin_settings(options: settngs.Config) -> settngs.Config:
|
||||
def register_talker_settings(manager: settngs.Manager) -> None:
|
||||
for talker_name, talker in comictaggerlib.ctoptions.talker_plugins.items():
|
||||
try:
|
||||
manager.add_persistent_group("talker_" + talker_name, talker.register_settings, False)
|
||||
except Exception:
|
||||
logger.exception("Failed to register settings for %s", talker_name)
|
||||
|
||||
|
||||
def validate_archive_settings(options: settngs.Config) -> settngs.Config:
|
||||
if "archiver" not in options[1]:
|
||||
return options
|
||||
cfg = settngs.normalize_config(options, file=True, cmdline=True, defaults=False)
|
||||
for archiver in comicapi.comicarchive.archivers:
|
||||
exe_name = archiver.exe.replace(" ", "-").replace("_", "-").strip().strip("-").replace("-", "_")
|
||||
exe_name = settngs.sanitize_name(archiver.exe)
|
||||
if (
|
||||
exe_name in cfg[0]["archiver"]
|
||||
and cfg[0]["archiver"][exe_name]
|
||||
@ -41,5 +50,25 @@ def validate_plugin_settings(options: settngs.Config) -> settngs.Config:
|
||||
return options
|
||||
|
||||
|
||||
def validate_talker_settings(options: settngs.Config) -> settngs.Config:
|
||||
# Apply talker settings from config file
|
||||
for talker_name, talker in list(comictaggerlib.ctoptions.talker_plugins.items()):
|
||||
try:
|
||||
talker.parse_settings(options[0]["talker_" + talker_name])
|
||||
except Exception as e:
|
||||
# Remove talker as we failed to apply the settings
|
||||
del comictaggerlib.ctoptions.talker_plugins[talker_name]
|
||||
logger.exception("Failed to initialize talker settings: %s", e)
|
||||
|
||||
return options
|
||||
|
||||
|
||||
def validate_plugin_settings(options: settngs.Config) -> settngs.Config:
|
||||
options = validate_archive_settings(options)
|
||||
options = validate_talker_settings(options)
|
||||
return options
|
||||
|
||||
|
||||
def register_plugin_settings(manager: settngs.Manager):
|
||||
manager.add_persistent_group("archiver", archiver, False)
|
||||
register_talker_settings(manager)
|
||||
|
@ -1,18 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from collections.abc import Mapping
|
||||
|
||||
import settngs
|
||||
|
||||
from comictalker.talkerbase import ComicTalker
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def register_talker_settings(parser: settngs.Manager, plugins: Mapping[str, ComicTalker]) -> None:
|
||||
for talker_name, talker in plugins.items():
|
||||
try:
|
||||
parser.add_group("talker_" + talker_name, talker.register_settings, False)
|
||||
except Exception:
|
||||
logger.exception("Failed to register settings for %s", talker_name)
|
@ -54,9 +54,8 @@ def update_publishers(options: settngs.Namespace) -> None:
|
||||
if json_file.exists():
|
||||
try:
|
||||
comicapi.utils.update_publishers(json.loads(json_file.read_text("utf-8")))
|
||||
except Exception:
|
||||
logger.exception("Failed to load publishers from %s", json_file)
|
||||
# show_exception_box(str(e))
|
||||
except Exception as e:
|
||||
logger.exception("Failed to load publishers from %s: %s", json_file, e)
|
||||
|
||||
|
||||
class App:
|
||||
@ -70,16 +69,16 @@ class App:
|
||||
|
||||
def run(self) -> None:
|
||||
opts = self.initialize()
|
||||
self.load_plugins()
|
||||
self.initialize_dirs(opts.config)
|
||||
self.talker_plugins = ct_api.get_talkers(version, opts.config.user_cache_dir)
|
||||
self.load_plugins(opts)
|
||||
self.register_options()
|
||||
self.parse_options(opts.config)
|
||||
self.options = self.parse_options(opts.config)
|
||||
|
||||
self.main()
|
||||
|
||||
def load_plugins(self) -> None:
|
||||
def load_plugins(self, opts) -> None:
|
||||
comicapi.comicarchive.load_archive_plugins()
|
||||
ctoptions.talker_plugins = ct_api.get_talkers(version, opts.config.user_cache_dir)
|
||||
|
||||
def initialize(self) -> argparse.Namespace:
|
||||
opts, _ = self.initial_arg_parser.parse_known_args()
|
||||
@ -95,19 +94,15 @@ class App:
|
||||
ctoptions.register_commandline(self.manager)
|
||||
ctoptions.register_settings(self.manager)
|
||||
ctoptions.register_plugin_settings(self.manager)
|
||||
ctoptions.register_talker_settings(self.manager, self.talker_plugins)
|
||||
|
||||
def parse_options(self, config_paths: ctoptions.ComicTaggerPaths) -> None:
|
||||
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)
|
||||
def parse_options(self, config_paths: ctoptions.ComicTaggerPaths) -> settngs.Config:
|
||||
options, self.config_load_success = self.manager.parse_config(config_paths.user_config_dir / "settings.json")
|
||||
options = self.manager.get_namespace(options)
|
||||
|
||||
self.options = ctoptions.validate_commandline_options(self.options, self.manager)
|
||||
self.options = ctoptions.validate_settings(self.options)
|
||||
self.options = ctoptions.validate_plugin_settings(self.options)
|
||||
self.options = self.options
|
||||
options = ctoptions.validate_commandline_options(options, self.manager)
|
||||
options = ctoptions.validate_settings(options)
|
||||
options = ctoptions.validate_plugin_settings(options)
|
||||
return options
|
||||
|
||||
def initialize_dirs(self, paths: ctoptions.ComicTaggerPaths) -> None:
|
||||
paths.user_data_dir.mkdir(parents=True, exist_ok=True)
|
||||
@ -121,16 +116,6 @@ class App:
|
||||
logger.debug("user_state_dir: %s", paths.user_state_dir)
|
||||
logger.debug("user_log_dir: %s", paths.user_log_dir)
|
||||
|
||||
def initialize_talkers(self) -> None:
|
||||
# Apply talker settings from config file
|
||||
for talker_name, talker in list(self.talker_plugins.items()):
|
||||
try:
|
||||
talker.parse_settings(self.options[0]["talker_" + talker_name])
|
||||
except Exception as e:
|
||||
# Remove talker as we failed to apply the settings
|
||||
del self.talker_plugins[talker_name] # type: ignore[attr-defined]
|
||||
logger.exception("Failed to initialize talker settings. Error %s", e)
|
||||
|
||||
def main(self) -> None:
|
||||
assert self.options is not None
|
||||
# options already loaded
|
||||
@ -156,10 +141,10 @@ class App:
|
||||
|
||||
try:
|
||||
talker_api = self.talker_plugins[self.options[0].talkers_source]
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
logger.exception(f"Unable to load talker {self.options[0].talkers_source}")
|
||||
# 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}", True)
|
||||
error = (f"Unable to load talker {self.options[0].talkers_source}: {e}", True)
|
||||
|
||||
if not self.config_load_success:
|
||||
error = (
|
||||
|
@ -17,7 +17,6 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import pathlib
|
||||
from collections.abc import Mapping
|
||||
|
||||
import comictalker.talkers.comicvine
|
||||
from comictalker.talkerbase import ComicTalker, TalkerError
|
||||
@ -25,7 +24,7 @@ from comictalker.talkerbase import ComicTalker, TalkerError
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_talkers(version: str, cache: pathlib.Path) -> Mapping[str, ComicTalker]:
|
||||
def get_talkers(version: str, cache: pathlib.Path) -> dict[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: dict[str, ComicTalker] = {}
|
||||
|
Loading…
Reference in New Issue
Block a user