Skip to content

config_updater

Utility functions for updating GenericConfig instances.

populate_config_from_session(session, config)

Update the configuration attributes of a GenericConfig object using values from a session. If a key already exists in the config's configuration and has a different value from the session, an exception will be raised.

Parameters:

Name Type Description Default
session Dict

A session containing configuration attributes.

required
config GenericConfig

A GenericConfig object to be updated.

required
Source code in oteapi/utils/config_updater.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def populate_config_from_session(
    session: "Dict[str, Any]",
    config: GenericConfig,
) -> None:
    """
    Update the configuration attributes of a GenericConfig object
    using values from a session. If a key already exists in the
    config's configuration and has a different value from the session,
    an exception will be raised.

    Args:
        session (Dict): A session containing configuration attributes.
        config (GenericConfig): A GenericConfig object to be updated.
    """
    # Determine which keys to update
    keys_to_update = list(session.keys())

    for key in keys_to_update:
        if (
            # "configuration" in config
            key in config.configuration
            and session[key] != config.configuration[key]
        ):
            raise ValueError(
                f"Key '{key}' in config has different value than in session."
            )

        try:
            config.configuration[key] = session[key]
        except Exception as error:
            raise RuntimeError(
                f"Failed to update key '{key}' in the config. Reason: {str(error)}"
            ) from error