Skip to content
Snippets Groups Projects
Verified Commit 7211ac98 authored by Dominik Rimpf's avatar Dominik Rimpf :chipmunk:
Browse files

chg: use ctx.obj propperly (according to documentation)

parent c8210a71
No related branches found
No related tags found
1 merge request!1Click support and argument deprecation
"""click framework helper functions for config loading"""
import configparser
import stat
import types
from pathlib import Path
from pprint import pprint
from typing import Optional
import click
from netdb_client.util.config import NETDBConfig
def load_config_section_values(config: configparser.ConfigParser, endpoint: str) -> dict[str, str]:
base_url = config.get(endpoint, 'base_url', fallback=None)
if base_url is None:
base_url = endpoint
token = config.get(endpoint, 'token', fallback=None)
def _get_defaults(config: NETDBConfig, endpoint: str) -> dict[str, str]:
base_url = config.base_url(endpoint)
token = config.token(endpoint)
return {'netdb_base_url': base_url, 'netdb_token': token}
......@@ -21,37 +20,30 @@ def netdb_config_callback(ctx: click.Context, _param: click.Parameter, config_pa
if ctx.resilient_parsing:
return
config = configparser.ConfigParser()
config_path_expanded = config_path.expanduser()
if config_path_expanded.is_file():
if config_path_expanded.stat().st_mode & stat.S_IRWXO:
raise click.ClickException('Config file is readable by others. Please set it at least to 600 but never other-readable.', )
config.read(config_path_expanded)
config = NETDBConfig(config_path=config_path)
ctx.obj = config
obj = ctx.ensure_object(dict)
obj['netdb-config'] = config # TODO: keep internal state somewhere else
endpoint = config.get('DEFAULT', 'endpoint', fallback=None)
default_endpoint = config.default_endpoint
# if we have no endpoint in config, we can abort here
if endpoint is None:
if default_endpoint is None:
return
ctx.default_map = ctx.default_map or {} # preserve existing defaults
ctx.default_map['netdb_endpoint'] = endpoint
ctx.default_map['netdb_endpoint'] = default_endpoint
config_options = load_config_section_values(config, endpoint)
ctx.default_map.update(config_options)
ctx.default_map.update(_get_defaults(config, default_endpoint))
def netdb_endpoint_callback(ctx: click.Context, _param: click.Parameter, endpoint: str):
def netdb_endpoint_callback(ctx: click.Context, _param: click.Parameter, endpoint: str) -> Optional[str]:
if ctx.resilient_parsing:
return
obj = ctx.find_object(dict)
config = ctx.find_object(NETDBConfig)
if config is None:
raise click.ClickException('Could not find config object in context. This should not happen.')
ctx.default_map = ctx.default_map or {} # preserve existing defaults
config_options = load_config_section_values(obj['netdb-config'], endpoint)
ctx.default_map.update(config_options)
ctx.default_map.update(_get_defaults(config, endpoint))
return endpoint
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment