fix future config handling, use abstract classes

This commit is contained in:
Maximilian Keßler 2022-02-08 17:21:36 +01:00
parent 1a6989734e
commit 6cd8d9655d
4 changed files with 17 additions and 9 deletions

View file

@ -5,6 +5,7 @@ from typing import List, Dict, Tuple, Optional
from .constants import * from .constants import *
from .pytex_formatter import PyTeXFormatter from .pytex_formatter import PyTeXFormatter
from ..logger import logger from ..logger import logger
from .formatting_config import FormattingConfig
class DictFormatter(PyTeXFormatter): class DictFormatter(PyTeXFormatter):
@ -21,6 +22,9 @@ class DictFormatter(PyTeXFormatter):
self._dict_name = self.input_file.name.split('.')[0] self._dict_name = self.input_file.name.split('.')[0]
self._translations = None self._translations = None
def dependencies(self) -> List[str]:
return [] # No dependencies for dictionaries
@property @property
def translations(self) -> Dict: def translations(self) -> Dict:
if self._translations is None: if self._translations is None:
@ -50,7 +54,7 @@ class DictFormatter(PyTeXFormatter):
translations[self._languages[n]][line[0]] = line[n] translations[self._languages[n]][line[0]] = line[n]
return translations return translations
def format(self, build_dir: Path, overwrite: bool = False) -> Optional[List[Tuple[str, Dict]]]: def format(self, build_dir: Path, overwrite: bool = False) -> List[Tuple[str, FormattingConfig]]:
build_dir.mkdir(parents=True, exist_ok=True) build_dir.mkdir(parents=True, exist_ok=True)
self.make_header() self.make_header()
for language in self._languages: for language in self._languages:
@ -86,4 +90,4 @@ class DictFormatter(PyTeXFormatter):
logger.info( logger.info(
f'Successfully wrote dictionary file {output_file.name}.' f'Successfully wrote dictionary file {output_file.name}.'
) )
return None # No future configuration needed return [] # No future configuration needed

View file

@ -3,6 +3,7 @@ from typing import List, Optional, Dict, Tuple
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from .config import Config from .config import Config
from .formatting_config import FormattingConfig
class FormatterIF(ABC): class FormatterIF(ABC):
@ -20,7 +21,7 @@ class FormatterIF(ABC):
self._config: Optional[Config] = config self._config: Optional[Config] = config
@abstractmethod @abstractmethod
def format(self, build_dir: Path, overwrite: bool = False) -> Optional[List[Tuple[str, Dict]]]: def format(self, build_dir: Path, overwrite: bool = False) -> List[Tuple[str, FormattingConfig]]:
""" """
:param build_dir: Directory where output files are written to :param build_dir: Directory where output files are written to
:param overwrite: overwrite existing files :param overwrite: overwrite existing files
@ -44,6 +45,7 @@ class FormatterIF(ABC):
:return: List of dependencies (as str filenames) :return: List of dependencies (as str filenames)
""" """
@property @property
def input_file(self) -> Path: def input_file(self) -> Path:
if self._input_file is None: if self._input_file is None:

View file

@ -1,3 +1,4 @@
from .formatting_config import FormattingConfig
from .tex_formatter import TexFormatter from .tex_formatter import TexFormatter
from pathlib import Path from pathlib import Path
from typing import Optional, List, Tuple, Dict from typing import Optional, List, Tuple, Dict
@ -14,11 +15,11 @@ class SimpleTeXFormatter(TexFormatter):
def close_output_stream(self): def close_output_stream(self):
self._output_file.close() self._output_file.close()
def future_config(self) -> Optional[List[Tuple[str, Dict]]]: def future_config(self) -> List[Tuple[str, FormattingConfig]]:
pass return [] # TODO
def dependencies(self) -> List[str]: def dependencies(self) -> List[str]:
return [] return [] # TODO
def output_files(self) -> List[str]: def output_files(self) -> List[str]:
return [] return [self.input_file.with_suffix('').name]

View file

@ -2,6 +2,7 @@ from pathlib import Path
from typing import List, TextIO, Optional, Tuple, Dict from typing import List, TextIO, Optional, Tuple, Dict
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from .formatting_config import FormattingConfig
from .macros import Macro from .macros import Macro
from .pytex_formatter import PyTeXFormatter from .pytex_formatter import PyTeXFormatter
@ -76,7 +77,7 @@ class TexFormatter(PyTeXFormatter, ABC):
@abstractmethod @abstractmethod
@property @property
def future_config(self) -> Optional[List[Tuple[str, Dict]]]: def future_config(self) -> List[Tuple[str, FormattingConfig]]:
""" """
# TODO # TODO
:return: :return:
@ -135,7 +136,7 @@ class TexFormatter(PyTeXFormatter, ABC):
break break
self._shipout_line() self._shipout_line()
def format(self, build_dir: Path, overwrite: bool = False) -> Optional[List[Tuple[str, Dict]]]: def format(self, build_dir: Path, overwrite: bool = False) -> List[Tuple[str, FormattingConfig]]:
self.open_output_stream(build_dir) self.open_output_stream(build_dir)
self.format_pre_header() self.format_pre_header()
self.format_header() self.format_header()