-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add yoyo-migrations library & config reader
- Loading branch information
Showing
10 changed files
with
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[run] | ||
omit = | ||
rose/__main__.py | ||
setup.py |
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 |
---|---|---|
|
@@ -4,3 +4,5 @@ __pycache__ | |
.mypy_cache | ||
.coverage | ||
result | ||
db.sqlite3 | ||
htmlcov |
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 |
---|---|---|
@@ -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" | ||
``` |
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
click | ||
fuse | ||
mutagen | ||
yoyo-migrations | ||
]; | ||
dev-deps = with python.pkgs; [ | ||
black | ||
|
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,2 @@ | ||
class RoseError(Exception): | ||
pass |
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,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 |
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,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'" |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -13,5 +13,6 @@ | |
"click", | ||
"fuse-python", | ||
"mutagen", | ||
"yoyo-migrations", | ||
], | ||
) |
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,8 @@ | ||
# This is for development use only. | ||
|
||
[DEFAULT] | ||
sources = migrations | ||
migration_table = _yoyo_migration | ||
batch_mode = off | ||
verbosity = 0 | ||
database = sqlite:///db.sqlite3 |