|
4 | 4 | import io |
5 | 5 | import logging |
6 | 6 | import os |
7 | | -import re |
8 | 7 | import shutil |
9 | 8 | import sys |
10 | 9 | import tempfile |
|
13 | 12 |
|
14 | 13 | from .compat import IS_TYPE_CHECKING, PY2, StringIO, to_env |
15 | 14 | from .parser import Binding, parse_stream |
| 15 | +from .variables import parse_variables |
16 | 16 |
|
17 | 17 | logger = logging.getLogger(__name__) |
18 | 18 |
|
19 | 19 | if IS_TYPE_CHECKING: |
20 | | - from typing import ( |
21 | | - Dict, Iterable, Iterator, Match, Optional, Pattern, Union, Text, IO, Tuple |
22 | | - ) |
| 20 | + from typing import (IO, Dict, Iterable, Iterator, Mapping, Optional, Text, |
| 21 | + Tuple, Union) |
23 | 22 | if sys.version_info >= (3, 6): |
24 | 23 | _PathLike = os.PathLike |
25 | 24 | else: |
|
30 | 29 | else: |
31 | 30 | _StringIO = StringIO[Text] |
32 | 31 |
|
33 | | -__posix_variable = re.compile( |
34 | | - r""" |
35 | | - \$\{ |
36 | | - (?P<name>[^\}:]*) |
37 | | - (?::- |
38 | | - (?P<default>[^\}]*) |
39 | | - )? |
40 | | - \} |
41 | | - """, |
42 | | - re.VERBOSE, |
43 | | -) # type: Pattern[Text] |
44 | | - |
45 | 32 |
|
46 | 33 | def with_warn_for_invalid_lines(mappings): |
47 | 34 | # type: (Iterator[Binding]) -> Iterator[Binding] |
@@ -83,13 +70,14 @@ def dict(self): |
83 | 70 | if self._dict: |
84 | 71 | return self._dict |
85 | 72 |
|
| 73 | + raw_values = self.parse() |
| 74 | + |
86 | 75 | if self.interpolate: |
87 | | - values = resolve_nested_variables(self.parse()) |
| 76 | + self._dict = OrderedDict(resolve_variables(raw_values)) |
88 | 77 | else: |
89 | | - values = OrderedDict(self.parse()) |
| 78 | + self._dict = OrderedDict(raw_values) |
90 | 79 |
|
91 | | - self._dict = values |
92 | | - return values |
| 80 | + return self._dict |
93 | 81 |
|
94 | 82 | def parse(self): |
95 | 83 | # type: () -> Iterator[Tuple[Text, Optional[Text]]] |
@@ -217,27 +205,22 @@ def unset_key(dotenv_path, key_to_unset, quote_mode="always"): |
217 | 205 | return removed, key_to_unset |
218 | 206 |
|
219 | 207 |
|
220 | | -def resolve_nested_variables(values): |
221 | | - # type: (Iterable[Tuple[Text, Optional[Text]]]) -> Dict[Text, Optional[Text]] |
222 | | - def _replacement(name, default): |
223 | | - # type: (Text, Optional[Text]) -> Text |
224 | | - default = default if default is not None else "" |
225 | | - ret = new_values.get(name, os.getenv(name, default)) |
226 | | - return ret # type: ignore |
| 208 | +def resolve_variables(values): |
| 209 | + # type: (Iterable[Tuple[Text, Optional[Text]]]) -> Mapping[Text, Optional[Text]] |
227 | 210 |
|
228 | | - def _re_sub_callback(match): |
229 | | - # type: (Match[Text]) -> Text |
230 | | - """ |
231 | | - From a match object gets the variable name and returns |
232 | | - the correct replacement |
233 | | - """ |
234 | | - matches = match.groupdict() |
235 | | - return _replacement(name=matches["name"], default=matches["default"]) # type: ignore |
| 211 | + new_values = {} # type: Dict[Text, Optional[Text]] |
236 | 212 |
|
237 | | - new_values = {} |
| 213 | + for (name, value) in values: |
| 214 | + if value is None: |
| 215 | + result = None |
| 216 | + else: |
| 217 | + atoms = parse_variables(value) |
| 218 | + env = {} # type: Dict[Text, Optional[Text]] |
| 219 | + env.update(os.environ) # type: ignore |
| 220 | + env.update(new_values) |
| 221 | + result = "".join(atom.resolve(env) for atom in atoms) |
238 | 222 |
|
239 | | - for (k, v) in values: |
240 | | - new_values[k] = __posix_variable.sub(_re_sub_callback, v) if v is not None else None |
| 223 | + new_values[name] = result |
241 | 224 |
|
242 | 225 | return new_values |
243 | 226 |
|
|
0 commit comments