def test_extra_text(): fs = FormattedString( "{surrounding stuff {artist} here too }always present {{album}}") tagged_whole = Taggable(album="Album", artist="Artist") assert (fs.write(tagged_whole) == "surrounding stuff Artist here too always present Album") tagged_only_album = Taggable(album="Album") assert fs.write(tagged_only_album) == "always present Album" tagged_empty = Taggable() assert fs.write(tagged_empty) == "always present "
def test_empty_string(): fs = FormattedString("{{artist}}") tagged = Taggable() with pytest.raises(EmptyStringException): fs.write(tagged) fs = FormattedString("{*{artist}}") assert fs.write(tagged) == "Unknown artist"
def get_value(self, taggable: Taggable) -> str: value = getattr(taggable, self.tag_name, None) if value is None: return "" else: if self.tag_name in Taggable.get_numeric_tag_names(): if self.tag_name == "tracknumber": match = re.fullmatch(r"(\d+)/\d+", value) if match is not None: value = match.group(1) value = int(value) if self.format_spec is not None: value = f"{{{self.format_spec}}}".format(value) return str(value)
def __init__(self, raw_string: str): pattern = r"(\*?)(.*)\{(\w*)(\:.+)?\}(.*)" match = re.fullmatch(pattern, raw_string) if match is None: raise Exception() self.is_required = match.group(1) == "*" self.text_before = match.group(2) self.tag_name = match.group(3) self.format_spec = match.group(4) self.text_after = match.group(5) if self.tag_name not in Taggable.get_tag_names(): raise ValueError( f"unknown tag name [yellow]{self.tag_name}[/yellow].")
def get_error_message(self) -> Message: message: list[String] = [] message.append( Text.assemble( "formatted string ", (self.raw_formatted_string, "config"), " resulted in empty string from using the following tags:", )) for tag_name in Taggable.get_tag_names(): message.append( Text.assemble((tag_name, "tag"), f" {getattr(self.taggable, tag_name)}")) message.append( Text.assemble( "Try using the 'required' marker (", ("{*{tag}}", "config"), ") to prevent this.", )) return message
def test_rich_tracknumber(): fs = FormattedString("{{tracknumber:02d}}") tagged = Taggable(tracknumber="03/12") assert fs.write(tagged) == "03"
def test_number_formatting(): fs = FormattedString("{{tracknumber:03d}}") tagged = Taggable(tracknumber="34") assert fs.write(tagged) == "034"
def __init__(self, tag: str, formatted_string: FormattedString): if tag not in Taggable.get_tag_names(): raise UnknownTagException(tag) self.tag = tag self.formatted_string = formatted_string