add auto formatter choosing

This commit is contained in:
Maximilian Keßler 2022-02-08 18:26:25 +01:00
parent a0dbb3882e
commit f89ccf245b
6 changed files with 57 additions and 10 deletions

View file

@ -0,0 +1,35 @@
from pathlib import Path
from typing import Optional, Dict, Union, Type
from .formatting_config import FormattingConfig
from .dict_formatter import DictFormatter
from .simple_tex_formatter import SimpleTeXFormatter
from .dtx_formatter import DTXFormatter
from .pytex_formatter import PyTeXFormatter
def from_file_extension(
cls,
input_file: Path,
config: Optional[FormattingConfig] = None,
locate_file_config: bool = True,
allow_infile_config: bool = True
) -> PyTeXFormatter:
switcher: Dict[str, Type[Union[DTXFormatter, SimpleTeXFormatter, DictFormatter]]] = {
'.dtx.pytex': DTXFormatter,
'.sty.pytex': SimpleTeXFormatter,
'.cls.pytex': SimpleTeXFormatter,
'.dict.pytex': DictFormatter
}
try:
[name, extension] = input_file.name.split('.', maxsplit=1)
except:
raise NotImplementedError
return switcher[extension](
input_file=input_file,
config=config,
locate_file_config=locate_file_config,
allow_infile_config=allow_infile_config
)

View file

@ -31,6 +31,7 @@ class DictFormatter(PyTeXFormatter):
self._translations = self.parse()
return self._translations
@property
def output_files(self) -> List[str]:
return [
DICTIONARY_NAMING_PATTERN.format(

View file

@ -1,5 +1,21 @@
from .tex_formatter import TexFormatter
from typing import List, Tuple
from pathlib import Path
from .formatting_config import FormattingConfig
class DTXFormatter(TexFormatter):
def dependencies(self) -> List[str]:
pass
def future_config(self) -> List[Tuple[str, FormattingConfig]]:
pass
def output_files(self) -> List[str]:
pass
def open_output_stream(self, build_dir: Path) -> None:
pass
def close_output_stream(self) -> None:
pass

View file

@ -30,22 +30,21 @@ class FormatterIF(ABC):
needed configurations. Else None.
"""
@abstractmethod
@property
@abstractmethod
def output_files(self) -> List[str]:
"""
:return: List of files that will be built when the formatter is invoked
"""
@abstractmethod
@property
@abstractmethod
def dependencies(self) -> List[str]:
"""
:return: List of dependencies (as str filenames)
"""
@property
def input_file(self) -> Path:
if self._input_file is None:

View file

@ -3,12 +3,11 @@ from pathlib import Path
from typing import Optional
from .constants import *
from .enums import TeXType, TeXFlavour
from .formatterif import FormatterIF
from .formatting_config import FormattingConfig
from .generic_text import GenericText
from ..logger import logger
from abc import ABC, abstractmethod
from abc import ABC
class PyTeXFormatter(FormatterIF, ABC):
@ -16,8 +15,6 @@ class PyTeXFormatter(FormatterIF, ABC):
self,
input_file: Optional[Path] = None,
config: Optional[FormattingConfig] = None,
tex_type: Optional[TeXType] = None,
tex_flavour: Optional[TeXFlavour] = None,
locate_file_config: bool = True,
allow_infile_config: bool = True
):
@ -26,8 +23,6 @@ class PyTeXFormatter(FormatterIF, ABC):
config=config
)
self._config: Optional[FormattingConfig] = self._config # for type-hinting
self._tex_type: Optional[TeXType] = tex_type
self._tex_flavour: Optional[TeXFlavour] = tex_flavour
self._allow_infile_config = allow_infile_config
self._header: Optional[GenericText] = None
if locate_file_config:

View file

@ -21,5 +21,6 @@ class SimpleTeXFormatter(TexFormatter):
def dependencies(self) -> List[str]:
return [] # sty / cls file does not depend on anything
@property
def output_files(self) -> List[str]:
return [self.input_file.with_suffix('').name]