Created
October 6, 2025 11:52
-
-
Save MG-MW/60246e9ebc0fd839ffeeedd1155f89b8 to your computer and use it in GitHub Desktop.
#dotenv as optional install
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Check, if option dotenv dependency is installed and not skipped by user. | |
| This is modified from | |
| https://github.com/pallets/flask/blob/2c1b30d0503cfb064f1cb252e6614a06915a362a/src/flask/helpers.py#L36 | |
| get_load_dotenv | |
| and | |
| https://github.com/pallets/flask/blob/2c1b30d0503cfb064f1cb252e6614a06915a362a/src/flask/cli.py#L706 | |
| load_dotenv | |
| Copyright 2010 Pallets | |
| Redistribution and use in source and binary forms, with or without | |
| modification, are permitted provided that the following conditions are | |
| met: | |
| 1. Redistributions of source code must retain the above copyright | |
| notice, this list of conditions and the following disclaimer. | |
| 2. Redistributions in binary form must reproduce the above copyright | |
| notice, this list of conditions and the following disclaimer in the | |
| documentation and/or other materials provided with the distribution. | |
| 3. Neither the name of the copyright holder nor the names of its | |
| contributors may be used to endorse or promote products derived from | |
| this software without specific prior written permission. | |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
| PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | |
| TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| """ | |
| import os | |
| from pathlib import Path | |
| from app.constants import APPLICATION_ENV_PREFIX | |
| def get_load_dotenv(default: bool = True) -> bool: # noqa: FBT001, FBT002 | |
| """Check whether user has disabled loading default dotenv files. | |
| Environment variable `APP_NAME__SKIP_DOTENV` is checked. | |
| Args: | |
| default: What to return if the env var isn't set. | |
| """ | |
| expected_prefix = "APP_NAME__" | |
| if not expected_prefix == APPLICATION_ENV_PREFIX: | |
| raise ValueError( | |
| f"Aplication env prefix does not match expected {expected_prefix}. " | |
| "Was the env prefix changed? If so, please adjust the doc string." | |
| ) | |
| val = os.environ.get(f"{APPLICATION_ENV_PREFIX}SKIP_DOTENV") | |
| if val is None: | |
| return default | |
| return val.casefold() in ("0", "false", "no") | |
| def load_dotenv(path: str | os.PathLike[str] | None = None, *, load_defaults: bool = True) -> bool: | |
| """Load "dotenv" files to set environment variables. | |
| A given path takes precedence over `.env`. After | |
| loading and combining these files, values are only set if the key is not | |
| already set in `os.environ`. | |
| This is a no-op if `python-dotenv`_ is not installed. | |
| `python-dotenv`: https://github.com/theskumar/python-dotenv#readme | |
| Args: | |
| path: Load the file at this location. | |
| load_defaults: Search for and load the default `.env` file. | |
| Returns: | |
| `True` if at least one env var was loaded. | |
| """ | |
| try: | |
| import dotenv | |
| except ImportError: | |
| if path or Path(".env").is_file(): | |
| print( | |
| " * Tip: There are .env files present. Install python-dotenv to use them.", | |
| ) | |
| return False | |
| data: dict[str, str | None] = {} | |
| if load_defaults: | |
| default_env_files = (".env",) | |
| for default_name in default_env_files: | |
| if not (default_path := dotenv.find_dotenv(default_name, usecwd=True)): | |
| continue | |
| data |= dotenv.dotenv_values(default_path, encoding="utf-8") | |
| if path is not None and Path(path).is_file(): | |
| data |= dotenv.dotenv_values(path, encoding="utf-8") | |
| for key, value in data.items(): | |
| if key in os.environ or value is None: | |
| continue | |
| os.environ[key] = value | |
| return bool(data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment