def add_values(song: Song, tag: SongTag, new_values: Union[List[str], List[List[str]], None]): """Append values sent to the tag in the song. Parameters ---------- song : Song - Song where the data should change\n tag : SongTag - The tag whose values should change\n new_values : Union[List[str], List[List[str]], None] - The values to be added """ if new_values is None: add_tag(song, tag) return values = tag.get_tag(song.id3) if values is None: set_tag(song, tag, new_values) return values_copy = values.copy() for value in new_values: if value not in values: values_copy.append(value) # type: ignore tag.set_tag(song.id3, values_copy) song.save()
def split_values(song: Song, tag: SongTag): """Split the values in a tag on the given string in settings. Parameters ---------- song : Song - Song to be edited\n tag : SongTag - Tag where data should be split """ values_to_set: Union[List, List[str], List[List[str]]] = [] values = tag.get_tag(song.id3) if values is None: return for value in values: if tag.value_length == 1: values_to_set = cast(List[str], values_to_set) value = cast(str, value) values_split = _split_value(value) values_to_set.extend(values_split) else: values_to_set = cast(List[List[str]], values_to_set) value = cast(List[str], value) role, person = value split_roles = _split_value(role) split_people = _split_value(person) for split_role in split_roles: for split_person in split_people: values_to_set.append([split_role, split_person]) tag.set_tag(song.id3, values_to_set) song.save()
def remove_values(song: Song, tag: SongTag, values_to_remove: Union[List[str], List[List[str]], None]): """Remove the given values from the tag for this song. Parameters ---------- song : Song - Song to be edited\n tag : SongTag - Tag the values awill be removed from\n remove_values : Union[List[str], List[List[str]], None] - Values to be removed """ if values_to_remove is None: return values = tag.get_tag(song.id3) if values is None: return values = values.copy() for value in values_to_remove: try: values.remove(value) # type: ignore except ValueError: continue if len(values) > 0: tag.set_tag(song.id3, values) else: tag.remove_tag(song.id3) song.save()
def remove_tag(song: Song, tag: SongTag): """Remove all values a tag and then remove the tag from the song. Parameters ---------- song : Song - Song that the tag should be removed from\n tag : SongTag - Tag that should be removed. """ if tag.has_tag(song.id3): tag.remove_tag(song.id3) song.save()
def add_tag(song: Song, tag: SongTag): """Add a non-existent tag to the tags in a song. Parameters ---------- song : Song - Song that the tag should be added to\n tag : SongTag - Tag that should be added. """ if not tag.has_tag(song.id3): tag.set_tag(song.id3, []) song.save()
def set_tag(song: Song, tag: SongTag, new_values: Union[List[str], List[List[str]], None]): """Sets the values of the given tag to the values sent. Parameters ---------- song : Song - Song where the tag should be set\n tag : SongTag - Tag that should be set\n new_values : Union[List[str], List[List[str]], None] - Values that should be set. """ if new_values is None: add_tag(song, tag) return tag.set_tag(song.id3, new_values) song.save()
def replace_values(song: Song, tag: SongTag, new_values: Union[List[List[str]], None]): """Replace the values in the given tag if they match the strings in new values. Parameters ---------- song : Song - Song to be edited\n tag : SongTag - Tag being edited\n new_values : Union[List[List[str]], None] - Values to change """ if not new_values: return new_values = cast(List[List[str]], new_values) values = tag.get_tag(song.id3) if values is None: return values_copy = np.array(values) if tag.value_length == 1: values = cast(List[str], values) for index, item in enumerate(values): _replace_value(values_copy=values_copy, new_values=new_values, tag_length=tag.value_length, item_index=index, item=item) elif tag.value_length == 2: values = cast(List[List[str]], values) for index_of_pair, pair in enumerate(values): for index_in_pair, pair_item in enumerate(pair): _replace_value(values_copy=values_copy, new_values=new_values, tag_length=tag.value_length, index_of_pair=index_of_pair, pair=pair, item_index=index_in_pair, item=pair_item) values_copy = values_copy.tolist() tag.set_tag(song.id3, values_copy) song.save()
def remove_duplicates(song: Song, tag: SongTag): """Remove duplicate values from a specific tag. Parameters ---------- song : Song - Song to be edited\n tag : SongTag - Tag to be edited """ values = tag.get_tag(song.id3) if values is None: return values = tuple( tuple(value) # type: ignore if isinstance(value, list) else value for value in values) new_list = [ list(value) if isinstance(value, tuple) else value for value in list(OrderedDict.fromkeys((values))) ] tag.set_tag(song.id3, new_list) song.save()