Skip to content

# Credential loader

The tool to check the availability or syntax of domain, IP or URL.

::

    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Provides our credential loader.

Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks: https://pyfunceble.github.io/#/special-thanks

Contributors: https://pyfunceble.github.io/#/contributors

Project link: https://github.com/funilrys/PyFunceble

Project documentation: https://docs.pyfunceble.com

Project homepage: https://pyfunceble.github.io/

License: ::

    Copyright 2017, 2018, 2019, 2020, 2022, 2023, 2024, 2025 Nissar Chababy

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

## `CredentialLoader`

Provides our credentials loader.

The idea is to have an interface which provides the credential to use but at the same time load and initiate the credential interface after loading it from the user input or the environment variables.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`db_type` | `Optional[str]` | The database type to load the credential for. | `None`

Source code in `PyFunceble/cli/credential_loader.py`

     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    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
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310

    class CredentialLoader:
        """
        Provides our credentials loader.

        The idea is to have an interface which provides the credential to use
        but at the same time load and initiate the credential interface after
        loading it from the user input or the environment variables.

        :param db_type:
            The database type to load the credential for.
        """

        DB_TYPE2OBJ: dict = {
            "csv": None,
            "mysql": MySQLCredential,
            "mariadb": MariaDBCredential,
            "postgresql": PostgreSQLCredential,
        }

        credential: Optional[CredentialBase] = None
        """
        The credential (itself).
        """

        _db_type: Optional[str] = None

        def __init__(self, db_type: Optional[str] = None) -> None:
            if db_type is not None:
                self.db_type = db_type
            elif PyFunceble.facility.ConfigLoader.is_already_loaded():
                self.db_type = PyFunceble.storage.CONFIGURATION.cli_testing.db_type

        def ensure_db_type_is_given(func):  # pylint: disable=no-self-argument
            """
            Ensure that the db type is given before launching the decorated
            method.

            :raise TypeError:
                When the db type is not given.
            """

            @functools.wraps(func)
            def wrapper(self, *args, **kwargs):
                if not isinstance(self.db_type, str):
                    if PyFunceble.facility.ConfigLoader.is_already_loaded():
                        self.db_type = PyFunceble.storage.CONFIGURATION.cli_testing.db_type
                    else:
                        raise TypeError(f"<self.db_type> should be {str}.")

                return func(self, *args, **kwargs)  # pylint: disable=not-callable

            return wrapper

        def execute_if_authorized(default: Any = None):  # pylint: disable=no-self-argument
            """
            Executes the decorated method only if we are authorized to process.
            Otherwise, apply the given :code:`default`.
            """

            def inner_method(func):
                @functools.wraps(func)
                def wrapper(self, *args, **kwargs):
                    if self.authorized:
                        return func(self, *args, **kwargs)  # pylint: disable=not-callable
                    return self if default is None else default

                return wrapper

            return inner_method

        def start_if_not_started(func):  # pylint: disable=no-self-argument
            """
            Launches the start method before launching the decorated method.
            """

            @functools.wraps(func)
            def wrapper(self, *args, **kwargs):
                if not isinstance(self.credential, CredentialBase):
                    self.start()

                return func(self, *args, **kwargs)  # pylint: disable=not-callable

            return wrapper

        @property
        def authorized(self) -> bool:
            """
            Provides the authorization to run.
            """

            return self.db_type != "csv"

        @property
        def db_type(self) -> Optional[str]:
            """
            Provides the current state of the :code:`_db_type` attribute.
            """

            return self._db_type

        @db_type.setter
        def db_type(self, value: str) -> None:
            """
            Sets the database type to work with.

            :param value:
                The value to set.

            :raise TypeError:
                When the given :code:`value` is not a :py:class:`str`.
            :raise ValueError:
                When the given :code:`value` is empty or not supported.
            """

            if not isinstance(value, str):
                raise TypeError(f"<value> should be {str}, {type(value)} given.")

            if not value:
                raise ValueError("<value> should not be empty.")

            value = value.lower()

            if value not in self.DB_TYPE2OBJ:
                raise ValueError(f"<value> ({value!r}) is not supported.")

            self._db_type = value

        def set_db_type(self, value: str) -> "CredentialLoader":
            """
            Sets the database type to work with.

            :param value:
                The value to set.
            """

            self.db_type = value

            return self

        @execute_if_authorized(False)
        def is_already_loaded(self) -> bool:
            """
            Checks if the credential was already loaded.
            """

            return isinstance(self.credential, CredentialBase)

        def set_credential_var(self, var_name: str, value: Any) -> "CredentialLoader":
            """
            Sets the given :code:`var_name` of the credential object with the given
            :code:`value`.

            :param var_name:
                The name of the variable to set.
            :param value:
                The value of to set.
            """

            try:
                setattr(self.credential, var_name, value)
            except TypeError:
                setattr(self.credential, var_name, int(value))

        def ask_for_info(self, var_name: str, default: Any) -> str:
            """
            Asks the credential to the end-user.

            :param var_name:
                The name of the part to ask for example :code:`host` or
                :code:`password`.
            :param default:
                The default value to return if the user don't give us anything.
            """

            message = (
                f"[{self.db_type.upper()}] Please give us your DB {var_name} "
                f"(Default: {default!r}): "
            )

            if var_name == "password":
                user_input = getpass(message)
            else:
                user_input = input(message)

            return user_input or default

        @execute_if_authorized(None)
        @ensure_db_type_is_given
        def start(self, *, ignore_cli: bool = True) -> "CredentialLoader":
            """
            Starts the loading of the credential.

            :param ignore_cli:
                Ignore questions to end-user.
            """

            if not isinstance(self.credential, CredentialBase) and self.authorized:
                # We directly share the credential object into the DBSession object.
                # This will let us use the DBSession without having to think about
                # any other headache.
                self.credential = PyFunceble.cli.factory.DBSession.credential = (
                    self.DB_TYPE2OBJ[self.db_type]()
                )

                env_var_helper = EnvironmentVariableHelper(
                    env_file_path=self.credential.get_dot_env_file()
                )

                for cred_var, env_var in self.credential.VAR2ENV.items():
                    if env_var_helper.set_name(env_var).exists():
                        self.set_credential_var(cred_var, env_var_helper.get_value())
                    else:
                        from_file = env_var_helper.get_value_from_env_file()

                        if from_file:
                            self.set_credential_var(cred_var, from_file)
                        elif not ignore_cli:
                            self.set_credential_var(
                                cred_var,
                                self.ask_for_info(
                                    cred_var,
                                    getattr(self.credential, f"STD_{cred_var.upper()}"),
                                ),
                            )
                        else:
                            self.set_credential_var(
                                cred_var,
                                getattr(self.credential, f"STD_{cred_var.upper()}"),
                            )

                        env_var_helper.set_value_in_env_file(
                            str(getattr(self.credential, cred_var))
                        )

            return self

        @execute_if_authorized("")
        @start_if_not_started
        def get_uri(self) -> str:
            """
            Provides the URI to use.
            """

            return self.credential.get_uri()

