Skip to content

Commit

Permalink
add yoyo-migrations library & config reader
Browse files Browse the repository at this point in the history
  • Loading branch information
azuline committed Oct 9, 2023
1 parent a4d0123 commit d6d4498
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[run]
omit =
rose/__main__.py
setup.py
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ __pycache__
.mypy_cache
.coverage
result
db.sqlite3
htmlcov
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# rose

Rose is a Linux music library manager.

## Configuration

Rose reads its configuration from `${XDG_CONFIG_HOME:-$HOME/.config}/rose/config.toml`.

The configuration parameters, with examples, are:

```toml
# The directory containing the music to manage.
music_source_dir = "~/.music-src"
# The directory to mount the library's virtual filesystem on.
fuse_mount_dir = "~/music"
```
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
click
fuse
mutagen
yoyo-migrations
];
dev-deps = with python.pkgs; [
black
Expand Down
2 changes: 2 additions & 0 deletions rose/base_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class RoseError(Exception):
pass
43 changes: 43 additions & 0 deletions rose/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import annotations

import os
from dataclasses import dataclass
from pathlib import Path

import tomllib

from rose.base_error import RoseError

CONFIG_HOME = Path(os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"))
CONFIG_PATH = CONFIG_HOME / "rose" / "config.toml"


class ConfigNotFoundError(RoseError):
pass


class MissingConfigKeyError(RoseError):
pass


@dataclass
class Config:
music_source_dir: Path
fuse_mount_dir: Path

@classmethod
def read(cls, config_path_override: Path | None = None) -> Config:
cfgpath = config_path_override or CONFIG_PATH
try:
with cfgpath.open("rb") as fp:
data = tomllib.load(fp)
except FileNotFoundError as e:
raise ConfigNotFoundError(f"Configuration file not found ({CONFIG_PATH})") from e

try:
return cls(
music_source_dir=Path(data["music_source_dir"]).expanduser(),
fuse_mount_dir=Path(data["fuse_mount_dir"]).expanduser(),
)
except KeyError as e:
raise MissingConfigKeyError(f"Missing key in configuration file: {e}") from e
39 changes: 39 additions & 0 deletions rose/config_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import tempfile
from pathlib import Path

import pytest

from rose.config import Config, ConfigNotFoundError, MissingConfigKeyError


def test_config() -> None:
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir) / "config.toml"
with path.open("w") as fp:
fp.write(
"""\
music_source_dir = "~/.music-src"
fuse_mount_dir = "~/music"
"""
)

c = Config.read(config_path_override=path)
assert str(c.music_source_dir) == os.environ["HOME"] + "/.music-src"
assert str(c.fuse_mount_dir) == os.environ["HOME"] + "/music"


def test_config_not_found() -> None:
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir) / "config.toml"
with pytest.raises(ConfigNotFoundError):
Config.read(config_path_override=path)


def test_config_missing_key() -> None:
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir) / "config.toml"
path.touch()
with pytest.raises(MissingConfigKeyError) as excinfo:
Config.read(config_path_override=path)
assert str(excinfo.value) == "Missing key in configuration file: 'music_source_dir'"
2 changes: 0 additions & 2 deletions rose/hello_test.py

This file was deleted.

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"click",
"fuse-python",
"mutagen",
"yoyo-migrations",
],
)
8 changes: 8 additions & 0 deletions yoyo.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This is for development use only.

[DEFAULT]
sources = migrations
migration_table = _yoyo_migration
batch_mode = off
verbosity = 0
database = sqlite:///db.sqlite3

0 comments on commit d6d4498

Please sign in to comment.