From b168b4f7fc357e41c2c5692fce81cd7bd1a5b2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Fri, 18 Feb 2022 16:23:43 +0100 Subject: [PATCH] add more general formatter selection interface --- PyTeX/build/build/builder.py | 3 +- PyTeX/build/build/enums.py | 11 ++++++- PyTeX/build/build/pytex_file.py | 7 +++-- PyTeX/format/auto_format.py | 52 ++++++++++++++++++++++++++------- PyTeX/format/enums.py | 6 ++++ 5 files changed, 64 insertions(+), 15 deletions(-) diff --git a/PyTeX/build/build/builder.py b/PyTeX/build/build/builder.py index e32fe0a..29098fb 100644 --- a/PyTeX/build/build/builder.py +++ b/PyTeX/build/build/builder.py @@ -166,7 +166,8 @@ class PyTeXBuilder: file ), default_config=self.pytex_config.default_formatting_config, - git_version_info=self._git_version_info + git_version_info=self._git_version_info, + target=self._build_target_type.to_target() ) ) diff --git a/PyTeX/build/build/enums.py b/PyTeX/build/build/enums.py index 255a88e..2354b34 100644 --- a/PyTeX/build/build/enums.py +++ b/PyTeX/build/build/enums.py @@ -1,5 +1,7 @@ from enum import Enum +from PyTeX.format.enums import Target + class PyTeXRootDirType(Enum): BUILD = 1 @@ -7,9 +9,16 @@ class PyTeXRootDirType(Enum): DOC = 3 TEX_SOURCE = 4 + def to_target(self) -> Target: + return { + PyTeXRootDirType.TEX_SOURCE: Target.tex_source, + PyTeXRootDirType.DOC: Target.documentation, + PyTeXRootDirType.BUILD: Target.tex + }[self] + class PyTeXFileType(Enum): PyTeXSourceFile = 'PyTeXSourceFile' TeXSourceFile = 'TeXSourceFile' TeXOutputFile = 'TeXOutputFile' - TeXDocumentationFile = 'TeXDocumentationFile' \ No newline at end of file + TeXDocumentationFile = 'TeXDocumentationFile' diff --git a/PyTeX/build/build/pytex_file.py b/PyTeX/build/build/pytex_file.py index 171f551..6e43993 100644 --- a/PyTeX/build/build/pytex_file.py +++ b/PyTeX/build/build/pytex_file.py @@ -6,6 +6,7 @@ from PyTeX.format.formatterif import FormatterIF from PyTeX.build.build.enums import PyTeXFileType from .hashing import md5 from ...exceptions import PyTeXException +from ...format.enums import Target from ...format.errors import PyTeXError from ...format.formatting_config import FormattingConfig from ...format.auto_format import formatter_from_file_extension @@ -19,7 +20,8 @@ class PyTeXSourceFile: formatter: Optional[FormatterIF] = None, default_config: Optional[FormattingConfig] = None, git_version_info: Optional[GitVersionInfo] = None, - pytex_file_type: Optional[PyTeXFileType] = None + pytex_file_type: Optional[PyTeXFileType] = None, + target: Optional[Target] = None ): self._relative_path: RelativePath = relative_path if formatter is not None: @@ -30,7 +32,8 @@ class PyTeXSourceFile: config=default_config, git_version_info=git_version_info, locate_file_config=True, - allow_infile_config=True + allow_infile_config=True, + target=target ) self._pytex_file_type: Optional[PyTeXFileType] = pytex_file_type self._file_hash: Optional[str] = None diff --git a/PyTeX/format/auto_format.py b/PyTeX/format/auto_format.py index 6ffacc7..e48d981 100644 --- a/PyTeX/format/auto_format.py +++ b/PyTeX/format/auto_format.py @@ -8,8 +8,11 @@ from .simple_tex_formatter import SimpleTeXFormatter from .dtx_formatter import DTXFormatter from .pytex_formatter import PyTeXFormatter from .git_version_info import GitVersionInfo +from .docstrip_formatter import DocStripFormatter +from .nothing_formatter import NothingFormatter +from .copy_formatter import CopyFormatter from .default_macros import get_default_macros -from .enums import TeXType +from .enums import TeXType, Target def formatter_from_file_extension( @@ -19,26 +22,53 @@ def formatter_from_file_extension( locate_file_config: bool = True, allow_infile_config: bool = True, default_macros: bool = True, + target: Optional[Target] = None, ) -> PyTeXFormatter: - switcher: Dict[str, Type[Union[DTXFormatter, SimpleTeXFormatter, DictFormatter]]] = { + + extension_switcher: Dict[str, TeXType] = { + 'dtx.pytex': TeXType.TeXDocstrip, + 'dtx': TeXType.TeXDocstrip, + 'sty.pytex': TeXType.TeXPackage, + 'sty': TeXType.TeXPackage, + 'cls.pytex': TeXType.TeXClass, + 'cls': TeXType.TeXClass, + 'dict.pytex': TeXType.TeXDictionary, + 'dict': TeXType.TeXDictionary, + 'tex.pytex': TeXType.TeXDocumentation, + 'tex': TeXType.TeXDocumentation, + } + source_formatter_switcher: Dict[str, Type[PyTeXFormatter]] = { 'dtx.pytex': DTXFormatter, 'sty.pytex': SimpleTeXFormatter, 'cls.pytex': SimpleTeXFormatter, 'dict.pytex': DictFormatter } - switcher2: Dict[str, TeXType] = { - 'dtx.pytex': TeXType.TeXDocstrip, - 'sty.pytex': TeXType.TeXPackage, - 'cls.pytex': TeXType.TeXClass, - 'dict.pytex': TeXType.TeXDictionary + tex_formatter_switcher = { + '.ins': NothingFormatter, + '.drv': NothingFormatter, + '.dtx': DocStripFormatter, + '.dict': CopyFormatter, + '.cls': CopyFormatter, + '.sty': CopyFormatter, } - # TODO: other formatters + documentation_formatter_switcher = { + + } + + if target == Target.tex_source: + switcher = source_formatter_switcher + elif target == Target.tex: + switcher = tex_formatter_switcher + elif target == Target.documentation: + switcher = documentation_formatter_switcher + else: + switcher = source_formatter_switcher | tex_formatter_switcher # Default case try: [name, extension] = input_file.name.split('.', maxsplit=1) - except: + except ValueError: raise NotImplementedError - config.tex_type = switcher2[extension] # This sets the textype from file extension + config.tex_type = extension_switcher[extension] # This sets the textype from file extension formatter = switcher[extension]( input_file=input_file, @@ -47,7 +77,7 @@ def formatter_from_file_extension( locate_file_config=locate_file_config, allow_infile_config=allow_infile_config ) - if default_macros: + if default_macros and target == Target.tex_source: formatter.macros = get_default_macros(formatter.config.tex_flavour, formatter.config.tex_type) return formatter diff --git a/PyTeX/format/enums.py b/PyTeX/format/enums.py index df741d4..1d6ec02 100644 --- a/PyTeX/format/enums.py +++ b/PyTeX/format/enums.py @@ -148,3 +148,9 @@ class Argument(MacroReplacementAtomIF, Enum): four = 4 five = 5 six = 6 + + +class Target(Enum): + tex_source = 1 + tex = 2 + documentation = 3