Skip to content

Config

Config loads and exposes your project's config/*.cfg files for the currently selected environment. See the Configuration guide for a walkthrough of settings.cfg, database.cfg, and environment.cfg.

from grace.application import Application

app = Application()
app.load()

app.config.get("client", "guild_id")
app.config.database_uri

grace.config.Config

Config()

This class is the application configurations. It loads all the configuration for the given environment

The config environment is chosen by checking the value of the GRACE_ENV environment variable. If the variable is not set it will load with development by default (see Application.load).

There can be only one config loaded at once. Which means thar if you instantiate a second or multiple Config object, they will all share the same environment. This is to say, that the config objects are identical.

Source code in grace/config.py
75
76
77
78
79
80
81
82
83
84
85
def __init__(self) -> None:
    load_dotenv(".env")

    self.__environment: Optional[str] = None
    self.__config: ConfigParser = ConfigParser(
        interpolation=EnvironmentInterpolation()
    )

    self.read("config/settings.cfg")
    self.read("config/database.cfg")
    self.read("config/environment.cfg")

database property

database

client property

client

environment property

environment

current_environment property

current_environment

database_uri property

database_uri

database_name property

database_name

read

read(file)

Read the configuration file.

Source code in grace/config.py
128
129
130
def read(self, file: str):
    """Read the configuration file."""
    self.__config.read(file)

get

get(section_key, value_key, fallback=None)

Get the value from the configuration file.

PARAMETER DESCRIPTION
section_key

The section key to get the value from.

TYPE: str

value_key

The value key to get the value from.

TYPE: str

fallback

The value to return if not found (default: None).

TYPE: Any DEFAULT: None

Source code in grace/config.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def get(
    self, section_key: str, value_key: str, fallback: Any = None
) -> ConfigValue:
    """Get the value from the configuration file.

    :param section_key: The section key to get the value from.
    :type section_key: str
    :param value_key: The value key to get the value from.
    :type value_key: str
    :param fallback: The value to return if not found (default: None).
    :type fallback: Optional[Union[str, int, float, bool, list]]
    """
    value: str = self.__config.get(section_key, value_key, fallback=fallback)

    if value and match(r"^[\d.]*$|^(?:True|False)*$|\[(.*?)\]", value):
        return literal_eval(value)
    return value

set_environment

set_environment(environment)

Set the environment for the configuration.

PARAMETER DESCRIPTION
environment

The environment to set.

TYPE: str

Source code in grace/config.py
150
151
152
153
154
155
156
def set_environment(self, environment: str):
    """Set the environment for the configuration.

    :param environment: The environment to set.
    :type environment: str
    """
    self.__environment = environment

grace.config.EnvironmentInterpolation

Bases: BasicInterpolation

Interpolation which expands environment variables in values.

With this literal '${NAME}', the config will process the value from the given environment variable and use it as it's value in the config.

This includes exported environment variable (ex. 'export MY_VAR=...') and variable in '.env' files.

Usage example. token = ${MY_SECRET_VAR}

In the example above, token will take the value of the environment variable called 'MY_SECRET_VAR'. In case 'MY_SECRET_VAR' doesn't exist, the value will not be evaluated.

before_get

before_get(parser, section, option, value, defaults)

Interpolate the value before getting it from the parser.

PARAMETER DESCRIPTION
parser

The parser to get the value from.

TYPE: MutableMapping[str, Mapping[str, str]]

section

The section to get the value from.

TYPE: str

option

The option to get the value from.

TYPE: str

value

The value to interpolate.

TYPE: str

defaults

The default values to use.

TYPE: Mapping[str, str]

Source code in grace/config.py
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
def before_get(
    self,
    parser: MutableMapping[str, Mapping[str, str]],
    section: str,
    option: str,
    value: str,
    defaults: Mapping[str, str],
) -> str:
    """Interpolate the value before getting it from the parser.

    :param parser: The parser to get the value from.
    :type parser: MutableMapping[str, Mapping[str, str]]
    :param section: The section to get the value from.
    :type section: str
    :param option: The option to get the value from.
    :type option: str
    :param value: The value to interpolate.
    :type value: str
    :param defaults: The default values to use.
    """
    value = super().before_get(parser, section, option, value, defaults)
    expandvars: str = path.expandvars(value)
    between_brackets = value.startswith("${") and value.endswith("}")

    if between_brackets and value == expandvars:
        try:
            return str(parser.get(section, value))
        except NoOptionError:
            return ""
    return expandvars