Skip to content

Commit

Permalink
refactor!: replace lib_path arg for fmt_item_path with optional parent
Browse files Browse the repository at this point in the history
This is more flexible as it allows specifying the direct parent for albums, extras and tracks instead of just albums.
  • Loading branch information
jtpavlock committed Dec 20, 2022
1 parent ae0889d commit cc267d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 34 deletions.
44 changes: 12 additions & 32 deletions moe/move/move_core.py
Expand Up @@ -90,29 +90,32 @@ def e_unique(extra: Extra) -> str:
########################################################################################
# Format paths
########################################################################################
def fmt_item_path(item: LibItem, lib_path: Optional[Path] = None) -> Path:
def fmt_item_path(item: LibItem, parent: Optional[Path] = None) -> Path:
"""Returns a formatted item path according to the user configuration.
Args:
item: Item whose path will be formatted.
lib_path: Optional library path the outputted path will be relative to. By
default, this will be the ``library_path`` setting in the config.
parent: Optional path the formatted path will be relative to. By
default, this will be according to the configuration path settings.
Returns:
A formatted path as defined by the ``{album/extra/track}_path`` config template
settings relative to ``lib_path``.
settings relative to ``parent``.
"""
log.debug(f"Formatting item path. [path={item.path}]")

lib_path = lib_path or Path(config.CONFIG.settings.library_path).expanduser()

if isinstance(item, Album):
new_path = _fmt_album_path(item, lib_path)
parent = parent or Path(config.CONFIG.settings.library_path).expanduser()
item_path = _eval_path_template(config.CONFIG.settings.move.album_path, item)
elif isinstance(item, Extra):
new_path = _fmt_extra_path(item, lib_path)
parent = parent or fmt_item_path(item.album)
item_path = _eval_path_template(config.CONFIG.settings.move.extra_path, item)
else:
assert isinstance(item, Track)
new_path = _fmt_track_path(item, lib_path)
parent = parent or fmt_item_path(item.album)
item_path = _eval_path_template(config.CONFIG.settings.move.track_path, item)

new_path = parent / item_path

if config.CONFIG.settings.move.asciify_paths:
new_path = Path(unidecode(str(new_path)))
Expand All @@ -121,29 +124,6 @@ def fmt_item_path(item: LibItem, lib_path: Optional[Path] = None) -> Path:
return new_path


def _fmt_album_path(album: Album, lib_path: Path) -> Path:
"""Returns a formatted album directory according to the user configuration."""
album_path = _eval_path_template(config.CONFIG.settings.move.album_path, album)

return lib_path / album_path


def _fmt_extra_path(extra: Extra, lib_path: Path) -> Path:
"""Returns a formatted extra path according to the user configuration."""
album_path = _fmt_album_path(extra.album, lib_path)
extra_path = _eval_path_template(config.CONFIG.settings.move.extra_path, extra)

return album_path / extra_path


def _fmt_track_path(track: Track, lib_path: Path) -> Path:
"""Returns a formatted track path according to the user configuration."""
album_path = _fmt_album_path(track.album, lib_path)
track_path = _eval_path_template(config.CONFIG.settings.move.track_path, track)

return album_path / track_path


def _eval_path_template(template, lib_item) -> str:
"""Evaluates and sanitizes a path template.
Expand Down
4 changes: 2 additions & 2 deletions tests/move/test_move_core.py
Expand Up @@ -153,8 +153,8 @@ def test_asciify_paths(self, tmp_config):
assert str(moe_move.fmt_item_path(album)).isascii()

@pytest.mark.usefixtures("_tmp_move_config")
def test_given_lib_path(self, tmp_path):
"""If provided, paths should be relative to ``lib_path``."""
def test_given_parent(self, tmp_path):
"""If provided, paths should be relative to ``parent``."""
track = track_factory()
track_path = moe_move.fmt_item_path(track, tmp_path)

Expand Down

0 comments on commit cc267d5

Please sign in to comment.