start annotation

This commit is contained in:
Emmanuel Garette 2018-04-28 08:39:07 +02:00
parent a9919e438b
commit 165b28cb00
2 changed files with 79 additions and 69 deletions

View File

@ -15,12 +15,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# ____________________________________________________________ # ____________________________________________________________
from inspect import ismethod, getdoc, signature from inspect import ismethod, getdoc, signature
from .error import APIError, ConfigError, SlaveError
from .i18n import _
from .setting import ConfigBag, owners, undefined, FORBIDDEN_SET_PROPERTIES
from .option import ChoiceOption
from time import time from time import time
from copy import deepcopy from copy import deepcopy
from typing import List, Any, Optional, Callable, Union, Dict
from .error import APIError, ConfigError, SlaveError, PropertiesOptionError
from .i18n import _
from .setting import ConfigBag, owners, Undefined, undefined, FORBIDDEN_SET_PROPERTIES
from .config import Config, SubConfig, GroupConfig, MetaConfig
from .option import ChoiceOption
TIRAMISU_VERSION = 3 TIRAMISU_VERSION = 3
@ -88,11 +92,11 @@ class TiramisuHelp:
tmpl_help = '{0}{1} {2}: \n{0} {3}\n' tmpl_help = '{0}{1} {2}: \n{0} {3}\n'
def help(self, def help(self,
init=True, init: bool=True,
space="", space: str="",
root='', root: str='',
_display=True, _display: bool=True,
_valid=False): _valid: bool=False) -> List[str]:
options = [] options = []
if init and isinstance(self, TiramisuAPI): if init and isinstance(self, TiramisuAPI):
options.append(self.tmpl_help.format(space, self.icon, root + 'unrestraint', _('access to option without property restriction'))) options.append(self.tmpl_help.format(space, self.icon, root + 'unrestraint', _('access to option without property restriction')))
@ -154,7 +158,7 @@ class CommonTiramisu(TiramisuHelp):
allow_optiondescription = True allow_optiondescription = True
registers = {} registers = {}
def _get_option(self): def _get_option(self) -> Any:
option = self.config_bag.option option = self.config_bag.option
if option is None: if option is None:
option = self.subconfig.cfgimpl_get_description().impl_getchild(self._name, option = self.subconfig.cfgimpl_get_description().impl_getchild(self._name,
@ -184,11 +188,11 @@ class CommonTiramisuOption(CommonTiramisu):
slave_need_index = True slave_need_index = True
def __init__(self, def __init__(self,
name, name: Optional[str],
path=None, path: Optional[str]=None,
index=None, index: Optional[int]=None,
subconfig=None, subconfig: Any=None,
config_bag=None): config_bag: Optional[ConfigBag]=None) -> None:
self._path = path self._path = path
self.index = index self.index = index
self.config_bag = config_bag self.config_bag = config_bag
@ -197,7 +201,7 @@ class CommonTiramisuOption(CommonTiramisu):
if config_bag is not None and self.slave_need_index: if config_bag is not None and self.slave_need_index:
self._test_slave_index() self._test_slave_index()
def _test_slave_index(self): def _test_slave_index(self) -> None:
option = self._get_option() option = self._get_option()
if not option.impl_is_optiondescription(): if not option.impl_is_optiondescription():
if self.index is None and option.impl_is_master_slaves('slave'): if self.index is None and option.impl_is_master_slaves('slave'):
@ -264,7 +268,7 @@ class TiramisuOptionOption(CommonTiramisuOption):
self._get_option() self._get_option()
return self._name return self._name
def path(self): def path(self) -> str:
"""get option path""" """get option path"""
self._get_option() self._get_option()
return self._path return self._path
@ -305,7 +309,7 @@ class TiramisuOptionOption(CommonTiramisuOption):
option = self._get_option() option = self._get_option()
return option.impl_getrequires() return option.impl_getrequires()
def __getattr__(self, name): def __getattr__(self, name: str) -> Callable:
if not self._get_option().impl_is_optiondescription() and name != 'get_option': if not self._get_option().impl_is_optiondescription() and name != 'get_option':
subkey = '_' + name subkey = '_' + name
if subkey in dir(self): if subkey in dir(self):
@ -323,11 +327,11 @@ class TiramisuOptionOwner(CommonTiramisuOption):
"""manager option's owner""" """manager option's owner"""
def __init__(self, def __init__(self,
name, name: Optional[str],
path=None, path: Optional[str]=None,
index=None, index: Optional[int]=None,
subconfig=None, subconfig: Union[None, Config, SubConfig]=None,
config_bag=None): config_bag: Optional[ConfigBag]=None) -> None:
super().__init__(name, super().__init__(name,
path, path,
@ -377,11 +381,11 @@ class TiramisuOptionProperty(CommonTiramisuOption):
slave_need_index = False slave_need_index = False
def __init__(self, def __init__(self,
name, name: Optional[str],
path=None, path: Optional[str]=None,
index=None, index: Optional[int]=None,
subconfig=None, subconfig: Union[None, Config, SubConfig]=None,
config_bag=None): config_bag: Optional[ConfigBag]=None) -> None:
super().__init__(name, super().__init__(name,
path, path,
index, index,
@ -446,11 +450,11 @@ class TiramisuOptionPermissive(CommonTiramisuOption):
slave_need_index = False slave_need_index = False
def __init__(self, def __init__(self,
name, name: Optional[str],
path=None, path: Optional[str]=None,
index=None, index: Optional[int]=None,
subconfig=None, subconfig: Union[None, Config, SubConfig]=None,
config_bag=None): config_bag: Optional[ConfigBag]=None) -> None:
super().__init__(name, super().__init__(name,
path, path,
index, index,
@ -580,7 +584,7 @@ class TiramisuOptionValue(CommonTiramisuOption):
self._length = self.subconfig.cfgimpl_get_length_slave(option, self.config_bag) self._length = self.subconfig.cfgimpl_get_length_slave(option, self.config_bag)
return self._length return self._length
def __getattr__(self, name): def __getattr__(self, name: str) -> Callable:
if name == 'list' and isinstance(self._get_option(), ChoiceOption): if name == 'list' and isinstance(self._get_option(), ChoiceOption):
return self._list return self._list
elif name == 'pop' and self._get_option().impl_is_master_slaves('master'): elif name == 'pop' and self._get_option().impl_is_master_slaves('master'):
@ -595,7 +599,7 @@ class TiramisuOptionValue(CommonTiramisuOption):
return self.config_bag.option.impl_get_values(self.config_bag) return self.config_bag.option.impl_get_values(self.config_bag)
def registers(registers, prefix): def registers(registers: Dict[str, type], prefix: str) -> None:
for module_name in globals().keys(): for module_name in globals().keys():
if module_name != prefix and module_name.startswith(prefix): if module_name != prefix and module_name.startswith(prefix):
module = globals()[module_name] module = globals()[module_name]
@ -605,12 +609,11 @@ def registers(registers, prefix):
class TiramisuOption(CommonTiramisu): class TiramisuOption(CommonTiramisu):
def __init__(self, def __init__(self,
name, name: Optional[str],
path=None, path: Optional[str]=None,
index=None, index: Optional[int]=None,
subconfig=None, subconfig: Union[None, Config, SubConfig]=None,
config_bag=None): config_bag: Optional[ConfigBag]=None) -> None:
self._name = name self._name = name
self.subconfig = subconfig self.subconfig = subconfig
self._path = path self._path = path
@ -619,7 +622,7 @@ class TiramisuOption(CommonTiramisu):
self.registers = {} self.registers = {}
registers(self.registers, self.__class__.__name__) registers(self.registers, self.__class__.__name__)
def __getattr__(self, subfunc): def __getattr__(self, subfunc: str) -> Any:
if subfunc in self.registers: if subfunc in self.registers:
return self.registers[subfunc](self._name, return self.registers[subfunc](self._name,
self._path, self._path,
@ -653,10 +656,10 @@ class TiramisuOption(CommonTiramisu):
withvalue=withvalue) withvalue=withvalue)
def _find(self, def _find(self,
name, name: str,
value=undefined, value: Union[bool, Undefined]=undefined,
type=None, type=None,
first=False): first: bool=False):
"""find an option by name (only for optiondescription)""" """find an option by name (only for optiondescription)"""
if not first: if not first:
ret = [] ret = []
@ -704,7 +707,7 @@ class TiramisuOption(CommonTiramisu):
class TiramisuContext(TiramisuHelp): class TiramisuContext(TiramisuHelp):
def __init__(self, def __init__(self,
config_bag): config_bag: Optional[ConfigBag]) -> None:
self.config_bag = config_bag self.config_bag = config_bag
self.registers = {} self.registers = {}
registers(self.registers, self.__class__.__name__) registers(self.registers, self.__class__.__name__)
@ -736,7 +739,7 @@ class TiramisuContextValue(TiramisuContext):
return self.config_bag.config.cfgimpl_get_values().mandatory_warnings(self.config_bag) return self.config_bag.config.cfgimpl_get_values().mandatory_warnings(self.config_bag)
def set(self, def set(self,
path, path: str,
value, value,
index=None, index=None,
only_config=undefined, only_config=undefined,
@ -967,9 +970,9 @@ class TiramisuContextOption(TiramisuContext):
class TiramisuContextConfig(TiramisuContext): class TiramisuContextConfig(TiramisuContext):
"""configuration methods""" """configuration methods"""
def find(self, def find(self,
name, name: str,
value=undefined, value=undefined,
first=False): first: bool=False):
"""find a path from option name and optionnaly a value to MetaConfig or GroupConfig""" """find a path from option name and optionnaly a value to MetaConfig or GroupConfig"""
if first: if first:
return self.config_bag.config.find_firsts(byname=name, return self.config_bag.config.find_firsts(byname=name,
@ -984,7 +987,7 @@ class TiramisuDispatcher:
class TiramisuDispatcherConfig(TiramisuDispatcher, TiramisuContextConfig): class TiramisuDispatcherConfig(TiramisuDispatcher, TiramisuContextConfig):
def __call__(self, path): def __call__(self, path: Optional[str]):
"""select a child Tiramisu configuration (only with MetaConfig or GroupConfig)""" """select a child Tiramisu configuration (only with MetaConfig or GroupConfig)"""
config = self.config_bag.config config = self.config_bag.config
if path is None: if path is None:
@ -1001,7 +1004,7 @@ class TiramisuDispatcherConfig(TiramisuDispatcher, TiramisuContextConfig):
class TiramisuDispatcherOption(TiramisuDispatcher, TiramisuContextOption): class TiramisuDispatcherOption(TiramisuDispatcher, TiramisuContextOption):
subhelp = TiramisuOption subhelp = TiramisuOption
def __call__(self, path, index=None): def __call__(self, path: str, index: Optional[int]=None) -> TiramisuOption:
"""select a option (index only for slave option)""" """select a option (index only for slave option)"""
config_bag = self.config_bag.copy() config_bag = self.config_bag.copy()
validate = not config_bag.force_unrestraint validate = not config_bag.force_unrestraint
@ -1019,9 +1022,9 @@ class TiramisuDispatcherOption(TiramisuDispatcher, TiramisuContextOption):
class TiramisuAPI(TiramisuHelp): class TiramisuAPI(TiramisuHelp):
def __init__(self, def __init__(self,
config, config: Union[Config, GroupConfig, MetaConfig],
force_permissive=False, force_permissive: bool=False,
force_unrestraint=False): force_unrestraint: bool=False) -> None:
self._config = config self._config = config
self.force_permissive = force_permissive self.force_permissive = force_permissive
self.force_unrestraint = force_unrestraint self.force_unrestraint = force_unrestraint
@ -1029,7 +1032,7 @@ class TiramisuAPI(TiramisuHelp):
registers(self.registers, 'TiramisuContext') registers(self.registers, 'TiramisuContext')
registers(self.registers, 'TiramisuDispatcher') registers(self.registers, 'TiramisuDispatcher')
def __getattr__(self, subfunc): def __getattr__(self, subfunc: str) -> Any:
if subfunc == 'forcepermissive': if subfunc == 'forcepermissive':
return TiramisuAPI(config=self._config, return TiramisuAPI(config=self._config,
force_permissive=True, force_permissive=True,
@ -1048,7 +1051,7 @@ class TiramisuAPI(TiramisuHelp):
@count @count
def getapi(config): def getapi(config: Union[Config, GroupConfig, MetaConfig]):
"""instanciate TiramisuAPI """instanciate TiramisuAPI
:param config: Config object :param config: Config object

View File

@ -18,21 +18,24 @@
# the whole pypy projet is under MIT licence # the whole pypy projet is under MIT licence
# ____________________________________________________________ # ____________________________________________________________
"enables us to carry out a calculation and return an option's value" "enables us to carry out a calculation and return an option's value"
from .error import PropertiesOptionError, ConfigError, SlaveError, display_list from typing import Any, Optional, Union, Callable, Dict, List
from .error import PropertiesOptionError, ConfigError, SlaveError
from .i18n import _ from .i18n import _
from .setting import undefined from .setting import undefined, ConfigBag, Undefined
from .option.symlinkoption import DynSymLinkOption from .option.symlinkoption import DynSymLinkOption
from .storage import get_default_values_storages, get_default_settings_storages from .storage import get_default_values_storages, get_default_settings_storages
from .function import ParamValue, ParamContext, ParamIndex from .function import ParamValue, ParamContext, ParamIndex, ParamOption, Params
# ____________________________________________________________ # ____________________________________________________________
def manager_callback(callbk, def manager_callback(callbk: Union[ParamOption, ParamValue],
option, option,
index, index: Optional[int],
orig_value, orig_value,
config_bag, config_bag: ConfigBag,
context): context) -> Any:
"""replace Param by true value""" """replace Param by true value"""
if isinstance(callbk, ParamValue): if isinstance(callbk, ParamValue):
return callbk.value return callbk.value
@ -94,12 +97,12 @@ def manager_callback(callbk,
def carry_out_calculation(option, def carry_out_calculation(option,
context, context,
callback, callback: Callable,
callback_params, callback_params: Optional[Params],
index, index: Optional[int],
config_bag, config_bag: Optional[ConfigBag],
orig_value=undefined, orig_value=undefined,
is_validator=False): is_validator: int=False):
"""a function that carries out a calculation for an option's value """a function that carries out a calculation for an option's value
@ -266,7 +269,11 @@ def carry_out_calculation(option,
return ret return ret
def calculate(option, callback, is_validator, args, kwargs): def calculate(option,
callback: Callable,
is_validator: bool,
args,
kwargs):
"""wrapper that launches the 'callback' """wrapper that launches the 'callback'
:param callback: callback function :param callback: callback function