pytex/PyTeX/format/generic_text.py

114 lines
3.5 KiB
Python
Raw Normal View History

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
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)
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]:
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
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
@text.setter
def text(self, content: Union[List[str], Path, None]) -> None:
if isinstance(content, List):
self._content = content
self._path = None
self._initialized = True
elif isinstance(content, Path):
self._content = None
self._path = content
self._initialized = True
else:
self._content = None
self._path = None
self._initialized = False
@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 = []
for line in self.text:
2022-02-05 22:37:09 +01:00
try:
line = '% ' + line.rstrip().format(**kwargs).ljust(77) + '%'
2022-02-06 16:50:09 +01:00
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)
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:
if not self.has_value():
return other
if isinstance(other, GenericText):
2022-02-08 23:33:43 +01:00
if not other.has_value():
return self
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():
if isinstance(other, GenericText):
self.text = other.text
else:
self.text = other
2022-02-08 23:45:55 +01:00
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