File: config.py

package info (click to toggle)
python-cads-api-client 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 248 kB
  • sloc: python: 2,041; makefile: 42
file content (29 lines) | stat: -rw-r--r-- 802 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from __future__ import annotations

import json
import os
from typing import Any

SUPPORTED_API_VERSION = "v1"


def read_configuration_file(config_path: str | None = None) -> dict[Any, Any]:
    if config_path is None:
        config_path = os.getenv("CADS_API_RC", "~/.cads-api-client.json")
    config_path = os.path.expanduser(config_path)
    try:
        with open(config_path) as fin:
            config = json.load(fin)
        assert isinstance(config, dict)
    except FileNotFoundError:
        raise
    except Exception:
        raise ValueError(f"failed to parse {config_path!r} file")
    return config


def get_config(key: str, config_path: str | None = None) -> Any:
    return (
        os.getenv(f"CADS_API_{key.upper()}")
        or read_configuration_file(config_path)[key]
    )