tags: TagLine constructor now supports multiline taglines and multiple spaces
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""
|
||||
Module implementing tag related functions and classes.
|
||||
"""
|
||||
from typing import Type, TypeVar, Optional
|
||||
from typing import Type, TypeVar, Optional, Final
|
||||
|
||||
TagInst = TypeVar('TagInst', bound='Tag')
|
||||
TagLineInst = TypeVar('TagLineInst', bound='TagLine')
|
||||
@@ -16,9 +16,9 @@ class Tag(str):
|
||||
A single tag. A string that can contain anything but the default separator (' ').
|
||||
"""
|
||||
# default separator
|
||||
default_separator = ' '
|
||||
default_separator: Final[str] = ' '
|
||||
# alternative separators (e. g. for backwards compatibility)
|
||||
alternative_separators = [',']
|
||||
alternative_separators: Final[list[str]] = [',']
|
||||
|
||||
def __new__(cls: Type[TagInst], string: str) -> TagInst:
|
||||
"""
|
||||
@@ -98,14 +98,16 @@ class TagLine(str):
|
||||
the tags.
|
||||
"""
|
||||
# the prefix
|
||||
prefix = 'TAGS:'
|
||||
prefix: Final[str] = 'TAGS:'
|
||||
|
||||
def __new__(cls: Type[TagLineInst], string: str) -> TagLineInst:
|
||||
"""
|
||||
Make sure the tagline string starts with the prefix.
|
||||
Make sure the tagline string starts with the prefix. Also replace newlines
|
||||
and multiple spaces with ' ', in order to support multiline TagLines.
|
||||
"""
|
||||
if not string.startswith(cls.prefix):
|
||||
raise TagError(f"TagLine '{string}' is missing prefix '{cls.prefix}'")
|
||||
string = ' '.join(string.split())
|
||||
instance = super().__new__(cls, string)
|
||||
return instance
|
||||
|
||||
|
||||
Reference in New Issue
Block a user