2022-02-08 23:45:55 +01:00
|
|
|
from __future__ import annotations
|
2022-02-05 22:37:09 +01:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Union, List, Optional
|
2022-02-07 18:36:30 +01:00
|
|
|
|
2022-02-06 22:20:16 +01:00
|
|
|
from ..logger import logger
|
2022-02-05 22:37:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
class GenericText:
|
2022-02-07 18:33:00 +01:00
|
|
|
def __init__(self, content: Optional[Union[List[str], Path, str]] = None):
|
2022-02-07 21:00:51 +01:00
|
|
|
# TODO: what if paths are not absolute? Have a root available?
|
2022-02-07 00:42:03 +01:00
|
|
|
if isinstance(content, list):
|
2022-02-05 22:37:09 +01:00
|
|
|
self._content: Optional[List[str]] = content
|
|
|
|
self._path = None
|
2022-02-06 23:52:44 +01:00
|
|
|
self._initialized = True
|
2022-02-07 18:33:00 +01:00
|
|
|
elif isinstance(content, Path) or isinstance(content, str):
|
2022-02-08 16:24:42 +01:00
|
|
|
self._content = None
|
2022-02-07 18:33:00 +01:00
|
|
|
self._path = Path(content)
|
2022-02-06 23:52:44 +01:00
|
|
|
self._initialized = True
|
|
|
|
else:
|
|
|
|
self._content = None
|
|
|
|
self._path = None
|
|
|
|
self._initialized = False
|
2022-02-05 22:37:09 +01:00
|
|
|
|
|
|
|
@property
|
2022-02-06 15:10:00 +01:00
|
|
|
def text(self) -> List[str]:
|
2022-02-06 23:52:44 +01:00
|
|
|
if self._initialized:
|
|
|
|
if self._content is None:
|
2022-02-08 16:24:42 +01:00
|
|
|
if self._path is None:
|
|
|
|
raise NotImplementedError # Programming error
|
2022-02-06 23:52:44 +01:00
|
|
|
try:
|
|
|
|
with open(self._path, 'r') as file:
|
|
|
|
self._content = file.readlines()
|
|
|
|
except FileNotFoundError:
|
|
|
|
raise NotImplementedError
|
|
|
|
except:
|
|
|
|
raise NotImplementedError
|
|
|
|
return self._content
|
|
|
|
else:
|
|
|
|
return []
|
2022-02-05 22:37:09 +01:00
|
|
|
|
2022-02-06 15:28:04 +01:00
|
|
|
@text.setter
|
2022-02-06 23:52:44 +01:00
|
|
|
def text(self, content: Union[List[str], Path, None]) -> None:
|
2022-02-06 15:28:04 +01:00
|
|
|
if isinstance(content, List):
|
|
|
|
self._content = content
|
|
|
|
self._path = None
|
2022-02-06 23:52:44 +01:00
|
|
|
self._initialized = True
|
|
|
|
elif isinstance(content, Path):
|
2022-02-06 15:28:04 +01:00
|
|
|
self._content = None
|
|
|
|
self._path = content
|
2022-02-06 23:52:44 +01:00
|
|
|
self._initialized = True
|
|
|
|
else:
|
|
|
|
self._content = None
|
|
|
|
self._path = None
|
|
|
|
self._initialized = False
|
2022-02-06 15:28:04 +01:00
|
|
|
|
2022-02-06 23:52:44 +01:00
|
|
|
@property
|
2022-02-06 22:20:16 +01:00
|
|
|
def path(self) -> Optional[Path]:
|
|
|
|
return self._path
|
|
|
|
|
2022-02-07 00:42:03 +01:00
|
|
|
@property
|
|
|
|
def pathname(self) -> Optional[str]:
|
|
|
|
return str(self._path) if self._path else None
|
|
|
|
|
2022-02-05 22:37:09 +01:00
|
|
|
def format(self, **kwargs) -> str:
|
2022-02-06 16:50:09 +01:00
|
|
|
lines = []
|
2022-02-06 23:52:44 +01:00
|
|
|
for line in self.text:
|
2022-02-05 22:37:09 +01:00
|
|
|
try:
|
2022-02-06 16:50:09 +01:00
|
|
|
line = '% ' + line.format(**kwargs).rjust(77) + '%'
|
|
|
|
if len(line) > 80:
|
|
|
|
logger.warning(
|
|
|
|
'Line too long') # TODO
|
|
|
|
lines.append(line)
|
2022-02-05 22:37:09 +01:00
|
|
|
except ValueError:
|
|
|
|
raise NotImplementedError
|
2022-02-06 16:50:09 +01:00
|
|
|
return '\n'.join(lines)
|
2022-02-06 15:28:04 +01:00
|
|
|
|
2022-02-06 23:52:44 +01:00
|
|
|
def has_value(self) -> bool:
|
|
|
|
return self._initialized
|
|
|
|
|
|
|
|
@property
|
|
|
|
def real_text(self) -> Optional[List[str]]:
|
|
|
|
if self.has_value():
|
|
|
|
return self._content
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2022-02-08 23:45:55 +01:00
|
|
|
def __add__(self, other: Union[None, GenericText, List[str]]) -> GenericText:
|
2022-02-06 23:52:44 +01:00
|
|
|
if not self.has_value():
|
|
|
|
return other
|
2022-02-06 15:28:04 +01:00
|
|
|
if isinstance(other, GenericText):
|
2022-02-08 23:33:43 +01:00
|
|
|
if not other.has_value():
|
|
|
|
return self
|
2022-02-06 15:28:04 +01:00
|
|
|
return GenericText(self.text + other.text)
|
|
|
|
else:
|
|
|
|
return GenericText(self.text + other)
|
|
|
|
|
2022-02-08 23:45:55 +01:00
|
|
|
def __iadd__(self, other: Union[None, GenericText, List[str]]) -> GenericText:
|
|
|
|
if not self.has_value():
|
|
|
|
self.text = other
|
|
|
|
elif isinstance(other, GenericText):
|
|
|
|
if other.has_value():
|
|
|
|
self.text += other.text
|
|
|
|
else:
|
|
|
|
self.text += other
|
|
|
|
return self
|
|
|
|
|
2022-02-08 23:33:43 +01:00
|
|
|
def __radd__(self, other):
|
|
|
|
if other is None:
|
|
|
|
return self
|
|
|
|
return other + self
|