Configuration_retrievers

This module contains functionality related to the the configuration_retrievers module for core.

Configuration_retrievers

BaseConfigurationRetriever

Bases: ABC

Abstract base class for configuration retrieval.

This class defines the common interface and functionality for retrieving application configurations from various sources. Concrete implementations should extend this class to provide specific retrieval mechanisms.

Attributes:
  • CONFIGURATIONS_DIR (str) –

    Directory where configuration files are stored.

Source code in src/core/configuration_retrievers.py
 9
10
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
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
79
80
class BaseConfigurationRetriever(ABC):
    """Abstract base class for configuration retrieval.

    This class defines the common interface and functionality for retrieving
    application configurations from various sources. Concrete implementations
    should extend this class to provide specific retrieval mechanisms.

    Attributes:
        CONFIGURATIONS_DIR (str): Directory where configuration files are stored.
    """

    CONFIGURATIONS_DIR = "configurations"

    def __init__(
        self,
        configuration_class: Type[BaseConfiguration],
        metadata: MetadataConfiguration,
        logger: logging.Logger = LoggerConfiguration.get_logger(__name__),
    ):
        """Initialize the configuration retriever.

        Args:
            configuration_class: Class of the configuration object to be retrieved.
            metadata: Applicaton metadata.
            logger: Logger instance for this class.
        """
        self.metadata = metadata
        self.logger = logger
        self._configuration_class = configuration_class
        self.configuration = None

    @abstractmethod
    def get(self, verbose: bool = True) -> BaseConfiguration:
        """Retrieve the configuration.

        Args:
            verbose: Whether to log detailed information during retrieval.

        Returns:
            The parsed configuration object of `_configuration_class` type.
        """
        pass

    def _parse_configuration(self, configuration_json: dict, verbose=True):
        """Parse JSON configuration into a configuration object.

        Args:
            configuration_json: Dictionary containing the configuration data.
            verbose: Whether to log the parsed configuration.

        Returns:
            Parsed configuration object of `_configuration_class` type..
        """
        secrets_filepath = self._get_secrets_filepath()
        configuration = self._configuration_class.model_validate_json(
            configuration_json, context={"secrets_file": secrets_filepath}
        )
        configuration.metadata = self.metadata

        if verbose:
            self.logger.info(f"::Environment:{self.metadata.environment}")
            self.logger.info(configuration.model_dump_json(indent=4))

        return configuration

    def _get_secrets_filepath(self) -> str:
        """Get the path to the secrets file based on environment.

        Returns:
            Path to the secrets file.
        """
        return f"{OnPremConfigurationRetriever.CONFIGURATIONS_DIR}/secrets.{self.metadata.environment.value}.env"

__init__(configuration_class, metadata, logger=LoggerConfiguration.get_logger(__name__))

Initialize the configuration retriever.

Parameters:
  • configuration_class (Type[BaseConfiguration]) –

    Class of the configuration object to be retrieved.

  • metadata (MetadataConfiguration) –

    Applicaton metadata.

  • logger (Logger, default: get_logger(__name__) ) –

    Logger instance for this class.

Source code in src/core/configuration_retrievers.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def __init__(
    self,
    configuration_class: Type[BaseConfiguration],
    metadata: MetadataConfiguration,
    logger: logging.Logger = LoggerConfiguration.get_logger(__name__),
):
    """Initialize the configuration retriever.

    Args:
        configuration_class: Class of the configuration object to be retrieved.
        metadata: Applicaton metadata.
        logger: Logger instance for this class.
    """
    self.metadata = metadata
    self.logger = logger
    self._configuration_class = configuration_class
    self.configuration = None

get(verbose=True) abstractmethod

Retrieve the configuration.

Parameters:
  • verbose (bool, default: True ) –

    Whether to log detailed information during retrieval.

Returns:
  • BaseConfiguration

    The parsed configuration object of _configuration_class type.

Source code in src/core/configuration_retrievers.py
40
41
42
43
44
45
46
47
48
49
50
@abstractmethod
def get(self, verbose: bool = True) -> BaseConfiguration:
    """Retrieve the configuration.

    Args:
        verbose: Whether to log detailed information during retrieval.

    Returns:
        The parsed configuration object of `_configuration_class` type.
    """
    pass

ConfiguratioRetriverRegistry

Registry for configuration retrievers.

Provides factory methods to get the appropriate configuration retriever based on context.

