2015-02-21 18:30:32 -08:00
|
|
|
"""Functions for renaming files based on metadata"""
|
2024-01-29 09:14:25 -08:00
|
|
|
|
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
|
2012-12-14 21:54:12 -08:00
|
|
|
|
2022-04-18 18:59:17 -07:00
|
|
|
import calendar
|
2024-06-21 20:07:07 -07:00
|
|
|
import datetime
|
2022-04-04 18:59:26 -07:00
|
|
|
import logging
|
2012-12-14 21:54:12 -08:00
|
|
|
import os
|
2022-04-29 23:27:58 -07:00
|
|
|
import pathlib
|
2022-04-18 18:44:20 -07:00
|
|
|
import string
|
2024-06-21 20:07:07 -07:00
|
|
|
from collections.abc import Collection, Iterable, Mapping, Sequence, Sized
|
2022-11-23 22:16:46 -08:00
|
|
|
from typing import Any, cast
|
2022-04-18 18:44:20 -07:00
|
|
|
|
2022-08-19 20:20:37 -07:00
|
|
|
from pathvalidate import Platform, normalize_platform, sanitize_filename
|
2015-02-13 15:08:07 -08:00
|
|
|
|
2022-07-09 22:27:45 -07:00
|
|
|
from comicapi.comicarchive import ComicArchive
|
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
|
|
|
from comicapi.genericmetadata import GenericMetadata
|
|
|
|
from comicapi.issuestring import IssueString
|
2022-11-23 22:16:46 -08:00
|
|
|
from comictaggerlib.defaults import DEFAULT_REPLACEMENTS, Replacement, Replacements
|
2012-12-14 21:54:12 -08:00
|
|
|
|
2022-04-04 18:59:26 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-02-13 15:08:07 -08:00
|
|
|
|
2022-07-09 22:27:45 -07:00
|
|
|
def get_rename_dir(ca: ComicArchive, rename_dir: str | pathlib.Path | None) -> pathlib.Path:
|
|
|
|
folder = ca.path.parent.absolute()
|
|
|
|
if rename_dir is not None:
|
|
|
|
if isinstance(rename_dir, str):
|
2024-04-27 19:25:33 -07:00
|
|
|
rename_dir = pathlib.Path(rename_dir.strip())
|
|
|
|
folder = rename_dir.absolute()
|
2022-07-09 22:27:45 -07:00
|
|
|
return folder
|
|
|
|
|
|
|
|
|
2024-06-21 20:07:07 -07:00
|
|
|
def _isnamedtupleinstance(x: Any) -> bool: # pragma: no cover
|
|
|
|
t = type(x)
|
|
|
|
b = t.__bases__
|
|
|
|
|
|
|
|
if len(b) != 1 or b[0] != tuple:
|
|
|
|
return False
|
|
|
|
|
|
|
|
f = getattr(t, "_fields", None)
|
|
|
|
if not isinstance(f, tuple):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return all(isinstance(n, str) for n in f)
|
|
|
|
|
|
|
|
|
2022-04-18 18:44:20 -07:00
|
|
|
class MetadataFormatter(string.Formatter):
|
2022-08-18 13:48:23 -07:00
|
|
|
def __init__(
|
2022-11-23 22:16:46 -08:00
|
|
|
self, smart_cleanup: bool = False, platform: str = "auto", replacements: Replacements = DEFAULT_REPLACEMENTS
|
2022-08-18 13:48:23 -07:00
|
|
|
) -> None:
|
2022-04-18 18:44:20 -07:00
|
|
|
super().__init__()
|
|
|
|
self.smart_cleanup = smart_cleanup
|
2022-11-23 12:34:59 -08:00
|
|
|
self.platform = normalize_platform(platform)
|
2022-08-18 13:48:23 -07:00
|
|
|
self.replacements = replacements
|
2022-04-18 18:44:20 -07:00
|
|
|
|
2022-05-17 13:57:04 -07:00
|
|
|
def format_field(self, value: Any, format_spec: str) -> str:
|
2022-04-18 18:44:20 -07:00
|
|
|
if value is None or value == "":
|
|
|
|
return ""
|
2022-05-17 13:57:04 -07:00
|
|
|
return cast(str, super().format_field(value, format_spec))
|
|
|
|
|
2024-05-21 18:22:30 -07:00
|
|
|
def convert_field(self, value: Any, conversion: str | None) -> str:
|
2024-06-21 20:07:07 -07:00
|
|
|
if isinstance(value, Iterable) and not isinstance(value, str) and not _isnamedtupleinstance(value):
|
|
|
|
if conversion == "C":
|
|
|
|
if isinstance(value, Sized):
|
|
|
|
return str(len(value))
|
|
|
|
return ""
|
|
|
|
if conversion and conversion.isdecimal():
|
|
|
|
if not isinstance(value, Collection):
|
|
|
|
return ""
|
|
|
|
i = int(conversion) - 1
|
|
|
|
if i < 0:
|
|
|
|
i = 0
|
|
|
|
if i < len(value):
|
|
|
|
try:
|
|
|
|
return sorted(value)[i]
|
|
|
|
except Exception:
|
|
|
|
...
|
|
|
|
return list(value)[i]
|
|
|
|
return ""
|
|
|
|
try:
|
|
|
|
return ", ".join(list(self.convert_field(v, conversion) for v in sorted(value)))
|
|
|
|
except Exception:
|
|
|
|
...
|
|
|
|
return ", ".join(list(self.convert_field(v, conversion) for v in value))
|
|
|
|
if not conversion:
|
|
|
|
return cast(str, super().convert_field(value, conversion))
|
2022-10-04 16:15:20 -07:00
|
|
|
if conversion == "u":
|
|
|
|
return str(value).upper()
|
|
|
|
if conversion == "l":
|
|
|
|
return str(value).casefold()
|
|
|
|
if conversion == "c":
|
|
|
|
return str(value).capitalize()
|
|
|
|
if conversion == "S":
|
|
|
|
return str(value).swapcase()
|
|
|
|
if conversion == "t":
|
|
|
|
return str(value).title()
|
2024-06-21 20:07:07 -07:00
|
|
|
if conversion.isdecimal():
|
|
|
|
return ""
|
2022-10-04 16:15:20 -07:00
|
|
|
return cast(str, super().convert_field(value, conversion))
|
|
|
|
|
2022-09-17 01:28:26 -07:00
|
|
|
def handle_replacements(self, string: str, replacements: list[Replacement]) -> str:
|
|
|
|
for find, replace, strict_only in replacements:
|
|
|
|
if self.is_strict() or not strict_only:
|
|
|
|
string = string.replace(find, replace)
|
2022-08-18 13:48:23 -07:00
|
|
|
return string
|
|
|
|
|
2022-10-04 16:15:20 -07:00
|
|
|
def none_replacement(self, value: Any, replacement: str, r: str) -> Any:
|
|
|
|
if r == "-" and value is None or value == "":
|
|
|
|
return replacement
|
|
|
|
if r == "+" and value is not None:
|
|
|
|
return replacement
|
|
|
|
return value
|
|
|
|
|
|
|
|
def split_replacement(self, field_name: str) -> tuple[str, str, str]:
|
|
|
|
if "-" in field_name:
|
|
|
|
return field_name.rpartition("-")
|
|
|
|
if "+" in field_name:
|
|
|
|
return field_name.rpartition("+")
|
|
|
|
return field_name, "", ""
|
|
|
|
|
2022-09-17 01:28:26 -07:00
|
|
|
def is_strict(self) -> bool:
|
|
|
|
return self.platform in [Platform.UNIVERSAL, Platform.WINDOWS]
|
|
|
|
|
2022-05-17 13:57:04 -07:00
|
|
|
def _vformat(
|
|
|
|
self,
|
|
|
|
format_string: str,
|
2023-04-22 17:54:58 -07:00
|
|
|
args: Sequence[Any],
|
|
|
|
kwargs: Mapping[str, Any],
|
2022-05-17 13:57:04 -07:00
|
|
|
used_args: set[Any],
|
|
|
|
recursion_depth: int,
|
|
|
|
auto_arg_index: int = 0,
|
|
|
|
) -> tuple[str, int]:
|
2022-04-18 18:44:20 -07:00
|
|
|
if recursion_depth < 0:
|
|
|
|
raise ValueError("Max string recursion exceeded")
|
|
|
|
result = []
|
|
|
|
lstrip = False
|
|
|
|
for literal_text, field_name, format_spec, conversion in self.parse(format_string):
|
|
|
|
# output the literal text
|
|
|
|
if literal_text:
|
|
|
|
if lstrip:
|
2022-04-18 22:46:46 -07:00
|
|
|
literal_text = literal_text.lstrip("-_)}]#")
|
|
|
|
if self.smart_cleanup:
|
2022-09-17 01:28:26 -07:00
|
|
|
literal_text = self.handle_replacements(literal_text, self.replacements.literal_text)
|
2022-04-29 16:29:01 -07:00
|
|
|
lspace = literal_text[0].isspace() if literal_text else False
|
|
|
|
rspace = literal_text[-1].isspace() if literal_text else False
|
2022-04-18 22:46:46 -07:00
|
|
|
literal_text = " ".join(literal_text.split())
|
|
|
|
if literal_text == "":
|
|
|
|
literal_text = " "
|
|
|
|
else:
|
|
|
|
if lspace:
|
|
|
|
literal_text = " " + literal_text
|
|
|
|
if rspace:
|
|
|
|
literal_text += " "
|
|
|
|
result.append(literal_text)
|
|
|
|
|
2022-04-18 18:44:20 -07:00
|
|
|
lstrip = False
|
|
|
|
# if there's a field, output it
|
2022-04-29 16:29:01 -07:00
|
|
|
if field_name is not None and field_name != "":
|
2022-10-04 16:15:20 -07:00
|
|
|
field_name, r, replacement = self.split_replacement(field_name)
|
2022-07-01 16:22:01 -07:00
|
|
|
field_name = field_name.casefold()
|
2022-04-18 22:46:46 -07:00
|
|
|
# this is some markup, find the object and do the formatting
|
2022-04-18 18:44:20 -07:00
|
|
|
|
2022-07-13 19:56:34 -07:00
|
|
|
# handle arg indexing when digit field_names are given.
|
|
|
|
if field_name.isdigit():
|
|
|
|
raise ValueError("cannot use a number as a field name")
|
2022-04-18 18:44:20 -07:00
|
|
|
|
|
|
|
# given the field_name, find the object it references
|
|
|
|
# and the argument it came from
|
|
|
|
obj, arg_used = self.get_field(field_name, args, kwargs)
|
|
|
|
used_args.add(arg_used)
|
|
|
|
|
2022-10-04 16:15:20 -07:00
|
|
|
obj = self.none_replacement(obj, replacement, r)
|
2022-04-18 18:44:20 -07:00
|
|
|
# do any conversion on the resulting object
|
2024-05-21 18:22:30 -07:00
|
|
|
obj = self.convert_field(obj, conversion)
|
2024-06-21 20:07:07 -07:00
|
|
|
obj = self.none_replacement(obj, replacement, r)
|
2022-04-18 18:44:20 -07:00
|
|
|
|
|
|
|
# expand the format spec, if needed
|
2022-07-13 19:56:34 -07:00
|
|
|
format_spec, _ = self._vformat(
|
|
|
|
cast(str, format_spec), args, kwargs, used_args, recursion_depth - 1, auto_arg_index=False
|
2022-04-18 18:44:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
# format the object and append to the result
|
|
|
|
fmt_obj = self.format_field(obj, format_spec)
|
2022-08-08 18:03:29 -07:00
|
|
|
if fmt_obj == "" and result and self.smart_cleanup and literal_text:
|
|
|
|
if self.str_contains(result[-1], "({["):
|
|
|
|
lstrip = True
|
2022-04-29 16:29:01 -07:00
|
|
|
if result:
|
2022-06-26 01:53:40 -07:00
|
|
|
if " " in result[-1]:
|
2022-08-08 18:03:29 -07:00
|
|
|
result[-1], _, _ = result[-1].rstrip().rpartition(" ")
|
2022-04-29 16:29:01 -07:00
|
|
|
result[-1] = result[-1].rstrip("-_({[#")
|
2022-04-18 22:46:46 -07:00
|
|
|
if self.smart_cleanup:
|
2022-09-17 01:28:26 -07:00
|
|
|
# colons and slashes get special treatment
|
|
|
|
fmt_obj = self.handle_replacements(fmt_obj, self.replacements.format_value)
|
2022-04-18 22:46:46 -07:00
|
|
|
fmt_obj = " ".join(fmt_obj.split())
|
2022-05-17 13:57:04 -07:00
|
|
|
fmt_obj = str(sanitize_filename(fmt_obj, platform=self.platform))
|
2022-04-18 18:44:20 -07:00
|
|
|
result.append(fmt_obj)
|
|
|
|
|
2022-07-13 19:56:34 -07:00
|
|
|
return "".join(result), False
|
2022-04-18 18:44:20 -07:00
|
|
|
|
2022-08-08 18:03:29 -07:00
|
|
|
def str_contains(self, chars: str, string: str) -> bool:
|
|
|
|
for char in chars:
|
|
|
|
if char in string:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2022-04-18 18:44:20 -07:00
|
|
|
|
2022-04-29 23:27:58 -07:00
|
|
|
class FileRenamer:
|
2022-11-23 22:16:46 -08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
metadata: GenericMetadata | None,
|
|
|
|
platform: str = "auto",
|
|
|
|
replacements: Replacements = DEFAULT_REPLACEMENTS,
|
|
|
|
) -> None:
|
2022-04-18 18:59:17 -07:00
|
|
|
self.template = "{publisher}/{series}/{series} v{volume} #{issue} (of {issue_count}) ({year})"
|
2015-02-12 14:57:46 -08:00
|
|
|
self.smart_cleanup = True
|
|
|
|
self.issue_zero_padding = 3
|
2022-05-17 13:57:04 -07:00
|
|
|
self.metadata = metadata or GenericMetadata()
|
2022-04-18 18:44:20 -07:00
|
|
|
self.move = False
|
2022-04-18 22:46:46 -07:00
|
|
|
self.platform = platform
|
2022-11-23 22:16:46 -08:00
|
|
|
self.replacements = replacements
|
2024-04-27 19:25:33 -07:00
|
|
|
self.original_name = ""
|
|
|
|
self.move_only = False
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2024-04-27 19:25:33 -07:00
|
|
|
def set_metadata(self, metadata: GenericMetadata, original_name: str) -> None:
|
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
|
|
|
self.metadata = metadata
|
2024-04-27 19:25:33 -07:00
|
|
|
self.original_name = original_name
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2022-05-17 13:57:04 -07:00
|
|
|
def set_issue_zero_padding(self, count: int) -> None:
|
2015-02-12 14:57:46 -08:00
|
|
|
self.issue_zero_padding = count
|
|
|
|
|
2022-05-17 13:57:04 -07:00
|
|
|
def set_smart_cleanup(self, on: bool) -> None:
|
2015-02-12 14:57:46 -08:00
|
|
|
self.smart_cleanup = on
|
|
|
|
|
2022-05-17 13:57:04 -07:00
|
|
|
def set_template(self, template: str) -> None:
|
2015-02-12 14:57:46 -08:00
|
|
|
self.template = template
|
|
|
|
|
2022-05-17 13:57:04 -07:00
|
|
|
def determine_name(self, ext: str) -> str:
|
2022-11-22 16:51:26 -08:00
|
|
|
class Default(dict[str, Any]):
|
2022-05-17 13:57:04 -07:00
|
|
|
def __missing__(self, key: str) -> str:
|
2022-04-18 18:44:20 -07:00
|
|
|
return "{" + key + "}"
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2022-04-18 18:44:20 -07:00
|
|
|
md = self.metadata
|
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
|
|
|
|
2022-04-18 18:44:20 -07:00
|
|
|
template = self.template
|
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
|
|
|
|
2022-04-18 18:44:20 -07:00
|
|
|
new_name = ""
|
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
|
|
|
|
2022-11-23 22:16:46 -08:00
|
|
|
fmt = MetadataFormatter(self.smart_cleanup, platform=self.platform, replacements=self.replacements)
|
2022-04-18 18:59:17 -07:00
|
|
|
md_dict = vars(md)
|
2024-06-21 20:07:07 -07:00
|
|
|
md_dict.update(
|
|
|
|
dict(
|
|
|
|
month_name=None,
|
|
|
|
month_abbr=None,
|
|
|
|
date=None,
|
|
|
|
genre=None,
|
|
|
|
story_arc=None,
|
|
|
|
series_group=None,
|
|
|
|
web_link=None,
|
|
|
|
character=None,
|
|
|
|
team=None,
|
|
|
|
location=None,
|
|
|
|
)
|
|
|
|
)
|
2024-02-17 17:40:21 -08:00
|
|
|
|
2023-06-15 20:25:19 -07:00
|
|
|
md_dict["issue"] = IssueString(md.issue).as_string(pad=self.issue_zero_padding)
|
2024-06-21 20:07:07 -07:00
|
|
|
for role in ["writer", "penciller", "inker", "colorist", "letterer", "cover artist", "editor", "translator"]:
|
2022-04-18 18:59:17 -07:00
|
|
|
md_dict[role] = md.get_primary_credit(role)
|
|
|
|
|
|
|
|
if (isinstance(md.month, int) or isinstance(md.month, str) and md.month.isdigit()) and 0 < int(md.month) < 13:
|
|
|
|
md_dict["month_name"] = calendar.month_name[int(md.month)]
|
|
|
|
md_dict["month_abbr"] = calendar.month_abbr[int(md.month)]
|
2024-06-21 20:07:07 -07:00
|
|
|
|
|
|
|
if md.year is not None and datetime.MINYEAR <= md.year <= datetime.MAXYEAR:
|
|
|
|
md_dict["date"] = datetime.datetime(year=md.year, month=md.month or 1, day=md.day or 1)
|
|
|
|
|
|
|
|
if md.genres:
|
|
|
|
md_dict["genre"] = sorted(md.genres)[0]
|
|
|
|
if md.story_arcs:
|
|
|
|
md_dict["story_arc"] = md.story_arcs[0]
|
|
|
|
if md.series_groups:
|
|
|
|
md_dict["series_group"] = md.series_groups[0]
|
|
|
|
if md.web_links:
|
|
|
|
md_dict["web_link"] = md.web_links[0]
|
|
|
|
if md.characters:
|
|
|
|
md_dict["character"] = sorted(md.characters)[0]
|
|
|
|
if md.teams:
|
|
|
|
md_dict["team"] = sorted(md.teams)[0]
|
|
|
|
if md.locations:
|
|
|
|
md_dict["location"] = sorted(md.locations)[0]
|
2022-04-18 18:59:17 -07:00
|
|
|
|
2022-07-18 12:17:13 -07:00
|
|
|
new_basename = ""
|
|
|
|
for component in pathlib.PureWindowsPath(template).parts:
|
2022-05-17 13:57:04 -07:00
|
|
|
new_basename = str(
|
2022-07-18 12:17:13 -07:00
|
|
|
sanitize_filename(fmt.vformat(component, args=[], kwargs=Default(md_dict)), platform=self.platform)
|
2022-04-18 22:46:46 -07:00
|
|
|
).strip()
|
|
|
|
new_name = os.path.join(new_name, new_basename)
|
2015-02-12 14:57:46 -08:00
|
|
|
|
2024-04-27 19:25:33 -07:00
|
|
|
if self.move_only:
|
|
|
|
new_folder = os.path.join(new_name, os.path.splitext(self.original_name)[0])
|
|
|
|
return new_folder + ext
|
2022-04-18 18:44:20 -07:00
|
|
|
if self.move:
|
2024-04-27 19:25:33 -07:00
|
|
|
return new_name.strip() + ext
|
|
|
|
return new_basename.strip() + ext
|