Files
2026-03-03 14:52:19 +09:00

73 lines
1.9 KiB
Python
Executable File

"""
Project folder naming and structure creation.
Derives a human-readable project name from audio metadata and creates the
subdirectory layout under MEDIA_DIR.
"""
import re
from pathlib import Path
from typing import Dict
_ILLEGAL = re.compile(r'[<>:"/\\|?*\x00-\x1f]')
def _sanitize(s: str) -> str:
"""Remove filesystem-illegal characters and trim trailing dots/spaces."""
s = _ILLEGAL.sub('', s).strip().rstrip('. ')
return s or ''
def project_name(artist: str, title: str, filename_stem: str = '') -> str:
"""
Return a human-readable project folder name.
Format: "Artist - Title". Falls back to filename_stem if both are missing.
"""
a = _sanitize(artist)
t = _sanitize(title)
if a and t:
return f"{a} - {t}"
if t:
return t
if a:
return a
fs = _sanitize(filename_stem)
return fs if fs else 'Unknown Project'
def make_project_dirs(
name: str,
media_dir: Path,
presets: Dict[str, bool],
) -> Dict[str, Path]:
"""
Create project subdirectories under media_dir/name/.
Always creates Stems/. Creates Instrumental/, Static/, Dynamic/ only when
the corresponding preset flag is True.
Returns a dict mapping role names ('project', 'stems', 'instrumental',
'static', 'dynamic') to their Path objects.
"""
project_dir = media_dir / name
stems_dir = project_dir / 'Stems'
stems_dir.mkdir(parents=True, exist_ok=True)
dirs: Dict[str, Path] = {'project': project_dir, 'stems': stems_dir}
if presets.get('instrumental'):
d = project_dir / 'Instrumental'
d.mkdir(exist_ok=True)
dirs['instrumental'] = d
if presets.get('static'):
d = project_dir / 'Static'
d.mkdir(exist_ok=True)
dirs['static'] = d
if presets.get('dynamic'):
d = project_dir / 'Dynamic'
d.mkdir(exist_ok=True)
dirs['dynamic'] = d
return dirs