symplify tiramisu/option/option.py

This commit is contained in:
Emmanuel Garette 2018-11-15 18:35:14 +01:00
parent 92b469bd43
commit f6bdd0c17e
13 changed files with 115 additions and 119 deletions

View File

@ -19,7 +19,7 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from ..setting import undefined
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
from .option import Option
@ -30,8 +30,8 @@ class BoolOption(Option):
_display_name = _('boolean')
def _validate(self,
value,
*args,
**kwargs):
value: bool,
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
if not isinstance(value, bool):
raise ValueError()

View File

@ -21,7 +21,7 @@
from IPy import IP
from ..error import ConfigError
from ..setting import undefined
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
from .option import Option
@ -31,9 +31,9 @@ class BroadcastOption(Option):
_display_name = _('broadcast address')
def _validate(self,
value,
*args,
**kwargs):
value: str,
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
if not isinstance(value, str):
raise ValueError(_('invalid string'))
if value.count('.') != 3:

View File

@ -20,7 +20,7 @@
# ____________________________________________________________
from datetime import datetime
from ..setting import undefined
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
from .option import Option
@ -30,9 +30,9 @@ class DateOption(Option):
_display_name = _('date')
def _validate(self,
value,
*args,
**kwargs):
value: str,
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
if not isinstance(value, str):
raise ValueError(_('invalid string'))
try:

View File

@ -21,7 +21,7 @@
import re
from IPy import IP
from ..setting import undefined
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
from .option import Option
@ -98,9 +98,9 @@ class DomainnameOption(Option):
return 63
def _validate(self,
value,
*args,
**kwargs):
value: str,
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
def _valid_length(val):
if len(val) < 1:
raise ValueError(_("invalid length (min 1)"))

View File

@ -19,7 +19,7 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from ..setting import undefined
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
from .option import Option
@ -30,8 +30,8 @@ class FloatOption(Option):
_display_name = _('float')
def _validate(self,
value,
*args,
**kwargs):
value: float,
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
if not isinstance(value, float):
raise ValueError()

View File

@ -19,7 +19,7 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from ..setting import undefined
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
from .option import Option
@ -44,9 +44,9 @@ class IntOption(Option):
def _validate(self,
value,
*args,
**kwargs):
value: int,
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
if not isinstance(value, int):
raise ValueError()
min_number = self.impl_get_extra('min_number')

View File

@ -21,7 +21,7 @@
from IPy import IP
from ..error import ConfigError
from ..setting import undefined
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
from .option import Option
@ -63,9 +63,9 @@ class IPOption(Option):
extra=extra)
def _validate(self,
value,
*args,
**kwargs):
value: str,
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
# sometimes an ip term starts with a zero
# but this does not fit in some case, for example bind does not like it
if not isinstance(value, str):

View File

@ -21,7 +21,7 @@
from IPy import IP
from ..error import ConfigError
from ..setting import undefined
from ..setting import undefined, OptionBag, Undefined
from ..i18n import _
from .option import Option
@ -33,8 +33,8 @@ class NetmaskOption(Option):
def _validate(self,
value: str,
*args,
**kwargs) -> None:
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
if not isinstance(value, str):
raise ValueError(_('invalid string'))
if value.count('.') != 3:

View File

