From 237203b149324ec7895535d696c6edbe9c271976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Tue, 8 Feb 2022 16:34:59 +0100 Subject: [PATCH] fix some type errors --- PyTeX/format/config.py | 10 +++++++--- PyTeX/format/formatting_config.py | 9 +++++++++ PyTeX/format/pytex_formatter.py | 22 +++++++++++++--------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/PyTeX/format/config.py b/PyTeX/format/config.py index 7562382..0f8c878 100644 --- a/PyTeX/format/config.py +++ b/PyTeX/format/config.py @@ -44,10 +44,14 @@ class Config: config.set_from_json(json_content) return config + @classmethod - def from_yaml(cls, path: Path): - with open(path, 'r') as file: - json_content: Dict = yaml.safe_load(file) + def from_yaml(cls, content: Union[Path, str]): + if isinstance(content, Path): + with open(content, 'r') as file: + json_content: Dict = yaml.safe_load(file) + else: + json_content = yaml.safe_load(content) return cls.from_json(json_content) def set_from_json(self, content: Optional[Dict]): diff --git a/PyTeX/format/formatting_config.py b/PyTeX/format/formatting_config.py index e1bd3da..7d43213 100644 --- a/PyTeX/format/formatting_config.py +++ b/PyTeX/format/formatting_config.py @@ -81,6 +81,8 @@ class FormattingConfig(Config): self._tex_type: Optional[TeXType] = None self._tex_flavour: Optional[TeXFlavour] = None + self._escape_character: Optional[str] = None + def set_from_json(self, content: Optional[Dict]): filled_content = self._fill_keys(content) @@ -381,6 +383,13 @@ class FormattingConfig(Config): def tex_dependencies(self, dependencies: List[str]): self._tex_dependencies = dependencies + @property + def escape_character(self) -> str: + if self._escape_character is None: + return '!' + else: + return self._escape_character + class DocFormattingConfig: def __init__(self): diff --git a/PyTeX/format/pytex_formatter.py b/PyTeX/format/pytex_formatter.py index 986156a..06f4ca9 100644 --- a/PyTeX/format/pytex_formatter.py +++ b/PyTeX/format/pytex_formatter.py @@ -45,19 +45,19 @@ class PyTeXFormatter(FormatterIF): infile_config = self.parse_infile_config() self._config = infile_config.merge_with(self._config) - def parse_file_config(self) -> Optional[FormattingConfig]: + def parse_file_config(self) -> FormattingConfig: config_file = self.input_file.with_name(self.input_file.name + PYTEX_CONFIG_FILE_EXTENSION) if config_file.exists(): - with open(config_file, 'r') as file: - config = file.readlines() try: - return FormattingConfig.from_yaml(config) + return FormattingConfig.from_yaml(config_file) except: raise NotImplementedError # Invalid yaml file format else: - return None + return FormattingConfig() - def parse_infile_config(self) -> Optional[FormattingConfig]: + def parse_infile_config(self) -> FormattingConfig: + if self._input_file is None: + raise NotImplementedError # no file initialised yet with open(self._input_file, "r") as file: line = file.readline() if re.match(self.config.escape_character + INFILE_CONFIG_BEGIN_CONFIG, line): @@ -79,11 +79,11 @@ class PyTeXFormatter(FormatterIF): raise NotImplementedError # No matching end block config.append(line.lstrip('%').rstrip()) try: - return FormattingConfig.from_yaml(config) + return FormattingConfig.from_yaml('\n'.join(config)) except: raise NotImplementedError # Invalid yaml file format else: - return None + return FormattingConfig() @property def config(self) -> FormattingConfig: @@ -91,12 +91,16 @@ class PyTeXFormatter(FormatterIF): raise NotImplementedError return self._config + @config.setter + def config(self, formatting_config: FormattingConfig): + self._config = formatting_config + @property def header(self) -> GenericText: if self._header is None: if not ( self.config.include_extra_header - or self.config.include_build_time + or self.config.include_time or self.config.include_pytex_version or self.config.include_pytex_info_text or self.config.include_repo_version