poetry-plugin-dotenv 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- poetry_plugin_dotenv/__init__.py +16 -0
- poetry_plugin_dotenv/dotenv/__init__.py +12 -0
- poetry_plugin_dotenv/dotenv/core.py +195 -0
- poetry_plugin_dotenv/dotenv/parsers.py +247 -0
- poetry_plugin_dotenv/dotenv/variables.py +68 -0
- poetry_plugin_dotenv/plugin.py +103 -0
- poetry_plugin_dotenv/py.typed +0 -0
- poetry_plugin_dotenv-0.4.0.dist-info/LICENSE +21 -0
- poetry_plugin_dotenv-0.4.0.dist-info/METADATA +233 -0
- poetry_plugin_dotenv-0.4.0.dist-info/RECORD +12 -0
- poetry_plugin_dotenv-0.4.0.dist-info/WHEEL +4 -0
- poetry_plugin_dotenv-0.4.0.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""poetry-plugin-dotenv - is the plugin that automatically loads environment variables from a dotenv file into the environment before poetry commands are run."""
|
|
2
|
+
|
|
3
|
+
__title__ = "poetry-plugin-dotenv"
|
|
4
|
+
__summary__ = "poetry-plugin-dotenv - is the plugin that automatically loads environment variables from a dotenv file into the environment before poetry commands are run."
|
|
5
|
+
__uri__ = "https://github.com/volopivoshenko/poetry-plugin-dotenv"
|
|
6
|
+
|
|
7
|
+
__version__ = "0.4.0"
|
|
8
|
+
|
|
9
|
+
__author__ = "Volodymyr Pivoshenko"
|
|
10
|
+
__email__ = "volodymyr.pivoshenko@gmail.com"
|
|
11
|
+
|
|
12
|
+
__license__ = "MIT"
|
|
13
|
+
__copyright__ = "Copyright 2023, Volodymyr Pivoshenko"
|
|
14
|
+
|
|
15
|
+
from poetry_plugin_dotenv import dotenv
|
|
16
|
+
from poetry_plugin_dotenv import plugin
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Package for the dotenv functionality.
|
|
2
|
+
|
|
3
|
+
This package is a fork of the ``python-dotenv`` package.
|
|
4
|
+
|
|
5
|
+
References
|
|
6
|
+
----------
|
|
7
|
+
#. `python-dotenv <https://github.com/theskumar/python-dotenv>`_
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from poetry_plugin_dotenv.dotenv import core
|
|
11
|
+
from poetry_plugin_dotenv.dotenv import parsers
|
|
12
|
+
from poetry_plugin_dotenv.dotenv import variables
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""Module that contains core dotenv functionality."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import io
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
import typing
|
|
9
|
+
import contextlib
|
|
10
|
+
|
|
11
|
+
from collections import OrderedDict
|
|
12
|
+
|
|
13
|
+
from poetry_plugin_dotenv.dotenv import parsers
|
|
14
|
+
from poetry_plugin_dotenv.dotenv import variables
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class DotEnv:
|
|
18
|
+
"""Model of a dotenv file."""
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
filepath: str | None = None,
|
|
23
|
+
stream: typing.IO[str] | None = None,
|
|
24
|
+
interpolate: bool = True,
|
|
25
|
+
override: bool = True,
|
|
26
|
+
) -> None:
|
|
27
|
+
"""Initialize."""
|
|
28
|
+
|
|
29
|
+
self.stream = stream
|
|
30
|
+
self.override = override
|
|
31
|
+
self.filepath = filepath
|
|
32
|
+
self.interpolate = interpolate
|
|
33
|
+
|
|
34
|
+
self.encoding = "utf-8"
|
|
35
|
+
|
|
36
|
+
self._dict: typing.OrderedDict[str, str] | None = None
|
|
37
|
+
|
|
38
|
+
def dict(self) -> typing.OrderedDict[str, str]:
|
|
39
|
+
"""Return content of a dotenv file."""
|
|
40
|
+
|
|
41
|
+
if self._dict:
|
|
42
|
+
return self._dict
|
|
43
|
+
|
|
44
|
+
raw_values = self.parse()
|
|
45
|
+
|
|
46
|
+
if self.interpolate:
|
|
47
|
+
self._dict = resolve(raw_values, override=self.override)
|
|
48
|
+
|
|
49
|
+
else:
|
|
50
|
+
self._dict = OrderedDict(raw_values)
|
|
51
|
+
|
|
52
|
+
return self._dict
|
|
53
|
+
|
|
54
|
+
def parse(self) -> typing.Iterator[tuple[str, str]]:
|
|
55
|
+
"""Parse a dotenv file."""
|
|
56
|
+
|
|
57
|
+
with self._get_stream() as stream:
|
|
58
|
+
for mapping in parsers.parse_stream(stream):
|
|
59
|
+
if mapping.key is not None:
|
|
60
|
+
yield mapping.key, mapping.value # type: ignore
|
|
61
|
+
|
|
62
|
+
def set_as_environment_variables(self) -> bool:
|
|
63
|
+
"""Load current dotenv as a system environment variable."""
|
|
64
|
+
|
|
65
|
+
if self.dict():
|
|
66
|
+
for key, value in self.dict().items():
|
|
67
|
+
if key in os.environ and not self.override:
|
|
68
|
+
continue
|
|
69
|
+
|
|
70
|
+
if value:
|
|
71
|
+
os.environ[key] = value
|
|
72
|
+
|
|
73
|
+
return True
|
|
74
|
+
|
|
75
|
+
return False # pragma: nocover
|
|
76
|
+
|
|
77
|
+
@contextlib.contextmanager
|
|
78
|
+
def _get_stream(self) -> typing.Iterator[typing.IO[str]]:
|
|
79
|
+
"""Get a dotenv stream."""
|
|
80
|
+
|
|
81
|
+
if self.filepath and os.path.isfile(self.filepath):
|
|
82
|
+
with open(self.filepath, encoding=self.encoding) as stream:
|
|
83
|
+
yield stream
|
|
84
|
+
|
|
85
|
+
elif self.stream:
|
|
86
|
+
yield self.stream
|
|
87
|
+
|
|
88
|
+
else:
|
|
89
|
+
yield io.StringIO("") # pragma: nocover
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
# noinspection PyShadowingNames
|
|
93
|
+
def resolve(
|
|
94
|
+
values: typing.Iterable[tuple[str, str]],
|
|
95
|
+
override: bool,
|
|
96
|
+
) -> typing.OrderedDict[str, str]:
|
|
97
|
+
"""Resolve dotenv variables."""
|
|
98
|
+
|
|
99
|
+
new_values: typing.OrderedDict[str, str] = OrderedDict()
|
|
100
|
+
|
|
101
|
+
for name, value in values:
|
|
102
|
+
if value is None:
|
|
103
|
+
result = None # pragma: nocover
|
|
104
|
+
|
|
105
|
+
else:
|
|
106
|
+
atoms = variables.parse(value)
|
|
107
|
+
env: typing.OrderedDict[str, str] = OrderedDict()
|
|
108
|
+
|
|
109
|
+
if override:
|
|
110
|
+
env.update(os.environ)
|
|
111
|
+
env.update(new_values)
|
|
112
|
+
|
|
113
|
+
else:
|
|
114
|
+
env.update(new_values)
|
|
115
|
+
env.update(os.environ)
|
|
116
|
+
|
|
117
|
+
result = "".join(atom.resolve(env) for atom in atoms)
|
|
118
|
+
|
|
119
|
+
new_values[name] = result # type: ignore
|
|
120
|
+
|
|
121
|
+
return new_values
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def walk_to_root(path: str) -> typing.Iterator[str]:
|
|
125
|
+
"""Yield directories starting from the given directory up to the root."""
|
|
126
|
+
|
|
127
|
+
if not os.path.exists(path):
|
|
128
|
+
raise OSError("Starting path not found.") # pragma: nocover
|
|
129
|
+
|
|
130
|
+
if os.path.isfile(path):
|
|
131
|
+
path = os.path.dirname(path) # pragma: nocover
|
|
132
|
+
|
|
133
|
+
last_dir = None
|
|
134
|
+
current_dir = os.path.abspath(path)
|
|
135
|
+
|
|
136
|
+
while last_dir != current_dir:
|
|
137
|
+
yield current_dir
|
|
138
|
+
parent_dir = os.path.abspath(os.path.join(current_dir, os.path.pardir))
|
|
139
|
+
last_dir, current_dir = current_dir, parent_dir
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def find(filename: str = ".env", usecwd: bool = False) -> str:
|
|
143
|
+
"""Search in increasingly higher folders for the given file."""
|
|
144
|
+
|
|
145
|
+
if usecwd or getattr(sys, "frozen", False):
|
|
146
|
+
path = os.getcwd()
|
|
147
|
+
|
|
148
|
+
else: # pragma: no cover
|
|
149
|
+
# WPS437 - call of a protected method is an only way to get a frame
|
|
150
|
+
# noinspection PyUnresolvedReferences,PyProtectedMember
|
|
151
|
+
frame = sys._getframe() # noqa: WPS437
|
|
152
|
+
current_file = __file__
|
|
153
|
+
|
|
154
|
+
while frame.f_code.co_filename == current_file:
|
|
155
|
+
# S101 - this asserts is part of the original package
|
|
156
|
+
assert frame.f_back is not None # noqa: S101
|
|
157
|
+
frame = frame.f_back
|
|
158
|
+
|
|
159
|
+
frame_filename = frame.f_code.co_filename
|
|
160
|
+
path = os.path.dirname(os.path.abspath(frame_filename))
|
|
161
|
+
|
|
162
|
+
for dirname in walk_to_root(path):
|
|
163
|
+
check_path = os.path.join(dirname, filename)
|
|
164
|
+
|
|
165
|
+
if os.path.isfile(check_path):
|
|
166
|
+
return check_path
|
|
167
|
+
|
|
168
|
+
return ""
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def load(
|
|
172
|
+
filepath: str | None = None,
|
|
173
|
+
stream: typing.IO[str] | None = None,
|
|
174
|
+
interpolate: bool = True,
|
|
175
|
+
override: bool = True,
|
|
176
|
+
) -> bool:
|
|
177
|
+
"""Parse a dotenv file and then load all the variables found as environment variables."""
|
|
178
|
+
|
|
179
|
+
dotenv = DotEnv(
|
|
180
|
+
filepath=filepath,
|
|
181
|
+
interpolate=interpolate,
|
|
182
|
+
override=override,
|
|
183
|
+
stream=stream,
|
|
184
|
+
)
|
|
185
|
+
return dotenv.set_as_environment_variables()
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def values(
|
|
189
|
+
filepath: str | None = None,
|
|
190
|
+
stream: typing.IO[str] | None = None,
|
|
191
|
+
interpolate: bool = True,
|
|
192
|
+
) -> typing.OrderedDict[str, str]:
|
|
193
|
+
"""Parse a dotenv file and return its content as a dictionary."""
|
|
194
|
+
|
|
195
|
+
return DotEnv(filepath=filepath, stream=stream, interpolate=interpolate).dict()
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"""Module that contains dotenv parsers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
import codecs
|
|
7
|
+
import dataclasses
|
|
8
|
+
|
|
9
|
+
from collections.abc import Iterator
|
|
10
|
+
from typing import IO
|
|
11
|
+
from typing import NamedTuple
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Original(NamedTuple):
|
|
15
|
+
"""Position of the original string in the file."""
|
|
16
|
+
|
|
17
|
+
string: str
|
|
18
|
+
line: int
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Binding(NamedTuple):
|
|
22
|
+
"""Binding of a key and a value."""
|
|
23
|
+
|
|
24
|
+
key: str | None
|
|
25
|
+
value: str | None
|
|
26
|
+
original: Original
|
|
27
|
+
error: bool
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class DotenvParseError(Exception):
|
|
31
|
+
"""Exception for dotenv parse errors."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def make_regex(string: str, extra_flags: int = 0) -> re.Pattern[str]:
|
|
35
|
+
"""Construct a regex expression."""
|
|
36
|
+
|
|
37
|
+
return re.compile(string, re.UNICODE | extra_flags)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
_newline = make_regex(r"(\r\n|\n|\r)")
|
|
41
|
+
_multiline_whitespace = make_regex(r"\s*", extra_flags=re.MULTILINE)
|
|
42
|
+
_whitespace = make_regex(r"[^\S\r\n]*")
|
|
43
|
+
_export = make_regex(r"(?:export[^\S\r\n]+)?")
|
|
44
|
+
_single_quoted_key = make_regex("'([^']+)'")
|
|
45
|
+
_unquoted_key = make_regex(r"([^=\#\s]+)")
|
|
46
|
+
_equal_sign = make_regex(r"(=[^\S\r\n]*)")
|
|
47
|
+
_single_quoted_value = make_regex(r"'((?:\\'|[^'])*)'")
|
|
48
|
+
_double_quoted_value = make_regex(r'"((?:\\"|[^"])*)"')
|
|
49
|
+
_unquoted_value = make_regex(r"([^\r\n]*)")
|
|
50
|
+
_comment = make_regex(r"(?:[^\S\r\n]*#[^\r\n]*)?")
|
|
51
|
+
_end_of_line = make_regex(r"[^\S\r\n]*(?:\r\n|\n|\r|$)")
|
|
52
|
+
_rest_of_line = make_regex(r"[^\r\n]*(?:\r|\n|\r\n)?")
|
|
53
|
+
_double_quote_escapes = make_regex(r"\\[\\'\"abfnrtv]")
|
|
54
|
+
_single_quote_escapes = make_regex(r"\\[\\']")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclasses.dataclass
|
|
58
|
+
class Position:
|
|
59
|
+
"""Model of a cursor position."""
|
|
60
|
+
|
|
61
|
+
chars: int
|
|
62
|
+
line: int
|
|
63
|
+
|
|
64
|
+
@classmethod
|
|
65
|
+
def start(cls) -> Position:
|
|
66
|
+
"""Get a start position."""
|
|
67
|
+
|
|
68
|
+
return cls(chars=0, line=1)
|
|
69
|
+
|
|
70
|
+
def set(self, other: Position) -> None:
|
|
71
|
+
"""Set a position."""
|
|
72
|
+
|
|
73
|
+
self.chars = other.chars
|
|
74
|
+
self.line = other.line
|
|
75
|
+
|
|
76
|
+
def advance(self, string: str) -> None:
|
|
77
|
+
"""Update a position."""
|
|
78
|
+
|
|
79
|
+
self.chars += len(string)
|
|
80
|
+
self.line += len(re.findall(_newline, string))
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class Reader:
|
|
84
|
+
"""Dotenv reader."""
|
|
85
|
+
|
|
86
|
+
def __init__(self, stream: IO[str]) -> None:
|
|
87
|
+
"""Initialize."""
|
|
88
|
+
|
|
89
|
+
self.string = stream.read()
|
|
90
|
+
self.position = Position.start()
|
|
91
|
+
self.mark = Position.start()
|
|
92
|
+
|
|
93
|
+
def has_next(self) -> bool:
|
|
94
|
+
"""Check if a dotenv has the next position."""
|
|
95
|
+
|
|
96
|
+
return self.position.chars < len(self.string)
|
|
97
|
+
|
|
98
|
+
def set_mark(self) -> None: # noqa: WPS615
|
|
99
|
+
"""Set a mark."""
|
|
100
|
+
|
|
101
|
+
# WPS615 - this setter is not a standard OOP setter
|
|
102
|
+
|
|
103
|
+
self.mark.set(self.position)
|
|
104
|
+
|
|
105
|
+
def get_marked(self) -> Original:
|
|
106
|
+
"""Get a mark."""
|
|
107
|
+
|
|
108
|
+
# fmt: off
|
|
109
|
+
return Original(
|
|
110
|
+
string=self.string[self.mark.chars: self.position.chars],
|
|
111
|
+
line=self.mark.line,
|
|
112
|
+
)
|
|
113
|
+
# fmt: on
|
|
114
|
+
|
|
115
|
+
def peek(self, count: int) -> str:
|
|
116
|
+
"""Peek a dotenv."""
|
|
117
|
+
|
|
118
|
+
# fmt: off
|
|
119
|
+
return self.string[self.position.chars: self.position.chars + count]
|
|
120
|
+
# fmt: on
|
|
121
|
+
|
|
122
|
+
def read_regex(self, regex: re.Pattern[str]) -> tuple[str, ...]:
|
|
123
|
+
"""Read a dotenv with a regex."""
|
|
124
|
+
|
|
125
|
+
match = regex.match(self.string, self.position.chars)
|
|
126
|
+
|
|
127
|
+
if match is None:
|
|
128
|
+
raise DotenvParseError("Pattern not found.")
|
|
129
|
+
|
|
130
|
+
# fmt: off
|
|
131
|
+
matched = self.string[match.start(): match.end()]
|
|
132
|
+
# fmt: on
|
|
133
|
+
|
|
134
|
+
self.position.advance(matched)
|
|
135
|
+
return match.groups()
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def decode_match(match: re.Match[str]) -> str:
|
|
139
|
+
"""Decode matches."""
|
|
140
|
+
|
|
141
|
+
return codecs.decode(match.group(0), "unicode-escape")
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def decode_escapes(regex: re.Pattern[str], string: str) -> str:
|
|
145
|
+
"""Decode escape symbols."""
|
|
146
|
+
|
|
147
|
+
return regex.sub(decode_match, string)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def parse_key(reader: Reader) -> str | None:
|
|
151
|
+
"""Parse a dotenv key."""
|
|
152
|
+
|
|
153
|
+
char = reader.peek(1)
|
|
154
|
+
|
|
155
|
+
if char == "#":
|
|
156
|
+
return None
|
|
157
|
+
|
|
158
|
+
elif char == "'":
|
|
159
|
+
key, *_ = reader.read_regex(_single_quoted_key)
|
|
160
|
+
|
|
161
|
+
else:
|
|
162
|
+
key, *_ = reader.read_regex(_unquoted_key)
|
|
163
|
+
|
|
164
|
+
return key
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def parse_unquoted_value(reader: Reader) -> str:
|
|
168
|
+
"""Parse an unquoted dotenv value."""
|
|
169
|
+
|
|
170
|
+
part, *_ = reader.read_regex(_unquoted_value)
|
|
171
|
+
return re.sub(r"\s+#.*", "", part).rstrip()
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def parse_value(reader: Reader) -> str:
|
|
175
|
+
"""Parse a dotenv value."""
|
|
176
|
+
|
|
177
|
+
char = reader.peek(1)
|
|
178
|
+
|
|
179
|
+
if char == "'":
|
|
180
|
+
value, *_ = reader.read_regex(_single_quoted_value)
|
|
181
|
+
return decode_escapes(_single_quote_escapes, value)
|
|
182
|
+
|
|
183
|
+
elif char == '"':
|
|
184
|
+
value, *_ = reader.read_regex(_double_quoted_value)
|
|
185
|
+
return decode_escapes(_double_quote_escapes, value)
|
|
186
|
+
|
|
187
|
+
elif char in {"", "\n", "\r"}:
|
|
188
|
+
return ""
|
|
189
|
+
|
|
190
|
+
return parse_unquoted_value(reader)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def parse_binding(reader: Reader) -> Binding:
|
|
194
|
+
"""Parse a dotenv binding."""
|
|
195
|
+
|
|
196
|
+
reader.set_mark()
|
|
197
|
+
|
|
198
|
+
# WPS229 - this part is a part of the original package
|
|
199
|
+
try: # noqa: WPS229
|
|
200
|
+
reader.read_regex(_multiline_whitespace)
|
|
201
|
+
|
|
202
|
+
if not reader.has_next():
|
|
203
|
+
return Binding(
|
|
204
|
+
key=None,
|
|
205
|
+
value=None,
|
|
206
|
+
original=reader.get_marked(),
|
|
207
|
+
error=False,
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
reader.read_regex(_export)
|
|
211
|
+
key = parse_key(reader)
|
|
212
|
+
reader.read_regex(_whitespace)
|
|
213
|
+
|
|
214
|
+
if reader.peek(1) == "=":
|
|
215
|
+
reader.read_regex(_equal_sign)
|
|
216
|
+
value = parse_value(reader)
|
|
217
|
+
|
|
218
|
+
else:
|
|
219
|
+
value = None
|
|
220
|
+
|
|
221
|
+
reader.read_regex(_comment)
|
|
222
|
+
reader.read_regex(_end_of_line)
|
|
223
|
+
|
|
224
|
+
return Binding(
|
|
225
|
+
key=key,
|
|
226
|
+
value=value,
|
|
227
|
+
original=reader.get_marked(),
|
|
228
|
+
error=False,
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
except DotenvParseError:
|
|
232
|
+
reader.read_regex(_rest_of_line)
|
|
233
|
+
return Binding(
|
|
234
|
+
key=None,
|
|
235
|
+
value=None,
|
|
236
|
+
original=reader.get_marked(),
|
|
237
|
+
error=True,
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def parse_stream(stream: IO[str]) -> Iterator[Binding]:
|
|
242
|
+
"""Parse a dotenv stream."""
|
|
243
|
+
|
|
244
|
+
reader = Reader(stream)
|
|
245
|
+
|
|
246
|
+
while reader.has_next():
|
|
247
|
+
yield parse_binding(reader)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Module that contains models of the dotenv variables/literals."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
import typing
|
|
7
|
+
import dataclasses
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
_posix_variable = re.compile(
|
|
11
|
+
r"""
|
|
12
|
+
\$\{
|
|
13
|
+
(?P<name>[^}:]*)
|
|
14
|
+
(?::-
|
|
15
|
+
(?P<default>[^}]*)
|
|
16
|
+
)?
|
|
17
|
+
}
|
|
18
|
+
""",
|
|
19
|
+
re.VERBOSE,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclasses.dataclass(frozen=True)
|
|
24
|
+
class Literal:
|
|
25
|
+
"""Model of a literal."""
|
|
26
|
+
|
|
27
|
+
value: str
|
|
28
|
+
|
|
29
|
+
def resolve(self, *args, **kwargs) -> str:
|
|
30
|
+
"""Get a literal value."""
|
|
31
|
+
|
|
32
|
+
return self.value
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclasses.dataclass(frozen=True)
|
|
36
|
+
class Variable:
|
|
37
|
+
"""Model of a variable."""
|
|
38
|
+
|
|
39
|
+
name: str
|
|
40
|
+
default: str | None = None
|
|
41
|
+
|
|
42
|
+
def resolve(self, env: typing.OrderedDict[str, str], *args, **kwargs) -> str:
|
|
43
|
+
"""Get a variable value."""
|
|
44
|
+
|
|
45
|
+
default = self.default if self.default else ""
|
|
46
|
+
env_val = env.get(self.name, default)
|
|
47
|
+
return env_val if env_val else ""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def parse(value: str) -> typing.Iterator[Literal | Variable]:
|
|
51
|
+
"""Parse values."""
|
|
52
|
+
|
|
53
|
+
cursor = 0
|
|
54
|
+
|
|
55
|
+
for match in _posix_variable.finditer(value):
|
|
56
|
+
start, end = match.span()
|
|
57
|
+
name = match.groupdict()["name"]
|
|
58
|
+
default = match.groupdict()["default"]
|
|
59
|
+
|
|
60
|
+
if start > cursor:
|
|
61
|
+
yield Literal(value=value[cursor:start])
|
|
62
|
+
|
|
63
|
+
yield Variable(name=name, default=default)
|
|
64
|
+
cursor = end
|
|
65
|
+
|
|
66
|
+
length = len(value)
|
|
67
|
+
if cursor < length:
|
|
68
|
+
yield Literal(value=value[cursor:length])
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Module that contains the core functionality of the plugin."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import enum
|
|
7
|
+
|
|
8
|
+
from cleo.events.console_command_event import ConsoleCommandEvent
|
|
9
|
+
from cleo.events.console_events import COMMAND
|
|
10
|
+
from poetry.console.application import Application
|
|
11
|
+
from poetry.console.commands.env_command import EnvCommand
|
|
12
|
+
from poetry.plugins.application_plugin import ApplicationPlugin
|
|
13
|
+
|
|
14
|
+
from poetry_plugin_dotenv import dotenv
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Verbosity(enum.Enum): # pragma: no cover
|
|
18
|
+
"""Levels of verbosity."""
|
|
19
|
+
|
|
20
|
+
info = "<info>{0!s}</info>"
|
|
21
|
+
debug = "<debug>{0!s}</debug>"
|
|
22
|
+
warning = "<warning>{0!s}</warning>"
|
|
23
|
+
error = "<error>{0!s}</error>"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Logger: # pragma: no cover
|
|
27
|
+
"""Logger of the ``poetry`` events.
|
|
28
|
+
|
|
29
|
+
Because this logger is used for the plugin that are running before the main command,
|
|
30
|
+
all the messages will be logged only in the debug mode.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
def __init__(self, event: ConsoleCommandEvent) -> None:
|
|
34
|
+
"""Initialize."""
|
|
35
|
+
|
|
36
|
+
self.event = event
|
|
37
|
+
|
|
38
|
+
def info(self, msg: str) -> None:
|
|
39
|
+
"""Log a info message."""
|
|
40
|
+
|
|
41
|
+
if self.event.io.is_debug():
|
|
42
|
+
self.event.io.write_line(Verbosity.info.value.format(msg))
|
|
43
|
+
|
|
44
|
+
def debug(self, msg: str) -> None:
|
|
45
|
+
"""Log a debug message."""
|
|
46
|
+
|
|
47
|
+
if self.event.io.is_debug():
|
|
48
|
+
self.event.io.write_line(Verbosity.debug.value.format(msg))
|
|
49
|
+
|
|
50
|
+
def warning(self, msg: str) -> None:
|
|
51
|
+
"""Log a warning message."""
|
|
52
|
+
|
|
53
|
+
if self.event.io.is_debug():
|
|
54
|
+
self.event.io.write_line(Verbosity.warning.value.format(msg))
|
|
55
|
+
|
|
56
|
+
def error(self, msg: str) -> None:
|
|
57
|
+
"""Log a error message."""
|
|
58
|
+
|
|
59
|
+
if self.event.io.is_debug():
|
|
60
|
+
self.event.io.write_error_line(Verbosity.error.value.format(msg))
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class DotenvPlugin(ApplicationPlugin):
|
|
64
|
+
"""Plugin that automatically loads environment variables from a dotenv file.
|
|
65
|
+
|
|
66
|
+
Plugin that automatically loads environment variables from a dotenv file into the environment
|
|
67
|
+
before ``poetry`` commands are run.
|
|
68
|
+
|
|
69
|
+
Notes
|
|
70
|
+
-----
|
|
71
|
+
To prevent ``poetry`` from loading the dotenv file, set the ``POETRY_DONT_LOAD_DOTENV``
|
|
72
|
+
environment variable.
|
|
73
|
+
|
|
74
|
+
If your dotenv file is located in a different path or has a different name you may set
|
|
75
|
+
the ``POETRY_DOTENV_LOCATION`` environment variable.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
def activate(self, application: Application) -> None: # pragma: no cover
|
|
79
|
+
"""Activate the plugin."""
|
|
80
|
+
|
|
81
|
+
application.event_dispatcher.add_listener(COMMAND, listener=self.load) # type: ignore
|
|
82
|
+
|
|
83
|
+
def load(self, event: ConsoleCommandEvent, *args, **kwargs) -> None:
|
|
84
|
+
"""Load a dotenv file."""
|
|
85
|
+
|
|
86
|
+
logger = Logger(event)
|
|
87
|
+
|
|
88
|
+
dont_load_dotenv = os.getenv("POETRY_DONT_LOAD_DOTENV")
|
|
89
|
+
dotenv_location = os.getenv("POETRY_DOTENV_LOCATION")
|
|
90
|
+
is_env_command = isinstance(event.command, EnvCommand)
|
|
91
|
+
|
|
92
|
+
if is_env_command and dont_load_dotenv:
|
|
93
|
+
logger.warning("Not loading environment variables.")
|
|
94
|
+
|
|
95
|
+
elif is_env_command and not dont_load_dotenv:
|
|
96
|
+
filepath = dotenv_location if dotenv_location else dotenv.core.find(usecwd=True)
|
|
97
|
+
|
|
98
|
+
if os.path.isfile(filepath):
|
|
99
|
+
logger.info(f"Loading environment variables from {filepath!r}.")
|
|
100
|
+
dotenv.core.load(filepath=filepath)
|
|
101
|
+
|
|
102
|
+
else:
|
|
103
|
+
logger.error(f"File {filepath!r} doesn't exist.")
|
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Volodymyr Pivoshenko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: poetry-plugin-dotenv
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: poetry-plugin-dotenv - is the plugin that automatically loads environment variables from a dotenv file into the environment before poetry commands are run.
|
|
5
|
+
Home-page: https://github.com/volopivoshenko/poetry-plugin-dotenv
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: python,pypi,poetry,plugin,plugins,poetry-plugin,poetry-plugins,env,dotenv,cross-platform,hacktoberfest
|
|
8
|
+
Author: Volodymyr Pivoshenko
|
|
9
|
+
Author-email: volodymyr.pivoshenko@gmail.com
|
|
10
|
+
Maintainer: Volodymyr Pivoshenko
|
|
11
|
+
Maintainer-email: volodymyr.pivoshenko@gmail.com
|
|
12
|
+
Requires-Python: >=3.8.1,<4.0
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Environment :: Other Environment
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Intended Audience :: Information Technology
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Natural Language :: English
|
|
19
|
+
Classifier: Operating System :: OS Independent
|
|
20
|
+
Classifier: Programming Language :: Python
|
|
21
|
+
Classifier: Programming Language :: Python :: 3
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
25
|
+
Classifier: Programming Language :: Python :: 3
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
28
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
29
|
+
Classifier: Topic :: Scientific/Engineering
|
|
30
|
+
Classifier: Topic :: Software Development
|
|
31
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
32
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
33
|
+
Requires-Dist: poetry (>=1.4.2,<2.0.0)
|
|
34
|
+
Project-URL: Documentation, https://github.com/volopivoshenko/poetry-plugin-dotenv
|
|
35
|
+
Project-URL: Issues, https://github.com/volopivoshenko/poetry-plugin-dotenv/issues
|
|
36
|
+
Project-URL: Repository, https://github.com/volopivoshenko/poetry-plugin-dotenv
|
|
37
|
+
Project-URL: Releases, https://github.com/volopivoshenko/poetry-plugin-dotenv/releases
|
|
38
|
+
Project-URL: Say Thanks!, https://www.buymeacoffee.com/volopivoshenko
|
|
39
|
+
Description-Content-Type: text/markdown
|
|
40
|
+
|
|
41
|
+
<div align="center">
|
|
42
|
+
<a href="https://pypi.org/project/poetry-plugin-dotenv">
|
|
43
|
+
<img alt="logo" src="https://github.com/volopivoshenko/poetry-plugin-dotenv/blob/main/docs/static/assets/logo.svg?raw=True" height=200>
|
|
44
|
+
</a>
|
|
45
|
+
</div>
|
|
46
|
+
|
|
47
|
+
<p align="center">
|
|
48
|
+
<a href="https://opensource.org/licenses/MIT">
|
|
49
|
+
<img alt="license" src="https://img.shields.io/pypi/l/poetry-plugin-dotenv?logo=opensourceinitiative">
|
|
50
|
+
</a>
|
|
51
|
+
<a href="https://pypi.org/project/poetry-plugin-dotenv">
|
|
52
|
+
<img alt="python" src="https://img.shields.io/pypi/pyversions/poetry-plugin-dotenv?logo=python">
|
|
53
|
+
</a>
|
|
54
|
+
<a href="https://pypi.org/project/poetry-plugin-dotenv">
|
|
55
|
+
<img alt="pypi" src="https://img.shields.io/pypi/v/poetry-plugin-dotenv?logo=pypi">
|
|
56
|
+
</a>
|
|
57
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/releases">
|
|
58
|
+
<img alt="release" src="https://img.shields.io/github/v/release/volopivoshenko/poetry-plugin-dotenv?logo=github">
|
|
59
|
+
</a>
|
|
60
|
+
<a href="https://numpydoc.readthedocs.io/en/latest/format.html">
|
|
61
|
+
<img alt="numpydoc" src="https://img.shields.io/badge/docstrings-numpy-1f425f.svg?logo=numpy">
|
|
62
|
+
</a>
|
|
63
|
+
</p>
|
|
64
|
+
|
|
65
|
+
<p align="center">
|
|
66
|
+
<a href="https://github.com/psf/black">
|
|
67
|
+
<img alt="black" src="https://img.shields.io/badge/code_style-black-black.svg?logo=windowsterminal">
|
|
68
|
+
</a>
|
|
69
|
+
<a href="https://pycqa.github.io/isort/index.html">
|
|
70
|
+
<img alt="isort" src="https://img.shields.io/badge/imports-isort-black.svg?logo=windowsterminal">
|
|
71
|
+
</a>
|
|
72
|
+
<a href="https://wemake-python-stylegui.de/en/latest/index.html">
|
|
73
|
+
<img alt="wemake" src="https://img.shields.io/badge/style-wemake-black.svg?logo=windowsterminal">
|
|
74
|
+
</a>
|
|
75
|
+
<a href="https://mypy.readthedocs.io/en/stable/index.html">
|
|
76
|
+
<img alt="mypy" src="https://img.shields.io/badge/mypy-checked-success.svg?logo=python">
|
|
77
|
+
</a>
|
|
78
|
+
<a href="https://github.com/semantic-release/semantic-release">
|
|
79
|
+
<img alt="semantic_release" src="https://img.shields.io/badge/semantic_release-angular-e10079?logo=semantic-release">
|
|
80
|
+
</a>
|
|
81
|
+
</p>
|
|
82
|
+
|
|
83
|
+
<p align="center">
|
|
84
|
+
<a href="https://github.com/dependabot">
|
|
85
|
+
<img alt="dependabot" src="https://img.shields.io/badge/dependabot-enable-success?logo=Dependabot">
|
|
86
|
+
</a>
|
|
87
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/actions/workflows/ci.yaml">
|
|
88
|
+
<img alt="CI" src="https://img.shields.io/github/actions/workflow/status/volopivoshenko/poetry-plugin-dotenv/ci.yaml?label=CI&logo=github">
|
|
89
|
+
</a>
|
|
90
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/actions/workflows/cd.yaml">
|
|
91
|
+
<img alt="CD" src="https://img.shields.io/github/actions/workflow/status/volopivoshenko/poetry-plugin-dotenv/cd.yaml?label=CD&logo=github">
|
|
92
|
+
</a>
|
|
93
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/actions/workflows/codeql.yaml">
|
|
94
|
+
<img alt="CodeQL" src="https://img.shields.io/github/actions/workflow/status/volopivoshenko/poetry-plugin-dotenv/codeql.yaml?label=CodeQL&logo=github">
|
|
95
|
+
</a>
|
|
96
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/actions/workflows/dependency-review.yaml">
|
|
97
|
+
<img alt="Dependency Review" src="https://img.shields.io/github/actions/workflow/status/volopivoshenko/poetry-plugin-dotenv/dependency-review.yaml?label=Dependency%20Review&logo=github">
|
|
98
|
+
</a>
|
|
99
|
+
<a href="https://pypi.org/project/poetry-plugin-dotenv">
|
|
100
|
+
<img alt="wheel" src="https://img.shields.io/pypi/wheel/poetry-plugin-dotenv?logo=pypi">
|
|
101
|
+
</a>
|
|
102
|
+
</p>
|
|
103
|
+
|
|
104
|
+
<p align="center">
|
|
105
|
+
<a href="https://codecov.io/gh/volopivoshenko/poetry-plugin-dotenv">
|
|
106
|
+
<img alt="coverage" src="https://img.shields.io/codecov/c/gh/volopivoshenko/poetry-plugin-dotenv?logo=codecov&token=yyck08xfTN"/>
|
|
107
|
+
</a>
|
|
108
|
+
<a href="https://codeclimate.com/github/volopivoshenko/poetry-plugin-dotenv/maintainability">
|
|
109
|
+
<img alt="codeclimate" src="https://img.shields.io/codeclimate/maintainability/volopivoshenko/poetry-plugin-dotenv?logo=codeclimate">
|
|
110
|
+
</a>
|
|
111
|
+
<a href="https://pypi.org/project/poetry-plugin-dotenv">
|
|
112
|
+
<img alt="downloads" src="https://img.shields.io/pypi/dm/poetry-plugin-dotenv?logo=pypi">
|
|
113
|
+
</a>
|
|
114
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/">
|
|
115
|
+
<img alt="stars" src="https://img.shields.io/github/stars/volopivoshenko/poetry-plugin-dotenv?logo=github">
|
|
116
|
+
</a>
|
|
117
|
+
</p>
|
|
118
|
+
|
|
119
|
+
<p align="center">
|
|
120
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/issues">
|
|
121
|
+
<img alt="issues" src="https://img.shields.io/github/issues/volopivoshenko/poetry-plugin-dotenv?logo=github">
|
|
122
|
+
</a>
|
|
123
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/issues">
|
|
124
|
+
<img alt="issues" src="https://img.shields.io/github/issues-closed/volopivoshenko/poetry-plugin-dotenv?logo=github">
|
|
125
|
+
</a>
|
|
126
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/pulls">
|
|
127
|
+
<img alt="pr" src="https://img.shields.io/github/issues-pr/volopivoshenko/poetry-plugin-dotenv?logo=github">
|
|
128
|
+
</a>
|
|
129
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/pulls">
|
|
130
|
+
<img alt="pr" src="https://img.shields.io/github/issues-pr-closed/volopivoshenko/poetry-plugin-dotenv?logo=github">
|
|
131
|
+
</a>
|
|
132
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/graphs/contributors">
|
|
133
|
+
<img alt="contributors" src="https://img.shields.io/github/contributors/volopivoshenko/poetry-plugin-dotenv?logo=github">
|
|
134
|
+
</a>
|
|
135
|
+
<a href="https://github.com/volopivoshenko/poetry-plugin-dotenv/commits/main">
|
|
136
|
+
<img alt="commit" src="https://img.shields.io/github/last-commit/volopivoshenko/poetry-plugin-dotenv?logo=github">
|
|
137
|
+
</a>
|
|
138
|
+
</p>
|
|
139
|
+
|
|
140
|
+
<p align="center">
|
|
141
|
+
<!-- <a href="https://www.buymeacoffee.com/volopivoshenko" target="_blank">
|
|
142
|
+
<img alt="buymeacoffee" src="https://img.shields.io/badge/buy_me_-a_coffee-ff6964?logo=buymeacoffee">
|
|
143
|
+
</a> -->
|
|
144
|
+
<a href="https://stand-with-ukraine.pp.ua/">
|
|
145
|
+
<img alt="standwithukraine" src="https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB">
|
|
146
|
+
</a>
|
|
147
|
+
<a href="https://stand-with-ukraine.pp.ua">
|
|
148
|
+
<img alt="standwithukraine" src="https://img.shields.io/badge/made_in-Ukraine-ffd700.svg?labelColor=0057b7">
|
|
149
|
+
</a>
|
|
150
|
+
</p>
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
- [🔮 Overview](#-overview)
|
|
155
|
+
- [⚙️ Installation](#️-installation)
|
|
156
|
+
- [👩🏻💻 Usage](#-usage)
|
|
157
|
+
|
|
158
|
+
# 🔮 Overview
|
|
159
|
+
|
|
160
|
+
`poetry-plugin-dotenv` - is the plugin that automatically loads environment variables from a dotenv file into the environment before `poetry` commands are run.
|
|
161
|
+
|
|
162
|
+
**This plugin doesn't have any dependencies, but therefore it also supports features that `python-dotenv` supports (e.g. templates, interpolating variables using `POSIX` variable expansions etc).**
|
|
163
|
+
|
|
164
|
+
# ⚙️ Installation
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
poetry self add poetry-plugin-dotenv
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
# 👩🏻💻 Usage
|
|
171
|
+
|
|
172
|
+
By default, plugin will load the `.env` file from the current working directory or "higher directories".
|
|
173
|
+
|
|
174
|
+
To prevent ``poetry`` from loading the dotenv file, set the ``POETRY_DONT_LOAD_DOTENV`` environment variable.
|
|
175
|
+
|
|
176
|
+
If your dotenv file is located in a different path or has a different name you may set the ``POETRY_DOTENV_LOCATION`` environment variable.
|
|
177
|
+
|
|
178
|
+
```dotenv
|
|
179
|
+
# .env
|
|
180
|
+
DB__HOST=localhost
|
|
181
|
+
DB__DBNAME=prod
|
|
182
|
+
DB__USER=admin
|
|
183
|
+
DB__PASSWORD=admin
|
|
184
|
+
DB__ENGINE=postgresql://${DB__USER}:${DB__PASSWORD}@${DB__HOST}/${DB__DBNAME}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
```dotenv
|
|
188
|
+
# .env.dev
|
|
189
|
+
DB__HOST=localhost
|
|
190
|
+
DB__DBNAME=dev
|
|
191
|
+
DB__USER=root
|
|
192
|
+
DB__PASSWORD=root
|
|
193
|
+
DB__ENGINE=postgresql://${DB__USER}:${DB__PASSWORD}@${DB__HOST}/${DB__DBNAME}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
# main.py
|
|
198
|
+
import os
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
if __name__ == "__main__":
|
|
202
|
+
try:
|
|
203
|
+
print(f"Host: {os.environ['DB__HOST']!r}")
|
|
204
|
+
print(f"Name: {os.environ['DB__DBNAME']!r}")
|
|
205
|
+
print(f"Username: {os.environ['DB__USER']!r}")
|
|
206
|
+
print(f"Password: {os.environ['DB__PASSWORD']!r}")
|
|
207
|
+
print(f"Engine: {os.environ['DB__ENGINE']!r}")
|
|
208
|
+
|
|
209
|
+
except KeyError:
|
|
210
|
+
print("Environment variables not set!")
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
poetry run -vvv python main.py
|
|
215
|
+
# Loading environment variables from '.env'.
|
|
216
|
+
# Host: 'localhost'
|
|
217
|
+
# Name: 'prod'
|
|
218
|
+
# Username: 'admin'
|
|
219
|
+
# Password: 'admin'
|
|
220
|
+
# Engine 'postgresql://admin:admin@localhost/prod'
|
|
221
|
+
|
|
222
|
+
export POETRY_DOTENV_LOCATION=.env.dev && poetry run -vvv python main.py
|
|
223
|
+
# Loading environment variables from '.env.dev'.
|
|
224
|
+
# Host: 'localhost'
|
|
225
|
+
# Name: 'dev'
|
|
226
|
+
# Username: 'root'
|
|
227
|
+
# Password: 'root'
|
|
228
|
+
# Engine 'postgresql://root:root@localhost/dev'
|
|
229
|
+
|
|
230
|
+
export POETRY_DONT_LOAD_DOTENV=1 && poetry run -vvv python main.py
|
|
231
|
+
# Not loading environment variables.
|
|
232
|
+
```
|
|
233
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
poetry_plugin_dotenv/__init__.py,sha256=jZEDCd5q9Rdy6wmH1OXeVNueGs0X0lYOT0VDp7kLUsw,699
|
|
2
|
+
poetry_plugin_dotenv/dotenv/__init__.py,sha256=6UIR90i2M6lCC-i5QSmTeVDme2Dr4pyViEg8Fi-eM5o,335
|
|
3
|
+
poetry_plugin_dotenv/dotenv/core.py,sha256=Bx9aDP5uiJaUN27PbmF4oD2MBFCiFmZSrC_mb9Go_Vg,5423
|
|
4
|
+
poetry_plugin_dotenv/dotenv/parsers.py,sha256=sErTqlDdElPp_6W2vfx4HXrXg12SbvRa0wEIkfKdOLs,6010
|
|
5
|
+
poetry_plugin_dotenv/dotenv/variables.py,sha256=kxQMngpIu_wNHbviwW1E5yaQEfPe8TZAGNgXKwtOf1Q,1445
|
|
6
|
+
poetry_plugin_dotenv/plugin.py,sha256=-PUJ3gn9ZJzku0aLLQH6upO61s5nL3zDPwB6P2pINOw,3395
|
|
7
|
+
poetry_plugin_dotenv/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
poetry_plugin_dotenv-0.4.0.dist-info/LICENSE,sha256=VlG_sGO2jx1EioJshzOjyGdooI_GDeT27ScNzJnNHFA,1077
|
|
9
|
+
poetry_plugin_dotenv-0.4.0.dist-info/METADATA,sha256=Sbl8wu6AvVzUYT2UKnrEPiAQbvW39XuGclMwf0FxPh4,10611
|
|
10
|
+
poetry_plugin_dotenv-0.4.0.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
|
|
11
|
+
poetry_plugin_dotenv-0.4.0.dist-info/entry_points.txt,sha256=UG5S6luUjVBQdhg0nAVR0wpJSpTJEw7vi-ZLsx2iBn8,91
|
|
12
|
+
poetry_plugin_dotenv-0.4.0.dist-info/RECORD,,
|