From 1ce32d2f18881d3484889c6a20758dbf0a8d59d9 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 31 May 2021 18:19:05 +0200 Subject: [PATCH] Add CLI option for credential file auth to kit-ilias-web --- CHANGELOG.md | 1 + PFERD/__main__.py | 5 ++++- PFERD/cli/__init__.py | 5 +++-- PFERD/cli/command_kit_ilias_web.py | 23 +++++++++++++++++++---- PFERD/cli/parser.py | 4 ++++ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dffa1e..451853b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ ambiguous situations. ### Added - `credential-file` authenticator +- `--credential-file` option for `kit-ilias-web` command ### Fixed - Date parsing now also works correctly in non-group exercises diff --git a/PFERD/__main__.py b/PFERD/__main__.py index 9d61264..1cca8b1 100644 --- a/PFERD/__main__.py +++ b/PFERD/__main__.py @@ -5,7 +5,7 @@ import os import sys from pathlib import Path -from .cli import PARSER, load_default_section +from .cli import PARSER, ParserLoadError, load_default_section from .config import Config, ConfigDumpError, ConfigLoadError, ConfigOptionError from .logging import log from .pferd import Pferd, PferdLoadError @@ -36,6 +36,9 @@ def load_config(args: argparse.Namespace) -> Config: log.error(str(e)) log.error_contd(e.reason) sys.exit(1) + except ParserLoadError as e: + log.error(str(e)) + sys.exit(1) def configure_logging_from_args(args: argparse.Namespace) -> None: diff --git a/PFERD/cli/__init__.py b/PFERD/cli/__init__.py index f9cb5d2..d70ecd9 100644 --- a/PFERD/cli/__init__.py +++ b/PFERD/cli/__init__.py @@ -1,11 +1,12 @@ # isort: skip_file # The order of imports matters because each command module registers itself -# with the parser from ".parser". Because of this, isort is disabled for this +# with the parser from ".parser" and the import order affects the order in +# which they appear in the help. Because of this, isort is disabled for this # file. Also, since we're reexporting or just using the side effect of # importing itself, we get a few linting warnings, which we're disabling as # well. from . import command_local # noqa: F401 imported but unused from . import command_kit_ilias_web # noqa: F401 imported but unused -from .parser import PARSER, load_default_section # noqa: F401 imported but unused +from .parser import PARSER, ParserLoadError, load_default_section # noqa: F401 imported but unused diff --git a/PFERD/cli/command_kit_ilias_web.py b/PFERD/cli/command_kit_ilias_web.py index c21b6a4..12803a6 100644 --- a/PFERD/cli/command_kit_ilias_web.py +++ b/PFERD/cli/command_kit_ilias_web.py @@ -4,7 +4,8 @@ from pathlib import Path from ..crawl.ilias.file_templates import Links from ..logging import log -from .parser import CRAWLER_PARSER, SUBPARSERS, BooleanOptionalAction, load_crawler, show_value_error +from .parser import (CRAWLER_PARSER, SUBPARSERS, BooleanOptionalAction, ParserLoadError, load_crawler, + show_value_error) SUBPARSER = SUBPARSERS.add_parser( "kit-ilias-web", @@ -38,6 +39,12 @@ GROUP.add_argument( action=BooleanOptionalAction, help="use the system keyring to store and retrieve passwords" ) +GROUP.add_argument( + "--credential-file", + type=Path, + metavar="PATH", + help="read username and password from a credential file" +) GROUP.add_argument( "--links", type=show_value_error(Links.from_string), @@ -88,11 +95,19 @@ def load( parser["auth:ilias"] = {} auth_section = parser["auth:ilias"] - auth_section["type"] = "simple" + if args.credential_file is not None: + if args.username is not None: + raise ParserLoadError("--credential-file and --username can't be used together") + if args.keyring: + raise ParserLoadError("--credential-file and --keyring can't be used together") + auth_section["type"] = "credential-file" + auth_section["path"] = str(args.credential_file) + elif args.keyring: + auth_section["type"] = "keyring" + else: + auth_section["type"] = "simple" if args.username is not None: auth_section["username"] = args.username - if args.keyring: - auth_section["type"] = "keyring" SUBPARSER.set_defaults(command=load) diff --git a/PFERD/cli/parser.py b/PFERD/cli/parser.py index 754b8ad..f5fb215 100644 --- a/PFERD/cli/parser.py +++ b/PFERD/cli/parser.py @@ -8,6 +8,10 @@ from ..output_dir import OnConflict, Redownload from ..version import NAME, VERSION +class ParserLoadError(Exception): + pass + + # TODO Replace with argparse version when updating to 3.9? class BooleanOptionalAction(argparse.Action): def __init__(