-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implemented *.env State
- Loading branch information
Showing
5 changed files
with
39 additions
and
2 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
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,24 @@ | ||
from io import StringIO | ||
from pathlib import Path | ||
from tempfile import TemporaryDirectory | ||
from typing import Any | ||
|
||
import dotenv | ||
|
||
from iokit.state import State | ||
|
||
|
||
class Env(State, suffix="env"): | ||
def __init__(self, data: dict[str, str], **kwargs: Any): | ||
with TemporaryDirectory() as root: | ||
path = Path(root) / "env" | ||
for key, value in data.items(): | ||
dotenv.set_key( | ||
dotenv_path=path, key_to_set=key, value_to_set=value, quote_mode="auto" | ||
) | ||
data_bytes = path.read_bytes() | ||
super().__init__(data=data_bytes, **kwargs) | ||
|
||
def load(self) -> dict[str, str | None]: | ||
stream = StringIO(self.data.getvalue().decode()) | ||
return dict(dotenv.dotenv_values(stream=stream)) |
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,9 @@ | ||
from iokit import Env | ||
|
||
|
||
def test_env_state() -> None: | ||
data = {"login": "user", "password": "pass"} | ||
state = Env(data, name="creds") | ||
assert state.size > 0 | ||
assert state.name == "creds.env" | ||
assert state.load() == data |