### `authorized` `property`

Provides the authorization to run.

### `credential = None` `class-attribute` `instance-attribute`

The credential (itself).

### `db_type` `property` `writable`

Provides the current state of the :code:`_db_type` attribute.

### `ask_for_info(var_name, default)`

Asks the credential to the end-user.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`var_name` | `str` | The name of the part to ask for example :code:`host` or :code:`password`. | required
`default` | `Any` | The default value to return if the user don't give us anything. | required

Source code in `PyFunceble/cli/credential_loader.py`

    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251

    def ask_for_info(self, var_name: str, default: Any) -> str:
        """
        Asks the credential to the end-user.

        :param var_name:
            The name of the part to ask for example :code:`host` or
            :code:`password`.
        :param default:
            The default value to return if the user don't give us anything.
        """

        message = (
            f"[{self.db_type.upper()}] Please give us your DB {var_name} "
            f"(Default: {default!r}): "
        )

        if var_name == "password":
            user_input = getpass(message)
        else:
            user_input = input(message)

        return user_input or default

### `ensure_db_type_is_given(func)`

Ensure that the db type is given before launching the decorated method.

Raises:

Type | Description
--- | ---
`TypeError` | When the db type is not given.

Source code in `PyFunceble/cli/credential_loader.py`

     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118

    def ensure_db_type_is_given(func):  # pylint: disable=no-self-argument
        """
        Ensure that the db type is given before launching the decorated
        method.

        :raise TypeError:
            When the db type is not given.
        """

        @functools.wraps(func)
        def wrapper(self, *args, **kwargs):
            if not isinstance(self.db_type, str):
                if PyFunceble.facility.ConfigLoader.is_already_loaded():
                    self.db_type = PyFunceble.storage.CONFIGURATION.cli_testing.db_type
                else:
                    raise TypeError(f"<self.db_type> should be {str}.")

            return func(self, *args, **kwargs)  # pylint: disable=not-callable

        return wrapper

### `execute_if_authorized(default=None)`

