Skip to content

# Base

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

::

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

Provides the base of all our credential holders.

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.

## `CredentialBase`

Provides the base of all our credential holder.

Source code in `PyFunceble/database/credential/base.py`

     61
     62
     63
     64
     65
     66
     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
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483

    class CredentialBase:
        """
        Provides the base of all our credential holder.
        """

        STD_HOST: str = "localhost"
        STD_PORT: int = 3306
        STD_NAME: str = PyFunceble.storage.PROJECT_NAME.lower()
        STD_USERNAME: str = PyFunceble.storage.PROJECT_NAME.lower()
        STD_PASSWORD: str = f"{PyFunceble.storage.PROJECT_NAME}:15_93le"
        STD_CHARSET: str = "utf8mb4"

        VAR2ENV: dict = {
            "host": "PYFUNCEBLE_DB_HOST",
            "port": "PYFUNCEBLE_DB_PORT",
            "name": "PYFUNCEBLE_DB_NAME",
            "username": "PYFUNCEBLE_DB_USERNAME",
            "password": "PYFUNCEBLE_DB_PASSWORD",
            "charset": "PYFUNCEBLE_DB_CHARSET",
        }
        """
        Maps our credential variable with environment variable.
        """

        dotenv_locations: List[str] = []
        """
        Provides the location of the dotenv to work with.

        .. warning::
            The order is important. The last one in the list will be taken as
            default if everything else is not found in the filesystem.
        """

        protocol: Optional[str] = None

        _host: Optional[str] = None
        _port: Optional[int] = None
        _name: Optional[str] = None
        _username: Optional[str] = None
        _password: Optional[str] = None
        _charset: Optional[str] = None

        _config_dir: Optional[str] = None

        def __init__(
            self,
            *,
            host: Optional[str] = None,
            port: Optional[int] = None,
            name: Optional[str] = None,
            username: Optional[str] = None,
            password: Optional[str] = None,
            charset: Optional[str] = None,
            config_dir: Optional[str] = None,
        ) -> None:
            if host is not None:
                self.host = host
            else:
                self.host = self.STD_HOST

            if port is not None:
                self.port = port
            else:
                self.port = self.STD_PORT

            if name is not None:
                self.name = name
            else:
                self.name = self.STD_NAME

            if username is not None:
                self.username = username
            else:
                self.username = self.STD_USERNAME

            if password is not None:
                self.password = password
            else:
                self.password = self.STD_PASSWORD

            if charset is not None:
                self.charset = charset
            else:
                self.charset = self.STD_CHARSET

            if config_dir is not None:
                self.config_dir = config_dir
            else:
                self.config_dir = PyFunceble.storage.CONFIG_DIRECTORY

            self.dotenv_locations = [
                os.path.realpath(PyFunceble.storage.ENV_FILENAME),
                os.path.join(self.config_dir, PyFunceble.storage.ENV_FILENAME),
            ]

        def ensure_protocol_is_given(func):  # pylint: disable=no-self-argument
            """
            Ensures that the protocol is given before launching the decorated
            method.

            :raise ValueError:
                When the :code:`protocol` is not given.
            """

            @functools.wraps(func)
            def wrapper(self, *args, **kwargs):
                if not self.protocol:
                    raise ValueError("<self.protocol> is not given.")

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

            return wrapper

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

            return self._config_dir

        @config_dir.setter
        def config_dir(self, value: str) -> None:
            """
            Sets the configuration directory.

            :param value:
                The value to set.

            :raise TypeError:
                When value is not a :py:class:`str`.
            """

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

            self._config_dir = value

        def set_config_dir(self, value: str) -> "CredentialBase":
            """
            Sets the configuration directory.

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

            self.config_dir = value

            return self

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

            return self._host

        @host.setter
        def host(self, value: str) -> None:
            """
            Sets the hosts to interact 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.
            """

            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.")

            self._host = value

        def set_host(self, value: str) -> "CredentialBase":
            """
            Sets the hosts to interact with.

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

            self.host = value

            return self

        @property
        def port(self) -> Optional[int]:
            """
            Provides the current state of the :code:`_port` attribute.
            """

            return self._port

        @port.setter
        def port(self, value: int) -> None:
            """
            Sets the port to interact with.

            :param value:
                The value to set.

            :raise TypeError:
                When the given :code:`value` is not a :py:class:`int`.
            """

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

            self._port = value

        def set_port(self, value: int) -> "CredentialBase":
            """
            Sets the port to interact with.

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

            self.port = value

            return self

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

            return self._name

        @name.setter
        def name(self, value: str) -> None:
            """
            Sets the name of the database to interact 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.
            """

            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.")

            self._name = value

        def set_name(self, value: str) -> "CredentialBase":
            """
            Sets the name of the database to interact with.

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

            self.name = value

            return self

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

            return self._username

        @username.setter
        def username(self, value: str) -> None:
            """
            Sets the username to use to authenticate ourselves.

            :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.
            """

            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.")

            self._username = value

        def set_username(self, value: str) -> "CredentialBase":
            """
            Sets the username to use to authenticate ourselves.

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

            self.username = value

            return self

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

            return self._password

        @password.setter
        def password(self, value: str) -> None:
            """
            Sets the password to use to authenticate ourselves.

            :param value:
                The value to set.

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

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

            self._password = value

        def set_password(self, value: str) -> "CredentialBase":
            """
            Sets the password to use to authenticate ourselves.

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

            self.host = value

            return self

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

            return self._charset

        @charset.setter
        def charset(self, value: str) -> None:
            """
            Sets the charset to use.

            :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.
            """

            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.")

            self._charset = value

        def set_charset(self, value: str) -> "CredentialBase":
            """
            Sets the charset to use.

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

            self.charset = value

            return self

        @ensure_protocol_is_given
        def get_uri(self) -> str:
            """
            Provides the SQLAlchemy URI.
            """

            if self.host.startswith("/"):
                return (
                    f"{self.protocol}://{self.username}:{self.password}"
                    f"@localhost/{self.name}?unix_socket={self.host}"
                    f"&charset={self.charset}"
                )

            return (
                f"{self.protocol}://{self.username}:{self.password}"
                f"@{self.host}/{self.name}"
                f"?charset={self.charset}"
            )

        def get_dot_env_file(self) -> str:
            """
            Provides the dotenv file to work with.
            """

            file_helper = FileHelper()

            for file in self.dotenv_locations:
                if file_helper.set_path(file).exists():
                    return file_helper.path

            return self.dotenv_locations[-1]

