Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize variables code #434

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/dotenv/variables.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import re
from abc import ABCMeta
from abc import ABCMeta, abstractmethod
from typing import Iterator, Mapping, Optional, Pattern

_posix_variable = re.compile(
_posix_variable: Pattern[str] = re.compile(
r"""
\$\{
(?P<name>[^\}:]*)
Expand All @@ -12,28 +12,26 @@
\}
""",
re.VERBOSE,
) # type: Pattern[str]
)


class Atom():
__metaclass__ = ABCMeta

class Atom(metaclass=ABCMeta):
def __ne__(self, other: object) -> bool:
result = self.__eq__(other)
if result is NotImplemented:
return NotImplemented
return not result

def resolve(self, env: Mapping[str, Optional[str]]) -> str:
raise NotImplementedError
@abstractmethod
def resolve(self, env: Mapping[str, Optional[str]]) -> str: ...


class Literal(Atom):
def __init__(self, value: str) -> None:
self.value = value

def __repr__(self) -> str:
return "Literal(value={})".format(self.value)
return f"Literal(value={self.value})"

def __eq__(self, other: object) -> bool:
if not isinstance(other, self.__class__):
Expand All @@ -53,7 +51,7 @@ def __init__(self, name: str, default: Optional[str]) -> None:
self.default = default

def __repr__(self) -> str:
return "Variable(name={}, default={})".format(self.name, self.default)
return f"Variable(name={self.name}, default={self.default})"

def __eq__(self, other: object) -> bool:
if not isinstance(other, self.__class__):
Expand All @@ -74,8 +72,8 @@ def parse_variables(value: str) -> Iterator[Atom]:

for match in _posix_variable.finditer(value):
(start, end) = match.span()
name = match.groupdict()["name"]
default = match.groupdict()["default"]
name = match["name"]
default = match["default"]

if start > cursor:
yield Literal(value=value[cursor:start])
Expand Down