Compare commits
4 Commits
d5326b7874
...
0d6ed2ba63
| Author | SHA1 | Date | |
|---|---|---|---|
| 0d6ed2ba63 | |||
| cd746fcd45 | |||
| b16bfc7454 | |||
| 2d7e1baaa1 |
@ -65,5 +65,5 @@ class Config():
|
||||
with open(path, 'w') as f:
|
||||
yaml.dump(asdict(self), f)
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
def asdict(self) -> dict[str, Any]:
|
||||
return asdict(self)
|
||||
|
||||
@ -121,7 +121,7 @@ class Message():
|
||||
@classmethod
|
||||
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'],
|
||||
answer=data.get('answer', None),
|
||||
@ -133,28 +133,16 @@ class Message():
|
||||
"""
|
||||
Return only the tags from the given Message file.
|
||||
"""
|
||||
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
|
||||
return set() # FIXME
|
||||
|
||||
@classmethod
|
||||
def from_file(cls: Type[MessageInst], file_path: pathlib.Path) -> MessageInst:
|
||||
"""
|
||||
Create a Message from the given file. Expects the following file structures:
|
||||
For '.txt':
|
||||
* TagLine
|
||||
* Question.Header
|
||||
* Question
|
||||
* Answer.Header
|
||||
For '.yaml':
|
||||
TODO
|
||||
Create a Message from the given file. Expects the following file structure:
|
||||
* TagLine (from 'self.tags')
|
||||
* Question.Header
|
||||
* Question
|
||||
* Answer.Header
|
||||
"""
|
||||
if not file_path.exists():
|
||||
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:
|
||||
"""
|
||||
Write Message to the given file. Creates the following file structures:
|
||||
For '.txt':
|
||||
* TagLine
|
||||
* Question.Header
|
||||
* Question
|
||||
* Answer.Header
|
||||
* Answer
|
||||
For '.yaml':
|
||||
TODO
|
||||
Write Message to the given file. Creates the following file structure:
|
||||
* TagLine (from 'self.tags')
|
||||
* Question.Header
|
||||
* Question
|
||||
* Answer.Header
|
||||
* Answer
|
||||
"""
|
||||
if file_path:
|
||||
self.file_path = file_path
|
||||
@ -198,13 +183,7 @@ class Message():
|
||||
raise MessageError("Got no valid path to write message")
|
||||
if self.file_path.suffix not in self.file_suffixes:
|
||||
raise MessageError(f"File type '{self.file_path.suffix}' is not supported")
|
||||
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
|
||||
pass
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
def asdict(self) -> dict[str, Any]:
|
||||
return asdict(self)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user