### `VAR2ENV = {'host': 'PYFUNCEBLE_DB_HOST', 'port': 'PYFUNCEBLE_DB_PORT', 'name': 'PYFUNCEBLE_DB_NAME', 'username': 'PYFUNCEBLE_DB_USERNAME', 'password': 'PYFUNCEBLE_DB_PASSWORD', 'charset': 'PYFUNCEBLE_DB_CHARSET'}` `class-attribute` `instance-attribute`

Maps our credential variable with environment variable.

### `charset` `property` `writable`

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

### `config_dir` `property` `writable`

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

### `dotenv_locations = [os.path.realpath(PyFunceble.storage.ENV_FILENAME), os.path.join(self.config_dir, PyFunceble.storage.ENV_FILENAME)]` `class-attribute` `instance-attribute`

Provides the location of the dotenv to work with.

.. warning:: The order is important. The last one in the list will be taken as default if everything else is not found in the filesystem.

### `host` `property` `writable`

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

### `name` `property` `writable`

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

### `password` `property` `writable`

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

### `port` `property` `writable`

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

### `username` `property` `writable`

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

### `ensure_protocol_is_given(func)`

Ensures that the protocol is given before launching the decorated method.

Raises:

Type | Description
--- | ---
`ValueError` | When the :code:`protocol` is not given.

Source code in `PyFunceble/database/credential/base.py`

    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172

    def ensure_protocol_is_given(func):  # pylint: disable=no-self-argument
        """
        Ensures that the protocol is given before launching the decorated
        method.

        :raise ValueError:
            When the :code:`protocol` is not given.
        """

        @functools.wraps(func)
        def wrapper(self, *args, **kwargs):
            if not self.protocol:
                raise ValueError("<self.protocol> is not given.")

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

        return wrapper

### `get_dot_env_file()`

Provides the dotenv file to work with.

Source code in `PyFunceble/database/credential/base.py`

    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483

    def get_dot_env_file(self) -> str:
        """
        Provides the dotenv file to work with.
        """

        file_helper = FileHelper()

        for file in self.dotenv_locations:
            if file_helper.set_path(file).exists():
                return file_helper.path

        return self.dotenv_locations[-1]

### `get_uri()`

Provides the SQLAlchemy URI.

Source code in `PyFunceble/database/credential/base.py`

    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470

    @ensure_protocol_is_given
    def get_uri(self) -> str:
        """
        Provides the SQLAlchemy URI.
        """

        if self.host.startswith("/"):
            return (
                f"{self.protocol}://{self.username}:{self.password}"
                f"@localhost/{self.name}?unix_socket={self.host}"
                f"&charset={self.charset}"
            )

        return (
            f"{self.protocol}://{self.username}:{self.password}"
            f"@{self.host}/{self.name}"
            f"?charset={self.charset}"
        )

### `set_charset(value)`

Sets the charset to use.

Parameters:

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

Source code in `PyFunceble/database/credential/base.py`

    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451

    def set_charset(self, value: str) -> "CredentialBase":
        """
        Sets the charset to use.

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

        self.charset = value

        return self

### `set_config_dir(value)`

Sets the configuration directory.

Parameters:

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

Source code in `PyFunceble/database/credential/base.py`

    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209

    def set_config_dir(self, value: str) -> "CredentialBase":
        """
        Sets the configuration directory.

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

        self.config_dir = value

        return self

### `set_host(value)`

Sets the hosts to interact with.

Parameters:

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

Source code in `PyFunceble/database/credential/base.py`

    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251

    def set_host(self, value: str) -> "CredentialBase":
        """
        Sets the hosts to interact with.

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

        self.host = value

        return self

### `set_name(value)`

Sets the name of the database to interact with.

Parameters:

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

Source code in `PyFunceble/database/credential/base.py`

    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330

    def set_name(self, value: str) -> "CredentialBase":
        """
        Sets the name of the database to interact with.

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

        self.name = value

        return self

### `set_password(value)`

Sets the password to use to authenticate ourselves.

Parameters:

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

Source code in `PyFunceble/database/credential/base.py`

    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409

    def set_password(self, value: str) -> "CredentialBase":
        """
        Sets the password to use to authenticate ourselves.

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

        self.host = value

        return self

### `set_port(value)`

Sets the port to interact with.

Parameters:

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

Source code in `PyFunceble/database/credential/base.py`

    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288

    def set_port(self, value: int) -> "CredentialBase":
        """
        Sets the port to interact with.

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

        self.port = value

        return self

### `set_username(value)`

Sets the username to use to authenticate ourselves.

Parameters:

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

Source code in `PyFunceble/database/credential/base.py`

    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372

    def set_username(self, value: str) -> "CredentialBase":
        """
        Sets the username to use to authenticate ourselves.

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

        self.username = value

        return self