Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple variable parsing and expansion
This is now done in two steps: - Parse the value into a sequence of atoms (literal of variable). - Resolve that sequence into a string.
- Loading branch information
Showing
3 changed files
with
162 additions
and
38 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
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,106 @@ | ||
import re | ||
from abc import ABCMeta | ||
|
||
from .compat import IS_TYPE_CHECKING | ||
|
||
if IS_TYPE_CHECKING: | ||
from typing import Iterator, Mapping, Optional, Pattern, Text | ||
|
||
|
||
_posix_variable = re.compile( | ||
r""" | ||
\$\{ | ||
(?P<name>[^\}:]*) | ||
(?::- | ||
(?P<default>[^\}]*) | ||
)? | ||
\} | ||
""", | ||
re.VERBOSE, | ||
) # type: Pattern[Text] | ||
|
||
|
||
class Atom(): | ||
__metaclass__ = ABCMeta | ||
|
||
def __ne__(self, other): | ||
# type: (object) -> bool | ||
result = self.__eq__(other) | ||
if result is NotImplemented: | ||
return NotImplemented | ||
return not result | ||
|
||
def resolve(self, env): | ||
# type: (Mapping[Text, Optional[Text]]) -> Text | ||
raise NotImplementedError | ||
|
||
|
||
class Literal(Atom): | ||
def __init__(self, value): | ||
# type: (Text) -> None | ||
self.value = value | ||
|
||
def __repr__(self): | ||
# type: () -> str | ||
return "Literal(value={})".format(self.value) | ||
|
||
def __eq__(self, other): | ||
# type: (object) -> bool | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
return self.value == other.value | ||
|
||
def __hash__(self): | ||
# type: () -> int | ||
return hash((self.__class__, self.value)) | ||
|
||
def resolve(self, env): | ||
# type: (Mapping[Text, Optional[Text]]) -> Text | ||
return self.value | ||
|
||
|
||
class Variable(Atom): | ||
def __init__(self, name, default): | ||
# type: (Text, Optional[Text]) -> None | ||
self.name = name | ||
self.default = default | ||
|
||
def __repr__(self): | ||
# type: () -> str | ||
return "Variable(name={}, default={})".format(self.name, self.default) | ||
|
||
def __eq__(self, other): | ||
# type: (object) -> bool | ||
if not isinstance(other, self.__class__): | ||
return NotImplemented | ||
return (self.name, self.default) == (other.name, other.default) | ||
|
||
def __hash__(self): | ||
# type: () -> int | ||
return hash((self.__class__, self.name, self.default)) | ||
|
||
def resolve(self, env): | ||
# type: (Mapping[Text, Optional[Text]]) -> Text | ||
default = self.default if self.default is not None else "" | ||
result = env.get(self.name, default) | ||
return result if result is not None else "" | ||
|
||
|
||
def parse_variables(value): | ||
# type: (Text) -> Iterator[Atom] | ||
cursor = 0 | ||
|
||
for match in _posix_variable.finditer(value): | ||
(start, end) = match.span() | ||
name = match.groupdict()["name"] | ||
default = match.groupdict()["default"] | ||
|
||
if start > cursor: | ||
yield Literal(value=value[cursor:start]) | ||
|
||
yield Variable(name=name, default=default) | ||
cursor = end | ||
|
||
length = len(value) | ||
if cursor < length: | ||
yield Literal(value=value[cursor:length]) |
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,35 @@ | ||
import pytest | ||
|
||
from dotenv.variables import Literal, Variable, parse_variables | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value,expected", | ||
[ | ||
("", []), | ||
("a", [Literal(value="a")]), | ||
("${a}", [Variable(name="a", default=None)]), | ||
("${a:-b}", [Variable(name="a", default="b")]), | ||
( | ||
"${a}${b}", | ||
[ | ||
Variable(name="a", default=None), | ||
Variable(name="b", default=None), | ||
], | ||
), | ||
( | ||
"a${b}c${d}e", | ||
[ | ||
Literal(value="a"), | ||
Variable(name="b", default=None), | ||
Literal(value="c"), | ||
Variable(name="d", default=None), | ||
Literal(value="e"), | ||
], | ||
), | ||
] | ||
) | ||
def test_parse_variables(value, expected): | ||
result = parse_variables(value) | ||
|
||
assert list(result) == expected |