Skip to content

Configuration and Settings

CalendarApi comes with a lightweight toml config loader and parser for advanced usage.

This module is not yet integrated and exposed as public API

ConfigSection dataclass

ConfigSection()

Methods:

  • from_config_file

    Charge la configuration depuis un fichier toml respectant la structure attendue.

from_config_file classmethod

from_config_file(
    fpath: str | Path, *, with_section: bool = True
) -> ConfigSection

Charge la configuration depuis un fichier toml respectant la structure attendue.

Parameters:

  • fpath

    (str) –

    Chemin du fichier de configuration .toml

Returns:

  • DatashopConfig ( ConfigSection ) –

    Instance configurée selon le fichier renseigné.

Raises:

  • ConfigurationError

    Si strict=True et que le fichier de configuration n'existe pas!

Source code in pycalendar_api/config/base.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@classmethod
def from_config_file(cls, fpath: str|Path, *, with_section: bool=True) -> "ConfigSection":
    """Charge la configuration depuis un fichier toml respectant la structure attendue.

    Args:
        fpath (str): Chemin du fichier de configuration `.toml`

    Returns:
        DatashopConfig: Instance configurée selon le fichier renseigné.

    Raises:
        ConfigurationError: Si `strict=True` et que le fichier de configuration n'existe pas!
    """
    fpath = Path(fpath)
    if not fpath.exists():
        raise ConfigurationError(f"Le chemin du fichier de configuration n'existe pas. {fpath=}")

    dico = {}
    try:
        with open(fpath) as fd:
            dico = tomllib.loads(fd.read())

        dico = toml_doc_to_dict(dico)
    except PermissionError:
        msg = f"Accès refusé au chemin {fpath=}"
        raise ConfigurationError(msg)

    if cls.section == 'root':
        return cls(**dico)

    if with_section:
        kwargs = dico.get(cls.section, {})
        if cls.key is None:
            return cls(**kwargs)
        else:
            return cls(**kwargs.get(cls.key, {}))
    else:
        # Flat file without nesting
        if cls.key:
            return cls(**dico.get(cls.key, {}))

ApiConfig dataclass

ApiConfig(
    section: ClassVar = "api",
    host: str = "https://calendar-api.ma",
    api_endpoint: str = "/api/v1",
    api_key: str = "UNDEFINED",
)

API server config section

Methods:

  • from_config_file

    Charge la configuration depuis un fichier toml respectant la structure attendue.

from_config_file classmethod

from_config_file(
    fpath: str | Path, *, with_section: bool = True
) -> ConfigSection

Charge la configuration depuis un fichier toml respectant la structure attendue.

Parameters:

  • fpath

    (str) –

    Chemin du fichier de configuration .toml

Returns:

  • DatashopConfig ( ConfigSection ) –

    Instance configurée selon le fichier renseigné.

Raises:

  • ConfigurationError

    Si strict=True et que le fichier de configuration n'existe pas!

Source code in pycalendar_api/config/base.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@classmethod
def from_config_file(cls, fpath: str|Path, *, with_section: bool=True) -> "ConfigSection":
    """Charge la configuration depuis un fichier toml respectant la structure attendue.

    Args:
        fpath (str): Chemin du fichier de configuration `.toml`

    Returns:
        DatashopConfig: Instance configurée selon le fichier renseigné.

    Raises:
        ConfigurationError: Si `strict=True` et que le fichier de configuration n'existe pas!
    """
    fpath = Path(fpath)
    if not fpath.exists():
        raise ConfigurationError(f"Le chemin du fichier de configuration n'existe pas. {fpath=}")

    dico = {}
    try:
        with open(fpath) as fd:
            dico = tomllib.loads(fd.read())

        dico = toml_doc_to_dict(dico)
    except PermissionError:
        msg = f"Accès refusé au chemin {fpath=}"
        raise ConfigurationError(msg)

    if cls.section == 'root':
        return cls(**dico)

    if with_section:
        kwargs = dico.get(cls.section, {})
        if cls.key is None:
            return cls(**kwargs)
        else:
            return cls(**kwargs.get(cls.key, {}))
    else:
        # Flat file without nesting
        if cls.key:
            return cls(**dico.get(cls.key, {}))

HTTPClientConfig dataclass

HTTPClientConfig(
    section: ClassVar = "http_client",
    default_encoding: str = "utf-8",
    timeout: int = 10,
    retries: int = 3,
    verify: bool = True,
    follow_redirects: bool = True,
)

