Skip to content

Commit

Permalink
Modernize main.py and parser.py code
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 00cc7ae commit cadf4fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
28 changes: 14 additions & 14 deletions src/dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ def with_warn_for_invalid_lines(mappings: Iterator[Binding]) -> Iterator[Binding
yield mapping


class DotEnv():
class DotEnv:
def __init__(
self,
dotenv_path: Optional[Union[str, os.PathLike]],
stream: Optional[IO[str]] = None,
verbose: bool = False,
encoding: Union[None, str] = None,
encoding: Optional[str] = None,
interpolate: bool = True,
override: bool = True,
) -> None:
self.dotenv_path = dotenv_path # type: Optional[Union[str, os.PathLike]]
self.stream = stream # type: Optional[IO[str]]
self._dict = None # type: Optional[Dict[str, Optional[str]]]
self.verbose = verbose # type: bool
self.encoding = encoding # type: Union[None, str]
self.interpolate = interpolate # type: bool
self.override = override # type: bool
self.dotenv_path: Optional[Union[str, os.PathLike]] = dotenv_path
self.stream: Optional[IO[str]] = stream
self._dict: Optional[Dict[str, Optional[str]]] = None
self.verbose: bool = verbose
self.encoding: Optional[str] = encoding
self.interpolate: bool = interpolate
self.override: bool = override

@contextmanager
def _get_stream(self) -> Iterator[IO[str]]:
Expand Down Expand Up @@ -153,7 +153,7 @@ def set_key(
an orphan .env somewhere in the filesystem
"""
if quote_mode not in ("always", "auto", "never"):
raise ValueError("Unknown quote_mode: {}".format(quote_mode))
raise ValueError(f"Unknown quote_mode: {quote_mode}")

quote = (
quote_mode == "always"
Expand All @@ -165,9 +165,9 @@ def set_key(
else:
value_out = value_to_set
if export:
line_out = 'export {}={}\n'.format(key_to_set, value_out)
line_out = f'export {key_to_set}={value_out}\n'
else:
line_out = "{}={}\n".format(key_to_set, value_out)
line_out = f"{key_to_set}={value_out}\n"

with rewrite(dotenv_path, encoding=encoding) as (source, dest):
replaced = False
Expand Down Expand Up @@ -222,14 +222,14 @@ def resolve_variables(
values: Iterable[Tuple[str, Optional[str]]],
override: bool,
) -> Mapping[str, Optional[str]]:
new_values = {} # type: Dict[str, Optional[str]]
new_values: Dict[str, Optional[str]] = {}

for (name, value) in values:
if value is None:
result = None
else:
atoms = parse_variables(value)
env = {} # type: Dict[str, Optional[str]]
env: Dict[str, Optional[str]] = {}
if override:
env.update(os.environ) # type: ignore
env.update(new_values)
Expand Down
29 changes: 11 additions & 18 deletions src/dotenv/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,16 @@ def make_regex(string: str, extra_flags: int = 0) -> Pattern[str]:
_single_quote_escapes = make_regex(r"\\[\\']")


Original = NamedTuple(
"Original",
[
("string", str),
("line", int),
],
)

Binding = NamedTuple(
"Binding",
[
("key", Optional[str]),
("value", Optional[str]),
("original", Original),
("error", bool),
],
)
class Original(NamedTuple):
string: str
line: int


class Binding(NamedTuple):
key: Optional[str]
value: Optional[str]
original: Original
error: bool


class Position:
Expand Down Expand Up @@ -155,7 +148,7 @@ def parse_binding(reader: Reader) -> Binding:
reader.read_regex(_whitespace)
if reader.peek(1) == "=":
reader.read_regex(_equal_sign)
value = parse_value(reader) # type: Optional[str]
value: Optional[str] = parse_value(reader)
else:
value = None
reader.read_regex(_comment)
Expand Down

0 comments on commit cadf4fc

Please sign in to comment.