2015-02-21 18:30:32 -08:00
|
|
|
"""A python app to (automatically) tag comic archives"""
|
2022-06-02 18:32:16 -07:00
|
|
|
#
|
2023-02-16 17:23:13 -08:00
|
|
|
# Copyright 2012-2014 ComicTagger Authors
|
2022-06-02 18:32:16 -07:00
|
|
|
#
|
2015-02-21 18:30:32 -08:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2022-06-02 18:32:16 -07:00
|
|
|
#
|
2015-02-21 18:30:32 -08:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2022-06-02 18:32:16 -07:00
|
|
|
#
|
2015-02-21 18:30:32 -08:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2022-06-02 18:32:16 -07:00
|
|
|
from __future__ import annotations
|
2013-02-06 17:20:05 -08:00
|
|
|
|
2022-12-06 00:20:01 -08:00
|
|
|
import argparse
|
2022-05-19 13:28:18 -07:00
|
|
|
import json
|
2023-04-22 22:00:26 -07:00
|
|
|
import locale
|
|
|
|
import logging
|
2022-04-04 18:59:26 -07:00
|
|
|
import logging.handlers
|
2023-04-22 22:00:26 -07:00
|
|
|
import os
|
2013-02-06 17:20:05 -08:00
|
|
|
import signal
|
2023-04-22 22:00:26 -07:00
|
|
|
import subprocess
|
Code cleanup
Remove no longer used google scripts
Remove convenience files from comicataggerlib and import comicapi directly
Add type-hints to facilitate auto-complete tools
Make PyQt5 code more compatible with PyQt6
Implement automatic tooling
isort and black for code formatting
Line length has been set to 120
flake8 for code standards with exceptions:
E203 - Whitespace before ':' - format compatiblity with black
E501 - Line too long - flake8 line limit cannot be set
E722 - Do not use bare except - fixing bare except statements is a
lot of overhead and there are already
many in the codebase
These changes, along with some manual fixes creates much more readable code.
See examples below:
diff --git a/comicapi/comet.py b/comicapi/comet.py
index d1741c5..52dc195 100644
--- a/comicapi/comet.py
+++ b/comicapi/comet.py
@@ -166,7 +166,2 @@ class CoMet:
- if credit['role'].lower() in set(self.editor_synonyms):
- ET.SubElement(
- root,
- 'editor').text = "{0}".format(
- credit['person'])
@@ -174,2 +169,4 @@ class CoMet:
self.indent(root)
+ if credit["role"].lower() in set(self.editor_synonyms):
+ ET.SubElement(root, "editor").text = str(credit["person"])
diff --git a/comictaggerlib/autotagmatchwindow.py b/comictaggerlib/autotagmatchwindow.py
index 4338176..9219f01 100644
--- a/comictaggerlib/autotagmatchwindow.py
+++ b/comictaggerlib/autotagmatchwindow.py
@@ -63,4 +63,3 @@ class AutoTagMatchWindow(QtWidgets.QDialog):
self.skipButton, QtWidgets.QDialogButtonBox.ActionRole)
- self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setText(
- "Accept and Write Tags")
+ self.buttonBox.button(QtWidgets.QDialogButtonBox.StandardButton.Ok).setText("Accept and Write Tags")
diff --git a/comictaggerlib/cli.py b/comictaggerlib/cli.py
index 688907d..dbd0c2e 100644
--- a/comictaggerlib/cli.py
+++ b/comictaggerlib/cli.py
@@ -293,7 +293,3 @@ def process_file_cli(filename, opts, settings, match_results):
if opts.raw:
- print((
- "{0}".format(
- str(
- ca.readRawCIX(),
- errors='ignore'))))
+ print(ca.read_raw_cix())
else:
2022-04-01 16:50:46 -07:00
|
|
|
import sys
|
2023-06-09 16:20:00 -07:00
|
|
|
from typing import cast
|
2018-01-14 07:41:27 -08:00
|
|
|
|
2022-12-14 23:13:53 -08:00
|
|
|
import settngs
|
|
|
|
|
2023-06-09 16:20:00 -07:00
|
|
|
import comicapi.comicarchive
|
|
|
|
import comicapi.utils
|
2023-02-09 19:33:10 -08:00
|
|
|
import comictalker
|
2023-01-31 20:21:39 -08:00
|
|
|
from comictaggerlib import cli, ctsettings
|
2023-06-09 16:20:00 -07:00
|
|
|
from comictaggerlib.ctsettings import ct_ns
|
2022-04-04 18:59:26 -07:00
|
|
|
from comictaggerlib.ctversion import version
|
2022-12-06 00:20:01 -08:00
|
|
|
from comictaggerlib.log import setup_logging
|
2013-02-06 17:20:05 -08:00
|
|
|
|
2022-06-23 13:05:27 -07:00
|
|
|
if sys.version_info < (3, 10):
|
|
|
|
import importlib_metadata
|
|
|
|
else:
|
|
|
|
import importlib.metadata as importlib_metadata
|
|
|
|
|
2023-02-09 19:33:10 -08:00
|
|
|
logger = logging.getLogger("comictagger")
|
|
|
|
|
2022-04-04 18:59:26 -07:00
|
|
|
|
2022-12-06 00:20:01 -08:00
|
|
|
logger.setLevel(logging.DEBUG)
|
2022-04-04 18:59:26 -07:00
|
|
|
|
2022-12-06 00:20:01 -08:00
|
|
|
|
2023-04-22 22:00:26 -07:00
|
|
|
def _lang_code_mac() -> str:
|
|
|
|
"""
|
|
|
|
stolen from https://github.com/mu-editor/mu
|
|
|
|
Returns the user's language preference as defined in the Language & Region
|
|
|
|
preference pane in macOS's System Preferences.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Uses the shell command `defaults read -g AppleLocale` that prints out a
|
|
|
|
# language code to standard output. Assumptions about the command:
|
|
|
|
# - It exists and is in the shell's PATH.
|
|
|
|
# - It accepts those arguments.
|
|
|
|
# - It returns a usable language code.
|
|
|
|
#
|
|
|
|
# Reference documentation:
|
|
|
|
# - The man page for the `defaults` command on macOS.
|
|
|
|
# - The macOS underlying API:
|
|
|
|
# https://developer.apple.com/documentation/foundation/nsuserdefaults.
|
|
|
|
|
|
|
|
lang_detect_command = "defaults read -g AppleLocale"
|
|
|
|
|
|
|
|
status, output = subprocess.getstatusoutput(lang_detect_command)
|
|
|
|
if status == 0:
|
|
|
|
# Command was successful.
|
|
|
|
lang_code = output
|
|
|
|
else:
|
|
|
|
logging.warning("Language detection command failed: %r", output)
|
|
|
|
lang_code = ""
|
|
|
|
|
|
|
|
return lang_code
|
|
|
|
|
|
|
|
|
|
|
|
def configure_locale() -> None:
|
|
|
|
if sys.platform == "darwin" and "LANG" not in os.environ:
|
|
|
|
code = _lang_code_mac()
|
|
|
|
if code != "":
|
|
|
|
os.environ["LANG"] = f"{code}.utf-8"
|
|
|
|
|
|
|
|
locale.setlocale(locale.LC_ALL, "")
|
|
|
|
sys.stdout.reconfigure(encoding=sys.getdefaultencoding()) # type: ignore[attr-defined]
|
|
|
|
sys.stderr.reconfigure(encoding=sys.getdefaultencoding()) # type: ignore[attr-defined]
|
|
|
|
sys.stdin.reconfigure(encoding=sys.getdefaultencoding()) # type: ignore[attr-defined]
|
|
|
|
|
|
|
|
|
2023-06-09 16:20:00 -07:00
|
|
|
def update_publishers(config: settngs.Config[ct_ns]) -> None:
|
2023-11-19 23:14:40 -08:00
|
|
|
json_file = config[0].Runtime_Options__config.user_config_dir / "publishers.json"
|
2022-05-19 13:28:18 -07:00
|
|
|
if json_file.exists():
|
|
|
|
try:
|
2023-01-12 14:43:12 -08:00
|
|
|
comicapi.utils.update_publishers(json.loads(json_file.read_text("utf-8")))
|
2023-02-03 20:13:58 -08:00
|
|
|
except Exception as e:
|
|
|
|
logger.exception("Failed to load publishers from %s: %s", json_file, e)
|
2022-12-06 00:20:01 -08:00
|
|
|
|
|
|
|
|
|
|
|
class App:
|
|
|
|
"""docstring for App"""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
2023-06-09 16:20:00 -07:00
|
|
|
self.config: settngs.Config[ct_ns]
|
2023-01-31 20:21:39 -08:00
|
|
|
self.initial_arg_parser = ctsettings.initial_commandline_parser()
|
2022-12-15 18:58:53 -08:00
|
|
|
self.config_load_success = False
|
2022-12-06 00:20:01 -08:00
|
|
|
|
|
|
|
def run(self) -> None:
|
2023-04-22 22:00:26 -07:00
|
|
|
configure_locale()
|
2023-01-31 20:21:39 -08:00
|
|
|
conf = self.initialize()
|
|
|
|
self.initialize_dirs(conf.config)
|
|
|
|
self.load_plugins(conf)
|
|
|
|
self.register_settings()
|
|
|
|
self.config = self.parse_settings(conf.config)
|
2022-12-06 00:20:01 -08:00
|
|
|
|
2022-12-15 18:58:53 -08:00
|
|
|
self.main()
|
2022-12-06 00:20:01 -08:00
|
|
|
|
2023-02-09 19:33:10 -08:00
|
|
|
def load_plugins(self, opts: argparse.Namespace) -> None:
|
2023-01-30 21:36:47 -08:00
|
|
|
comicapi.comicarchive.load_archive_plugins()
|
2023-02-09 19:33:10 -08:00
|
|
|
ctsettings.talkers = comictalker.get_talkers(version, opts.config.user_cache_dir)
|
2023-01-30 21:36:47 -08:00
|
|
|
|
2023-09-05 00:55:12 -07:00
|
|
|
def list_plugins(
|
|
|
|
self, talkers: list[comictalker.ComicTalker], archivers: list[type[comicapi.comicarchive.Archiver]]
|
|
|
|
) -> None:
|
2023-12-17 15:51:43 -08:00
|
|
|
if self.config[0].Runtime_Options__json:
|
|
|
|
for talker in talkers:
|
|
|
|
print( # noqa: T201
|
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"type": "talker",
|
|
|
|
"id": talker.id,
|
|
|
|
"name": talker.name,
|
|
|
|
"website": talker.website,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
for archiver in archivers:
|
|
|
|
try:
|
|
|
|
a = archiver()
|
|
|
|
print( # noqa: T201
|
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"type": "archiver",
|
|
|
|
"enabled": a.enabled,
|
|
|
|
"name": a.name(),
|
|
|
|
"extension": a.extension(),
|
|
|
|
"exe": a.exe,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
print( # noqa: T201
|
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"type": "archiver",
|
|
|
|
"enabled": archiver.enabled,
|
|
|
|
"name": "",
|
|
|
|
"extension": "",
|
|
|
|
"exe": archiver.exe,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
print("Metadata Sources: (ID: Name URL)") # noqa: T201
|
|
|
|
for talker in talkers:
|
|
|
|
print(f"{talker.id}: {talker.name} {talker.website}") # noqa: T201
|
|
|
|
|
|
|
|
print("\nComic Archive: (Name: extension, exe)") # noqa: T201
|
|
|
|
for archiver in archivers:
|
|
|
|
a = archiver()
|
|
|
|
print(f"{a.name()}: {a.extension()}, {a.exe}") # noqa: T201
|
2023-09-05 00:55:12 -07:00
|
|
|
|
2022-12-06 00:20:01 -08:00
|
|
|
def initialize(self) -> argparse.Namespace:
|
2023-12-17 15:51:43 -08:00
|
|
|
conf, _ = self.initial_arg_parser.parse_known_intermixed_args()
|
|
|
|
|
2023-01-31 20:21:39 -08:00
|
|
|
assert conf is not None
|
|
|
|
setup_logging(conf.verbose, conf.config.user_log_dir)
|
|
|
|
return conf
|
2022-12-06 00:20:01 -08:00
|
|
|
|
2023-01-31 20:21:39 -08:00
|
|
|
def register_settings(self) -> None:
|
2022-12-14 23:13:53 -08:00
|
|
|
self.manager = settngs.Manager(
|
2023-12-17 15:51:43 -08:00
|
|
|
description="A utility for reading and writing metadata to comic archives.\n\n\n"
|
|
|
|
+ "If no options are given, %(prog)s will run in windowed mode.\nPlease keep the '-v' option separated '-so -v' not '-sov'",
|
|
|
|
epilog="For more help visit the wiki at: https://github.com/comictagger/comictagger/wiki",
|
2022-12-05 18:58:28 -08:00
|
|
|
)
|
2023-01-31 20:21:39 -08:00
|
|
|
ctsettings.register_commandline_settings(self.manager)
|
|
|
|
ctsettings.register_file_settings(self.manager)
|
|
|
|
ctsettings.register_plugin_settings(self.manager)
|
2022-12-14 23:13:53 -08:00
|
|
|
|
2023-06-09 16:20:00 -07:00
|
|
|
def parse_settings(self, config_paths: ctsettings.ComicTaggerPaths, *args: str) -> settngs.Config[ct_ns]:
|
2023-11-17 17:34:10 -08:00
|
|
|
cfg, self.config_load_success = ctsettings.parse_config(
|
|
|
|
self.manager, config_paths.user_config_dir / "settings.json", list(args) or None
|
2023-06-09 16:20:00 -07:00
|
|
|
)
|
|
|
|
config = cast(settngs.Config[ct_ns], self.manager.get_namespace(cfg, file=True, cmdline=True))
|
2023-11-19 23:14:40 -08:00
|
|
|
config[0].Runtime_Options__config = config_paths
|
2022-12-15 20:10:35 -08:00
|
|
|
|
2023-01-31 20:21:39 -08:00
|
|
|
config = ctsettings.validate_commandline_settings(config, self.manager)
|
|
|
|
config = ctsettings.validate_file_settings(config)
|
|
|
|
config = ctsettings.validate_plugin_settings(config)
|
|
|
|
return config
|
2022-12-06 00:20:01 -08:00
|
|
|
|
2023-01-31 20:21:39 -08:00
|
|
|
def initialize_dirs(self, paths: ctsettings.ComicTaggerPaths) -> None:
|
2023-01-25 16:52:02 -08:00
|
|
|
paths.user_data_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
paths.user_config_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
paths.user_cache_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
paths.user_state_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
paths.user_log_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
logger.debug("user_data_dir: %s", paths.user_data_dir)
|
|
|
|
logger.debug("user_config_dir: %s", paths.user_config_dir)
|
|
|
|
logger.debug("user_cache_dir: %s", paths.user_cache_dir)
|
|
|
|
logger.debug("user_state_dir: %s", paths.user_state_dir)
|
|
|
|
logger.debug("user_log_dir: %s", paths.user_log_dir)
|
2022-12-06 00:20:01 -08:00
|
|
|
|
2022-12-15 18:58:53 -08:00
|
|
|
def main(self) -> None:
|
2023-01-31 20:21:39 -08:00
|
|
|
assert self.config is not None
|
|
|
|
# config already loaded
|
2022-12-15 18:58:53 -08:00
|
|
|
error = None
|
2022-12-06 00:20:01 -08:00
|
|
|
|
2023-02-20 16:02:15 -08:00
|
|
|
talkers = ctsettings.talkers
|
|
|
|
del ctsettings.talkers
|
|
|
|
|
|
|
|
if len(talkers) < 1:
|
|
|
|
error = error = (
|
2023-11-09 18:23:57 -08:00
|
|
|
"Failed to load any talkers, please re-install and check the log located in '"
|
2023-11-19 23:14:40 -08:00
|
|
|
+ str(self.config[0].Runtime_Options__config.user_log_dir)
|
2023-11-09 18:23:57 -08:00
|
|
|
+ "' for more details",
|
2023-02-20 16:02:15 -08:00
|
|
|
True,
|
|
|
|
)
|
|
|
|
|
2022-12-06 00:20:01 -08:00
|
|
|
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|
|
|
|
|
|
|
logger.debug("Installed Packages")
|
|
|
|
for pkg in sorted(importlib_metadata.distributions(), key=lambda x: x.name):
|
|
|
|
logger.debug("%s\t%s", pkg.metadata["Name"], pkg.metadata["Version"])
|
2022-04-04 18:59:26 -07:00
|
|
|
|
2023-01-12 14:43:12 -08:00
|
|
|
comicapi.utils.load_publishers()
|
2023-02-09 19:33:10 -08:00
|
|
|
update_publishers(self.config)
|
2022-11-25 15:35:34 -08:00
|
|
|
|
2023-11-19 23:14:40 -08:00
|
|
|
if self.config[0].Commands__list_plugins:
|
2023-09-05 00:55:12 -07:00
|
|
|
self.list_plugins(list(talkers.values()), comicapi.comicarchive.archivers)
|
|
|
|
return
|
|
|
|
|
2023-11-19 23:14:40 -08:00
|
|
|
if self.config[0].Commands__only_save_config:
|
2023-02-01 16:53:13 -08:00
|
|
|
if self.config_load_success:
|
2023-11-19 23:14:40 -08:00
|
|
|
settings_path = self.config[0].Runtime_Options__config.user_config_dir / "settings.json"
|
2023-09-05 00:56:56 -07:00
|
|
|
if self.config_load_success:
|
2023-11-17 17:34:10 -08:00
|
|
|
ctsettings.save_file(self.config, settings_path)
|
|
|
|
print("Settings saved") # noqa: T201
|
2023-02-01 16:53:13 -08:00
|
|
|
return
|
2022-12-15 18:58:53 -08:00
|
|
|
|
|
|
|
if not self.config_load_success:
|
|
|
|
error = (
|
2023-11-09 18:23:57 -08:00
|
|
|
"Failed to load settings, check the log located in '"
|
2023-11-19 23:14:40 -08:00
|
|
|
+ str(self.config[0].Runtime_Options__config.user_log_dir)
|
2023-11-09 18:23:57 -08:00
|
|
|
+ "' for more details",
|
2022-12-15 18:58:53 -08:00
|
|
|
True,
|
|
|
|
)
|
2022-08-17 15:53:19 -07:00
|
|
|
|
2023-11-19 23:14:40 -08:00
|
|
|
if not self.config[0].Runtime_Options__no_gui:
|
2022-12-06 00:20:01 -08:00
|
|
|
try:
|
2023-06-22 20:11:40 -07:00
|
|
|
from comictaggerlib import gui
|
|
|
|
|
2023-10-07 11:49:08 -07:00
|
|
|
if not gui.qt_available:
|
|
|
|
raise gui.import_error
|
2023-06-22 20:11:40 -07:00
|
|
|
return gui.open_tagger_window(talkers, self.config, error)
|
|
|
|
except ImportError:
|
2023-11-19 23:14:40 -08:00
|
|
|
self.config[0].Runtime_Options__no_gui = True
|
2023-06-22 20:11:40 -07:00
|
|
|
logger.warning("PyQt5 is not available. ComicTagger is limited to command-line mode.")
|
|
|
|
|
|
|
|
# GUI mode is not available or CLI mode was requested
|
|
|
|
if error and error[1]:
|
|
|
|
print(f"A fatal error occurred please check the log for more information: {error[0]}") # noqa: T201
|
|
|
|
raise SystemExit(1)
|
2023-12-17 15:51:43 -08:00
|
|
|
|
2023-06-22 20:11:40 -07:00
|
|
|
try:
|
|
|
|
cli.CLI(self.config[0], talkers).run()
|
|
|
|
except Exception:
|
|
|
|
logger.exception("CLI mode failed")
|
2023-01-21 00:27:39 -08:00
|
|
|
|
|
|
|
|
2023-02-09 19:33:10 -08:00
|
|
|
def main() -> None:
|
2023-01-21 00:27:39 -08:00
|
|
|
App().run()
|