Added prefix filtering to TagLine.tags() and Message.tags_from_file()
This commit is contained in:
parent
1a14c0e4a8
commit
1325789f18
@ -219,9 +219,12 @@ class Message():
|
|||||||
file_path=data.get(cls.file_yaml_key, None))
|
file_path=data.get(cls.file_yaml_key, None))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def tags_from_file(cls: Type[MessageInst], file_path: pathlib.Path) -> set[Tag]:
|
def tags_from_file(cls: Type[MessageInst],
|
||||||
|
file_path: pathlib.Path,
|
||||||
|
prefix: Optional[str] = None) -> set[Tag]:
|
||||||
"""
|
"""
|
||||||
Return only the tags from the given Message file.
|
Return only the tags from the given Message file,
|
||||||
|
optionally filtered based on prefix.
|
||||||
"""
|
"""
|
||||||
if not file_path.exists():
|
if not file_path.exists():
|
||||||
raise MessageError(f"Message file '{file_path}' does not exist")
|
raise MessageError(f"Message file '{file_path}' does not exist")
|
||||||
@ -229,11 +232,14 @@ class Message():
|
|||||||
raise MessageError(f"File type '{file_path.suffix}' is not supported")
|
raise MessageError(f"File type '{file_path.suffix}' is not supported")
|
||||||
if file_path.suffix == '.txt':
|
if file_path.suffix == '.txt':
|
||||||
with open(file_path, "r") as fd:
|
with open(file_path, "r") as fd:
|
||||||
tags = TagLine(fd.readline()).tags()
|
tags = TagLine(fd.readline()).tags(prefix)
|
||||||
else: # '.yaml'
|
else: # '.yaml'
|
||||||
with open(file_path, "r") as fd:
|
with open(file_path, "r") as fd:
|
||||||
data = yaml.load(fd, Loader=yaml.FullLoader)
|
data = yaml.load(fd, Loader=yaml.FullLoader)
|
||||||
tags = set(sorted(data[cls.tags_yaml_key]))
|
if prefix and len(prefix) > 0:
|
||||||
|
tags = set(sorted([t.strip() for t in data[cls.tags_yaml_key] if t.startswith(prefix)]))
|
||||||
|
else:
|
||||||
|
tags = set(sorted(data[cls.tags_yaml_key]))
|
||||||
return tags
|
return tags
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@ -118,9 +118,10 @@ class TagLine(str):
|
|||||||
"""
|
"""
|
||||||
return cls(' '.join([cls.prefix] + sorted([t for t in tags])))
|
return cls(' '.join([cls.prefix] + sorted([t for t in tags])))
|
||||||
|
|
||||||
def tags(self) -> set[Tag]:
|
def tags(self, prefix: Optional[str] = None) -> set[Tag]:
|
||||||
"""
|
"""
|
||||||
Returns all tags contained in this line as a set.
|
Returns all tags contained in this line as a set, optionally
|
||||||
|
filtered based on prefix.
|
||||||
"""
|
"""
|
||||||
tagstr = self[len(self.prefix):].strip()
|
tagstr = self[len(self.prefix):].strip()
|
||||||
separator = Tag.default_separator
|
separator = Tag.default_separator
|
||||||
@ -130,7 +131,10 @@ class TagLine(str):
|
|||||||
if s in tagstr:
|
if s in tagstr:
|
||||||
separator = s
|
separator = s
|
||||||
break
|
break
|
||||||
return set(sorted([Tag(t.strip()) for t in tagstr.split(separator)]))
|
if prefix and len(prefix) > 0:
|
||||||
|
return set(sorted([Tag(t.strip()) for t in tagstr.split(separator) if t.startswith(prefix)]))
|
||||||
|
else:
|
||||||
|
return set(sorted([Tag(t.strip()) for t in tagstr.split(separator)]))
|
||||||
|
|
||||||
def merge(self, taglines: set['TagLine']) -> 'TagLine':
|
def merge(self, taglines: set['TagLine']) -> 'TagLine':
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -543,7 +543,7 @@ class TagsFromFileTestCase(CmmTestCase):
|
|||||||
self.file_txt = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
|
self.file_txt = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
|
||||||
self.file_path_txt = pathlib.Path(self.file_txt.name)
|
self.file_path_txt = pathlib.Path(self.file_txt.name)
|
||||||
with open(self.file_path_txt, "w") as fd:
|
with open(self.file_path_txt, "w") as fd:
|
||||||
fd.write(f"""{TagLine.prefix} tag1 tag2
|
fd.write(f"""{TagLine.prefix} tag1 tag2 ptag3
|
||||||
{Question.txt_header}
|
{Question.txt_header}
|
||||||
This is a question.
|
This is a question.
|
||||||
{Answer.txt_header}
|
{Answer.txt_header}
|
||||||
@ -560,6 +560,7 @@ This is an answer.
|
|||||||
{Message.tags_yaml_key}:
|
{Message.tags_yaml_key}:
|
||||||
- tag1
|
- tag1
|
||||||
- tag2
|
- tag2
|
||||||
|
- ptag3
|
||||||
""")
|
""")
|
||||||
|
|
||||||
def tearDown(self) -> None:
|
def tearDown(self) -> None:
|
||||||
@ -570,11 +571,19 @@ This is an answer.
|
|||||||
|
|
||||||
def test_tags_from_file_txt(self) -> None:
|
def test_tags_from_file_txt(self) -> None:
|
||||||
tags = Message.tags_from_file(self.file_path_txt)
|
tags = Message.tags_from_file(self.file_path_txt)
|
||||||
self.assertSetEqual(tags, {Tag('tag1'), Tag('tag2')})
|
self.assertSetEqual(tags, {Tag('tag1'), Tag('tag2'), Tag('ptag3')})
|
||||||
|
|
||||||
def test_tags_from_file_yaml(self) -> None:
|
def test_tags_from_file_yaml(self) -> None:
|
||||||
tags = Message.tags_from_file(self.file_path_yaml)
|
tags = Message.tags_from_file(self.file_path_yaml)
|
||||||
self.assertSetEqual(tags, {Tag('tag1'), Tag('tag2')})
|
self.assertSetEqual(tags, {Tag('tag1'), Tag('tag2'), Tag('ptag3')})
|
||||||
|
|
||||||
|
def test_tags_from_file_txt_prefix(self) -> None:
|
||||||
|
tags = Message.tags_from_file(self.file_path_txt, prefix='p')
|
||||||
|
self.assertSetEqual(tags, {Tag('ptag3')})
|
||||||
|
|
||||||
|
def test_tags_from_file_yaml_prefix(self) -> None:
|
||||||
|
tags = Message.tags_from_file(self.file_path_yaml, prefix='p')
|
||||||
|
self.assertSetEqual(tags, {Tag('ptag3')})
|
||||||
|
|
||||||
|
|
||||||
class MessageIDTestCase(CmmTestCase):
|
class MessageIDTestCase(CmmTestCase):
|
||||||
|
|||||||
@ -49,6 +49,13 @@ class TestTagLine(CmmTestCase):
|
|||||||
tags = tagline.tags()
|
tags = tagline.tags()
|
||||||
self.assertEqual(tags, {Tag('tag1'), Tag('tag2')})
|
self.assertEqual(tags, {Tag('tag1'), Tag('tag2')})
|
||||||
|
|
||||||
|
def test_tags_prefix(self) -> None:
|
||||||
|
tagline = TagLine('TAGS: atag1 stag2 stag3')
|
||||||
|
tags = tagline.tags(prefix='a')
|
||||||
|
self.assertEqual(tags, {Tag('atag1')})
|
||||||
|
tags = tagline.tags(prefix='s')
|
||||||
|
self.assertEqual(tags, {Tag('stag2'), Tag('stag3')})
|
||||||
|
|
||||||
def test_merge(self) -> None:
|
def test_merge(self) -> None:
|
||||||
tagline1 = TagLine('TAGS: tag1 tag2')
|
tagline1 = TagLine('TAGS: tag1 tag2')
|
||||||
tagline2 = TagLine('TAGS: tag2 tag3')
|
tagline2 = TagLine('TAGS: tag2 tag3')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user