Skip to content

settings

oxytcmri.infrastructure.settings

This module provides classes for managing application settings.

The Settings class encapsulates the Dynaconf settings object and provides a clear interface for accessing and modifying settings. It allows for easy access and modification of settings using attribute-style access.

The ModuleSettings class is a helper class within Settings. It handles the settings of individual modules, providing attribute-style access to the module's settings.

Example

Suppose you have a settings file tests-data/test_settings.toml with the following content:

[database]
url = "sqlite:///test-data/test.db"

You can then use the Settings class to access the settings as follows:

>>> settings = Settings("tests-data/test_settings.toml")
>>> print(settings.database.url)
sqlite:///test-data/test.db

Classes:

Name Description
Settings

A class to manage application settings.

ModuleSettings

A helper class to handle the settings of individual modules.

Settings(filename)

A class to manage application settings.

This class encapsulates the Dynaconf settings object and provides a clear interface for accessing and modifying settings.

Attributes:

Name Type Description
filepath Path

The path to the settings file.

_dynaconf_settings Dynaconf

The Dynaconf settings object.

Methods:

Name Description
__getattr__

Retrieve the value of the attribute with the given name.

__setattr__

Set the value of the attribute with the given name.

Examples:

>>> settings = Settings("tests-data/test_settings.toml")
>>> print(settings.database.url)
sqlite:///test-data/test.db

Constructs all the necessary attributes for the Settings object.

Parameters:

Name Type Description Default
filename str

The path to the settings file.

required

Raises:

Type Description
FileNotFoundError

If the settings file does not exist.

Source code in oxytcmri/infrastructure/settings.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def __init__(self, filename: str):
    """
    Constructs all the necessary attributes for the Settings object.

    Parameters
    ----------
    filename : str
        The path to the settings file.

    Raises
    ------
    FileNotFoundError
        If the settings file does not exist.
    """
    filepath = Path(filename).resolve()
    if not filepath.exists():
        raise FileNotFoundError(f"Settings file not found: '{filename}'.")
    self.filepath = filepath
    self._dynaconf_settings = Dynaconf(settings_file=filepath)
    self.base_dir = filepath.parent

filepath = filepath instance-attribute

base_dir = filepath.parent instance-attribute

to_toml(filepath)

Convert the settings to a string in TOML format and save it to a file.

Returns:

Type Description
str

The settings as a TOML string.

Source code in oxytcmri/infrastructure/settings.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def to_toml(self, filepath: str):
    """
    Convert the settings to a string in TOML format and save it to a file.

    Returns
    -------
    str
        The settings as a TOML string.
    """
    # convert dynaconf settings as dict
    settings_dict = self._dynaconf_settings.as_dict()

    # export to toml
    with open(filepath, "w") as file:
        toml.dump(settings_dict, file)

ModuleSettings(module_name, dynabox, filepath)

A helper class to handle the settings of individual modules.

This class encapsulates the settings of a specific module and provides attribute-style access to the module's settings.

Attributes:

Name Type Description
module_name str

The name of the module.

_dynabox DynaBox

The DynaBox object that holds the actual settings.

filepath Path

The path to the settings file.

Methods:

Name Description
__getattr__

Retrieve the value of the attribute with the given name.

__setattr__

Set the value of the attribute with the given name.

Constructs all the necessary attributes for the ModuleSettings object.

Parameters:

Name Type Description Default
module_name str

The name of the module.

required
dynabox DynaBox

The DynaBox object that holds the actual settings.

required
filepath Path

The path to the settings file.

required
Source code in oxytcmri/infrastructure/settings.py
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
def __init__(self, module_name: str, dynabox: DynaBox, filepath: Path):
    """
    Constructs all the necessary attributes for the ModuleSettings object.

    Parameters
    ----------
    module_name : str
        The name of the module.
    dynabox : DynaBox
        The DynaBox object that holds the actual settings.
    filepath : Path
        The path to the settings file.
    """
    self.module_name = module_name
    self._dynabox = dynabox
    self.filepath = filepath

module_name = module_name instance-attribute

filepath = filepath instance-attribute

list_attributes()

List all the attributes of the module.

Returns:

Type Description
list

A list of all the module's attributes.

Source code in oxytcmri/infrastructure/settings.py
244
245
246
247
248
249
250
251
252
253
def list_attributes(self):
    """
    List all the attributes of the module.

    Returns
    -------
    list
        A list of all the module's attributes.
    """
    return list(self._dynabox.keys())