Skip to content

Commit

Permalink
Merge pull request #27 from EnhancedJax/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
EnhancedJax authored Jan 8, 2025
2 parents c4db403 + 9414a81 commit c0a5798
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 57 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.2.2

- Fix: remove obscure dependencies

## 0.2.1

- Lock dependencies, rookie mistake
Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "Bagels"
version = "0.2.1"
version = "0.2.2"
authors = [
{ name = "Jax", email = "enhancedjax@gmail.com" }
]
Expand Down Expand Up @@ -33,8 +33,6 @@ dependencies = [
"platformdirs==4.3.6",
"propcache==0.2.0",
"pydantic-core==2.23.4",
"pydantic-settings-yaml==0.2.0",
"pydantic-settings==2.6.1",
"pydantic==2.9.2",
"pygments==2.18.0",
"pyyaml==6.0.2",
Expand Down
32 changes: 19 additions & 13 deletions src/bagels/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
from typing import Any, Literal
import yaml
from pydantic import BaseModel, Field
from pydantic_settings import SettingsConfigDict
from pydantic_settings_yaml import YamlBaseSettings
from bagels.locations import config_file
from pathlib import Path


class Defaults(BaseModel):
Expand Down Expand Up @@ -68,29 +67,38 @@ class State(BaseModel):
theme: str = "dark"


class Config(YamlBaseSettings):
class Config(BaseModel):
hotkeys: Hotkeys = Hotkeys()
symbols: Symbols = Symbols()
defaults: Defaults = Defaults()
state: State = State()
model_config = SettingsConfigDict(
yaml_file=str(config_file()),
yaml_file_encoding="utf-8",
)

def __init__(self, **data):
super().__init__(**data)
config_data = self._load_yaml_config()
merged_data = {**self.model_dump(), **config_data, **data}
super().__init__(**merged_data)
self.ensure_yaml_fields()

def _load_yaml_config(self) -> dict[str, Any]:
config_path = config_file()
if not config_path.is_file():
return {}

try:
with open(config_path, "r") as f:
config = yaml.safe_load(f)
return config if isinstance(config, dict) else {}
except Exception as e:
warnings.warn(f"Error loading config file: {e}")
return {}

def ensure_yaml_fields(self):
# Load current config or create a new one if it doesn't exist
try:
with open(config_file(), "r") as f:
config = yaml.safe_load(f) or {}
except FileNotFoundError:
config = {}

# Update config with default values for missing fields
def update_config(default, current):
for key, value in default.items():
if isinstance(value, dict):
Expand All @@ -102,14 +110,12 @@ def update_config(default, current):
default_config = self.model_dump()
config = update_config(default_config, config)

# Write back to the YAML file
with open(config_file(), "w") as f:
yaml.dump(config, f, default_flow_style=False)

@classmethod
def get_default(cls):
# Create a default instance without reading from file
return cls.model_construct(
return cls(
hotkeys=Hotkeys(), symbols=Symbols(), defaults=Defaults(), state=State()
)

Expand Down
43 changes: 2 additions & 41 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c0a5798

Please sign in to comment.