sane-tag-music: refactor to_path

This commit is contained in:
2024-07-09 16:22:53 +00:00
parent 0b610a6683
commit 53fd4ee42f

View File

@@ -100,6 +100,14 @@ def clean_for_fs(a: str, single_field: bool=False) -> str:
a = a.replace("..", ".")
return a
def clean_fields_for_fs(a: list[str], single_fields: bool=True) -> str:
"""
like `clean_for_fs`, but each element of `a` represents its own field and the result is joined by hyphens.
e.g. `clean_fields_for_fs(["R.Q.", "butt-secks"])` -> `"R.Q.-butt.secks"`
"""
cleaned_fields = [clean_for_fs(i, single_field=single_fields) for i in a]
return "-".join(f for f in cleaned_fields if f)
@dataclass
class Tags:
# format matches mutagen's
@@ -230,17 +238,17 @@ class Tags:
if not (artist and self.album and self.title and ext):
return None
artist = clean_for_fs(artist[0], single_field=False)
album = "Singles" if is_single else clean_for_fs(self.album[0], single_field=True)
trackno = self.tracknumber and clean_for_fs(self.tracknumber[0], single_field=True)
title = clean_for_fs(self.title[0])
title_with_ext = clean_for_fs(title + f".{ext}", single_field=True)
trackno_prefix = f"{trackno:>02}-" if trackno else ""
artist = clean_fields_for_fs(artist, single_fields=False)
album = "Singles" if is_single else clean_fields_for_fs(self.album)
trackno = self.tracknumber and clean_fields_for_fs(self.tracknumber)
trackno = [f"{trackno:>02}"] if trackno else []
title = clean_fields_for_fs(self.title)
filename = clean_fields_for_fs(trackno + [ f"{title}.{ext}" ])
if not (artist and album and title_with_ext):
if not (artist and album and title):
return None
return f"{artist}/{album}/{trackno_prefix}{title_with_ext}"
return os.path.join(artist, album, filename)
@staticmethod
def from_path(p: str) -> 'Tags':