Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/cli.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
# WARNING: do not import unnecessary things here to keep cli startup time under | # WARNING: do not import unnecessary things here to keep cli startup time under | ||||
# control | # control | ||||
import os | import os | ||||
from typing import Any, Dict, Optional | from typing import Any, Dict, Optional | ||||
import click | import click | ||||
import yaml | |||||
from swh.core import config | from swh.core import config | ||||
from swh.core.cli import CONTEXT_SETTINGS | from swh.core.cli import CONTEXT_SETTINGS | ||||
from swh.core.cli import swh as swh_cli_group | from swh.core.cli import swh as swh_cli_group | ||||
# All generic config code should reside in swh.core.config | # All generic config code should reside in swh.core.config | ||||
DEFAULT_CONFIG_PATH = os.environ.get( | CONFIG_ENVVAR = "SWH_CONFIG_FILE" | ||||
"SWH_CONFIG_FILE", os.path.join(click.get_app_dir("swh"), "global.yml") | DEFAULT_CONFIG_PATH = os.path.join(click.get_app_dir("swh"), "global.yml") | ||||
) | DEFAULT_PATH = os.environ.get(CONFIG_ENVVAR, DEFAULT_CONFIG_PATH) | ||||
DEFAULT_CONFIG: Dict[str, Any] = { | DEFAULT_CONFIG: Dict[str, Any] = { | ||||
"web-api": { | "web-api": { | ||||
"url": "https://archive.softwareheritage.org/api/1/", | "url": "https://archive.softwareheritage.org/api/1/", | ||||
"auth-token": None, | "auth-token": None, | ||||
} | } | ||||
} | } | ||||
@swh_cli_group.group(name="scanner", context_settings=CONTEXT_SETTINGS) | CONFIG_FILE_HELP = f"""Configuration file: | ||||
\b | |||||
The CLI option or the environment variable will fail if invalid. | |||||
CLI option is checked first. | |||||
Then, environment variable {CONFIG_ENVVAR} is checked. | |||||
Then, if cannot load the default path, a set of default values are used. | |||||
Default config path is {DEFAULT_CONFIG_PATH}. | |||||
Default config values are: | |||||
\b | |||||
{yaml.dump(DEFAULT_CONFIG)}""" | |||||
SCANNER_HELP = f"""Software Heritage Scanner tools. | |||||
{CONFIG_FILE_HELP}""" | |||||
@swh_cli_group.group( | |||||
name="scanner", context_settings=CONTEXT_SETTINGS, help=SCANNER_HELP, | |||||
) | |||||
@click.option( | @click.option( | ||||
"-C", | "-C", | ||||
"--config-file", | "--config-file", | ||||
default=None, | default=None, | ||||
type=click.Path(exists=False, dir_okay=False, path_type=str), | type=click.Path(exists=False, dir_okay=False, path_type=str), | ||||
help=f"""YAML configuration file. If absent and cannot load the default one, | help="""YAML configuration file""", | ||||
zack: I guess you wanted this line to be CONFIG_FILE_HELP, and I don't get why CONFIG_FILE_HELP is… | |||||
Done Inline ActionsIn fact, because of https://bugs.python.org/issue28739 and also because it is quite long text, I moved config details to the scanner help. It gives it more space and make it actually readable. Click does not let use format the text, it even removes single line breaks... tenma: In fact, because of https://bugs.python.org/issue28739 and also because it is quite long text… | |||||
Done Inline Actions(sorry, last comment not complete) tenma: (sorry, last comment not complete)
Test it and see if you like it.
Also pay attention: there is… | |||||
default parameters are used. | |||||
Default config path is {DEFAULT_CONFIG_PATH}. | |||||
Default config values are {DEFAULT_CONFIG} | |||||
""", | |||||
) | ) | ||||
@click.pass_context | @click.pass_context | ||||
def scanner(ctx, config_file: Optional[str]): | def scanner(ctx, config_file: Optional[str]): | ||||
"""Software Heritage Scanner tools.""" | if config_file is None and config.config_exists(DEFAULT_PATH): | ||||
if config_file is None and config.config_exists(DEFAULT_CONFIG_PATH): | config_file = DEFAULT_PATH | ||||
config_file = DEFAULT_CONFIG_PATH | |||||
if config_file is None: | if config_file is None: | ||||
conf = DEFAULT_CONFIG | conf = DEFAULT_CONFIG | ||||
else: | else: | ||||
# read_raw_config do not fail on ENOENT | # read_raw_config do not fail on ENOENT | ||||
if not config.config_exists(config_file): | if not config.config_exists(config_file): | ||||
raise FileNotFoundError(config_file) | raise FileNotFoundError(config_file) | ||||
conf = config.read_raw_config(config.config_basepath(config_file)) | conf = config.read_raw_config(config.config_basepath(config_file)) | ||||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |
I guess you wanted this line to be CONFIG_FILE_HELP, and I don't get why CONFIG_FILE_HELP is referencing itself. Am I missing something magic or are these typos?