Skip to content

Commit

Permalink
Modernize some code in variables.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Nougat-Waffle authored and bbc2 committed Nov 11, 2022
1 parent 7e199c3 commit 00cc7ae
Showing 1 changed file with 10 additions and 12 deletions.
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

0 comments on commit 00cc7ae

Please sign in to comment.