-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrename_items.py
More file actions
54 lines (45 loc) · 1.67 KB
/
rename_items.py
File metadata and controls
54 lines (45 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import re
import sys
from concurrent.futures import ThreadPoolExecutor
from mtlogger import logger
from tqdm import tqdm
from _common import VOLUME_REGEX, CHAPTER_REGEX, exit_with_prompt, print_error
from _sound_utils import play_notification_sound
def main():
if len(sys.argv) > 1:
process_parent_folder(sys.argv[1])
else:
prompt_parent_folder()
def prompt_parent_folder():
parent_folder = input('Enter the path to the parent folder containing the chapter folders you want to rename:\n')
if not parent_folder:
return
if not os.path.isdir(parent_folder):
logger.error(f'The specified path "{parent_folder}" is not a directory.')
else:
process_parent_folder(parent_folder)
play_notification_sound()
exit_with_prompt()
def process_parent_folder(parent_folder):
for root, dirs, files in os.walk(parent_folder, topdown = False):
all_items = files + dirs
with ThreadPoolExecutor() as executor:
list(tqdm(executor.map(lambda item: process_item(root, item), all_items), total = len(all_items), desc=f'Processing "{root}"'))
def process_item(root, item_name):
new_name = get_processed_name(item_name)
if item_name != new_name:
old_path = os.path.join(root, item_name)
new_path = os.path.join(root, new_name)
os.rename(old_path, new_path)
def get_processed_name(name):
new_name = re.sub(r'\s+', ' ', name)
new_name = re.sub(rf'\b({CHAPTER_REGEX})\b\.?\s*', 'Ch.', new_name, flags = re.IGNORECASE)
new_name = re.sub(rf'\b({VOLUME_REGEX})\b\.?\s*', 'Vol.', new_name, flags = re.IGNORECASE)
return new_name
if __name__ == '__main__':
try:
main()
except Exception as ex:
print_error(ex)
exit_with_prompt()