diff --git a/mnamer/frontends.py b/mnamer/frontends.py index 0f47a6ac..f8386e7a 100644 --- a/mnamer/frontends.py +++ b/mnamer/frontends.py @@ -12,7 +12,7 @@ from mnamer.setting_store import SettingStore from mnamer.target import Target from mnamer.types import MessageType -from mnamer.utils import clear_cache, get_filesize, is_subtitle +from mnamer.utils import clear_cache, get_filesize, is_subtitle, remove_empty_directory class Frontend(ABC): @@ -149,6 +149,7 @@ def _process_targets(self) -> None: continue self._rename_and_move_file(target) + self._remove_empty_source_directory(target) def _announce_file(self, target: Target): media_type = target.metadata.to_media_type().value.title() @@ -187,6 +188,31 @@ def _rename_and_move_file(self, target: Target): tty.msg("OK!", MessageType.SUCCESS) self.success_count += 1 + def _remove_empty_source_directory(self, target: Target): + if self.settings.remove_empty_source_directory: + try: + directory_removed = remove_empty_directory( + target.source.parent, self.settings.test + ) + if directory_removed: + tty.msg( + "removed empty source directory " + + str(target.source.parent.absolute()), + MessageType.SUCCESS, + ) + else: + tty.msg( + "skipped removal of non empty source directory " + + str(target.source.parent.absolute()), + MessageType.SUCCESS, + ) + except: + tty.msg( + "failed to remove empty source directory " + + str(target.source.parent.absolute()), + MessageType.ERROR, + ) + def _report_results(self) -> None: if self.success_count == 0: message_type = MessageType.ERROR diff --git a/mnamer/metadata.py b/mnamer/metadata.py index df43382a..9117ea3b 100644 --- a/mnamer/metadata.py +++ b/mnamer/metadata.py @@ -112,6 +112,7 @@ class MetadataMovie(Metadata): name: str | None = None year: str | None = None + release_name: str | None = None id_imdb: str | None = None id_tmdb: str | None = None diff --git a/mnamer/setting_store.py b/mnamer/setting_store.py index 8981d335..d5ab2242 100644 --- a/mnamer/setting_store.py +++ b/mnamer/setting_store.py @@ -217,6 +217,19 @@ class SettingStore: help="--episode-format: set episode renaming format specification", ).as_dict(), ) + remove_empty_source_directory: bool = dataclasses.field( + default=False, + metadata=SettingSpec( + action="store_true", + dest="remove_empty_source_directory", + flags=[ + "--remove_empty_source_directory", + "--remove-empty-source-directory", + ], + group=SettingType.PARAMETER, + help="--remove-empty-source-directory: remove source directory if it's empty after file processing", + )(), + ) # directive attributes ----------------------------------------------------- diff --git a/mnamer/target.py b/mnamer/target.py index cd9927e2..fc876831 100644 --- a/mnamer/target.py +++ b/mnamer/target.py @@ -122,6 +122,7 @@ def _parse(self, file_path: Path): file_path = Path(self.source.parent, self.source.stem[:-2]) except MnamerException: pass + path_data["release_name"] = file_path.parent.name options = {"type": self._settings.media, "language": path_data["language"]} raw_data = dict(guessit(str(file_path), options)) if isinstance(raw_data.get("season"), list): @@ -179,6 +180,7 @@ def _parse(self, file_path: Path): if isinstance(self.metadata, MetadataMovie): self.metadata.name = path_data.get("title") self.metadata.year = path_data.get("year") + self.metadata.release_name = path_data.get("release_name") elif isinstance(self.metadata, MetadataEpisode): self.metadata.date = path_data.get("date") self.metadata.episode = path_data.get("episode") diff --git a/mnamer/utils.py b/mnamer/utils.py index 84df7870..b08cae7c 100644 --- a/mnamer/utils.py +++ b/mnamer/utils.py @@ -3,9 +3,10 @@ import datetime as dt import json import re -from os import walk +from os import listdir, walk from os.path import exists, expanduser, expandvars, getsize, splitdrive, splitext from pathlib import Path, PurePath +from shutil import rmtree from typing import Any, Callable, Iterator from unicodedata import normalize @@ -503,3 +504,12 @@ def year_range_parse(years: str | int | None, tolerance: int = 1) -> tuple[int, if not dash: end = start return start - tolerance, end + tolerance + + +def remove_empty_directory(path: Path, test: bool) -> bool: + if not listdir(path): + if not test: + rmtree(path) + return True + else: + return False diff --git a/tests/__init__.py b/tests/__init__.py index f340d06d..e2b4b6f3 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -29,6 +29,7 @@ "no_overwrite": False, "no_style": False, "recurse": False, + "remove_empty_source_directory": False, "replace_after": {"&": "and", ";": ",", "@": "at"}, "replace_before": {}, "scene": False,