-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6fdbe14
Showing
7 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2021 Michael Peteuil | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Poetry Dotenv Plugin | ||
|
||
A [Poetry](https://python-poetry.org/) plugin that automatically loads environment variables from `.env` files into the environment before poetry commands are run. | ||
|
||
Supports Python 3.6+ | ||
|
||
```sh | ||
$ cat .env | ||
MY_ENV_VAR='Hello World' | ||
|
||
$ poetry run python -c 'import os; print(os.environ.get("MY_ENV_VAR"))' | ||
Hello World | ||
``` | ||
|
||
This plugin depends on the [`python-dotenv` package](https://github.com/theskumar/python-dotenv) for its functionality and therefore also supports features that `python-dotenv` supports. Interpolating variables using POSIX variable expansion for example. | ||
|
||
### Origins | ||
|
||
Initial implementation based on the event handler application plugin example in the [Poetry docs](https://python-poetry.org/docs/plugins/#event-handler). | ||
|
||
## Install | ||
|
||
```sh | ||
poetry plugin add poetry-dotenv-plugin | ||
``` | ||
|
||
### Coming from Pipenv | ||
|
||
If you are transitioning from `pipenv` there shouldn't be much to change with regard to the `.env` loading. If you were a user of [`pipenv`'s environment variables](https://pipenv.pypa.io/en/latest/advanced/#automatic-loading-of-env) to control `.env` loading then you can use the analogous environment variables listed below. | ||
|
||
Pipenv env var | Poetry env var | ||
-------------- | ---------------------- | ||
PIPENV_DOTENV_LOCATION | POETRY_DOTENV_LOCATION | ||
PIPENV_DONT_LOAD_ENV | POETRY_DONT_LOAD_ENV |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import os | ||
|
||
from cleo.events.console_events import COMMAND | ||
import dotenv | ||
from poetry.console.application import Application | ||
from poetry.console.commands.env_command import EnvCommand | ||
from poetry.plugins.application_plugin import ApplicationPlugin | ||
|
||
class DotenvPlugin(ApplicationPlugin): | ||
def activate(self, application): | ||
application.event_dispatcher.add_listener(COMMAND, self.load_dotenv) | ||
|
||
def load_dotenv( | ||
self, | ||
event, | ||
event_name, | ||
dispatcher | ||
): | ||
POETRY_DONT_LOAD_ENV = bool(os.environ.get("POETRY_DONT_LOAD_ENV")) | ||
command = event.command | ||
|
||
if not isinstance(command, EnvCommand) or POETRY_DONT_LOAD_ENV: | ||
return | ||
|
||
POETRY_DOTENV_LOCATION = os.environ.get("POETRY_DOTENV_LOCATION") | ||
io = event.io | ||
|
||
if io.is_debug(): | ||
io.write_line("<debug>Loading environment variables.</debug>") | ||
|
||
path = POETRY_DOTENV_LOCATION or dotenv.find_dotenv(usecwd=True) | ||
dotenv.load_dotenv(dotenv_path=path, override=True) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[tool.poetry] | ||
name = "poetry-dotenv-plugin" | ||
version = "0.1.0a2" | ||
description = "A Poetry plugin to automatically load environment variables from .env files" | ||
authors = ["Michael Peteuil <michael.peteuil@gmail.com>"] | ||
license = "MIT" | ||
readme = "README.md" | ||
packages = [{include = "poetry_dotenv_plugin"}] | ||
homepage = "https://github.com/mpeteuil/poetry-dotenv-plugin" | ||
repository = "https://github.com/mpeteuil/poetry-dotenv-plugin" | ||
keywords = ["poetry", "poetry-plugin", "plugin", "dotenv"] | ||
classifiers = [ | ||
"Topic :: Software Development", | ||
"Topic :: System :: Systems Administration", | ||
"Topic :: Utilities", | ||
] | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.6" | ||
poetry = ">=1.2.0a1" | ||
python-dotenv = ">=0.10.0" | ||
|
||
[tool.poetry.dev-dependencies] | ||
pytest = "^6.2.3" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.poetry.plugins."poetry.application.plugin"] | ||
poetry-dotenv-plugin = "poetry_dotenv_plugin.dotenv_plugin:DotenvPlugin" |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from poetry_dotenv_plugin import dotenv_plugin | ||
|
||
dotenv_plugin.DotenvPlugin() |