tiramisu/tiramisu/api.py

376 lines
13 KiB
Python
Raw Normal View History

2017-10-22 09:48:08 +02:00
# -*- coding: utf-8 -*-
# Copyright (C) 2017 Team tiramisu (see AUTHORS for all contributors)
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ____________________________________________________________
from inspect import ismethod, getdoc
from .error import APIError
from .i18n import _
from .setting import owners, undefined
def getter(func):
def wrapper(self, path, *args, **kwargs):
opt = self._get_obj_by_path(path)
if opt.impl_is_master_slaves('slave'):
get = self._slave_get
else:
get = self._simple_get
return get(path, *args, **kwargs)
return wrapper
def setter(func):
def wrapper(self, path, *args, **kwargs):
opt = self._get_obj_by_path(path)
if opt.impl_is_master_slaves('slave'):
return self._slave_set(path, *args, **kwargs)
elif opt.impl_is_master_slaves('master'):
return self._master_set(path, *args, **kwargs)
else:
return self._simple_set(path, *args, **kwargs)
return wrapper
def resetter(func):
def wrapper(self, path, *args, **kwargs):
opt = self._get_obj_by_path(path)
if opt.impl_is_master_slaves('slave'):
reset = self._slave_reset
else:
reset = self._simple_reset
return reset(path, *args, **kwargs)
return wrapper
class CommonTiramisu(object):
icon = '\u2937'
tmpl_help = u' {} {}: {}'
2017-10-25 08:46:22 +02:00
allow_unrestraint = False
def __init__(self, config, force_permissive, force_unrestraint):
if not self.allow_unrestraint:
self._unrestraint_not_allowed(force_unrestraint)
def _unrestraint_not_allowed(self, force_unrestraint):
if force_unrestraint:
name = self.__class__.__name__[11:].lower()
raise APIError(_('{} cannot be unrestraint').format(name))
2017-10-22 09:48:08 +02:00
def _get_obj_by_path(self, path, index=None):
2017-10-25 08:46:22 +02:00
validate = not self.force_unrestraint
2017-10-22 09:48:08 +02:00
return self.config.unwrap_from_path(path,
2017-10-25 08:46:22 +02:00
validate=validate,
validate_properties=validate,
2017-10-22 09:48:08 +02:00
force_permissive=self.force_permissive,
index=index)
def __getattr__(self, name):
if name == 'help':
return self._help()
else:
if not hasattr(CommonTiramisu, name):
raise APIError(_('unknown method {}').format(name))
else:
2017-10-25 08:46:22 +02:00
super(CommonTiramisu, self).__getattribute__(name)
2017-10-22 09:48:08 +02:00
def _help(self):
txt = []
for func_name in dir(self):
if not func_name.startswith('_'):
func = getattr(self, func_name)
if ismethod(func):
txt.append(self.tmpl_help.format(self.icon, func_name, getdoc(func)))
return '\n'.join(txt)
class TiramisuAPIOption(CommonTiramisu):
"""get information from an option"""
2017-10-25 08:46:22 +02:00
allow_unrestraint = True
2017-10-22 09:48:08 +02:00
2017-10-25 08:46:22 +02:00
def __init__(self, config, force_permissive, force_unrestraint):
super(TiramisuAPIOption, self).__init__(config, force_permissive, force_unrestraint)
2017-10-22 09:48:08 +02:00
self.config = config
self.force_permissive = force_permissive
2017-10-25 08:46:22 +02:00
self.force_unrestraint = force_unrestraint
2017-10-22 09:48:08 +02:00
if config:
self.values = self.config.cfgimpl_get_values()
def ismulti(self, path):
"""test if option could have multi value"""
opt = self._get_obj_by_path(path)
return opt.impl_is_multi()
def ismasterslaves(self, path):
"""test if option is a master or a slave"""
opt = self._get_obj_by_path(path)
return opt.impl_is_master_slaves()
def ismaster(self, path):
"""test if option is a master"""
opt = self._get_obj_by_path(path)
return opt.impl_is_master_slaves('master')
def isslave(self, path):
"""test if option is a slave"""
opt = self._get_obj_by_path(path)
return opt.impl_is_master_slaves('slave')
2017-10-25 08:46:22 +02:00
def getname(self, path):
opt = self._get_obj_by_path(path)
return opt.impl_getname()
def getdoc(self, path):
opt = self._get_obj_by_path(path)
return opt.impl_get_display_name()
2017-10-22 09:48:08 +02:00
class TiramisuAPIOwner(CommonTiramisu):
"""manager option's owner"""
2017-10-25 08:46:22 +02:00
def __init__(self, config, force_permissive, force_unrestraint):
super(TiramisuAPIOwner, self).__init__(config, force_permissive, force_unrestraint)
2017-10-22 09:48:08 +02:00
self.config = config
self.force_permissive = force_permissive
2017-10-25 08:46:22 +02:00
self.force_unrestraint = force_unrestraint
2017-10-22 09:48:08 +02:00
if config:
self.values = self.config.cfgimpl_get_values()
@getter
def get(self):
"""get owner for a specified option"""
pass
def _simple_get(self, path):
##FIXME should not be here, just for check property
#self.config.getattr(path,
# force_permissive=self.force_permissive)
# FIXME doublons aussi
opt = self._get_obj_by_path(path)
return self.values.getowner(opt,
force_permissive=self.force_permissive)
def _slave_get(self, path, index):
##FIXME should not be here, just for check property
#self.config.getattr(path,
# index=index,
# force_permissive=self.force_permissive)
# FIXME doublons aussi
opt = self._get_obj_by_path(path,
index=index)
return self.values.getowner(opt,
index=index,
force_permissive=self.force_permissive)
def isdefault(self, path):
"""is option has defaut value"""
#FIXME isdefault for slave should have index!
opt = self._get_obj_by_path(path)
#FIXME should not be here, just for check property
self.config.getattr(path,
force_permissive=self.force_permissive)
return self.values.is_default_owner(opt,
force_permissive=self.force_permissive)
@setter
def set(self):
"""get owner for a specified option"""
pass
def _simple_set(self, path, owner):
#FIXME doublons, opt est deja dans le setter
opt = self._get_obj_by_path(path)
try:
obj_owner = getattr(owners, owner)
except AttributeError:
owners.addowner(owner)
obj_owner = getattr(owners, owner)
self.values.setowner(opt,
obj_owner,
force_permissive=self.force_permissive)
def _master_set(self, path, owner):
return self._simple_set(path, owner)
def _slave_set(self, path, index, owner):
#FIXME doublons, opt est deja dans le setter
opt = self._get_obj_by_path(path, index)
try:
obj_owner = getattr(owners, owner)
except AttributeError:
owners.addowner(owner)
obj_owner = getattr(owners, owner)
self.values.setowner(opt,
obj_owner,
index,
force_permissive=self.force_permissive)
class TiramisuAPIProperty(CommonTiramisu):
"""manager option's property"""
2017-10-25 08:46:22 +02:00
def __init__(self, config, force_permissive, force_unrestraint):
super(TiramisuAPIProperty, self).__init__(config, force_permissive, force_unrestraint)
2017-10-22 09:48:08 +02:00
self.config = config
self.force_permissive = force_permissive
2017-10-25 08:46:22 +02:00
self.force_unrestraint = force_unrestraint
2017-10-22 09:48:08 +02:00
if config:
self.settings = config.cfgimpl_get_settings()
@getter
def get(self, path):
"""get property for a specified option"""
def _simple_get(self, path):
opt = self._get_obj_by_path(path)
return self.settings.getproperties(opt, path, obj=False)
def _slave_get(self, path, index):
opt = self._get_obj_by_path(path)
return self.settings.getproperties(opt, path, index=index, obj=False)
def set(self, path, properties):
"""set properties for a specified option"""
opt = self._get_obj_by_path(path)
self.settings.setproperties(set(properties),
opt,
path)
def reset(self, path):
"""reset all personalised properties
"""
self._get_obj_by_path(path)
self.settings.reset(_path=path)
class TiramisuAPIValue(CommonTiramisu):
"""manager option's value"""
2017-10-25 08:46:22 +02:00
def __init__(self, config, force_permissive, force_unrestraint):
super(TiramisuAPIValue, self).__init__(config, force_permissive, force_unrestraint)
2017-10-22 09:48:08 +02:00
self.config = config
self.force_permissive = force_permissive
2017-10-25 08:46:22 +02:00
self.force_unrestraint = force_unrestraint
2017-10-22 09:48:08 +02:00
def append(self, path, value=undefined):
opt = self._get_obj_by_path(path)
if not opt.impl_is_master_slaves('master'):
raise APIError('append is only allowed for a master')
multi = self.config.getattr(path,
force_permissive=self.force_permissive)
multi.append(value, force_permissive=self.force_permissive)
@getter
def get(self):
"""get value for a specified option"""
pass
def _simple_get(self, path):
return self.config.getattr(path,
force_permissive=self.force_permissive)
def _slave_get(self, path, index):
return self.config.getattr(path,
index=index,
force_permissive=self.force_permissive)
@setter
def set(self):
"""set a value for a specified option"""
pass
def _simple_set(self, path, value):
self.config.setattr(path,
value,
force_permissive=self.force_permissive)
def _master_set(self, path, index, value):
multi = self.config.getattr(path,
force_permissive=self.force_permissive)
multi[index] = value
def _slave_set(self, path, index, value):
self.config.setattr(path,
value,
index=index,
force_permissive=self.force_permissive)
@resetter
def reset(self, path):
"""reset value for a value"""
pass
def _simple_reset(self, path):
opt = self._get_obj_by_path(path)
self.config.cfgimpl_get_values().reset(opt,
path=path,
force_permissive=self.force_permissive)
def _slave_reset(self, path, index):
2017-10-25 08:46:22 +02:00
self._get_obj_by_path(path)
#FIXME ... _p_ ...
self.config.cfgimpl_get_values()._p_.resetvalue_index(path, index)
2017-10-22 09:48:08 +02:00
class TiramisuAPI(object):
icon = '\u2937'
tmpl_help = ' {} {}: {}'
2017-10-25 08:46:22 +02:00
def __init__(self, config, force_permissive=False, force_unrestraint=False):
2017-10-22 09:48:08 +02:00
self.config = config
self.force_permissive = force_permissive
2017-10-25 08:46:22 +02:00
self.force_unrestraint = force_unrestraint
2017-10-22 09:48:08 +02:00
#FIXME ?
self.config.read_write()
self.config.cfgimpl_get_settings().setpermissive(('hidden',))
#/FIXME ?
self.registers = {}
self.prefix = self.__class__.__name__
for module_name in globals().keys():
if module_name != self.prefix and module_name.startswith(self.prefix):
module = globals()[module_name]
func_name = module_name[11:].lower()
self.registers[func_name] = module
def __getattr__(self, subfunc):
if subfunc in self.registers:
return self.registers[subfunc](self.config,
2017-10-25 08:46:22 +02:00
self.force_permissive,
self.force_unrestraint)
2017-10-22 09:48:08 +02:00
elif subfunc == 'permissive':
2017-10-25 08:46:22 +02:00
return TiramisuAPI(self.config, force_permissive=True, force_unrestraint=self.force_unrestraint)
elif subfunc == 'unrestraint':
return TiramisuAPI(self.config, force_permissive=self.force_permissive, force_unrestraint=True)
2017-10-22 09:48:08 +02:00
elif subfunc == 'help':
return self._help()
else:
raise APIError(_('please specify a valid sub function'))
def _help(self):
txt = ['[permissive]']
for module_name, module in self.registers.items():
module_doc = getdoc(module)
txt.append(self.tmpl_help.format(self.icon, module_name, module_doc))
txt.append(module(None, None).help)
return '\n'.join(txt)
def getapi(config):
"""instanciate TiramisuAPI
:param config: Config object
:type descr: an instance of ``config.Config``
"""
return TiramisuAPI(config)