Skip to content

Commit

Permalink
feat: v0.1.4 (#19)
Browse files Browse the repository at this point in the history
* implemented *.env State
  • Loading branch information
rilshok authored Jul 29, 2024
2 parents f685bcb + bcbf0dc commit c59e502
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies = [
"typing-extensions>=4.8.0",
"pytz>=2024.1",
"PyYAML>=6.0.1",
"python-dotenv>=1.0.1",
]

[tool.setuptools.dynamic]
Expand Down
5 changes: 3 additions & 2 deletions src/iokit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__all__ = [
"Dat",
"Env",
"Gzip",
"Json",
"Jsonl",
Expand All @@ -13,8 +14,8 @@
"save_file",
"save_temp",
]
__version__ = "0.1.3"
__version__ = "0.1.4"

from .extensions import Dat, Gzip, Json, Jsonl, Tar, Txt, Yaml
from .extensions import Dat, Env, Gzip, Json, Jsonl, Tar, Txt, Yaml
from .state import State, filter_states, find_state
from .storage import load_file, save_file, save_temp
2 changes: 2 additions & 0 deletions src/iokit/extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__all__ = [
"Dat",
"Env",
"Gzip",
"Json",
"Jsonl",
Expand All @@ -9,6 +10,7 @@
]

from .dat import Dat
from .env import Env
from .gz import Gzip
from .json import Json
from .jsonl import Jsonl
Expand Down
24 changes: 24 additions & 0 deletions src/iokit/extensions/env.py
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))
9 changes: 9 additions & 0 deletions tests/test_env.py
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

0 comments on commit c59e502

Please sign in to comment.