add to generic text. dump formatting config to json

This commit is contained in:
Maximilian Keßler 2022-02-06 23:52:44 +01:00
parent cfc6be9cd6
commit d1ed1718c4
3 changed files with 135 additions and 20 deletions

View file

@ -4,3 +4,31 @@ PYTEX_CONFIG_FILE_EXTENSION = '.conf'
DICTIONARY_KEY_COLUMN_NAME = 'key' DICTIONARY_KEY_COLUMN_NAME = 'key'
DICTIONARY_NAMING_PATTERN = 'translator-{dict_name}-dictionary-{language}.dict' DICTIONARY_NAMING_PATTERN = 'translator-{dict_name}-dictionary-{language}.dict'
FORMATTER_PREFIX = '!' FORMATTER_PREFIX = '!'
YAML_INFO = 'info'
YAML_NAMING_SCHEME = 'name'
YAML_LICENSE = 'license'
YAML_INCLUDE_LICENSE = 'include'
YAML_DESCRIPTION = 'description'
YAML_EXTRA = 'extra'
YAML_HEADER = 'header'
YAML_INCLUDE_EXTRA_HEADER = 'include'
YAML_INCLUDE_BUILD_TIME = 'time'
YAML_INCLUDE_VERSION = 'version'
YAML_INCLUDE_INFO_TEXT = 'text'
YAML_INCLUDE_TIME = 'time'
YAML_AUTHOR = 'author'
YAML_VERSION = 'version'
YAML_PATH = 'path'
YAML_INCLUDE_DRV = 'drv'
YAML_INCLUDE_INS = 'ins'
YAML_DOCSTRIP_GUARDS = 'guards'
YAML_DEPENDENCIES = 'dependencies'
YAML_DOC_DEPENDENCIES = 'doc'
YAML_TEX_DEPENDENCIES = 'tex'
YAML_TEX_FLAVOUR = 'flavour'
YAML_TEX_TYPE = 'type'
YAML_TEXT = 'text'
YAML_REPO = 'repo'
YAML_PYTEX = 'pytex'
YAML_DOCSTRIP = 'docstrip'

View file

@ -6,6 +6,8 @@ from .enums import NamingScheme
from .generic_text import GenericText from .generic_text import GenericText
from .formatterif import Config from .formatterif import Config
from .git_version_info import GitVersionInfo from .git_version_info import GitVersionInfo
from .constants import *
from .enums import TeXType, TeXFlavour
class VersionInfo: class VersionInfo:
@ -35,12 +37,12 @@ class FormattingConfig(Config):
self._description: Optional[str] = None self._description: Optional[str] = None
self._include_extra_header: Optional[bool] = None self._include_extra_header: Optional[bool] = None
self._include_build_time: Optional[bool] = None
self._include_pytex_version: Optional[bool] = None self._include_pytex_version: Optional[bool] = None
self._include_pytex_info_text: Optional[bool] = None self._include_pytex_info_text: Optional[bool] = None
self._include_repo_version: Optional[bool] = None self._include_repo_version: Optional[bool] = None
self._include_repo_info_text: Optional[bool] = None self._include_repo_info_text: Optional[bool] = None
self._include_time: Optional[bool] = None self._include_time: Optional[bool] = None
self._include_license: Optional[bool] = None
self._extra_header: Optional[GenericText] = None self._extra_header: Optional[GenericText] = None
self._author: Optional[str] = None self._author: Optional[str] = None
@ -55,6 +57,12 @@ class FormattingConfig(Config):
self._doc_dependencies: Optional[List[str]] = None self._doc_dependencies: Optional[List[str]] = None
self._tex_dependencies: Optional[List[str]] = None self._tex_dependencies: Optional[List[str]] = None
self._tex_type: Optional[TeXType] = None
self._tex_flavour: Optional[TeXFlavour] = None
def set_from_json(self, content: Dict):
pass
@classmethod @classmethod
def from_yaml(cls, content: Path): def from_yaml(cls, content: Path):
with open(content, 'r') as config: with open(content, 'r') as config:
@ -66,11 +74,60 @@ class FormattingConfig(Config):
if isinstance(content, Path): if isinstance(content, Path):
with open(content, 'r') as config: with open(content, 'r') as config:
content: Dict = json.load(config) content: Dict = json.load(config)
config = FormattingConfig()
config.set_from_json(content)
return config
def to_yaml(self) -> str: def to_yaml(self) -> str:
pass pass
def to_json(self) -> Dict:
return {
YAML_INFO: {
YAML_AUTHOR: self._author,
YAML_NAMING_SCHEME: self._naming_scheme,
YAML_TEX_FLAVOUR: self._tex_flavour,
YAML_TEX_TYPE: self._tex_type,
YAML_VERSION: self._version
},
YAML_HEADER: {
YAML_EXTRA: {
YAML_INCLUDE_EXTRA_HEADER: self._include_extra_header,
YAML_PATH: self._extra_header.path,
YAML_TEXT: self._extra_header.real_text
},
YAML_REPO: {
YAML_INCLUDE_INFO_TEXT: self._include_repo_info_text,
YAML_INCLUDE_VERSION: self._include_repo_version,
YAML_PATH: self._repo_info_text.path,
YAML_TEXT: self._repo_info_text.real_text
},
YAML_PYTEX: {
YAML_INCLUDE_INFO_TEXT: self._include_pytex_info_text,
YAML_INCLUDE_VERSION: self._include_pytex_version,
YAML_PATH: self._pytex_info_text.path,
YAML_TEXT: self._pytex_info_text.real_text
},
YAML_INCLUDE_TIME: self._include_time,
YAML_LICENSE: {
YAML_INCLUDE_LICENSE: self._include_license,
YAML_PATH: self._license.path,
YAML_TEXT: self._license.real_text
},
YAML_DESCRIPTION: self._description
},
YAML_DOCSTRIP: {
YAML_INCLUDE_DRV: self._include_drv,
YAML_INCLUDE_INS: self._include_ins,
YAML_DOCSTRIP_GUARDS: self._docstrip_guards
},
YAML_DEPENDENCIES: {
YAML_DOC_DEPENDENCIES: self._doc_dependencies,
YAML_TEX_DEPENDENCIES: self._tex_dependencies
}
}
@property @property
def naming_scheme(self) -> NamingScheme: def naming_scheme(self) -> NamingScheme:
if self._naming_scheme is None: if self._naming_scheme is None:
@ -85,7 +142,7 @@ class FormattingConfig(Config):
@property @property
def license(self) -> GenericText: def license(self) -> GenericText:
if self._license is None: if self._license is None:
return GenericText([]) return GenericText()
else: else:
return self._license return self._license
@ -162,7 +219,7 @@ class FormattingConfig(Config):
@property @property
def extra_header(self) -> GenericText: def extra_header(self) -> GenericText:
if self._extra_header is None: if self._extra_header is None:
return GenericText([]) return GenericText()
else: else:
return self._extra_header return self._extra_header
@ -195,7 +252,7 @@ class FormattingConfig(Config):
@property @property
def pytex_info_text(self) -> GenericText: def pytex_info_text(self) -> GenericText:
if self._pytex_info_text is None: if self._pytex_info_text is None:
return GenericText([]) return GenericText()
else: else:
return self._pytex_info_text return self._pytex_info_text
@ -206,7 +263,7 @@ class FormattingConfig(Config):
@property @property
def repo_info_text(self) -> GenericText: def repo_info_text(self) -> GenericText:
if self._repo_info_text is None: if self._repo_info_text is None:
return GenericText([]) return GenericText()
else: else:
return self._repo_info_text return self._repo_info_text

