Compare commits

..

2 Commits

Author SHA1 Message Date
be35a8ea9e added testcases for messages.py 2023-08-18 16:56:33 +02:00
2830b076f8 added new module 'message.py' 2023-08-18 16:56:33 +02:00
2 changed files with 11 additions and 55 deletions

View File

@ -64,6 +64,3 @@ class Config():
def to_file(self, path: str) -> None:
with open(path, 'w') as f:
yaml.dump(asdict(self), f)
def asdict(self) -> dict[str, Any]:
return asdict(self)

View File

@ -1,14 +1,10 @@
"""
Module implementing message related functions and classes.
"""
import pathlib
from typing import Type, TypeVar, Optional, Any
from dataclasses import dataclass, asdict
from .tags import Tag
from typing import Type, TypeVar
QuestionInst = TypeVar('QuestionInst', bound='Question')
AnswerInst = TypeVar('AnswerInst', bound='Answer')
MessageInst = TypeVar('MessageInst', bound='Message')
class MessageError(Exception):
@ -43,16 +39,16 @@ def source_code(text: str, include_delims: bool = False) -> list[str]:
class Question(str):
"""
A single question with a defined header.
A single question with a defined prefix.
"""
header = '=== QUESTION ==='
prefix = '=== QUESTION ==='
def __new__(cls: Type[QuestionInst], string: str) -> QuestionInst:
"""
Make sure the question string does not contain the header.
Make sure the question string does not contain the prefix.
"""
if cls.header in string:
raise MessageError(f"Question '{string}' contains the header '{cls.header}'")
if cls.prefix in string:
raise MessageError(f"Question '{string}' contains the prefix '{cls.prefix}'")
instance = super().__new__(cls, string)
return instance
@ -65,16 +61,16 @@ class Question(str):
class Answer(str):
"""
A single answer with a defined header.
A single answer with a defined prefix.
"""
header = '=== ANSWER ==='
prefix = '=== ANSWER ==='
def __new__(cls: Type[AnswerInst], string: str) -> AnswerInst:
"""
Make sure the answer string does not contain the header.
Make sure the answer string does not contain the prefix.
"""
if cls.header in string:
raise MessageError(f"Answer '{string}' contains the header '{cls.header}'")
if cls.prefix in string:
raise MessageError(f"Answer '{string}' contains the prefix '{cls.prefix}'")
instance = super().__new__(cls, string)
return instance
@ -83,40 +79,3 @@ class Answer(str):
Extract and return all source code sections.
"""
return source_code(self, include_delims)
@dataclass
class Message():
"""
Single message. Consists of a question and optionally an answer, a set of tags
and a file path.
"""
question: Question
answer: Optional[Answer]
tags: Optional[set[Tag]]
path: Optional[pathlib.Path]
# @classmethod
# def from_file(cls: Type[MessageInst], path: str) -> MessageInst:
# """
# Create a Message from the given file. Expects the following file structure:
# * TagLine (from 'self.tags')
# * Question.Header
# * Question
# * Answer.Header
# """
# pass
def to_file(self, path: str) -> None:
"""
Write Message to the given file. Creates the following file structure:
* TagLine (from 'self.tags')
* Question.Header
* Question
* Answer.Header
* Answer
"""
pass
def asdict(self) -> dict[str, Any]:
return asdict(self)