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 f019f56
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
32 changes: 32 additions & 0 deletions mnamer/frontends.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
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 +155,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 +194,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
21 changes: 19 additions & 2 deletions mnamer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
import datetime as dt
import json
import re
from os import walk
from os.path import exists, expanduser, expandvars, getsize, splitdrive, splitext
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 +511,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 f019f56

Please sign in to comment.