2022-02-05 21:13:43 +01:00
|
|
|
from typing import Optional, List, Dict, Tuple, Union
|
|
|
|
from pathlib import Path
|
2022-02-04 21:14:40 +01:00
|
|
|
from .enums import PyTeXFileType
|
2022-02-06 15:11:23 +01:00
|
|
|
from PyTeX.format.formatterif import FormatterIF
|
2022-02-05 19:09:09 +01:00
|
|
|
from PyTeX.build.paths import RelativePath
|
2022-02-06 20:38:51 +01:00
|
|
|
from ...format.enums import TeXType
|
2022-02-04 21:14:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PyTeXSourceFile:
|
2022-02-06 20:38:51 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
relative_path: RelativePath,
|
|
|
|
formatter: Optional[FormatterIF] = None,
|
|
|
|
pytex_file_type: Optional[PyTeXFileType] = None
|
|
|
|
):
|
2022-02-04 21:14:40 +01:00
|
|
|
self._relative_path: RelativePath = None
|
2022-02-06 20:38:51 +01:00
|
|
|
self._pytex_file_type: PyTeXFileType = None
|
2022-02-06 15:11:23 +01:00
|
|
|
self._formatter: FormatterIF = None
|
2022-02-04 21:14:40 +01:00
|
|
|
|
|
|
|
@property
|
2022-02-05 18:51:44 +01:00
|
|
|
def relative_path(self):
|
2022-02-04 21:14:40 +01:00
|
|
|
return self._relative_path
|
|
|
|
|
|
|
|
@property
|
|
|
|
def pytex_file_type(self) -> PyTeXFileType:
|
|
|
|
return self._pytex_file_type
|
|
|
|
|
|
|
|
@property
|
2022-02-05 18:51:44 +01:00
|
|
|
def output_files(self) -> List[RelativePath]:
|
|
|
|
files = self._formatter.output_files
|
|
|
|
files = [
|
|
|
|
self._relative_path.with_name(filename)
|
|
|
|
for filename in files
|
|
|
|
]
|
|
|
|
return files
|
|
|
|
|
2022-02-06 20:38:51 +01:00
|
|
|
@property
|
|
|
|
def formatter(self) -> FormatterIF:
|
|
|
|
return self._formatter
|
|
|
|
|
|
|
|
@formatter.setter
|
|
|
|
def formatter(self, formatter):
|
|
|
|
self._formatter = formatter
|
|
|
|
|
2022-02-05 21:13:43 +01:00
|
|
|
def format(self, target_root: Union[Path, RelativePath]) -> Optional[List[Tuple[RelativePath, Dict]]]:
|
2022-02-05 18:55:28 +01:00
|
|
|
try:
|
2022-02-05 21:13:43 +01:00
|
|
|
configs = self._formatter.format(
|
|
|
|
target_root.path if isinstance(target_root, RelativePath) else target_root
|
|
|
|
)
|
2022-02-05 18:55:28 +01:00
|
|
|
except Exception as e:
|
2022-02-05 19:05:50 +01:00
|
|
|
raise NotImplementedError
|
2022-02-05 18:51:44 +01:00
|
|
|
configs = [
|
|
|
|
(self._relative_path.with_name(filename), config)
|
|
|
|
for [filename, config] in configs
|
|
|
|
]
|
|
|
|
return configs
|