@ -21,11 +21,11 @@
# ____________________________________________________________
import warnings
import weakref
from typing import Any, List, Callable, Optional
from typing import Any, List, Callable, Optional, Dict, Union, Tuple
from .baseoption import BaseOption, submulti, STATIC_TUPLE
from ..i18n import _
from ..setting import log, undefined, OptionBag
from ..setting import log, undefined, OptionBag, Undefined
from ..autolib import carry_out_calculation
from ..error import (ConfigError, ValueWarning, PropertiesOptionError,
ValueOptionError, display_list)
@ -58,22 +58,21 @@ class Option(BaseOption):
)
_empty = ''
def __init__(self,
name,
doc,
default=undefined,
default_multi=None,
requires=None,
multi=False,
unique=undefined,
callback=None,
callback_params=None,
validator=None,
validator_params=None,
properties=None,
warnings_only=False,
extra=None,
allow_empty_list=undefined):
name: str,
doc: str,
default: Any=undefined,
default_multi: Any=None,
requires: List[Dict]=None,
multi: bool=False,
unique: bool=undefined,
callback: Optional[Callable]=None,
callback_params: Optional[Params]=None,
validator: Optional[Callable]=None,
validator_params: Optional[Params]=None,
properties: Optional[List[str]]=None,
warnings_only: bool=False,
extra: Optional[Dict]=None,
allow_empty_list: bool=undefined) -> None:
_setattr = object.__setattr__
if not multi and default_multi is not None:
raise ValueError(_("default_multi is set whereas multi is False"
@ -165,22 +164,22 @@ class Option(BaseOption):
#__________________________________________________________________________
# option's information
def impl_is_multi(self):
def impl_is_multi(self) -> bool:
return getattr(self, '_multi', 1) != 1
def impl_is_submulti(self):
def impl_is_submulti(self) -> bool:
return getattr(self, '_multi', 1) == 2
def impl_is_unique(self):
def impl_is_unique(self) -> bool:
return getattr(self, '_unique', False)
def impl_allow_empty_list(self):
def impl_allow_empty_list(self) -> Union[Undefined, bool]:
return getattr(self, '_allow_empty_list', undefined)
def impl_is_dynsymlinkoption(self):
def impl_is_dynsymlinkoption(self) -> bool:
return False
def impl_getdefault(self):
def impl_getdefault(self) -> Any:
"accessing the default value"
is_multi = self.impl_is_multi()
default = getattr(self, '_default', undefined)
@ -194,7 +193,7 @@ class Option(BaseOption):
default = list(default)
return default
def impl_getdefault_multi(self):
def impl_getdefault_multi(self) -> Any:
"accessing the default value for a multi"
if self.impl_is_submulti():
default_value = []
@ -203,7 +202,7 @@ class Option(BaseOption):
return getattr(self, '_default_multi', default_value)
def impl_get_extra(self,
key):
key: str) -> Any:
extra = getattr(self, '_extra', {})
if isinstance(extra, tuple):
if key in extra[0]:
@ -215,7 +214,7 @@ class Option(BaseOption):
#__________________________________________________________________________
# validator
def impl_get_validator(self):
def impl_get_validator(self) -> Tuple[Callable, Params]:
val = getattr(self, '_val_call', (None,))[0]
if val is None:
ret_val = (None, None)
@ -226,9 +225,9 @@ class Option(BaseOption):
return ret_val
def impl_validate(self,
value,
option_bag,
check_error=True):
value: Any,
option_bag: OptionBag,
check_error: bool=True) -> None:
"""
"""
config_bag = option_bag.config_bag
@ -353,12 +352,8 @@ class Option(BaseOption):
'{0}'.format(err))
def _validate_calculator(self,
callback,
callback_params):
"""callback_params:
* None
* {'': ((option, permissive),), 'ip': ((None,), (option, permissive))
"""
callback: Callable,
callback_params: Optional[Params]=None) -> None:
if callback is None:
return
default_multi = getattr(self, '_default_multi', None)
@ -370,14 +365,15 @@ class Option(BaseOption):
'is calculated').format(self.impl_getname()))
def _second_level_validation(self,
value,
warnings_only):
value: Any,
warnings_only: bool) -> None:
pass
#__________________________________________________________________________
# master/slaves
def impl_is_master_slaves(self, type_='both'):
def impl_is_master_slaves(self,
type_: str='both') -> bool:
master_slaves = self.impl_get_master_slaves()
if master_slaves is not None:
if type_ == 'both':
@ -397,9 +393,9 @@ class Option(BaseOption):
# consistencies
def impl_add_consistency(self,
func,
func: str,
*other_opts,
**params):
**params) -> None:
"""Add consistency means that value will be validate with other_opts
option's values.
@ -453,9 +449,9 @@ class Option(BaseOption):
opt._add_dependency(self)
def _add_consistency(self,
func,
all_cons_opts,
params):
func: str,
all_cons_opts: List[BaseOption],
params: Dict) -> None:
cons = (-1, func, all_cons_opts, params)
consistencies = getattr(self, '_consistencies', None)
if consistencies is None:
@ -466,17 +462,17 @@ class Option(BaseOption):
def get_consistencies(self):
return getattr(self, '_consistencies', STATIC_TUPLE)
def has_consistencies(self, context):
def has_consistencies(self, context) -> bool:
descr = context.cfgimpl_get_description()
if getattr(descr, '_cache_consistencies', None) is None:
return False
return self in descr._cache_consistencies
def valid_consistency(self,
option_bag,
value,
check_error,
option_warnings_only):
option_bag: OptionBag,
value: Any,
check_error: bool,
option_warnings_only: bool) -> None:
if option_bag.config_bag is not undefined:
descr = option_bag.config_bag.context.cfgimpl_get_description()
# no consistency found at all
@ -529,9 +525,9 @@ class Option(BaseOption):
option_bag.fromconsistency = []
def _valid_consistencies(self,
other_opts,
init=True,
func=None):
other_opts: List[BaseOption],
init: bool=True,
func: Optional[str]=None) -> None:
if self.issubdyn():
dynod = self.getsubdyn()
else:
@ -561,9 +557,8 @@ class Option(BaseOption):
if is_multi != opt.impl_is_multi():
raise ConfigError(_('every options in consistency must be '
'multi or none'))
if init:
# FIXME
if func != 'not_equal':
if init and func != 'not_equal':
opt._has_dependency = True
def launch_consistency(self,
@ -574,7 +569,7 @@ class Option(BaseOption):
value: Any,
opts: List[BaseOption],
warnings_only: bool,
transitive: bool):
transitive: bool) -> None:
"""Launch consistency now
"""
all_cons_vals = []
@ -642,11 +637,11 @@ class Option(BaseOption):
raise err
def get_consistency_value(self,
option_bag,
current_option,
cons_id,
value,
func):
option_bag: OptionBag,
current_option: BaseOption,
cons_id: int,
value: Any,
func: str) -> Any:
if option_bag.ori_option == current_option:
# orig_option is current option
# we have already value, so use it
@ -682,11 +677,11 @@ class Option(BaseOption):
return current_value
def _cons_not_equal(self,
current_opt,
opts,
vals,
warnings_only,
context):
current_opt: BaseOption,
opts: List[BaseOption],
vals: List[Any],
warnings_only: bool,
context) -> None:
equal = []
is_current = False
for idx_inf, val_inf in enumerate(vals):
@ -726,9 +721,9 @@ class RegexpOption(Option):
__slots__ = tuple()
def _validate(self,
value,
*args,
**kwargs):
value: Any,
option_bag: OptionBag,
current_opt: BaseOption=Undefined) -> None:
if not isinstance(value, str):
raise ValueError(_('invalid string'))
match = self._regexp.search(value)

View File

@ -19,7 +19,7 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from ..setting import undefined
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
from .option import Option
@ -30,8 +30,8 @@ class PasswordOption(Option):
_display_name = _('password')
def _validate(self,
value,
*args,
**kwargs):
value: str,
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
if not isinstance(value, str):
raise ValueError(_('invalid string'))

View File

@ -20,8 +20,9 @@
# ____________________________________________________________
import re
import sys
from typing import Union
from ..setting import undefined
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
from .option import Option
@ -97,9 +98,9 @@ class PortOption(Option):
extra=extra)
def _validate(self,
value,
*args,
**kwargs):
value: Union[int,str],
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
if isinstance(value, int):
value = str(value)
if not isinstance(value, str):

View File

@ -20,7 +20,7 @@
# ____________________________________________________________
import sys
from ..setting import undefined
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
from .option import Option
@ -31,9 +31,9 @@ class StrOption(Option):
_display_name = _('string')
def _validate(self,
value,
*args,
**kwargs):
value: str,
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
if not isinstance(value, str):
raise ValueError()

View File

@ -20,7 +20,7 @@
# ____________________________________________________________
import re
from ..setting import undefined
from ..setting import undefined, Undefined, OptionBag
from ..i18n import _
from .option import Option
from .domainnameoption import DomainnameOption
@ -33,9 +33,9 @@ class URLOption(DomainnameOption):
_display_name = _('URL')
def _validate(self,
value,
*args,
**kwargs):
value: str,
option_bag: OptionBag,
current_opt: Option=Undefined) -> None:
if not isinstance(value, str):
raise ValueError(_('invalid string'))
match = self.proto_re.search(value)
@ -62,8 +62,8 @@ class URLOption(DomainnameOption):
'65536'))
# validate domainname
super(URLOption, self)._validate(domain,
*args,
**kwargs)
option_bag,
current_opt)
super(URLOption, self)._second_level_validation(domain, False)
# validate file
if files is not None and files != '' and not self.path_re.search(files):