Config
User guide: Config.
Usage
from clak import Parser, XDGConfFileMixin
class App(XDGConfFileMixin, Parser):
class Meta:
app_name = "cool-cli"
# config_required = True # fail if file is missing
def cli_run(self, ctx, **_):
# dict from file (or {} if missing)
print(ctx.config)
# attribute access on the root parser
print(self.config.get("debug"))
if __name__ == "__main__":
App()
- JSON (
.json): always available (stdlib). - YAML (
.yaml/.yml): requires optional extra:pip install 'mrjk.clak[config]'. - Missing file → empty config unless
Meta.config_required = True. - Config is not merged into CLI args; read
ctx.config/self.configexplicitly.
clak.comp.config
XDG Base Directory path helpers and config-file loading.
Each XDG path flag is its own opt-in mixin, with defaults from
Meta.app_name / $XDG_*:
XDGConfFileMixin:--conf-file, loaded once viacli_hook__configXDGDataDirMixin:--data-dirXDGCacheDirMixin:--cache-dirXDGLogDirMixin:--log-dir
Plus WorkDirMixin (-W/--work-dir), which changes the process working
directory. XDGConfFileMixin looks up a project-local config file in the
work directory before falling back to the XDG default.
WorkDirMixin
-W/--work-dir flag: run as if started in another directory.
Defaults to the current directory inherited from the parent process.
The resolved path is available as ctx.work_dir and
ctx.plugins["work_dir"].
Source code in clak/comp/config.py
XDGCacheDirMixin
XDGConfFileMixin
Bases: _XDGPathMixin
--conf-file flag and config-file loading.
Default: $XDG_CONFIG_HOME/<app>/config.yaml, where <app> comes
from Meta.app_name, else the parser name / class name.
Lookup order (first match wins, one file is loaded):
- explicit
--conf-file(relative paths resolve against the work dir) - first existing file from
Meta.config_local_namesin the work dir (default.<app>.yaml,.<app>.yml,.<app>.json;{app}is substituted;[]disables the local lookup) - the XDG default above
The work dir is --work-dir when WorkDirMixin is used, else the cwd.
On dispatch, cli_hook__config loads --conf-file (JSON always;
YAML with the config extra). Missing file yields {} unless
Meta.config_required is true. Loaded data is available as
ctx.config (dict) and cli_root.config (attribute namespace).
Source code in clak/comp/config.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | |
cli_hook__config(instance, ctx, **_)
Load --conf-file once and expose it on ctx / root.
Source code in clak/comp/config.py
XDGDataDirMixin
XDGLogDirMixin
load_config_file(path)
Load a mapping from a JSON or YAML config file.
Format is detected from the file suffix (.json, .yaml, .yml).
YAML requires the optional config extra (PyYAML).
Raises:
| Type | Description |
|---|---|
ClakUserError
|
Unknown suffix, missing PyYAML, I/O/parse error, or non-mapping root document. |
Source code in clak/comp/config.py
resolve_work_dir(ctx)
Resolve (and apply once) the work directory for this dispatch.
Idempotent: the first call reads ctx.args.work_dir; when set, it
changes the process cwd to it. The result is cached in
ctx.plugins["work_dir"]. Without --work-dir, the inherited cwd is
the work directory. Any hook may call this, whatever the hook order.
Raises:
| Type | Description |
|---|---|
ClakUserError
|
|
Source code in clak/comp/config.py
resolve_xdg_paths(app_name)
Build conf/data/cache/log paths for app_name under XDG bases.
Source code in clak/comp/config.py
sanitize_xdg_app_name(name)
Turn an app name into a safe path segment under XDG directories.
xdg_dir(env_var, default=None)
Resolve an XDG base directory from the environment.
Uses $env_var when set and non-empty; otherwise expands default
(or the XDG Base Directory default for that variable).