From cc267d526f864eea63b9b8474f9a17ce2284eddb Mon Sep 17 00:00:00 2001 From: Jacob Pavlock Date: Sun, 18 Dec 2022 21:56:23 -0800 Subject: [PATCH] refactor!: replace lib_path arg for fmt_item_path with optional parent This is more flexible as it allows specifying the direct parent for albums, extras and tracks instead of just albums. --- moe/move/move_core.py | 44 ++++++++++-------------------------- tests/move/test_move_core.py | 4 ++-- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/moe/move/move_core.py b/moe/move/move_core.py index 18883d44..83fb6b0e 100644 --- a/moe/move/move_core.py +++ b/moe/move/move_core.py @@ -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))) @@ -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. diff --git a/tests/move/test_move_core.py b/tests/move/test_move_core.py index 514b2c54..14d28b1c 100644 --- a/tests/move/test_move_core.py +++ b/tests/move/test_move_core.py @@ -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)