Compare commits
4 Commits
6a4bbf1e60
...
0d5ba8b921
| Author | SHA1 | Date | |
|---|---|---|---|
| 0d5ba8b921 | |||
| 604e5ccf73 | |||
| ef46f5efc9 | |||
| b13a68836a |
26
chatmastermind/message.py
Normal file
26
chatmastermind/message.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
"""
|
||||||
|
Module implementing message related functions and classes.
|
||||||
|
"""
|
||||||
|
from typing import Type, TypeVar
|
||||||
|
|
||||||
|
QuestionInst = TypeVar('QuestionInst', bound='Question')
|
||||||
|
|
||||||
|
|
||||||
|
class MessageError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Question(str):
|
||||||
|
"""
|
||||||
|
A single question with a defined prefix.
|
||||||
|
"""
|
||||||
|
prefix = '=== QUESTION ==='
|
||||||
|
|
||||||
|
def __new__(cls: Type[QuestionInst], string: str) -> QuestionInst:
|
||||||
|
"""
|
||||||
|
Make sure the tag string does not contain the default separator.
|
||||||
|
"""
|
||||||
|
if cls.prefix in string:
|
||||||
|
raise MessageError(f"Question '{string}' contains the prefix '{cls.prefix}'")
|
||||||
|
instance = super().__new__(cls, string)
|
||||||
|
return instance
|
||||||
173
chatmastermind/tags.py
Normal file
173
chatmastermind/tags.py
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
"""
|
||||||
|
Module implementing tag related functions and classes.
|
||||||
|
"""
|
||||||
|
from typing import Type, TypeVar, Optional
|
||||||
|
|
||||||
|
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 TagError(f"Tag '{string}' contains the separator char '{cls.default_separator}'")
|
||||||
|
instance = super().__new__(cls, string)
|
||||||
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
def delete_tags(tags: set[Tag], tags_delete: set[Tag]) -> set[Tag]:
|
||||||
|
"""
|
||||||
|
Deletes the given tags and returns a new set.
|
||||||
|
"""
|
||||||
|
return tags.difference(tags_delete)
|
||||||
|
|
||||||
|
|
||||||
|
def add_tags(tags: set[Tag], tags_add: set[Tag]) -> set[Tag]:
|
||||||
|
"""
|
||||||
|
Adds the given tags and returns a new set.
|
||||||
|
"""
|
||||||
|
return set(sorted(tags | tags_add))
|
||||||
|
|
||||||
|
|
||||||
|
def merge_tags(tags: set[Tag], tags_merge: list[set[Tag]]) -> set[Tag]:
|
||||||
|
"""
|
||||||
|
Merges the tags in 'tags_merge' into the current one and returns a new set.
|
||||||
|
"""
|
||||||
|
for ts in tags_merge:
|
||||||
|
tags |= ts
|
||||||
|
return tags
|
||||||
|
|
||||||
|
|
||||||
|
def rename_tags(tags: set[Tag], tags_rename: set[tuple[Tag, Tag]]) -> set[Tag]:
|
||||||
|
"""
|
||||||
|
Renames the given tags and returns a new set. The first tuple element
|
||||||
|
is the old name, the second one is the new name.
|
||||||
|
"""
|
||||||
|
for t in tags_rename:
|
||||||
|
if t[0] in tags:
|
||||||
|
tags.remove(t[0])
|
||||||
|
tags.add(t[1])
|
||||||
|
return set(sorted(tags))
|
||||||
|
|
||||||
|
|
||||||
|
def match_tags(tags: set[Tag], tags_or: Optional[set[Tag]], tags_and: Optional[set[Tag]],
|
||||||
|
tags_not: Optional[set[Tag]]) -> bool:
|
||||||
|
"""
|
||||||
|
Checks if the given set 'tags' 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 'tags' 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').
|
||||||
|
"""
|
||||||
|
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 tags for tag in tags_or)) # noqa: W503
|
||||||
|
or (tags_and and all(tag in tags for tag in tags_and))): # noqa: W503
|
||||||
|
required_tags_present = True
|
||||||
|
if ((tags_not is None)
|
||||||
|
or (not any(tag in tags for tag in tags_not))): # noqa: W503
|
||||||
|
excluded_tags_missing = True
|
||||||
|
return required_tags_present and excluded_tags_missing
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
tags_merge = [tl.tags() for tl in taglines]
|
||||||
|
return self.from_set(merge_tags(self.tags(), tags_merge))
|
||||||
|
|
||||||
|
def delete_tags(self, tags_delete: set[Tag]) -> 'TagLine':
|
||||||
|
"""
|
||||||
|
Deletes the given tags and returns a new TagLine.
|
||||||
|
"""
|
||||||
|
return self.from_set(delete_tags(self.tags(), tags_delete))
|
||||||
|
|
||||||
|
def add_tags(self, tags_add: set[Tag]) -> 'TagLine':
|
||||||
|
"""
|
||||||
|
Adds the given tags and returns a new TagLine.
|
||||||
|
"""
|
||||||
|
return self.from_set(add_tags(self.tags(), tags_add))
|
||||||
|
|
||||||
|
def rename_tags(self, tags_rename: 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.
|
||||||
|
"""
|
||||||
|
return self.from_set(rename_tags(self.tags(), tags_rename))
|
||||||
|
|
||||||
|
def match_tags(self, tags_or: Optional[set[Tag]], tags_and: Optional[set[Tag]],
|
||||||
|
tags_not: Optional[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').
|
||||||
|
"""
|
||||||
|
return match_tags(self.tags(), tags_or, tags_and, tags_not)
|
||||||
@ -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,116 @@ 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(TagError):
|
||||||
|
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))
|
||||||
|
|
||||||
|
# Test case 8: 'tags_or' and 'tags_and' are None, match all tags
|
||||||
|
tags_not = set()
|
||||||
|
self.assertTrue(tagline.match_tags(None, None, tags_not))
|
||||||
|
|
||||||
|
# Test case 9: 'tags_or' and 'tags_and' are None, match all tags except excluded tags
|
||||||
|
tags_not = {Tag('tag2')}
|
||||||
|
self.assertFalse(tagline.match_tags(None, None, tags_not))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user