better decorator

This commit is contained in:
Maximilian Keßler 2022-02-06 00:02:59 +01:00
parent 1688463e96
commit c669b15fc9

View file

@ -1,22 +1,29 @@
from typing import List, Optional, Union
from typing import List, Optional, Union, Any, Tuple
from .enums import NamingScheme, License
from .generic_text import GenericText
def generate_properties(attr_names):
def generate_properties(attributes: List[Union[str, Tuple[str, Any]]]):
attributes = [
x if isinstance(x, Tuple) else (x, None) for x in attributes
]
def decorator(cls):
for attr_name in attr_names:
def get_attr(self, attr_name=attr_name):
return getattr(self, "_" + attr_name)
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, attr_name, prop)
setattr(cls, attribute, prop)
return cls
return decorator
@generate_properties(['naming_scheme'])
@generate_properties([('naming_scheme', False), 'license'])
class FormattingConfig:
def __init__(self):
self._naming_scheme: Optional[Union[NamingScheme, str]] = None