View file

@ -4,41 +4,57 @@ from ..logger import logger
class GenericText: class GenericText:
def __init__(self, content: Union[List[str], Path]): def __init__(self, content: Optional[Union[List[str], Path]] = None):
if isinstance(content, List): if isinstance(content, List):
self._content: Optional[List[str]] = content self._content: Optional[List[str]] = content
self._path = None self._path = None
else: self._initialized = True
if isinstance(content, Path):
self._content: Optional[List[str]] = None self._content: Optional[List[str]] = None
self._path = content self._path = content
self._initialized = True
else:
self._content = None
self._path = None
self._initialized = False
@property @property
def text(self) -> List[str]: def text(self) -> List[str]:
if self._content is None: if self._initialized:
try: if self._content is None:
with open(self._path, 'r') as file: try:
self._content = file.readlines() with open(self._path, 'r') as file:
except FileNotFoundError: self._content = file.readlines()
raise NotImplementedError except FileNotFoundError:
except: raise NotImplementedError
raise NotImplementedError except:
return self._content raise NotImplementedError
return self._content
else:
return []
@text.setter @text.setter
def text(self, content: Union[List[str], Path]) -> None: def text(self, content: Union[List[str], Path, None]) -> None:
if isinstance(content, List): if isinstance(content, List):
self._content = content self._content = content
self._path = None self._path = None
else: self._initialized = True
elif isinstance(content, Path):
self._content = None self._content = None
self._path = content self._path = content
self._initialized = True
else:
self._content = None
self._path = None
self._initialized = False
@property
def path(self) -> Optional[Path]: def path(self) -> Optional[Path]:
return self._path return self._path
def format(self, **kwargs) -> str: def format(self, **kwargs) -> str:
lines = [] lines = []
for line in self._content: for line in self.text:
try: try:
line = '% ' + line.format(**kwargs).rjust(77) + '%' line = '% ' + line.format(**kwargs).rjust(77) + '%'
if len(line) > 80: if len(line) > 80:
@ -49,7 +65,21 @@ class GenericText:
raise NotImplementedError raise NotImplementedError
return '\n'.join(lines) return '\n'.join(lines)
def has_value(self) -> bool:
return self._initialized
@property
def real_text(self) -> Optional[List[str]]:
if self.has_value():
return self._content
else:
return None
def __add__(self, other): def __add__(self, other):
if not self.has_value():
return other
if not other.has_value():
return self
if isinstance(other, GenericText): if isinstance(other, GenericText):
return GenericText(self.text + other.text) return GenericText(self.text + other.text)
else: else: