Skip to content

Commit

Permalink
fix config in the future but it kinda does the thing it is supposed t…
Browse files Browse the repository at this point in the history
…o right now
  • Loading branch information
djcopley committed Dec 22, 2023
1 parent 676c694 commit 18a81d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions shelloracle/config/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from collections.abc import MutableMapping
from pathlib import Path
from typing import Any

import tomlkit

data_home = Path.home() / "Library/Application Support" / "shelloracle"
data_home = Path.home() / ".shelloracle"


def _default_config() -> tomlkit.TOMLDocument:
Expand All @@ -20,27 +21,35 @@ class Configuration(MutableMapping):
def __init__(self) -> None:
self._ensure_config_exists()

def __getitem__(self, item):
def __getitem__(self, item: str) -> dict:
with self.filepath.open("r") as file:
config = tomlkit.load(file)
return config[item]

def __setitem__(self, key, value):
def __setitem__(self, key: str, value: Any) -> None:
with self.filepath.open("r") as file:
config = tomlkit.load(file)
config[key] = value
config.multiline = True
with self.filepath.open("w") as file:
tomlkit.dump(config, file)

def __delitem__(self, key):
raise NotImplementedError()
def __delitem__(self, key: str) -> None:
with self.filepath.open("r") as file:
config = tomlkit.load(file)
del config[key]
with self.filepath.open("w") as file:
tomlkit.dump(config, file)

def __iter__(self):
raise NotImplementedError()
def __iter__(self) -> None:
with self.filepath.open("r") as file:
config = tomlkit.load(file)
return iter(config)

def __len__(self) -> int:
raise NotImplementedError()
with self.filepath.open("r") as file:
config = tomlkit.load(file)
return len(config)

def _ensure_config_exists(self) -> None:
if self.filepath.exists():
Expand Down
2 changes: 1 addition & 1 deletion shelloracle/config/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, *, default: T | None = None) -> None:

def __set_name__(self, owner: type[Provider], name: str) -> None:
self.name = name
# Set the default value in the config dictionary
# Set the default value in the config dictionary if it doesn't exist
provider_table = config.global_config.get("provider", {})
provider_table.setdefault(owner.name, {}).setdefault(name, self.default)
config.global_config["provider"] = provider_table
Expand Down

0 comments on commit 18a81d7

Please sign in to comment.