Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Remove empty source directory' and 'release name formatting' #285

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion mnamer/frontends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions mnamer/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions mnamer/setting_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions mnamer/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down
12 changes: 11 additions & 1 deletion mnamer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down