default value setter
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
			
		||||
[metadata]
 | 
			
		||||
name = yamlhttpforms
 | 
			
		||||
version = 0.0.6
 | 
			
		||||
version = 0.0.7
 | 
			
		||||
author = Daniil F.
 | 
			
		||||
author_email = mail@placeholder123.to
 | 
			
		||||
description = HTTP forms defined in YAML
 | 
			
		||||
 
 | 
			
		||||
@@ -17,21 +17,7 @@ OptionsT = Dict[str, str]
 | 
			
		||||
class FormField:
 | 
			
		||||
    def __init__(self, name: str, default: DefaultInitT = None, options: OptionsT = None, required: bool = False):
 | 
			
		||||
        self.name: str = name
 | 
			
		||||
        self._default: Union[str, CallableDefaultT, None]
 | 
			
		||||
        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.default = default
 | 
			
		||||
        self.options: Optional[OptionsT] = None
 | 
			
		||||
        if options is not None:
 | 
			
		||||
            self.options = {str(k): str(v) for k, v in options.items()}
 | 
			
		||||
@@ -55,6 +41,23 @@ class FormField:
 | 
			
		||||
    def default(self) -> Optional[str]:
 | 
			
		||||
        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:
 | 
			
		||||
        return self.options is None or option in self.options.keys() or option in self.options.values()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user