pytex/PyTeX/format/auto_format.py

36 lines
1.1 KiB
Python
Raw Normal View History

2022-02-08 18:26:25 +01:00
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 formatter_from_file_extension(
2022-02-08 18:26:25 +01:00
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]]] = {
2022-02-08 19:01:18 +01:00
'dtx.pytex': DTXFormatter,
'sty.pytex': SimpleTeXFormatter,
'cls.pytex': SimpleTeXFormatter,
'dict.pytex': DictFormatter
2022-02-08 18:26:25 +01:00
}
# TODO: other formatters
2022-02-08 18:26:25 +01:00
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
)