sane-tag-music: refactor to_path
This commit is contained in:
@@ -100,6 +100,14 @@ def clean_for_fs(a: str, single_field: bool=False) -> str:
|
|||||||
a = a.replace("..", ".")
|
a = a.replace("..", ".")
|
||||||
return a
|
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
|
@dataclass
|
||||||
class Tags:
|
class Tags:
|
||||||
# format matches mutagen's
|
# format matches mutagen's
|
||||||
@@ -230,17 +238,17 @@ class Tags:
|
|||||||
if not (artist and self.album and self.title and ext):
|
if not (artist and self.album and self.title and ext):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
artist = clean_for_fs(artist[0], single_field=False)
|
artist = clean_fields_for_fs(artist, single_fields=False)
|
||||||
album = "Singles" if is_single else clean_for_fs(self.album[0], single_field=True)
|
album = "Singles" if is_single else clean_fields_for_fs(self.album)
|
||||||
trackno = self.tracknumber and clean_for_fs(self.tracknumber[0], single_field=True)
|
trackno = self.tracknumber and clean_fields_for_fs(self.tracknumber)
|
||||||
title = clean_for_fs(self.title[0])
|
trackno = [f"{trackno:>02}"] if trackno else []
|
||||||
title_with_ext = clean_for_fs(title + f".{ext}", single_field=True)
|
title = clean_fields_for_fs(self.title)
|
||||||
trackno_prefix = f"{trackno:>02}-" if trackno else ""
|
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 None
|
||||||
|
|
||||||
return f"{artist}/{album}/{trackno_prefix}{title_with_ext}"
|
return os.path.join(artist, album, filename)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_path(p: str) -> 'Tags':
|
def from_path(p: str) -> 'Tags':
|
||||||
|
Reference in New Issue
Block a user