pytex/PyTeX/build/pytex_file/pytex_file.py

56 lines
1.7 KiB
Python

from typing import Optional, List, Dict, Tuple, Union
from pathlib import Path
from .enums import PyTeXFileType
from PyTeX.format.formatterif import FormatterIF
from PyTeX.build.paths import RelativePath
from ...format.enums import TeXType
class PyTeXSourceFile:
def __init__(
self,
relative_path: RelativePath,
formatter: Optional[FormatterIF] = None,
pytex_file_type: Optional[PyTeXFileType] = None
):
self._relative_path: RelativePath = None
self._pytex_file_type: PyTeXFileType = None
self._formatter: FormatterIF = None
@property
def relative_path(self):
return self._relative_path
@property
def pytex_file_type(self) -> PyTeXFileType:
return self._pytex_file_type
@property
def output_files(self) -> List[RelativePath]:
files = self._formatter.output_files
files = [
self._relative_path.with_name(filename)
for filename in files
]
return files
@property
def formatter(self) -> FormatterIF:
return self._formatter
@formatter.setter
def formatter(self, formatter):
self._formatter = formatter
def format(self, target_root: Union[Path, RelativePath]) -> Optional[List[Tuple[RelativePath, Dict]]]:
try:
configs = self._formatter.format(
target_root.path if isinstance(target_root, RelativePath) else target_root
)
except Exception as e:
raise NotImplementedError
configs = [
(self._relative_path.with_name(filename), config)
for [filename, config] in configs
]
return configs