pytex/PyTeX/format/formatting_config.py

73 lines
2.6 KiB
Python
Raw Normal View History

2022-02-06 00:02:59 +01:00
from typing import List, Optional, Union, Any, Tuple
2022-02-04 11:39:15 +01:00
from .enums import NamingScheme, License
2022-02-05 22:40:10 +01:00
from .generic_text import GenericText
2022-02-04 11:39:15 +01:00
2022-02-06 00:02:59 +01:00
def generate_properties(attributes: List[Union[str, Tuple[str, Any]]]):
attributes = [
x if isinstance(x, Tuple) else (x, None) for x in attributes
]
2022-02-05 23:54:45 +01:00
def decorator(cls):
2022-02-06 00:02:59 +01:00
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
2022-02-05 23:54:45 +01:00
prop = property(get_attr)
2022-02-06 00:02:59 +01:00
setattr(cls, attribute, prop)
2022-02-05 23:54:45 +01:00
return cls
return decorator
2022-02-06 00:02:59 +01:00
@generate_properties([('naming_scheme', False), 'license'])
2022-02-05 19:36:42 +01:00
class FormattingConfig:
2022-02-04 11:39:15 +01:00
def __init__(self):
2022-02-05 23:54:45 +01:00
self._naming_scheme: Optional[Union[NamingScheme, str]] = None
2022-02-04 11:39:15 +01:00
self._license: Optional[List[License]] = None
2022-02-05 23:54:45 +01:00
self._extra_header: Optional[List[str]] = 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_info_text: Optional[bool] = None
self._include_repo_version: Optional[bool] = None
self._include_repo_info_text: Optional[bool] = None
2022-02-04 11:39:15 +01:00
2022-02-05 23:54:45 +01:00
self._include_drv: Optional[bool] = True
self._include_ins: Optional[bool] = True
2022-02-04 11:46:06 +01:00
self._use_docstrip_guards: Optional[List[str]] = None
2022-02-04 11:39:15 +01:00
2022-02-05 22:40:10 +01:00
self._author: Optional[str] = None
self._licenses = Optional[List[GenericText]]
2022-02-05 23:54:45 +01:00
self._version: Optional[str] = None
2022-02-05 22:40:10 +01:00
self._extra_header_file: Optional[GenericText] = None
self._pytex_version: Optional[str] = None
self._pytex_info_text: Optional[GenericText] = None
self._repo_version: Optional[str] = None
self._repo_info_text: Optional[GenericText] = None
2022-02-05 23:54:45 +01:00
def merge_with(self, other, strict: bool = False):
2022-02-05 22:40:10 +01:00
"""
Merges the other config into this one
:param other:
2022-02-05 23:54:45 +01:00
:param strict: whether conflicting options are allowed or not
2022-02-05 22:40:10 +01:00
:return: self
"""
2022-02-05 23:54:45 +01:00
for var in vars(self):
if not getattr(self, var):
setattr(self, var, getattr(other, var))
else:
if strict and getattr(other, var) is not None and getattr(self, var) != getattr(other, var):
raise NotImplementedError
2022-02-05 22:40:10 +01:00
return self
2022-02-04 11:39:15 +01:00
2022-02-04 11:46:06 +01:00
class DocFormattingConfig:
2022-02-04 11:39:15 +01:00
def __init__(self):
self._documents: Optional[List[str]] = None
self._dependencies: Optional[List[str]] = None