-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathutils.py
More file actions
73 lines (59 loc) · 2.4 KB
/
Copy pathutils.py
File metadata and controls
73 lines (59 loc) · 2.4 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
import os
import re
from typing import List
from datetime import datetime
import subprocess
import shutil
class Utils:
"""
A utility class providing common helper functions for various tasks.
"""
@staticmethod
def stop_vm():
try:
subprocess.run(['sudo', '/sbin/shutdown', 'now'], check=True)
print("Reboot command issued successfully.")
except subprocess.CalledProcessError as e:
print(f"Failed to reboot the VM: {e}")
except Exception as e:
print(f"An error occurred: {e}")
@staticmethod
def get_ordered_audio_files(folder_path: str) -> List[str]:
"""
Returns a list of .mp3 filenames from the folder, ordered by the number in the filename.
Example:
Input: ['peter_audio_1.mp3', 'stewie_audio_2.mp3', 'peter_3.mp3']
Output: ['peter_audio_1.mp3', 'stewie_audio_2.mp3', 'peter_3.mp3']
"""
try:
audio_files = [
f for f in os.listdir(folder_path)
if os.path.isfile(os.path.join(folder_path, f)) and f.endswith(".mp3")
]
def extract_number(filename: str) -> int:
match = re.search(r'(\d+)', filename)
return int(match.group(1)) if match else float('inf')
sorted_files = sorted(audio_files, key=extract_number)
return sorted_files
except Exception as e:
print(f"[Utils] Error reading audio files from '{folder_path}': {e}")
return []
@staticmethod
def archive_audio_assets():
"""
Moves all files from 'audio_assets/' to 'archive/YYYY-MM-DD/' and ensures the source folder is empty.
"""
source_dir = 'audio_assests'
archive_root = 'archives_audios'
today_str = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
target_dir = os.path.join(archive_root, today_str)
try:
os.makedirs(target_dir, exist_ok=True)
for filename in os.listdir(source_dir):
src_path = os.path.join(source_dir, filename)
if os.path.isfile(src_path):
shutil.move(src_path, os.path.join(target_dir, filename))
print(f"Archived files to {target_dir}.")
except Exception as e:
print(f"[Utils] Error archiving audio assets: {e}")
# Utils.archive_audio_assets()