Skip to content

Config

authkeys.config

Configuration loading for authkeys.

AuthkeysConfig is a thin :class:configparser.ConfigParser subclass with a getlist converter (newline-separated values) and a flexible from_config loader that accepts paths, colon-separated path lists, raw text, bytes, or dicts.

ConfigSource = Union[str, bytes, Path, dict] module-attribute

SYSTEM_CONF_PATHS = [Path('/etc/authkeys.conf'), Path('/etc/authkeys/authkeys.conf'), Path('/etc/ssh/authkeys.conf')] module-attribute

USER_CONF_PATH = Path('.ssh/authkeys.conf') module-attribute

AuthkeysConfig

Bases: ConfigParser

CONF_PATHS = [Path('./authkeys.conf')] class-attribute instance-attribute

from_config(config=None) classmethod

Source code in src/authkeys/config.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
@classmethod
def from_config(cls, config: "ConfigSource | Iterable[ConfigSource] | None" = None):
    conf = cls()
    conf.read_dict({"cache": {}, "globals": {}})

    if config is None:
        configs: Iterable = cls.CONF_PATHS
    elif isinstance(config, str):
        configs = _split_path_list(config)
    elif isinstance(config, (bytes, Path, dict)):
        configs = [config]
    else:
        configs = config

    for item in configs:
        if isinstance(item, str):
            item = Path(item)
        if isinstance(item, Path):
            if item.exists():
                conf.read_string(_interpolate_env(item.read_text()))
                break
        elif isinstance(item, bytes):
            conf.read_string(_interpolate_env(item.decode()))
        elif isinstance(item, dict):
            conf.read_dict(item)
    return conf

getlist(section, option, **kwargs)

Source code in src/authkeys/config.py
73
74
75
76
77
78
79
80
def getlist(self, section, option, **kwargs) -> "List[str]":
    value = self.get(section, option, **kwargs)
    if not isinstance(value, str):
        # A non-string fallback (e.g. a default list) was returned because
        # the option is absent; return a copy so a caller mutating the result
        # can't corrupt the shared default object.
        return list(value) if value is not None else value
    return list(filter(None, (x.strip() for x in value.splitlines())))