Compare commits

...

4 Commits

5 changed files with 52 additions and 21 deletions

View File

@ -1,12 +1,11 @@
from __future__ import annotations
import dataclasses
from collections import defaultdict
from collections.abc import Collection
from enum import auto
from typing import Any
from comicapi.utils import StrEnum, norm_fold
from comicapi.utils import DefaultDict, StrEnum, norm_fold
@dataclasses.dataclass
@ -55,19 +54,19 @@ def overlay(old: Any, new: Any) -> Any:
return new
attribute = defaultdict(
lambda: overlay,
attribute = DefaultDict(
{
Mode.OVERLAY: overlay,
Mode.ADD_MISSING: lambda old, new: overlay(new, old),
},
default=lambda x: overlay,
)
lists = defaultdict(
lambda: overlay,
lists = DefaultDict(
{
Mode.OVERLAY: merge_lists,
Mode.ADD_MISSING: lambda old, new: merge_lists(new, old),
},
default=lambda x: overlay,
)

View File

@ -22,11 +22,10 @@ import pathlib
import platform
import sys
import unicodedata
from collections import defaultdict
from collections.abc import Iterable, Mapping
from enum import Enum, auto
from shutil import which # noqa: F401
from typing import Any, TypeVar, cast
from typing import Any, Callable, TypeVar, cast
from comicfn2dict import comicfn2dict
@ -107,6 +106,17 @@ else:
logger = logging.getLogger(__name__)
class DefaultDict(dict):
def __init__(self, *args, default: Callable[[Any], Any] | None = None) -> None:
super().__init__(*args)
self.default = default
def __missing__(self, key: Any) -> Any:
if self.default is None:
return key
return self.default(key)
class Parser(StrEnum):
ORIGINAL = auto()
COMPLICATED = auto()
@ -360,7 +370,9 @@ def xlate_float(data: Any) -> float | None:
if isinstance(data, (int, float)):
i = data
else:
i = str(data).translate(defaultdict(lambda: None, zip((ord(c) for c in "1234567890."), "1234567890.")))
i = str(data).translate(
DefaultDict(zip((ord(c) for c in "1234567890."), "1234567890."), default=lambda x: None)
)
if i == "":
return None
try:
@ -493,9 +505,9 @@ def parse_version(s: str) -> tuple[int, int, int]:
return (parts[0], parts[1], parts[2])
_languages: dict[str | None, str | None] = defaultdict(lambda: None)
_languages: dict[str | None, str | None] = DefaultDict(default=lambda x: None)
_countries: dict[str | None, str | None] = defaultdict(lambda: None)
_countries: dict[str | None, str | None] = DefaultDict(default=lambda x: None)
def countries() -> dict[str | None, str | None]:
@ -517,6 +529,8 @@ def languages() -> dict[str | None, str | None]:
def get_language_from_iso(iso: str | None) -> str | None:
if not _languages:
return languages()[iso]
return _languages[iso]
@ -529,10 +543,12 @@ def get_language_iso(string: str | None) -> str | None:
lang = string.casefold()
found = None
for lng in isocodes.extendend_languages.items:
for x in ("alpha_2", "alpha_3", "bibliographic", "common_name", "name"):
if x in lng and lng[x].casefold() == lang:
found = lng
# break
if found:
break
@ -542,6 +558,8 @@ def get_language_iso(string: str | None) -> str | None:
def get_country_from_iso(iso: str | None) -> str | None:
if not _countries:
return countries()[iso]
return _countries[iso]

View File

@ -20,7 +20,7 @@ import logging
import operator
import natsort
from PyQt5 import QtWidgets, uic
from PyQt5 import QtCore, QtWidgets, uic
from comicapi import utils
from comicapi.genericmetadata import Credit
@ -75,7 +75,11 @@ class CreditEditorWindow(QtWidgets.QDialog):
self.cbRole.setCurrentIndex(i)
if credit.language != "":
i = self.cbLanguage.findText(credit.language)
i = (
self.cbLanguage.findData(credit.language, QtCore.Qt.ItemDataRole.UserRole)
if self.cbLanguage.findData(credit.language, QtCore.Qt.ItemDataRole.UserRole) > -1
else self.cbLanguage.findText(credit.language)
)
if i == -1:
self.cbLanguage.setEditText(credit.language)
else:
@ -84,9 +88,8 @@ class CreditEditorWindow(QtWidgets.QDialog):
self.cbPrimary.setChecked(credit.primary)
def get_credit(self) -> Credit:
return Credit(
self.leName.text(), self.cbRole.currentText(), self.cbPrimary.isChecked(), self.cbLanguage.currentText()
)
lang = self.cbLanguage.currentData() or self.cbLanguage.currentText()
return Credit(self.leName.text(), self.cbRole.currentText(), self.cbPrimary.isChecked(), lang)
def accept(self) -> None:
if self.leName.text() == "":

View File

@ -891,6 +891,7 @@ class TaggerWindow(QtWidgets.QMainWindow):
language = utils.get_language_from_iso(credit.language) or credit.language
item = QtWidgets.QTableWidgetItem(language)
item.setData(QtCore.Qt.ItemDataRole.ToolTipRole, credit.language)
item.setData(QtCore.Qt.ItemDataRole.UserRole, credit.language)
item.setFlags(QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled)
self.twCredits.setItem(row, self.md_attributes["credits.language"], item)
@ -970,7 +971,10 @@ class TaggerWindow(QtWidgets.QMainWindow):
for row in range(self.twCredits.rowCount()):
role = self.twCredits.item(row, self.md_attributes["credits.role"]).text()
lang = self.twCredits.item(row, self.md_attributes["credits.language"]).text()
lang = (
self.twCredits.item(row, self.md_attributes["credits.language"]).data(QtCore.Qt.ItemDataRole.UserRole)
or self.twCredits.item(row, self.md_attributes["credits.language"]).text()
)
name = self.twCredits.item(row, self.md_attributes["credits.person"]).text()
primary_flag = self.twCredits.item(row, self.md_attributes["credits.primary"]).text() != ""
@ -1268,11 +1272,15 @@ class TaggerWindow(QtWidgets.QMainWindow):
old = Credit()
if edit:
row = self.twCredits.currentRow()
lang = str(
self.twCredits.item(row, self.md_attributes["credits.language"]).data(QtCore.Qt.ItemDataRole.UserRole)
or utils.get_language_iso(self.twCredits.item(row, self.md_attributes["credits.language"]).text())
)
old = Credit(
self.twCredits.item(row, self.md_attributes["credits.person"]).text(),
self.twCredits.item(row, self.md_attributes["credits.role"]).text(),
self.twCredits.item(row, self.md_attributes["credits.primary"]).text() != "",
self.twCredits.item(row, self.md_attributes["credits.language"]).text(),
lang,
)
editor = CreditEditorWindow(self, CreditEditorWindow.ModeEdit, old)
@ -1309,9 +1317,13 @@ class TaggerWindow(QtWidgets.QMainWindow):
if ok_to_mod:
# modify it
if edit:
lang = utils.get_language_from_iso(new.language) or new.language
self.twCredits.item(row, self.md_attributes["credits.role"]).setText(new.role)
self.twCredits.item(row, self.md_attributes["credits.person"]).setText(new.person)
self.twCredits.item(row, self.md_attributes["credits.language"]).setText(new.language)
self.twCredits.item(row, self.md_attributes["credits.language"]).setText(lang)
self.twCredits.item(row, self.md_attributes["credits.language"]).setData(
QtCore.Qt.ItemDataRole.UserRole, new.language
)
self.update_credit_primary_flag(row, new.primary)
else:
# add new entry

View File

@ -22,7 +22,6 @@ import json
import logging
import pathlib
import time
from collections import defaultdict
from typing import Any, Callable, Generic, TypeVar, cast
from urllib.parse import parse_qsl, urljoin
@ -186,7 +185,7 @@ class ComicVineTalker(ComicTalker):
self.default_api_url = self.api_url = f"{self.website}/api/"
self.default_api_key = self.api_key = "27431e6787042105bd3e47e169a624521f89f3a4"
self.use_series_start_as_volume: bool = False
self.total_requests_made: dict[str, int] = defaultdict(int)
self.total_requests_made: dict[str, int] = utils.DefaultDict(default=lambda x: 0)
self.custom_url_parameters: dict[str, str] = {}
def _log_total_requests(self) -> None: