From e1dc1e367af11ffd178cf1da87d304d4fd5d0d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sat, 5 Feb 2022 22:37:09 +0100 Subject: [PATCH] add generic text class --- PyTeX/format/generic_text.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 PyTeX/format/generic_text.py diff --git a/PyTeX/format/generic_text.py b/PyTeX/format/generic_text.py new file mode 100644 index 0000000..e02cb24 --- /dev/null +++ b/PyTeX/format/generic_text.py @@ -0,0 +1,32 @@ +from pathlib import Path +from typing import Union, List, Optional + + +class GenericText: + def __init__(self, content: Union[List[str], Path]): + if isinstance(content, List): + self._content: Optional[List[str]] = content + self._path = None + else: + self._content: Optional[List[str]] = None + self._path = content + + @property + def content(self) -> List[str]: + if self._content is None: + try: + with open(self._path, 'r') as file: + self._content = file.readlines() + except FileNotFoundError: + raise NotImplementedError + except: + raise NotImplementedError + return self._content + + def format(self, **kwargs) -> str: + for line in self._content: + try: + line = line.format(**kwargs) + except ValueError: + raise NotImplementedError + return '\n'.join(self._content)