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-05 19:09:09 +01:00
|
|
|
from PyTeX.format.formatter import Formatter
|
|
|
|
from PyTeX.build.paths import RelativePath
|
2022-02-04 21:14:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PyTeXSourceFile:
|
|
|
|
def __init__(self):
|
|
|
|
self._pytex_file_type: PyTeXFileType = None
|
|
|
|
self._relative_path: RelativePath = None
|
2022-02-05 18:51:44 +01:00
|
|
|
self._formatter: Formatter = 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-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
|