Skip to content

Commit

Permalink
Added optional feature 'remove empty source directory'
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas2k committed Nov 30, 2023
1 parent 82da071 commit 0ddff8d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
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
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
12 changes: 11 additions & 1 deletion mnamer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import datetime as dt
import json
import re
from os import walk
from os import walk, listdir
from os.path import exists, expanduser, expandvars, getsize, splitdrive, splitext
from shutil import rmtree
from pathlib import Path, PurePath
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

0 comments on commit 0ddff8d

Please sign in to comment.