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/>.
# ____________________________________________________________
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 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
@ -88,11 +92,11 @@ class TiramisuHelp:
tmpl_help = '{0}{1} {2}: \n{0} {3}\n'
def help(self,
init=True,
space="",
root='',
_display=True,
_valid=False):
init: bool=True,
space: str="",
root: str='',
_display: bool=True,
_valid: bool=False) -> List[str]:
options = []
if init and isinstance(self, TiramisuAPI):
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
registers = {}
def _get_option(self):
def _get_option(self) -> Any:
option = self.config_bag.option
if option is None:
option = self.subconfig.cfgimpl_get_description().impl_getchild(self._name,
@ -184,11 +188,11 @@ class CommonTiramisuOption(CommonTiramisu):
slave_need_index = True
def __init__(self,
name,
path=None,
index=None,
subconfig=None,
config_bag=None):
name: Optional[str],
path: Optional[str]=None,
index: Optional[int]=None,
subconfig: Any=None,
config_bag: Optional[ConfigBag]=None) -> None:
self._path = path
self.index = index
self.config_bag = config_bag
@ -197,7 +201,7 @@ class CommonTiramisuOption(CommonTiramisu):
if config_bag is not None and self.slave_need_index:
self._test_slave_index()
def _test_slave_index(self):
def _test_slave_index(self) -> None:
option = self._get_option()
if not option.impl_is_optiondescription():
if self.index is None and option.impl_is_master_slaves('slave'):
@ -264,7 +268,7 @@ class TiramisuOptionOption(CommonTiramisuOption):
self._get_option()
return self._name
def path(self):
def path(self) -> str:
"""get option path"""
self._get_option()
return self._path
@ -305,7 +309,7 @@ class TiramisuOptionOption(CommonTiramisuOption):
option = self._get_option()
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':
subkey = '_' + name
if subkey in dir(self):
@ -323,11 +327,11 @@ class TiramisuOptionOwner(CommonTiramisuOption):
"""manager option's owner"""
def __init__(self,
name,
path=None,
index=None,
subconfig=None,
config_bag=None):
name: Optional[str],
path: Optional[str]=None,
index: Optional[int]=None,
subconfig: Union[None, Config, SubConfig]=None,
config_bag: Optional[ConfigBag]=None) -> None:
super().__init__(name,
path,
@ -377,11 +381,11 @@ class TiramisuOptionProperty(CommonTiramisuOption):
slave_need_index = False
def __init__(self,
name,
path=None,
index=None,
subconfig=None,
config_bag=None):
name: Optional[str],
path: Optional[str]=None,
index: Optional[int]=None,
subconfig: Union[None, Config, SubConfig]=None,
config_bag: Optional[ConfigBag]=None) -> None:
super().__init__(name,
path,
index,
@ -446,11 +450,11 @@ class TiramisuOptionPermissive(CommonTiramisuOption):
slave_need_index = False
def __init__(self,
name,
path=None,
index=None,
subconfig=None,
config_bag=None):
name: Optional[str],
path: Optional[str]=None,
index: Optional[int]=None,
subconfig: Union[None, Config, SubConfig]=None,
config_bag: Optional[ConfigBag]=None) -> None:
super().__init__(name,
path,
index,
@ -580,7 +584,7 @@ class TiramisuOptionValue(CommonTiramisuOption):
self._length = self.subconfig.cfgimpl_get_length_slave(option, self.config_bag)
return self._length
def __getattr__(self, name):
def __getattr__(self, name: str) -> Callable:
if name == 'list' and isinstance(self._get_option(), ChoiceOption):
return self._list
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)
def registers(registers, prefix):
def registers(registers: Dict[str, type], prefix: str) -> None:
for module_name in globals().keys():
if module_name != prefix and module_name.startswith(prefix):
module = globals()[module_name]
@ -605,12 +609,11 @@ def registers(registers, prefix):
class TiramisuOption(CommonTiramisu):
def __init__(self,
name,
path=None,
index=None,
subconfig=None,
config_bag=None):
name: Optional[str],
path: Optional[str]=None,
index: Optional[int]=None,
subconfig: Union[None, Config, SubConfig]=None,
config_bag: Optional[ConfigBag]=None) -> None:
self._name = name
self.subconfig = subconfig
self._path = path
@ -619,7 +622,7 @@ class TiramisuOption(CommonTiramisu):
self.registers = {}
registers(self.registers, self.__class__.__name__)
def __getattr__(self, subfunc):
def __getattr__(self, subfunc: str) -> Any:
if subfunc in self.registers:
return self.registers[subfunc](self._name,
self._path,
@ -653,10 +656,10 @@ class TiramisuOption(CommonTiramisu):
withvalue=withvalue)
def _find(self,
name,
value=undefined,
name: str,
value: Union[bool, Undefined]=undefined,
type=None,
first=False):
first: bool=False):
"""find an option by name (only for optiondescription)"""
if not first:
ret = []
@ -704,7 +707,7 @@ class TiramisuOption(CommonTiramisu):
class TiramisuContext(TiramisuHelp):
def __init__(self,
config_bag):
config_bag: Optional[ConfigBag]) -> None:
self.config_bag = config_bag
self.registers = {}
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)
def set(self,
path,
path: str,
value,
index=None,
only_config=undefined,
@ -967,9 +970,9 @@ class TiramisuContextOption(TiramisuContext):
class TiramisuContextConfig(TiramisuContext):
"""configuration methods"""
def find(self,
name,
name: str,
value=undefined,
first=False):
first: bool=False):
"""find a path from option name and optionnaly a value to MetaConfig or GroupConfig"""
if first:
return self.config_bag.config.find_firsts(byname=name,
@ -984,7 +987,7 @@ class TiramisuDispatcher:
class TiramisuDispatcherConfig(TiramisuDispatcher, TiramisuContextConfig):
def __call__(self, path):
def __call__(self, path: Optional[str]):
"""select a child Tiramisu configuration (only with MetaConfig or GroupConfig)"""
config = self.config_bag.config
if path is None:
@ -1001,7 +1004,7 @@ class TiramisuDispatcherConfig(TiramisuDispatcher, TiramisuContextConfig):
class TiramisuDispatcherOption(TiramisuDispatcher, TiramisuContextOption):
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)"""
config_bag = self.config_bag.copy()
validate = not config_bag.force_unrestraint
@ -1019,9 +1022,9 @@ class TiramisuDispatcherOption(TiramisuDispatcher, TiramisuContextOption):
class TiramisuAPI(TiramisuHelp):
def __init__(self,
config,
force_permissive=False,
force_unrestraint=False):
config: Union[Config, GroupConfig, MetaConfig],
force_permissive: bool=False,
force_unrestraint: bool=False) -> None:
self._config = config
self.force_permissive = force_permissive
self.force_unrestraint = force_unrestraint
@ -1029,7 +1032,7 @@ class TiramisuAPI(TiramisuHelp):
registers(self.registers, 'TiramisuContext')
registers(self.registers, 'TiramisuDispatcher')
def __getattr__(self, subfunc):
def __getattr__(self, subfunc: str) -> Any:
if subfunc == 'forcepermissive':
return TiramisuAPI(config=self._config,
force_permissive=True,
@ -1048,7 +1051,7 @@ class TiramisuAPI(TiramisuHelp):
@count
def getapi(config):
def getapi(config: Union[Config, GroupConfig, MetaConfig]):
"""instanciate TiramisuAPI
:param config: Config object

View File

@ -18,21 +18,24 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
"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 .setting import undefined
from .setting import undefined, ConfigBag, Undefined
from .option.symlinkoption import DynSymLinkOption
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,
index,
index: Optional[int],
orig_value,
config_bag,
context):
config_bag: ConfigBag,
context) -> Any:
"""replace Param by true value"""
if isinstance(callbk, ParamValue):
return callbk.value
@ -94,12 +97,12 @@ def manager_callback(callbk,
def carry_out_calculation(option,
context,
callback,
callback_params,
index,
config_bag,
callback: Callable,
callback_params: Optional[Params],
index: Optional[int],
config_bag: Optional[ConfigBag],
orig_value=undefined,
is_validator=False):
is_validator: int=False):
"""a function that carries out a calculation for an option's value
@ -266,7 +269,11 @@ def carry_out_calculation(option,
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'
:param callback: callback function