fix some type errors

This commit is contained in:
Maximilian Keßler 2022-02-08 16:34:59 +01:00
parent 582d6670b8
commit 237203b149
3 changed files with 29 additions and 12 deletions

View file

@ -44,10 +44,14 @@ class Config:
config.set_from_json(json_content) config.set_from_json(json_content)
return config return config
@classmethod @classmethod
def from_yaml(cls, path: Path): def from_yaml(cls, content: Union[Path, str]):
with open(path, 'r') as file: if isinstance(content, Path):
json_content: Dict = yaml.safe_load(file) 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) return cls.from_json(json_content)
def set_from_json(self, content: Optional[Dict]): def set_from_json(self, content: Optional[Dict]):

View file

@ -81,6 +81,8 @@ class FormattingConfig(Config):
self._tex_type: Optional[TeXType] = None self._tex_type: Optional[TeXType] = None
self._tex_flavour: Optional[TeXFlavour] = None self._tex_flavour: Optional[TeXFlavour] = None
self._escape_character: Optional[str] = None
def set_from_json(self, content: Optional[Dict]): def set_from_json(self, content: Optional[Dict]):
filled_content = self._fill_keys(content) filled_content = self._fill_keys(content)
@ -381,6 +383,13 @@ class FormattingConfig(Config):
def tex_dependencies(self, dependencies: List[str]): def tex_dependencies(self, dependencies: List[str]):
self._tex_dependencies = dependencies self._tex_dependencies = dependencies
@property
def escape_character(self) -> str:
if self._escape_character is None:
return '!'
else:
return self._escape_character
class DocFormattingConfig: class DocFormattingConfig:
def __init__(self): def __init__(self):

View file

@ -45,19 +45,19 @@ class PyTeXFormatter(FormatterIF):
infile_config = self.parse_infile_config() infile_config = self.parse_infile_config()
self._config = infile_config.merge_with(self._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) config_file = self.input_file.with_name(self.input_file.name + PYTEX_CONFIG_FILE_EXTENSION)
if config_file.exists(): if config_file.exists():
with open(config_file, 'r') as file:
config = file.readlines()
try: try:
return FormattingConfig.from_yaml(config) return FormattingConfig.from_yaml(config_file)
except: except:
raise NotImplementedError # Invalid yaml file format raise NotImplementedError # Invalid yaml file format
else: 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: with open(self._input_file, "r") as file:
line = file.readline() line = file.readline()
if re.match(self.config.escape_character + INFILE_CONFIG_BEGIN_CONFIG, line): 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 raise NotImplementedError # No matching end block
config.append(line.lstrip('%').rstrip()) config.append(line.lstrip('%').rstrip())
try: try:
return FormattingConfig.from_yaml(config) return FormattingConfig.from_yaml('\n'.join(config))
except: except:
raise NotImplementedError # Invalid yaml file format raise NotImplementedError # Invalid yaml file format
else: else:
return None return FormattingConfig()
@property @property
def config(self) -> FormattingConfig: def config(self) -> FormattingConfig:
@ -91,12 +91,16 @@ class PyTeXFormatter(FormatterIF):
raise NotImplementedError raise NotImplementedError
return self._config return self._config
@config.setter
def config(self, formatting_config: FormattingConfig):
self._config = formatting_config
@property @property
def header(self) -> GenericText: def header(self) -> GenericText:
if self._header is None: if self._header is None:
if not ( if not (
self.config.include_extra_header 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_version
or self.config.include_pytex_info_text or self.config.include_pytex_info_text
or self.config.include_repo_version or self.config.include_repo_version