Skip to content

Commit

Permalink
fix(dotenv): resolve issue with OrderedDict
Browse files Browse the repository at this point in the history
  • Loading branch information
pivoshenko committed Oct 22, 2022
1 parent 0641de3 commit 440171e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
38 changes: 17 additions & 21 deletions src/poetry_dotenv/dotenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
import io
import os
import sys
import typing
import contextlib

from collections import OrderedDict
from typing import IO
from typing import Iterable
from typing import Iterator
from typing import Optional
from typing import Tuple

from poetry_dotenv.dotenv import parsers
from poetry_dotenv.dotenv import variables
Expand All @@ -21,8 +17,8 @@ class DotEnv(object):

def __init__(
self,
filepath: Optional[str] = None,
stream: Optional[IO[str]] = None,
filepath: typing.Optional[str] = None,
stream: typing.Optional[typing.IO[str]] = None,
interpolate: bool = True,
override: bool = True,
) -> None:
Expand All @@ -35,9 +31,9 @@ def __init__(

self.encoding = "utf-8"

self._dict: Optional[OrderedDict[str, str]] = None
self._dict: typing.Optional[typing.OrderedDict[str, str]] = None

def dict(self) -> OrderedDict[str, str]:
def dict(self) -> typing.OrderedDict[str, str]:
"""Return content of a dotenv file."""

if self._dict:
Expand All @@ -53,7 +49,7 @@ def dict(self) -> OrderedDict[str, str]:

return self._dict

def parse(self) -> Iterator[Tuple[str, str]]:
def parse(self) -> typing.Iterator[typing.Tuple[str, str]]:
"""Parse a dotenv file."""

with self._get_stream() as stream:
Expand All @@ -78,7 +74,7 @@ def set_as_environment_variables(self) -> bool:
return False

@contextlib.contextmanager
def _get_stream(self) -> Iterator[IO[str]]:
def _get_stream(self) -> typing.Iterator[typing.IO[str]]:
"""Get a dotenv stream."""

if self.filepath and os.path.isfile(self.filepath):
Expand All @@ -93,20 +89,20 @@ def _get_stream(self) -> Iterator[IO[str]]:


def resolve(
values: Iterable[Tuple[str, str]],
values: typing.Iterable[typing.Tuple[str, str]],
override: bool,
) -> OrderedDict[str, str]:
) -> typing.OrderedDict[str, str]:
"""Resolve dotenv variables."""

new_values: OrderedDict[str, str] = OrderedDict()
new_values: typing.OrderedDict[str, str] = OrderedDict()

for (name, value) in values:
if value is None:
result = None

else:
atoms = variables.parse(value)
env: OrderedDict[str, str] = OrderedDict()
env: typing.OrderedDict[str, str] = OrderedDict()

if override:
env.update(os.environ)
Expand All @@ -123,7 +119,7 @@ def resolve(
return new_values


def walk_to_root(path: str) -> Iterator[str]:
def walk_to_root(path: str) -> typing.Iterator[str]:
"""Yield directories starting from the given directory up to the root."""

if not os.path.exists(path):
Expand Down Expand Up @@ -171,8 +167,8 @@ def find(filename: str = ".env", usecwd: bool = False) -> str:


def load(
filepath: Optional[str] = None,
stream: Optional[IO[str]] = None,
filepath: typing.Optional[str] = None,
stream: typing.Optional[typing.IO[str]] = None,
interpolate: bool = True,
override: bool = True,
) -> bool:
Expand All @@ -188,10 +184,10 @@ def load(


def values(
filepath: Optional[str] = None,
stream: Optional[IO[str]] = None,
filepath: typing.Optional[str] = None,
stream: typing.Optional[typing.IO[str]] = None,
interpolate: bool = True,
) -> OrderedDict[str, str]:
) -> typing.OrderedDict[str, str]:
"""Parse a dotenv file and return its content as a dictionary."""

return DotEnv(filepath=filepath, stream=stream, interpolate=interpolate).dict()
12 changes: 4 additions & 8 deletions src/poetry_dotenv/dotenv/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
from __future__ import annotations

import re
import typing
import dataclasses

from collections import OrderedDict
from typing import Iterator
from typing import Optional
from typing import Union


_posix_variable = re.compile(
r"""
Expand Down Expand Up @@ -41,17 +37,17 @@ class Variable(object):
"""Model of a variable."""

name: str
default: Optional[str] = None
default: typing.Optional[str] = None

def resolve(self, env: OrderedDict[str, str], *args, **kwargs) -> str:
def resolve(self, env: typing.OrderedDict[str, str], *args, **kwargs) -> str:
"""Get a variable value."""

default = self.default if self.default else ""
env_val = env.get(self.name, default)
return env_val if env_val else ""


def parse(value: str) -> Iterator[Union[Literal, Variable]]:
def parse(value: str) -> typing.Iterator[typing.Union[Literal, Variable]]:
"""Parse values."""

cursor = 0
Expand Down

0 comments on commit 440171e

Please sign in to comment.