better formatting config properties

This commit is contained in:
Maximilian Keßler 2022-02-06 15:09:25 +01:00
parent 406bf0bf7d
commit eccf857d19

View file

@ -1,35 +1,15 @@
from typing import List, Optional, Union, Any, Tuple from typing import List, Optional, Union, Any, Tuple
from .enums import NamingScheme, License from .enums import NamingScheme, License
from .generic_text import GenericText from .generic_text import GenericText
from .formatterif import Config
from .git_version_info import GitVersionInfo
def generate_properties(attributes: List[Union[str, Tuple[str, Any]]]): class FormattingConfig(Config):
attributes = [
x if isinstance(x, Tuple) else (x, None) for x in attributes
]
def decorator(cls):
for [attribute, default_value] in attributes:
def get_attr(self, attribute=attribute, default_value=default_value):
if getattr(self, "_" + attribute) is not None:
return getattr(self, "_" + attribute)
else:
return default_value
prop = property(get_attr)
setattr(cls, attribute, prop)
return cls
return decorator
@generate_properties([('naming_scheme', False), 'license'])
class FormattingConfig:
def __init__(self): def __init__(self):
self._naming_scheme: Optional[Union[NamingScheme, str]] = None self._naming_scheme: Optional[Union[NamingScheme, str]] = None
self._license: Optional[List[License]] = None self._license: Optional[List[License]] = None
self._extra_header: Optional[List[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_build_time: Optional[bool] = None
self._include_pytex_version: Optional[bool] = None self._include_pytex_version: Optional[bool] = None
@ -37,19 +17,20 @@ class FormattingConfig:
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_drv: Optional[bool] = True self._extra_header: Optional[List[str]] = None
self._include_ins: Optional[bool] = True
self._use_docstrip_guards: Optional[List[str]] = None
self._author: Optional[str] = None self._author: Optional[str] = None
self._licenses = Optional[List[GenericText]] self._licenses = Optional[List[GenericText]]
self._version: Optional[str] = None self._version: Optional[str] = None
self._extra_header_file: Optional[GenericText] = None self._extra_header_file: Optional[GenericText] = None
self._pytex_version: Optional[str] = None self._pytex_version: Optional[GitVersionInfo] = None
self._pytex_info_text: Optional[GenericText] = None self._pytex_info_text: Optional[GenericText] = None
self._repo_version: Optional[str] = None self._repo_version: Optional[GitVersionInfo] = None
self._repo_info_text: Optional[GenericText] = None self._repo_info_text: Optional[GenericText] = None
self._include_drv: Optional[bool] = True
self._include_ins: Optional[bool] = True
self._use_docstrip_guards: Optional[List[str]] = None
def merge_with(self, other, strict: bool = False): def merge_with(self, other, strict: bool = False):
""" """
Merges the other config into this one Merges the other config into this one
@ -65,6 +46,150 @@ class FormattingConfig:
raise NotImplementedError raise NotImplementedError
return self return self
@property
def naming_scheme(self) -> NamingScheme:
if self._naming_scheme is None:
return NamingScheme.prepend_author
else:
return self._naming_scheme
@property
def license(self) -> list:
if self._license is None:
return []
else:
return self._license
@property
def include_extra_header(self) -> bool:
if self._include_extra_header is None:
return False
else:
return self._include_extra_header
@property
def include_build_time(self) -> bool:
if self._include_build_time is None:
return False
else:
return self._include_build_time
@property
def include_pytex_version(self) -> bool:
if self._include_pytex_version is None:
return False
else:
return self._include_pytex_version
@property
def include_pytex_info_text(self) -> bool:
if self._include_pytex_info_text is None:
return False
else:
return self._include_pytex_info_text
@property
def include_repo_version(self) -> bool:
if self._include_repo_version is None:
return False
else:
return self._include_repo_version
@property
def include_repo_info_text(self) -> bool:
if self._include_repo_info_text is None:
return False
else:
return self._include_repo_info_text
@property
def extra_header(self) -> list:
if self._extra_header is None:
return []
else:
return self._extra_header
@property
def author(self) -> str:
if self._author is None:
return "MISSING AUTHOR"
else:
return self._author
@property
def licenses(self) -> GenericText:
if self._licenses is None:
return GenericText([])
else:
return self._licenses
@property
def version(self) -> str:
if self._version is None:
return "0.0.0"
else:
return self._version
@property
def extra_header_file(self) -> GenericText:
if self._extra_header_file is None:
return GenericText([])
else:
return self._extra_header_file
@property
def pytex_version(self) -> Optional[GitVersionInfo]:
return self._pytex_version
@property
def extra_header_file(self) -> GenericText:
if self._extra_header_file is None:
return GenericText([])
else:
return self._extra_header_file
@property
def pytex_info_text(self) -> GenericText:
if self._pytex_info_text is None:
return GenericText([])
else:
return self._pytex_info_text
@property
def repo_version(self) -> Optional[GitVersionInfo]:
if self._repo_version is None:
return None
else:
return self._repo_version
@property
def repo_info_text(self) -> GenericText:
if self._repo_info_text is None:
return GenericText([])
else:
return self._repo_info_text
@property
def include_drv(self) -> bool:
if self._include_drv is None:
return False
else:
return self._include_drv
@property
def include_ins(self) -> bool:
if self._include_ins is None:
return False
else:
return self._include_ins
@property
def use_docstrip_guards(self) -> list:
if self._use_docstrip_guards is None:
return []
else:
return self._use_docstrip_guards
class DocFormattingConfig: class DocFormattingConfig:
def __init__(self): def __init__(self):