Source code in src/core/configuration_retrievers.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class ConfiguratioRetriverRegistry:
    """Registry for configuration retrievers.

    Provides factory methods to get the appropriate configuration retriever based on context.
    """

    def get(on_prem: bool) -> BaseConfigurationRetriever:
        """Get the appropriate configuration retriever class.

        Args:
            on_prem: Whether to use on-premise configuration or remote.

        Returns:
            The appropriate configuration retriever class.
        """
        if on_prem:
            return OnPremConfigurationRetriever
        else:
            return RemoteConfigurationRetriever

get(on_prem)

Get the appropriate configuration retriever class.

Parameters:
  • on_prem (bool) –

    Whether to use on-premise configuration or remote.

Returns:
Source code in src/core/configuration_retrievers.py
159
160
161
162
163
164
165
166
167
168
169
170
171
def get(on_prem: bool) -> BaseConfigurationRetriever:
    """Get the appropriate configuration retriever class.

    Args:
        on_prem: Whether to use on-premise configuration or remote.

    Returns:
        The appropriate configuration retriever class.
    """
    if on_prem:
        return OnPremConfigurationRetriever
    else:
        return RemoteConfigurationRetriever

OnPremConfigurationRetriever

Bases: BaseConfigurationRetriever

Configuration retriever for on-premise file sources.

Retrieves configurations from local JSON files.

Source code in src/core/configuration_retrievers.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class OnPremConfigurationRetriever(BaseConfigurationRetriever):
    """Configuration retriever for on-premise file sources.

    Retrieves configurations from local JSON files.
    """

    def get(self, verbose: bool = True) -> BaseConfiguration:
        """Retrieve configuration from local files.

        This method implements caching to avoid re-reading configuration files.

        Args:
            verbose: Whether to log detailed information during retrieval.

        Returns:
            The parsed configuration object.
        """
        if not self.configuration:
            self.configuration = self._get(verbose)
        return self.configuration

    def _get(self, verbose) -> BaseConfiguration:
        """Internal method to retrieve and parse configuration.

        Args:
            verbose: Whether to log detailed information during retrieval.

        Returns:
            The parsed configuration object.
        """
        configuration_filepath = self._get_configuration_filepath()
        with open(configuration_filepath) as f:
            configuration_json = f.read()
            return self._parse_configuration(
                configuration_json=configuration_json, verbose=verbose
            )

    def _get_configuration_filepath(self) -> str:
        """Get the path to the configuration file based on environment.

        Returns:
            Path to the configuration JSON file.
        """
        return f"{OnPremConfigurationRetriever.CONFIGURATIONS_DIR}/configuration.{self.metadata.environment.value}.json"

get(verbose=True)

Retrieve configuration from local files.

This method implements caching to avoid re-reading configuration files.

Parameters:
  • verbose (bool, default: True ) –

    Whether to log detailed information during retrieval.

Returns:
  • BaseConfiguration

    The parsed configuration object.

Source code in src/core/configuration_retrievers.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def get(self, verbose: bool = True) -> BaseConfiguration:
    """Retrieve configuration from local files.

    This method implements caching to avoid re-reading configuration files.

    Args:
        verbose: Whether to log detailed information during retrieval.

    Returns:
        The parsed configuration object.
    """
    if not self.configuration:
        self.configuration = self._get(verbose)
    return self.configuration

RemoteConfigurationRetriever

Bases: BaseConfigurationRetriever

Configuration retriever for remote sources.

Retrieves configuration from remote sources like config servers or cloud storage. Currently not implemented.

Source code in src/core/configuration_retrievers.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
class RemoteConfigurationRetriever(BaseConfigurationRetriever):
    """Configuration retriever for remote sources.

    Retrieves configuration from remote sources like config servers or cloud storage.
    Currently not implemented.
    """

    def get(self, verbose: bool = True) -> BaseConfiguration:
        """Retrieve configuration from a remote source.

        Args:
            verbose: Whether to log detailed information during retrieval.

        Raises:
            NotImplementedError: This functionality is not yet implemented.

        Returns:
            The configuration object.
        """
        raise NotImplementedError(
            "Remote configuration is not implemented yet."
        )

get(verbose=True)

Retrieve configuration from a remote source.

Parameters:
  • verbose (bool, default: True ) –

    Whether to log detailed information during retrieval.

Raises:
  • NotImplementedError

    This functionality is not yet implemented.

Returns:
  • BaseConfiguration

    The configuration object.

Source code in src/core/configuration_retrievers.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def get(self, verbose: bool = True) -> BaseConfiguration:
    """Retrieve configuration from a remote source.

    Args:
        verbose: Whether to log detailed information during retrieval.

    Raises:
        NotImplementedError: This functionality is not yet implemented.

    Returns:
        The configuration object.
    """
    raise NotImplementedError(
        "Remote configuration is not implemented yet."
    )