Compare commits
4 Commits
0d6ed2ba63
...
d5326b7874
| Author | SHA1 | Date | |
|---|---|---|---|
| d5326b7874 | |||
| 176c1b634d | |||
| 03304bdbc6 | |||
| 57de2415ee |
@ -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 asdict(self) -> dict[str, Any]:
|
def as_dict(self) -> dict[str, Any]:
|
||||||
return asdict(self)
|
return asdict(self)
|
||||||
|
|||||||
@ -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 fromt he given dict.
|
Create a Message from the given dict.
|
||||||
"""
|
"""
|
||||||
return cls(question=data['question'],
|
return cls(question=data['question'],
|
||||||
answer=data.get('answer', None),
|
answer=data.get('answer', None),
|
||||||
@ -133,16 +133,28 @@ class Message():
|
|||||||
"""
|
"""
|
||||||
Return only the tags from the given Message file.
|
Return only the tags from the given Message file.
|
||||||
"""
|
"""
|
||||||
return set() # FIXME
|
if not file_path.exists():
|
||||||
|
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 structure:
|
Create a Message from the given file. Expects the following file structures:
|
||||||
* TagLine (from 'self.tags')
|
For '.txt':
|
||||||
* Question.Header
|
* TagLine
|
||||||
* Question
|
* Question.Header
|
||||||
* Answer.Header
|
* Question
|
||||||
|
* 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")
|
||||||
@ -170,12 +182,15 @@ 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 structure:
|
Write Message to the given file. Creates the following file structures:
|
||||||
* TagLine (from 'self.tags')
|
For '.txt':
|
||||||
* Question.Header
|
* TagLine
|
||||||
* Question
|
* Question.Header
|
||||||
* Answer.Header
|
* Question
|
||||||
* Answer
|
* Answer.Header
|
||||||
|
* Answer
|
||||||
|
For '.yaml':
|
||||||
|
TODO
|
||||||
"""
|
"""
|
||||||
if file_path:
|
if file_path:
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
@ -183,7 +198,13 @@ 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")
|
||||||
pass
|
if self.file_path.suffix == '.txt':
|
||||||
|
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 asdict(self) -> dict[str, Any]:
|
def as_dict(self) -> dict[str, Any]:
|
||||||
return asdict(self)
|
return asdict(self)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user