Compare commits
2 Commits
ebac55b7a8
...
8844622f6b
| Author | SHA1 | Date | |
|---|---|---|---|
| 8844622f6b | |||
| 5de41ef476 |
129
chatmastermind/tags.py
Normal file
129
chatmastermind/tags.py
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
"""
|
||||||
|
Module implementing tag related functions and classes.
|
||||||
|
"""
|
||||||
|
from typing import Type, TypeVar
|
||||||
|
|
||||||
|
TagInst = TypeVar('TagInst', bound='Tag')
|
||||||
|
TagLineInst = TypeVar('TagLineInst', bound='TagLine')
|
||||||
|
|
||||||
|
|
||||||
|
class TagError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Tag(str):
|
||||||
|
"""
|
||||||
|
A single tag. A string that can contain anything but the default separator (' ').
|
||||||
|
"""
|
||||||
|
# default separator
|
||||||
|
default_separator = ' '
|
||||||
|
# alternative separators (e. g. for backwards compatibility)
|
||||||
|
alternative_separators = [',']
|
||||||
|
|
||||||
|
def __new__(cls: Type[TagInst], string: str) -> TagInst:
|
||||||
|
"""
|
||||||
|
Make sure the tag string does not contain the default separator.
|
||||||
|
"""
|
||||||
|
if cls.default_separator in string:
|
||||||
|
raise TypeError(f"Tag '{string}' contains the separator char '{cls.default_separator}'")
|
||||||
|
instance = super().__new__(cls, string)
|
||||||
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
class TagLine(str):
|
||||||
|
"""
|
||||||
|
A line of tags. It starts with a prefix ('TAGS:'), followed by a list of tags,
|
||||||
|
separated by the defaut separator (' '). Any operations on a TagLine will sort
|
||||||
|
the tags.
|
||||||
|
"""
|
||||||
|
# the prefix
|
||||||
|
prefix = 'TAGS:'
|
||||||
|
|
||||||
|
def __new__(cls: Type[TagLineInst], string: str) -> TagLineInst:
|
||||||
|
"""
|
||||||
|
Make sure the tagline string starts with the prefix.
|
||||||
|
"""
|
||||||
|
if not string.startswith(cls.prefix):
|
||||||
|
raise TagError(f"TagLine '{string}' is missing prefix '{cls.prefix}'")
|
||||||
|
instance = super().__new__(cls, string)
|
||||||
|
return instance
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_set(cls: Type[TagLineInst], tags: set[Tag]) -> TagLineInst:
|
||||||
|
"""
|
||||||
|
Create a new TagLine from a set of tags.
|
||||||
|
"""
|
||||||
|
return cls(' '.join([TagLine.prefix] + sorted([t for t in tags])))
|
||||||
|
|
||||||
|
def tags(self) -> set[Tag]:
|
||||||
|
"""
|
||||||
|
Returns all tags contained in this line as a set.
|
||||||
|
"""
|
||||||
|
tagstr = self[len(self.prefix):].strip()
|
||||||
|
separator = Tag.default_separator
|
||||||
|
# look for alternative separators and use the first one found
|
||||||
|
# -> we don't support different separators in the same TagLine
|
||||||
|
for s in Tag.alternative_separators:
|
||||||
|
if s in tagstr:
|
||||||
|
separator = s
|
||||||
|
break
|
||||||
|
return set(sorted([Tag(t.strip()) for t in tagstr.split(separator)]))
|
||||||
|
|
||||||
|
def merge(self, taglines: set['TagLine']) -> 'TagLine':
|
||||||
|
"""
|
||||||
|
Merges the tags of all given taglines into the current one
|
||||||
|
and returns a new TagLine.
|
||||||
|
"""
|
||||||
|
merged_tags = self.tags()
|
||||||
|
for tl in taglines:
|
||||||
|
merged_tags |= tl.tags()
|
||||||
|
return self.from_set(set(sorted(merged_tags)))
|
||||||
|
|
||||||
|
def delete_tags(self, tags: set[Tag]) -> 'TagLine':
|
||||||
|
"""
|
||||||
|
Deletes the given tags and returns a new TagLine.
|
||||||
|
"""
|
||||||
|
return self.from_set(self.tags().difference(tags))
|
||||||
|
|
||||||
|
def add_tags(self, tags: set[Tag]) -> 'TagLine':
|
||||||
|
"""
|
||||||
|
Adds the given tags and returns a new TagLine.
|
||||||
|
"""
|
||||||
|
return self.from_set(set(sorted(self.tags() | tags)))
|
||||||
|
|
||||||
|
def rename_tags(self, tags: set[tuple[Tag, Tag]]) -> 'TagLine':
|
||||||
|
"""
|
||||||
|
Renames the given tags and returns a new TagLine. The first
|
||||||
|
tuple element is the old name, the second one is the new name.
|
||||||
|
"""
|
||||||
|
new_tags = self.tags()
|
||||||
|
for t in tags:
|
||||||
|
if t[0] in new_tags:
|
||||||
|
new_tags.remove(t[0])
|
||||||
|
new_tags.add(t[1])
|
||||||
|
return self.from_set(set(sorted(new_tags)))
|
||||||
|
|
||||||
|
def match_tags(self, tags_or: set[Tag], tags_and: set[Tag], tags_not: set[Tag]) -> bool:
|
||||||
|
"""
|
||||||
|
Checks if the current TagLine matches the given tag requirements:
|
||||||
|
- 'tags_or' : matches if this TagLine contains ANY of those tags
|
||||||
|
- 'tags_and': matches if this TagLine contains ALL of those tags
|
||||||
|
- 'tags_not': matches if this TagLine contains NONE of those tags
|
||||||
|
|
||||||
|
Note that it's sufficient if the TagLine matches one of 'tags_or' or 'tags_and',
|
||||||
|
i. e. you can select a TagLine if it either contains one of the tags in 'tags_or'
|
||||||
|
or all of the tags in 'tags_and' but it must never contain any of the tags in
|
||||||
|
'tags_not'. If 'tags_or' and 'tags_and' are 'None', they match all tags (tag
|
||||||
|
exclusion is still done if 'tags_not' is not 'None').
|
||||||
|
"""
|
||||||
|
tag_set = self.tags()
|
||||||
|
required_tags_present = False
|
||||||
|
excluded_tags_missing = False
|
||||||
|
if ((tags_or is None and tags_and is None)
|
||||||
|
or (tags_or and any(tag in tag_set for tag in tags_or)) # noqa: W503
|
||||||
|
or (tags_and and all(tag in tag_set for tag in tags_and))): # noqa: W503
|
||||||
|
required_tags_present = True
|
||||||
|
if ((tags_not is None)
|
||||||
|
or (not any(tag in tag_set for tag in tags_not))): # noqa: W503
|
||||||
|
excluded_tags_missing = True
|
||||||
|
return required_tags_present and excluded_tags_missing
|
||||||
@ -7,6 +7,7 @@ from chatmastermind.main import create_parser, ask_cmd
|
|||||||
from chatmastermind.api_client import ai
|
from chatmastermind.api_client import ai
|
||||||
from chatmastermind.configuration import Config
|
from chatmastermind.configuration import Config
|
||||||
from chatmastermind.storage import create_chat_hist, save_answers, dump_data
|
from chatmastermind.storage import create_chat_hist, save_answers, dump_data
|
||||||
|
from chatmastermind.tags import Tag, TagLine, TagError
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
from unittest.mock import patch, MagicMock, Mock, ANY
|
from unittest.mock import patch, MagicMock, Mock, ANY
|
||||||
|
|
||||||
@ -231,3 +232,108 @@ class TestCreateParser(CmmTestCase):
|
|||||||
mock_cmdparser.add_parser.assert_any_call('config', help=ANY, aliases=ANY)
|
mock_cmdparser.add_parser.assert_any_call('config', help=ANY, aliases=ANY)
|
||||||
mock_cmdparser.add_parser.assert_any_call('print', help=ANY, aliases=ANY)
|
mock_cmdparser.add_parser.assert_any_call('print', help=ANY, aliases=ANY)
|
||||||
self.assertTrue('.config.yaml' in parser.get_default('config'))
|
self.assertTrue('.config.yaml' in parser.get_default('config'))
|
||||||
|
|
||||||
|
|
||||||
|
class TestTag(CmmTestCase):
|
||||||
|
def test_valid_tag(self) -> None:
|
||||||
|
tag = Tag('mytag')
|
||||||
|
self.assertEqual(tag, 'mytag')
|
||||||
|
|
||||||
|
def test_invalid_tag(self) -> None:
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
Tag('tag with space')
|
||||||
|
|
||||||
|
def test_default_separator(self) -> None:
|
||||||
|
self.assertEqual(Tag.default_separator, ' ')
|
||||||
|
|
||||||
|
def test_alternative_separators(self) -> None:
|
||||||
|
self.assertEqual(Tag.alternative_separators, [','])
|
||||||
|
|
||||||
|
|
||||||
|
class TestTagLine(CmmTestCase):
|
||||||
|
def test_valid_tagline(self) -> None:
|
||||||
|
tagline = TagLine('TAGS: tag1 tag2')
|
||||||
|
self.assertEqual(tagline, 'TAGS: tag1 tag2')
|
||||||
|
|
||||||
|
def test_invalid_tagline(self) -> None:
|
||||||
|
with self.assertRaises(TagError):
|
||||||
|
TagLine('tag1 tag2')
|
||||||
|
|
||||||
|
def test_prefix(self) -> None:
|
||||||
|
self.assertEqual(TagLine.prefix, 'TAGS:')
|
||||||
|
|
||||||
|
def test_from_set(self) -> None:
|
||||||
|
tags = {Tag('tag1'), Tag('tag2')}
|
||||||
|
tagline = TagLine.from_set(tags)
|
||||||
|
self.assertEqual(tagline, 'TAGS: tag1 tag2')
|
||||||
|
|
||||||
|
def test_tags(self) -> None:
|
||||||
|
tagline = TagLine('TAGS: tag1 tag2')
|
||||||
|
tags = tagline.tags()
|
||||||
|
self.assertEqual(tags, {Tag('tag1'), Tag('tag2')})
|
||||||
|
|
||||||
|
def test_merge(self) -> None:
|
||||||
|
tagline1 = TagLine('TAGS: tag1 tag2')
|
||||||
|
tagline2 = TagLine('TAGS: tag2 tag3')
|
||||||
|
merged_tagline = tagline1.merge({tagline2})
|
||||||
|
self.assertEqual(merged_tagline, 'TAGS: tag1 tag2 tag3')
|
||||||
|
|
||||||
|
def test_delete_tags(self) -> None:
|
||||||
|
tagline = TagLine('TAGS: tag1 tag2 tag3')
|
||||||
|
new_tagline = tagline.delete_tags({Tag('tag1'), Tag('tag3')})
|
||||||
|
self.assertEqual(new_tagline, 'TAGS: tag2')
|
||||||
|
|
||||||
|
def test_add_tags(self) -> None:
|
||||||
|
tagline = TagLine('TAGS: tag1')
|
||||||
|
new_tagline = tagline.add_tags({Tag('tag2'), Tag('tag3')})
|
||||||
|
self.assertEqual(new_tagline, 'TAGS: tag1 tag2 tag3')
|
||||||
|
|
||||||
|
def test_rename_tags(self) -> None:
|
||||||
|
tagline = TagLine('TAGS: old1 old2')
|
||||||
|
new_tagline = tagline.rename_tags({(Tag('old1'), Tag('new1')), (Tag('old2'), Tag('new2'))})
|
||||||
|
self.assertEqual(new_tagline, 'TAGS: new1 new2')
|
||||||
|
|
||||||
|
def test_match_tags(self) -> None:
|
||||||
|
tagline = TagLine('TAGS: tag1 tag2 tag3')
|
||||||
|
|
||||||
|
# Test case 1: Match any tag in 'tags_or'
|
||||||
|
tags_or = {Tag('tag1'), Tag('tag4')}
|
||||||
|
tags_and: set[Tag] = set()
|
||||||
|
tags_not: set[Tag] = set()
|
||||||
|
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||||
|
|
||||||
|
# Test case 2: Match all tags in 'tags_and'
|
||||||
|
tags_or = set()
|
||||||
|
tags_and = {Tag('tag1'), Tag('tag2'), Tag('tag3')}
|
||||||
|
tags_not = set()
|
||||||
|
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||||
|
|
||||||
|
# Test case 3: Match any tag in 'tags_or' and match all tags in 'tags_and'
|
||||||
|
tags_or = {Tag('tag1'), Tag('tag4')}
|
||||||
|
tags_and = {Tag('tag1'), Tag('tag2')}
|
||||||
|
tags_not = set()
|
||||||
|
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||||
|
|
||||||
|
# Test case 4: Match any tag in 'tags_or', match all tags in 'tags_and', and exclude tags in 'tags_not'
|
||||||
|
tags_or = {Tag('tag1'), Tag('tag4')}
|
||||||
|
tags_and = {Tag('tag1'), Tag('tag2')}
|
||||||
|
tags_not = {Tag('tag5')}
|
||||||
|
self.assertTrue(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||||
|
|
||||||
|
# Test case 5: No matching tags in 'tags_or'
|
||||||
|
tags_or = {Tag('tag4'), Tag('tag5')}
|
||||||
|
tags_and = set()
|
||||||
|
tags_not = set()
|
||||||
|
self.assertFalse(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||||
|
|
||||||
|
# Test case 6: Not all tags in 'tags_and' are present
|
||||||
|
tags_or = set()
|
||||||
|
tags_and = {Tag('tag1'), Tag('tag2'), Tag('tag3'), Tag('tag4')}
|
||||||
|
tags_not = set()
|
||||||
|
self.assertFalse(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||||
|
|
||||||
|
# Test case 7: Some tags in 'tags_not' are present
|
||||||
|
tags_or = {Tag('tag1')}
|
||||||
|
tags_and = set()
|
||||||
|
tags_not = {Tag('tag2')}
|
||||||
|
self.assertFalse(tagline.match_tags(tags_or, tags_and, tags_not))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user