Compare commits

..

4 Commits

2 changed files with 16 additions and 37 deletions

View File

@ -65,5 +65,5 @@ class Config():
with open(path, 'w') as f: with open(path, 'w') as f:
yaml.dump(asdict(self), f) yaml.dump(asdict(self), f)
def as_dict(self) -> dict[str, Any]: def asdict(self) -> dict[str, Any]:
return asdict(self) return asdict(self)

View File

@ -121,7 +121,7 @@ class Message():
@classmethod @classmethod
def from_dict(cls: Type[MessageInst], data: dict[str, Any]) -> MessageInst: def from_dict(cls: Type[MessageInst], data: dict[str, Any]) -> MessageInst:
""" """
Create a Message from the given dict. Create a Message fromt he given dict.
""" """
return cls(question=data['question'], return cls(question=data['question'],
answer=data.get('answer', None), answer=data.get('answer', None),
@ -133,28 +133,16 @@ class Message():
""" """
Return only the tags from the given Message file. Return only the tags from the given Message file.
""" """
if not file_path.exists(): return set() # FIXME
raise MessageError(f"Message file '{file_path}' does not exist")
if file_path.suffix not in cls.file_suffixes:
raise MessageError(f"File type '{file_path.suffix}' is not supported")
if file_path.suffix == '.txt':
with open(file_path, "r") as fd:
tags = TagLine(fd.readline()).tags()
else: # '.yaml'
tags = set() # FIXME
return tags
@classmethod @classmethod
def from_file(cls: Type[MessageInst], file_path: pathlib.Path) -> MessageInst: def from_file(cls: Type[MessageInst], file_path: pathlib.Path) -> MessageInst:
""" """
Create a Message from the given file. Expects the following file structures: Create a Message from the given file. Expects the following file structure:
For '.txt': * TagLine (from 'self.tags')
* TagLine
* Question.Header * Question.Header
* Question * Question
* Answer.Header * Answer.Header
For '.yaml':
TODO
""" """
if not file_path.exists(): if not file_path.exists():
raise MessageError(f"Message file '{file_path}' does not exist") raise MessageError(f"Message file '{file_path}' does not exist")
@ -182,15 +170,12 @@ class Message():
def to_file(self, file_path: Optional[pathlib.Path]) -> None: def to_file(self, file_path: Optional[pathlib.Path]) -> None:
""" """
Write Message to the given file. Creates the following file structures: Write Message to the given file. Creates the following file structure:
For '.txt': * TagLine (from 'self.tags')
* TagLine
* Question.Header * Question.Header
* Question * Question
* Answer.Header * Answer.Header
* Answer * Answer
For '.yaml':
TODO
""" """
if file_path: if file_path:
self.file_path = file_path self.file_path = file_path
@ -198,13 +183,7 @@ class Message():
raise MessageError("Got no valid path to write message") raise MessageError("Got no valid path to write message")
if self.file_path.suffix not in self.file_suffixes: if self.file_path.suffix not in self.file_suffixes:
raise MessageError(f"File type '{self.file_path.suffix}' is not supported") raise MessageError(f"File type '{self.file_path.suffix}' is not supported")
if self.file_path.suffix == '.txt': pass
with open(self.file_path, "w") as fd:
msg_tags = self.tags or set()
fd.write(f'{TagLine.from_set(msg_tags)}\n')
fd.write(f'{Question.header}\n{self.question}\n')
fd.write(f'{Answer.header}\n{self.answer}\n')
# FIXME: write YAML format
def as_dict(self) -> dict[str, Any]: def asdict(self) -> dict[str, Any]:
return asdict(self) return asdict(self)