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: def to_file(self, path: str) -> None:
with open(path, 'w') as f: with open(path, 'w') as f:
yaml.dump(asdict(self), 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. Module implementing message related functions and classes.
""" """
import pathlib from typing import Type, TypeVar
from typing import Type, TypeVar, Optional, Any
from dataclasses import dataclass, asdict
from .tags import Tag
QuestionInst = TypeVar('QuestionInst', bound='Question') QuestionInst = TypeVar('QuestionInst', bound='Question')
AnswerInst = TypeVar('AnswerInst', bound='Answer') AnswerInst = TypeVar('AnswerInst', bound='Answer')
MessageInst = TypeVar('MessageInst', bound='Message')
class MessageError(Exception): class MessageError(Exception):
@ -43,16 +39,16 @@ def source_code(text: str, include_delims: bool = False) -> list[str]:
class Question(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: 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: if cls.prefix in string:
raise MessageError(f"Question '{string}' contains the header '{cls.header}'") raise MessageError(f"Question '{string}' contains the prefix '{cls.prefix}'")
instance = super().__new__(cls, string) instance = super().__new__(cls, string)
return instance return instance
@ -65,16 +61,16 @@ class Question(str):
class Answer(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: 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: if cls.prefix in string:
raise MessageError(f"Answer '{string}' contains the header '{cls.header}'") raise MessageError(f"Answer '{string}' contains the prefix '{cls.prefix}'")
instance = super().__new__(cls, string) instance = super().__new__(cls, string)
return instance return instance
@ -83,40 +79,3 @@ class Answer(str):
Extract and return all source code sections. Extract and return all source code sections.
""" """
return source_code(self, include_delims) 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)