default value setter
This commit is contained in:
parent
3001caf7ce
commit
2f506d7afd
@ -1,6 +1,6 @@
|
|||||||
[metadata]
|
[metadata]
|
||||||
name = yamlhttpforms
|
name = yamlhttpforms
|
||||||
version = 0.0.6
|
version = 0.0.7
|
||||||
author = Daniil F.
|
author = Daniil F.
|
||||||
author_email = mail@placeholder123.to
|
author_email = mail@placeholder123.to
|
||||||
description = HTTP forms defined in YAML
|
description = HTTP forms defined in YAML
|
||||||
|
@ -17,21 +17,7 @@ OptionsT = Dict[str, str]
|
|||||||
class FormField:
|
class FormField:
|
||||||
def __init__(self, name: str, default: DefaultInitT = None, options: OptionsT = None, required: bool = False):
|
def __init__(self, name: str, default: DefaultInitT = None, options: OptionsT = None, required: bool = False):
|
||||||
self.name: str = name
|
self.name: str = name
|
||||||
self._default: Union[str, CallableDefaultT, None]
|
self.default = default
|
||||||
if isinstance(default, dict):
|
|
||||||
try:
|
|
||||||
module, function = default['module'], default['function']
|
|
||||||
except KeyError:
|
|
||||||
raise TypeError(f"Default for field '{name}' is invalid. The default must be either a string or a "
|
|
||||||
f"dictionary with the special keys 'module' and 'function'.")
|
|
||||||
obj = import_module(module)
|
|
||||||
for attr in function.split('.'):
|
|
||||||
obj = getattr(obj, attr)
|
|
||||||
self._default = obj
|
|
||||||
elif default is None:
|
|
||||||
self._default = None
|
|
||||||
else:
|
|
||||||
self._default = str(default)
|
|
||||||
self.options: Optional[OptionsT] = None
|
self.options: Optional[OptionsT] = None
|
||||||
if options is not None:
|
if options is not None:
|
||||||
self.options = {str(k): str(v) for k, v in options.items()}
|
self.options = {str(k): str(v) for k, v in options.items()}
|
||||||
@ -55,6 +41,23 @@ class FormField:
|
|||||||
def default(self) -> Optional[str]:
|
def default(self) -> Optional[str]:
|
||||||
return self._default() if callable(self._default) else self._default
|
return self._default() if callable(self._default) else self._default
|
||||||
|
|
||||||
|
@default.setter
|
||||||
|
def default(self, default: DefaultInitT) -> None:
|
||||||
|
if isinstance(default, dict):
|
||||||
|
try:
|
||||||
|
module, function = default['module'], default['function']
|
||||||
|
except KeyError:
|
||||||
|
raise TypeError(f"Default for field '{self.name}' is invalid. The default must be either a string or "
|
||||||
|
f"`None` or a dictionary with the special keys 'module' and 'function'.")
|
||||||
|
obj = import_module(module)
|
||||||
|
for attr in function.split('.'):
|
||||||
|
obj = getattr(obj, attr)
|
||||||
|
self._default = obj
|
||||||
|
elif default is None:
|
||||||
|
self._default = None
|
||||||
|
else:
|
||||||
|
self._default = str(default)
|
||||||
|
|
||||||
def valid_option(self, option: str) -> bool:
|
def valid_option(self, option: str) -> bool:
|
||||||
return self.options is None or option in self.options.keys() or option in self.options.values()
|
return self.options is None or option in self.options.keys() or option in self.options.values()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user