HTTP client config section

Methods:

  • from_config_file

    Charge la configuration depuis un fichier toml respectant la structure attendue.

from_config_file classmethod

from_config_file(
    fpath: str | Path, *, with_section: bool = True
) -> ConfigSection

Charge la configuration depuis un fichier toml respectant la structure attendue.

Parameters:

  • fpath

    (str) –

    Chemin du fichier de configuration .toml

Returns:

  • DatashopConfig ( ConfigSection ) –

    Instance configurée selon le fichier renseigné.

Raises:

  • ConfigurationError

    Si strict=True et que le fichier de configuration n'existe pas!

Source code in pycalendar_api/config/base.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@classmethod
def from_config_file(cls, fpath: str|Path, *, with_section: bool=True) -> "ConfigSection":
    """Charge la configuration depuis un fichier toml respectant la structure attendue.

    Args:
        fpath (str): Chemin du fichier de configuration `.toml`

    Returns:
        DatashopConfig: Instance configurée selon le fichier renseigné.

    Raises:
        ConfigurationError: Si `strict=True` et que le fichier de configuration n'existe pas!
    """
    fpath = Path(fpath)
    if not fpath.exists():
        raise ConfigurationError(f"Le chemin du fichier de configuration n'existe pas. {fpath=}")

    dico = {}
    try:
        with open(fpath) as fd:
            dico = tomllib.loads(fd.read())

        dico = toml_doc_to_dict(dico)
    except PermissionError:
        msg = f"Accès refusé au chemin {fpath=}"
        raise ConfigurationError(msg)

    if cls.section == 'root':
        return cls(**dico)

    if with_section:
        kwargs = dico.get(cls.section, {})
        if cls.key is None:
            return cls(**kwargs)
        else:
            return cls(**kwargs.get(cls.key, {}))
    else:
        # Flat file without nesting
        if cls.key:
            return cls(**dico.get(cls.key, {}))

Settings dataclass

Settings(
    section: ClassVar = "root",
    api: ApiConfig = ApiConfig(),
    http_client: HTTPClientConfig = HTTPClientConfig(),
)

pycalendar_api client config file

Methods:

__post_init__

__post_init__()

One level config File

Source code in pycalendar_api/config/settings.py
49
50
51
52
53
54
55
def __post_init__(self):
    """One level config File"""
    for f in fields(self):
        attr = getattr(self, f.name)
        if isinstance(attr, dict):
            instance = f.type(**attr)
            setattr(self, f.name, instance)

from_config_file classmethod

from_config_file(
    fpath: str | Path, *, with_section: bool = True
) -> ConfigSection

Charge la configuration depuis un fichier toml respectant la structure attendue.

Parameters:

  • fpath

    (str) –

    Chemin du fichier de configuration .toml

Returns:

  • DatashopConfig ( ConfigSection ) –

    Instance configurée selon le fichier renseigné.

Raises:

  • ConfigurationError

    Si strict=True et que le fichier de configuration n'existe pas!

Source code in pycalendar_api/config/base.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@classmethod
def from_config_file(cls, fpath: str|Path, *, with_section: bool=True) -> "ConfigSection":
    """Charge la configuration depuis un fichier toml respectant la structure attendue.

    Args:
        fpath (str): Chemin du fichier de configuration `.toml`

    Returns:
        DatashopConfig: Instance configurée selon le fichier renseigné.

    Raises:
        ConfigurationError: Si `strict=True` et que le fichier de configuration n'existe pas!
    """
    fpath = Path(fpath)
    if not fpath.exists():
        raise ConfigurationError(f"Le chemin du fichier de configuration n'existe pas. {fpath=}")

    dico = {}
    try:
        with open(fpath) as fd:
            dico = tomllib.loads(fd.read())

        dico = toml_doc_to_dict(dico)
    except PermissionError:
        msg = f"Accès refusé au chemin {fpath=}"
        raise ConfigurationError(msg)

    if cls.section == 'root':
        return cls(**dico)

    if with_section:
        kwargs = dico.get(cls.section, {})
        if cls.key is None:
            return cls(**kwargs)
        else:
            return cls(**kwargs.get(cls.key, {}))
    else:
        # Flat file without nesting
        if cls.key:
            return cls(**dico.get(cls.key, {}))