{ "info": { "author": "David Lorenzo", "author_email": "", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "# Python-DotEnv-Settings-Handler\n\nA Settings Handler using [python-dotenv](https://github.com/theskumar/python-dotenv) and/or system environment variables, to read all the settings from a Settings class based on [pydantic](https://github.com/samuelcolvin/pydantic/).\n\nInstead of loading the environment variables (from now on 'env vars') as `os.getenv(\"MY_VAR\")`, create a class with all your env variables, and load them as `settings.MY_VAR`.\n\nThe Class init will look for env variables used as settings for your project. These variables can be system env variables, or be specified on a .env file.\n\n## Requirements\n\n- Python 3.x (tested on Python 3.7)\n- Libraries: pydantic (python-dotenv is not called from this library)\n\n## Installing\n\nFrom pip:\n```bash\npip install -U dotenv-settings-handler\n```\n\nFrom Github:\n```bash\ngit clone https://github.com/David-Lor/Python-DotEnv-Settings-Handler\ncd Python-DotEnv-Settings-Handler\npython setup.py install\n```\n\nTest:\n```bash\npip install -r requirements.txt\npython setup.py test\n```\n\n## Changelog\n\n- 0.0.3 - Add Tests\n- 0.0.2 - Removed calling to load_dotenv from the BaseSettingsHandler init, since it would look for the .env file in the wrong path. Manually calling load_dotenv from the target project context is now required.\n- 0.0.1 - Initial release\n\n## Examples\n\nStart by creating a `.env` file, as you would usually do using python-dotenv, or define all or some of these variables as system environment variables.\n\n```\nDATABASE_PORT=33306\nDATABASE_USER=foo\nDATABASE_PASSWORD=bar\nDATABASE_DB=mydb\n```\n\nThen, create a new class inheriting from BaseSettingsHandler:\n- BaseSettingsHandler inherits from pydantic.BaseSettings, working in a similar manner as pydantic.BaseModel. Check [pydantic documentation](https://pydantic-docs.helpmanual.io/#settings) to know more about it. You should be able to extend your class with pydantic features (read more in the examples).\n- Basically you must define on your custom class the wanted ENV variables as fields (class attributes), which must have the same name as on the .env file.\n- You can define a default value to be used if a certain variable is not defined on the .env file (in the example: DATABASE_SERVER, DATABASE_PORT).\n- You should set the data type (str, int) on the values without default value (in the example: DATABASE_USER, DATABASE_PASSWORD, DATABASE_DB). Values with a default value will use the type of that value.\n- If an env variable without default value not exists, when creating an instance of MySettings() a pydantic exception will be raised, asking to fill a required class attribute.\n- Calling load_dotenv() from your Python module is required if using .env file, in order to lookup for the .env file in your module directory context.\n\n```python\n# settings.py\n\nfrom dotenv_settings_handler import BaseSettingsHandler\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\n\nclass MySettings(BaseSettingsHandler):\n DATABASE_SERVER = \"127.0.0.1\"\n DATABASE_PORT = 3306\n DATABASE_USER: str\n DATABASE_PASSWORD: str\n DATABASE_DB: str\n\nmy_settings = MySettings()\n```\n\nFinally, you can import the `my_settings` class instance anywhere you want to use these settings:\n\n```python\n# connection.py\n\nfrom .settings import my_settings as settings\nimport pymysql\n\nconnection = pymysql.connect(\n host=settings.DATABASE_SERVER,\n port=settings.DATABASE_PORT,\n user=settings.DATABASE_USER,\n password=settings.DATABASE_PASSWORD,\n db=settings.DATABASE_DB,\n charset='utf8mb4',\n cursorclass=pymysql.cursors.DictCursor\n)\n```\n\nThis is what happened to each ENV var:\n\n- DATABASE_SERVER: not defined on .env, so defaults to \"127.0.0.1\"\n- DATABASE_PORT: defined on .env as 33306. Since in the class defaults to 3306 (an int), is automatically casted to int by pydantic.\n- DATABASE_USER, DATABASE_PASSWORD, DATABASE_DB: defined on .env, and have no default values, so they are required. Not defining them on the .env file, nor as system env variables, would raise a pydantic exception when creating the MySettings() instance at settings.py.\n\n### Ignore env case\n\nSince BaseSettingsHandler inherits from [pydantic.BaseSettings](https://pydantic-docs.helpmanual.io/#settings), we can take advantage of the features of BaseSettings, such as ignoring the env case. \nThis means we can have the env var names in our class as lowercase, and define them in our system as uppercase, lowercase or mixed lower/uppercase. They will still be detected by pydantic and the BaseSettingsHandler.\n\nJust modify your custom Settings class with the modifiers featured by pydantic.BaseSettings (under a nested `class Config`):\n\n```python\n# settings.py\n\nfrom dotenv_settings_handler import BaseSettingsHandler\n\nclass MySettings(BaseSettingsHandler):\n my_foo: str\n MY_BAR: int\n\n class Config:\n case_insensitive = True\n\nsettings = MySettings()\n```\n\nNow you can set your .env and/or system env variables with any case you want; i.e. `my_foo`, `MY_foo`, `MY_FOO`, `my_FOO`, `my_Foo` are all valid and will be accesible through `settings.my_foo`. \n\n### Set env prefix\n\nAnother cool feature from pydantic.BaseSettings is setting a prefix on your env variables. By default pydantic set this as `APP_`, but on BaseSettingsHandler this feature is disabled. However, you can enable it again setting the `env_prefix` option under `class Config`.\n\n```python\n# settings.py\n\nfrom dotenv_settings_handler import BaseSettingsHandler\n\nclass MySettings(BaseSettingsHandler):\n foo: str\n bar: int\n\n class Config:\n env_prefix = \"PYTHON_APP_\"\n\nsettings = MySettings()\n```\n\nNow the field `foo` will be read from the env var `PYTHON_APP_FOO` (note that it will look for an uppercase env var), and `bar` from `PYTHON_APP_BAR`.", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/David-Lor/Python-DotEnv-Settings-Handler", "keywords": "", "license": "Apache 2.0", "maintainer": "", "maintainer_email": "", "name": "dotenv-settings-handler", "package_url": "https://pypi.org/project/dotenv-settings-handler/", "platform": "", "project_url": "https://pypi.org/project/dotenv-settings-handler/", "project_urls": { "Homepage": "https://github.com/David-Lor/Python-DotEnv-Settings-Handler" }, "release_url": "https://pypi.org/project/dotenv-settings-handler/0.0.3/", "requires_dist": null, "requires_python": "", "summary": "Settings handler to load settings from a DotEnv file or system env variables, using python-dotenv and pydantic.", "version": "0.0.3" }, "last_serial": 5492954, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "7cb49948ff974817b5abdcf236830598", "sha256": "cd53ec70ea26616f928b1cebbc4fe1d26647da5265cb7122ee5c09ad061608fb" }, "downloads": -1, "filename": "dotenv-settings-handler-0.0.1.tar.gz", "has_sig": false, "md5_digest": "7cb49948ff974817b5abdcf236830598", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 2913, "upload_time": "2019-06-29T10:34:02", "url": "https://files.pythonhosted.org/packages/a7/55/fc5982b255f7b4b2e89e22d0ee732f96a5478d1958b82f5065c27b8c1d0d/dotenv-settings-handler-0.0.1.tar.gz" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "0809c75881e60761446dc8e881078e56", "sha256": "80a4f69b0812fbf16cbc03c3ca87491ee8c6743b63dd2d350b8a45ed71449dfd" }, "downloads": -1, "filename": "dotenv-settings-handler-0.0.2.tar.gz", "has_sig": false, "md5_digest": "0809c75881e60761446dc8e881078e56", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3150, "upload_time": "2019-06-29T20:34:12", "url": "https://files.pythonhosted.org/packages/23/e2/c2a71ced465b71e1e54cf4b0a6edd3b3228a88f8f0ac6bc745f8ca38206d/dotenv-settings-handler-0.0.2.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "bad5d4c2ffeb3aedd901b80a7259fd2e", "sha256": "8e47fe6f29251686a06dfbd46a5f58f10153bc501c7bd1cb1625be1dea834883" }, "downloads": -1, "filename": "dotenv-settings-handler-0.0.3.tar.gz", "has_sig": false, "md5_digest": "bad5d4c2ffeb3aedd901b80a7259fd2e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4017, "upload_time": "2019-07-05T20:20:00", "url": "https://files.pythonhosted.org/packages/01/0e/67ae09562714f91243a1463dcf045ab6df5bb535cc0442a7d7211bc029b3/dotenv-settings-handler-0.0.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "bad5d4c2ffeb3aedd901b80a7259fd2e", "sha256": "8e47fe6f29251686a06dfbd46a5f58f10153bc501c7bd1cb1625be1dea834883" }, "downloads": -1, "filename": "dotenv-settings-handler-0.0.3.tar.gz", "has_sig": false, "md5_digest": "bad5d4c2ffeb3aedd901b80a7259fd2e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4017, "upload_time": "2019-07-05T20:20:00", "url": "https://files.pythonhosted.org/packages/01/0e/67ae09562714f91243a1463dcf045ab6df5bb535cc0442a7d7211bc029b3/dotenv-settings-handler-0.0.3.tar.gz" } ] }