-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_common.py
More file actions
76 lines (61 loc) · 2.3 KB
/
_common.py
File metadata and controls
76 lines (61 loc) · 2.3 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import re
from concurrent.futures import ThreadPoolExecutor
from mtlogger import logger
from tqdm import tqdm
from _image_utils import is_image_file
from _sound_utils import play_notification_sound
NUMBER_REGEX = r'\.?\s*(\d+(?:\.\d+)?)'
VOLUME_REGEX = r'Vol(?:ume)?'
CHAPTER_REGEX = r'Ch(?:ap(?:ter)?)?|Ep(?:isode)?'
VOLUME_NUMBER_REGEX = rf'(?:{VOLUME_REGEX}){NUMBER_REGEX}'
CHAPTER_NUMBER_REGEX = rf'(?:{CHAPTER_REGEX}){NUMBER_REGEX}'
def select_parent_folder(prompt, callback, options = {}):
log_success = options.get('log_success', False)
loop = options.get('loop', True)
parent_folder = input(prompt).strip(' "\'')
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:
callback(parent_folder)
play_notification_sound()
if log_success:
logger.log(f'Finished processing "{parent_folder}".\n')
if loop:
select_parent_folder(prompt, callback)
else:
input('')
def process_folder_images(folder_path, callback):
files_to_process = []
for root, _, files in os.walk(folder_path):
for file in files:
if is_image_file(file):
file_path = os.path.join(root, file)
files_to_process.append(file_path)
with ThreadPoolExecutor() as executor, tqdm(total = len(files_to_process), desc = f'Processing "{folder_path}"') as progress:
for _ in executor.map(callback, files_to_process):
progress.update(1)
def delete_empty_folders(folder_path):
for root, dirs, _ in os.walk(folder_path, topdown = False):
for dir_name in dirs:
dir_path = os.path.join(root, dir_name)
try:
os.rmdir(dir_path)
except OSError:
pass
def get_volume_and_chapter(filename):
vol_match = re.search(VOLUME_NUMBER_REGEX, filename, re.IGNORECASE)
ch_match = re.search(CHAPTER_NUMBER_REGEX, filename, re.IGNORECASE)
volume = vol_match.group(1) if vol_match else None
chapter = ch_match.group(1) if ch_match else None
return (volume, chapter)
def get_chapter(filename):
ch_match = re.search(CHAPTER_NUMBER_REGEX, filename, re.IGNORECASE)
chapter = ch_match.group(1) if ch_match else None
return chapter
def print_error(error):
logger.error(f'An unexpected error occurred: {error}')
def exit_with_prompt():
input('Press Enter to exit...')