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): class TeXType(Enum):
TeXPackage = 'TeXPackage' TeXPackage = 'package'
TeXClass = 'TeXClass' TeXClass = 'class'
TeXDocstrip = 'TeXDocstrip' TeXDocstrip = 'docstrip file'
TeXDictionary = 'TeXDictionary' TeXDictionary = 'dictionary'
TeXDocumentation = 'TeXDocumentation' TeXDocumentation = 'documentation'
@staticmethod @staticmethod
def parse(tex_type: str) -> Optional[TeXType]: def parse(tex_type: str) -> Optional[TeXType]:
@ -72,11 +72,21 @@ class FormatterProperty(MacroReplacementAtomIF, Enum):
shortauthor = 'shortauthor' shortauthor = 'shortauthor'
date = 'date' date = 'date'
year = 'year' year = 'year'
version = 'version', raw_name = 'raw_name' # The 'raw' name of the package, without author prefix
source_file_name = 'source_file_name'
name = 'name' # class or package name name = 'name' # class or package name
version = 'version'
file_name = 'file_name'
source_file_name = 'source_file_name'
repo_version = 'repo_version' repo_version = 'repo_version'
repo_branch = 'repo_branch'
repo_commit = 'repo_commit'
repo_dirty = 'repo_dirty'
pytex_version = 'pytex_version' 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): class Argument(MacroReplacementAtomIF, Enum):

View file

@ -1,6 +1,6 @@
import re import re
from pathlib import Path from pathlib import Path
from typing import Optional, Dict from typing import Optional, Dict, TextIO
from .constants import * from .constants import *
from .formatterif import FormatterIF from .formatterif import FormatterIF
@ -8,8 +8,9 @@ from .formatting_config import FormattingConfig
from .git_version_info import GitVersionInfo from .git_version_info import GitVersionInfo
from .generic_text import GenericText from .generic_text import GenericText
from ..logger import logger from ..logger import logger
from abc import ABC from abc import ABC, abstractmethod
from .enums import * from .enums import *
from datetime import *
class PyTeXFormatter(FormatterIF, ABC): class PyTeXFormatter(FormatterIF, ABC):
def __init__( def __init__(
@ -43,6 +44,8 @@ class PyTeXFormatter(FormatterIF, ABC):
if allow_infile_config: if allow_infile_config:
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)
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: 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)
@ -94,6 +97,13 @@ class PyTeXFormatter(FormatterIF, ABC):
def config(self, formatting_config: FormattingConfig): def config(self, formatting_config: FormattingConfig):
self._config = formatting_config 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 @property
def header(self) -> GenericText: def header(self) -> GenericText:
if self._header is None: if self._header is None:
@ -120,16 +130,59 @@ class PyTeXFormatter(FormatterIF, ABC):
return self._header 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 @property
def attribute_dict(self) -> Dict: def attribute_dict(self) -> Dict:
return { if self._attribute_dict is None:
FormatterProperty.author.value: self.config.author, self._update_attribute_dict()
FormatterProperty.shortauthor.value: '', return self._attribute_dict
FormatterProperty.version.value: self.config.version,
FormatterProperty.source_file_name.value: self._input_file.name, @property
FormatterProperty.name.value: '', def shortauthor(self) -> str:
} parts = self.config.author.replace('ß', 'ss').split(' ') # TODO: better non-alphanumeric handling
# TODO 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: def make_header(self, **kwargs) -> str:
return '\n'.join( return '\n'.join(

View file

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