sane-tag-media: refactor (simplify)

This commit is contained in:
2024-08-03 03:57:09 +00:00
parent 365d33c357
commit 604782c3a6

View File

@@ -288,16 +288,37 @@ class Tags:
def __repr__(self) -> str:
return f"artist:{self.producer}/{self.albumartist}/{self.artist}, album:{self.album}, title:{self.title}, trackno:{self.tracknumber}"
def merge(self, other: 'Tags', merge_field) -> 'Tags':
def clone(self) -> 'Tags':
return Tags(
album=merge_field(self.album, other.album),
albumartist=merge_field(self.albumartist, other.albumartist),
artist=merge_field(self.artist, other.artist),
producer=merge_field(self.producer, other.producer),
title=merge_field(self.title, other.title),
tracknumber=merge_field(self.tracknumber, other.tracknumber),
album=self.album,
albumartist=self.albumartist,
artist=self.artist,
producer=self.producer,
title=self.title,
tracknumber=self.tracknumber,
)
def merge(self, other: 'Tags', merge_field) -> 'Tags':
new_tags = self.clone()
new_tags.merge_in_place(other, merge_field)
return new_tags
def map(self, f) -> 'Tags':
new_tags = self.clone()
new_tags.update(f)
return new_tags
def merge_in_place(self, other: 'Tags', merge_field) -> None:
self.album = merge_field(self.album, other.album)
self.albumartist = merge_field(self.albumartist, other.albumartist)
self.artist = merge_field(self.artist, other.artist)
self.producer = merge_field(self.producer, other.producer)
self.title = merge_field(self.title, other.title)
self.tracknumber = merge_field(self.tracknumber, other.tracknumber)
def update(self, f) -> None:
self.merge_in_place(self, lambda v, _: f(v))
def or_(self, other: 'Tags') -> 'Tags':
"""
substitute any tags missing tags in `self` with those from `fallback`.
@@ -315,7 +336,7 @@ class Tags:
return self.merge(other, merge_field)
def unambiguous(self) -> 'Tags':
return self.merge(self, lambda f, _: f if len(f) == 1 else [])
return self.map(lambda f: f if len(f) == 1 else [])
# def intersection(self, other: 'Tags') -> 'Tags':
# def merge_field(primary: list[str], secondary: list[str]):
@@ -331,13 +352,10 @@ class Tags:
stripped = [ i.strip().replace('\u200b', '') for i in field ]
unique = []
for i in stripped:
if i not in unique:
if i and i not in unique:
unique.append(i)
return unique
self.title = trim(self.title)
self.artist = trim(self.artist)
self.albumartist = trim(self.albumartist)
self.album = trim(self.album)
self.update(lambda f: trim(f))
def cleanup_trackno(self) -> None:
self.tracknumber = self.tracknumber[:] # to avoid modifying shared structures
@@ -832,19 +850,6 @@ class Tagger:
# old_tags overrule path_tags
new_tags = manual_tags.or_(old_tags).or_(path_tags)
# special case that explicitly supplying empty tags should delete the existing
if manual_tags.album == [""]:
new_tags.album = []
if manual_tags.albumartist == [""]:
new_tags.albumartist = []
if manual_tags.artist == [""]:
new_tags.artist = []
if manual_tags.producer == [""]:
new_tags.producer = []
if manual_tags.title == [""]:
new_tags.title = []
if manual_tags.tracknumber == [""]:
new_tags.tracknumber = []
new_tags.trim_fields()
new_tags.cleanup_trackno()
new_tags.expand_shorthands()