from .types import File, Tags from .scan_for_music import scan_for_music from .load_tag_information import load_tag_information from .substitutions import reduce_to_ascii_and_substitute from copy import deepcopy as deep_copy from os import rename as rename_file class ProcessCommandState: files: list[File] = [] class ProcessCommand(): def __init__(self, src: str, dry_run: bool): self.src = src self.dry_run = dry_run self.state = ProcessCommandState() def run(self): print("Renaming") self.scan_for_music() self.load_tag_information() self.rename_files() def scan_for_music(self): print("Scanning For Music") self.state.files = scan_for_music(self.src) def load_tag_information(self): print("Loading Tag Information") for file in self.state.files: tags = load_tag_information(file) file.tags = tags def _rename_file(self, file: File) -> File: filename = file.filename artist = file.tags.artist.replace("\n", "") title = file.tags.title.replace("\n", "") proper_filename = reduce_to_ascii_and_substitute(f"{artist} - {title}") if filename != proper_filename: print(f"Renaming \"{filename}\"", "to" + f"\"{proper_filename}\"" + "\n") new_file = deep_copy(file) new_file.filename = proper_filename if not self.dry_run: rename_file(file.join_path_to(), new_file.join_path_to()) # so that other steps after read the new file and not # the orig pre-rename file when not dry run return new_file else: return file def rename_files(self): print("Renaming files") for file in self.state.files: index = self.state.files.index(file) self.state.files[index] = self._rename_file(file)