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)
return config
@classmethod
def from_yaml(cls, path: Path):
with open(path, 'r') as 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]):

View file

@ -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):

View file

@ -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