Executes the decorated method only if we are authorized to process. Otherwise, apply the given :code:`default`.

Source code in `PyFunceble/cli/credential_loader.py`

    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135

    def execute_if_authorized(default: Any = None):  # pylint: disable=no-self-argument
        """
        Executes the decorated method only if we are authorized to process.
        Otherwise, apply the given :code:`default`.
        """

        def inner_method(func):
            @functools.wraps(func)
            def wrapper(self, *args, **kwargs):
                if self.authorized:
                    return func(self, *args, **kwargs)  # pylint: disable=not-callable
                return self if default is None else default

            return wrapper

        return inner_method

### `get_uri()`

Provides the URI to use.

Source code in `PyFunceble/cli/credential_loader.py`

    303
    304
    305
    306
    307
    308
    309
    310

    @execute_if_authorized("")
    @start_if_not_started
    def get_uri(self) -> str:
        """
        Provides the URI to use.
        """

        return self.credential.get_uri()

### `is_already_loaded()`

Checks if the credential was already loaded.

Source code in `PyFunceble/cli/credential_loader.py`

    206
    207
    208
    209
    210
    211
    212

    @execute_if_authorized(False)
    def is_already_loaded(self) -> bool:
        """
        Checks if the credential was already loaded.
        """

        return isinstance(self.credential, CredentialBase)

### `set_credential_var(var_name, value)`

Sets the given :code:`var_name` of the credential object with the given :code:`value`.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`var_name` | `str` | The name of the variable to set. | required
`value` | `Any` | The value of to set. | required

Source code in `PyFunceble/cli/credential_loader.py`

    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228

    def set_credential_var(self, var_name: str, value: Any) -> "CredentialLoader":
        """
        Sets the given :code:`var_name` of the credential object with the given
        :code:`value`.

        :param var_name:
            The name of the variable to set.
        :param value:
            The value of to set.
        """

        try:
            setattr(self.credential, var_name, value)
        except TypeError:
            setattr(self.credential, var_name, int(value))

### `set_db_type(value)`

Sets the database type to work with.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`value` | `str` | The value to set. | required

Source code in `PyFunceble/cli/credential_loader.py`

    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204

    def set_db_type(self, value: str) -> "CredentialLoader":
        """
        Sets the database type to work with.

        :param value:
            The value to set.
        """

        self.db_type = value

        return self

### `start(*, ignore_cli=True)`

Starts the loading of the credential.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`ignore_cli` | `bool` | Ignore questions to end-user. | `True`

Source code in `PyFunceble/cli/credential_loader.py`

    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301

    @execute_if_authorized(None)
    @ensure_db_type_is_given
    def start(self, *, ignore_cli: bool = True) -> "CredentialLoader":
        """
        Starts the loading of the credential.

        :param ignore_cli:
            Ignore questions to end-user.
        """

        if not isinstance(self.credential, CredentialBase) and self.authorized:
            # We directly share the credential object into the DBSession object.
            # This will let us use the DBSession without having to think about
            # any other headache.
            self.credential = PyFunceble.cli.factory.DBSession.credential = (
                self.DB_TYPE2OBJ[self.db_type]()
            )

            env_var_helper = EnvironmentVariableHelper(
                env_file_path=self.credential.get_dot_env_file()
            )

            for cred_var, env_var in self.credential.VAR2ENV.items():
                if env_var_helper.set_name(env_var).exists():
                    self.set_credential_var(cred_var, env_var_helper.get_value())
                else:
                    from_file = env_var_helper.get_value_from_env_file()

                    if from_file:
                        self.set_credential_var(cred_var, from_file)
                    elif not ignore_cli:
                        self.set_credential_var(
                            cred_var,
                            self.ask_for_info(
                                cred_var,
                                getattr(self.credential, f"STD_{cred_var.upper()}"),
                            ),
                        )
                    else:
                        self.set_credential_var(
                            cred_var,
                            getattr(self.credential, f"STD_{cred_var.upper()}"),
                        )

                    env_var_helper.set_value_in_env_file(
                        str(getattr(self.credential, cred_var))
                    )

        return self

### `start_if_not_started(func)`

Launches the start method before launching the decorated method.

Source code in `PyFunceble/cli/credential_loader.py`

    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149

    def start_if_not_started(func):  # pylint: disable=no-self-argument
        """
        Launches the start method before launching the decorated method.
        """

        @functools.wraps(func)
        def wrapper(self, *args, **kwargs):
            if not isinstance(self.credential, CredentialBase):
                self.start()

            return func(self, *args, **kwargs)  # pylint: disable=not-callable

        return wrapper