add attribute dict to pytex formatters to query attributes used in formatting

This commit is contained in:
Maximilian Keßler 2022-02-09 15:38:01 +01:00
parent 7b689255bc
commit c2e2a3fad5
3 changed files with 81 additions and 19 deletions

View file

@ -9,11 +9,11 @@ class NamingScheme(Enum):
class TeXType(Enum):
TeXPackage = 'TeXPackage'
TeXClass = 'TeXClass'
TeXDocstrip = 'TeXDocstrip'
TeXDictionary = 'TeXDictionary'
TeXDocumentation = 'TeXDocumentation'
TeXPackage = 'package'
TeXClass = 'class'
TeXDocstrip = 'docstrip file'
TeXDictionary = 'dictionary'
TeXDocumentation = 'documentation'
@staticmethod
def parse(tex_type: str) -> Optional[TeXType]:
@ -72,11 +72,21 @@ class FormatterProperty(MacroReplacementAtomIF, Enum):
shortauthor = 'shortauthor'
date = 'date'
year = 'year'
version = 'version',
source_file_name = 'source_file_name'
raw_name = 'raw_name' # The 'raw' name of the package, without author prefix
name = 'name' # class or package name
version = 'version'
file_name = 'file_name'
source_file_name = 'source_file_name'
repo_version = 'repo_version'
repo_branch = 'repo_branch'
repo_commit = 'repo_commit'
repo_dirty = 'repo_dirty'
pytex_version = 'pytex_version'
pytex_branch = 'pytex_branch'
pytex_commit = 'pytex_commit'
pytex_dirty = 'pytex_dirty'
tex_type = 'tex_type'
tex_flavour = 'latex_flavour'
class Argument(MacroReplacementAtomIF, Enum):
@ -85,4 +95,4 @@ class Argument(MacroReplacementAtomIF, Enum):
three = 3
four = 4
five = 5
six = 6
six = 6

View file

@ -1,6 +1,6 @@
import re
from pathlib import Path
from typing import Optional, Dict
from typing import Optional, Dict, TextIO
from .constants import *
from .formatterif import FormatterIF
@ -8,8 +8,9 @@ from .formatting_config import FormattingConfig
from .git_version_info import GitVersionInfo
from .generic_text import GenericText
from ..logger import logger
from abc import ABC
from abc import ABC, abstractmethod
from .enums import *
from datetime import *
class PyTeXFormatter(FormatterIF, ABC):
def __init__(
@ -43,6 +44,8 @@ class PyTeXFormatter(FormatterIF, ABC):
if allow_infile_config:
infile_config = self.parse_infile_config()
self._config = infile_config.merge_with(self.config)
self._output_file: Optional[TextIO] = None # This may change over time in case of multiple output files
self._attribute_dict: Optional[Dict] = None
def parse_file_config(self) -> FormattingConfig:
config_file = self.input_file.with_name(self.input_file.name + PYTEX_CONFIG_FILE_EXTENSION)
@ -94,6 +97,13 @@ class PyTeXFormatter(FormatterIF, ABC):
def config(self, formatting_config: FormattingConfig):
self._config = formatting_config
@property
def git_version_info(self) -> GitVersionInfo:
if self._git_version_info is None:
return GitVersionInfo()
else:
return self._git_version_info
@property
def header(self) -> GenericText:
if self._header is None:
@ -120,16 +130,59 @@ class PyTeXFormatter(FormatterIF, ABC):
return self._header
def _update_attribute_dict(self):
self._attribute_dict: Dict[str, str] = {
FormatterProperty.author.value: self.config.author,
FormatterProperty.shortauthor.value: self.shortauthor,
FormatterProperty.date.value: datetime.now().strftime('%Y/%m/%d'),
FormatterProperty.year.value: datetime.now().strftime('%Y'),
FormatterProperty.raw_name.value: self.raw_name,
FormatterProperty.name.value: self.name,
FormatterProperty.version.value: self.config.version,
FormatterProperty.file_name.value: self.current_file_name,
FormatterProperty.source_file_name.value: self._input_file.name,
FormatterProperty.repo_version.value: self.git_version_info.repo_version.version,
FormatterProperty.repo_branch.value: self.git_version_info.repo_version.branch,
FormatterProperty.repo_commit.value: self.git_version_info.repo_version.commit_hash,
FormatterProperty.repo_dirty.value: self.git_version_info.repo_version.dirty,
FormatterProperty.pytex_version.value: self.git_version_info.pytex_version.version,
FormatterProperty.pytex_branch.value: self.git_version_info.pytex_version.branch,
FormatterProperty.pytex_commit.value: self.git_version_info.pytex_version.commit_hash,
FormatterProperty.pytex_dirty.value: self.git_version_info.pytex_version.dirty,
FormatterProperty.tex_type.value: str(self.config.tex_type),
FormatterProperty.tex_flavour.value: str(self.config.tex_flavour),
}
@property
def attribute_dict(self) -> Dict:
return {
FormatterProperty.author.value: self.config.author,
FormatterProperty.shortauthor.value: '',
FormatterProperty.version.value: self.config.version,
FormatterProperty.source_file_name.value: self._input_file.name,
FormatterProperty.name.value: '',
}
# TODO
if self._attribute_dict is None:
self._update_attribute_dict()
return self._attribute_dict
@property
def shortauthor(self) -> str:
parts = self.config.author.replace('ß', 'ss').split(' ') # TODO: better non-alphanumeric handling
if len(parts) == 1:
return parts[0]
else:
return parts[0][0] + parts[-1]
@property
def raw_name(self) -> str:
try:
return self._input_file.name.split('.', maxsplit=1)[0]
except:
raise NotImplementedError
@property
def name(self):
if self.config.naming_scheme == NamingScheme.prepend_author:
return self.shortauthor + '-' + self.raw_name
else:
return self.raw_name
def current_file_name(self):
return self._output_file.name
def make_header(self, **kwargs) -> str:
return '\n'.join(

View file

@ -61,7 +61,6 @@ class TexFormatter(PyTeXFormatter, ABC):
super().__init__(*args, **kwargs)
self._macros: List[Macro] = []
self._line_stream: Optional[LineStream] = None
self._output_file: Optional[TextIO] = None
@abstractmethod
def open_output_stream(self, build_dir: Path) -> None: