Reformat and switch to ruff

This commit is contained in:
I-Al-Istannen
2025-10-19 15:19:43 +02:00
parent ee4625be78
commit 2cf0e060ed
31 changed files with 1507 additions and 587 deletions

View File

@@ -15,15 +15,15 @@ class ParserLoadError(Exception):
# TODO Replace with argparse version when updating to 3.9?
class BooleanOptionalAction(argparse.Action):
def __init__(
self,
option_strings: List[str],
dest: Any,
default: Any = None,
type: Any = None,
choices: Any = None,
required: Any = False,
help: Any = None,
metavar: Any = None,
self,
option_strings: List[str],
dest: Any,
default: Any = None,
type: Any = None,
choices: Any = None,
required: Any = False,
help: Any = None,
metavar: Any = None,
):
if len(option_strings) != 1:
raise ValueError("There must be exactly one option string")
@@ -48,11 +48,11 @@ class BooleanOptionalAction(argparse.Action):
)
def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: Union[str, Sequence[Any], None],
option_string: Optional[str] = None,
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: Union[str, Sequence[Any], None],
option_string: Optional[str] = None,
) -> None:
if option_string and option_string in self.option_strings:
value = not option_string.startswith("--no-")
@@ -67,11 +67,13 @@ def show_value_error(inner: Callable[[str], Any]) -> Callable[[str], Any]:
Some validation functions (like the from_string in our enums) raise a ValueError.
Argparse only pretty-prints ArgumentTypeErrors though, so we need to wrap our ValueErrors.
"""
def wrapper(input: str) -> Any:
try:
return inner(input)
except ValueError as e:
raise ArgumentTypeError(e)
return wrapper
@@ -81,52 +83,57 @@ CRAWLER_PARSER_GROUP = CRAWLER_PARSER.add_argument_group(
description="arguments common to all crawlers",
)
CRAWLER_PARSER_GROUP.add_argument(
"--redownload", "-r",
"--redownload",
"-r",
type=show_value_error(Redownload.from_string),
metavar="OPTION",
help="when to download a file that's already present locally"
help="when to download a file that's already present locally",
)
CRAWLER_PARSER_GROUP.add_argument(
"--on-conflict",
type=show_value_error(OnConflict.from_string),
metavar="OPTION",
help="what to do when local and remote files or directories differ"
help="what to do when local and remote files or directories differ",
)
CRAWLER_PARSER_GROUP.add_argument(
"--transform", "-T",
"--transform",
"-T",
action="append",
type=str,
metavar="RULE",
help="add a single transformation rule. Can be specified multiple times"
help="add a single transformation rule. Can be specified multiple times",
)
CRAWLER_PARSER_GROUP.add_argument(
"--tasks", "-n",
"--tasks",
"-n",
type=int,
metavar="N",
help="maximum number of concurrent tasks (crawling, downloading)"
help="maximum number of concurrent tasks (crawling, downloading)",
)
CRAWLER_PARSER_GROUP.add_argument(
"--downloads", "-N",
"--downloads",
"-N",
type=int,
metavar="N",
help="maximum number of tasks that may download data at the same time"
help="maximum number of tasks that may download data at the same time",
)
CRAWLER_PARSER_GROUP.add_argument(
"--task-delay", "-d",
"--task-delay",
"-d",
type=float,
metavar="SECONDS",
help="time the crawler should wait between subsequent tasks"
help="time the crawler should wait between subsequent tasks",
)
CRAWLER_PARSER_GROUP.add_argument(
"--windows-paths",
action=BooleanOptionalAction,
help="whether to repair invalid paths on windows"
help="whether to repair invalid paths on windows",
)
def load_crawler(
args: argparse.Namespace,
section: configparser.SectionProxy,
args: argparse.Namespace,
section: configparser.SectionProxy,
) -> None:
if args.redownload is not None:
section["redownload"] = args.redownload.value
@@ -152,79 +159,79 @@ PARSER.add_argument(
version=f"{NAME} {VERSION} (https://github.com/Garmelon/PFERD)",
)
PARSER.add_argument(
"--config", "-c",
"--config",
"-c",
type=Path,
metavar="PATH",
help="custom config file"
help="custom config file",
)
PARSER.add_argument(
"--dump-config",
action="store_true",
help="dump current configuration to the default config path and exit"
help="dump current configuration to the default config path and exit",
)
PARSER.add_argument(
"--dump-config-to",
metavar="PATH",
help="dump current configuration to a file and exit."
" Use '-' as path to print to stdout instead"
help="dump current configuration to a file and exit. Use '-' as path to print to stdout instead",
)
PARSER.add_argument(
"--debug-transforms",
action="store_true",
help="apply transform rules to files of previous run"
help="apply transform rules to files of previous run",
)
PARSER.add_argument(
"--crawler", "-C",
"--crawler",
"-C",
action="append",
type=str,
metavar="NAME",
help="only execute a single crawler."
" Can be specified multiple times to execute multiple crawlers"
help="only execute a single crawler. Can be specified multiple times to execute multiple crawlers",
)
PARSER.add_argument(
"--skip", "-S",
"--skip",
"-S",
action="append",
type=str,
metavar="NAME",
help="don't execute this particular crawler."
" Can be specified multiple times to skip multiple crawlers"
help="don't execute this particular crawler. Can be specified multiple times to skip multiple crawlers",
)
PARSER.add_argument(
"--working-dir",
type=Path,
metavar="PATH",
help="custom working directory"
help="custom working directory",
)
PARSER.add_argument(
"--explain",
action=BooleanOptionalAction,
help="log and explain in detail what PFERD is doing"
help="log and explain in detail what PFERD is doing",
)
PARSER.add_argument(
"--status",
action=BooleanOptionalAction,
help="print status updates while PFERD is crawling"
help="print status updates while PFERD is crawling",
)
PARSER.add_argument(
"--report",
action=BooleanOptionalAction,
help="print a report of all local changes before exiting"
help="print a report of all local changes before exiting",
)
PARSER.add_argument(
"--share-cookies",
action=BooleanOptionalAction,
help="whether crawlers should share cookies where applicable"
help="whether crawlers should share cookies where applicable",
)
PARSER.add_argument(
"--show-not-deleted",
action=BooleanOptionalAction,
help="print messages in status and report when PFERD did not delete a local only file"
help="print messages in status and report when PFERD did not delete a local only file",
)
def load_default_section(
args: argparse.Namespace,
parser: configparser.ConfigParser,
args: argparse.Namespace,
parser: configparser.ConfigParser,
) -> None:
section = parser[parser.default_section]