Compare commits
No commits in common. "87fc0fe0759605acd3678ed1267572e510c2ce2b" and "a5c91adc4138bc5163d6665a0e35a7c42b835da9" have entirely different histories.
87fc0fe075
...
a5c91adc41
@ -1,116 +0,0 @@
|
|||||||
"""
|
|
||||||
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]) -> 'TagLine':
|
|
||||||
# """
|
|
||||||
# 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'.
|
|
||||||
@ -7,7 +7,6 @@ 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
|
||||||
|
|
||||||
@ -232,63 +231,3 @@ 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')
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user