tiramisu/tiramisu/option/dynoptiondescription.py

94 lines
4.0 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2018-01-26 07:33:47 +01:00
# Copyright (C) 2017-2018 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/>.
#
# The original `Config` design model is unproudly borrowed from
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
import re
from ..i18n import _
from .optiondescription import OptionDescription
from ..setting import groups, undefined
from ..error import ConfigError
from ..autolib import carry_out_calculation
NAME_REGEXP = re.compile(r'^[a-zA-Z\d\-_]*$')
class DynOptionDescription(OptionDescription):
def __init__(self,
name,
doc,
children,
requires=None,
properties=None,
callback=None,
callback_params=None):
super(DynOptionDescription, self).__init__(name,
doc,
children,
requires,
properties)
# check children + set relation to this dynoptiondescription
for child in children:
if isinstance(child, OptionDescription):
if child.impl_get_group_type() != groups.master:
raise ConfigError(_('cannot set optiondescription in a '
'dynoptiondescription'))
2017-12-19 23:11:45 +01:00
for chld in child.impl_getchildren(config_bag=undefined):
chld._impl_setsubdyn(self)
if child.impl_is_symlinkoption():
raise ConfigError(_('cannot set symlinkoption in a '
'dynoptiondescription'))
child._impl_setsubdyn(self)
# add callback
self.impl_set_callback(callback,
callback_params)
2017-12-19 23:11:45 +01:00
def _validate_calculator(self,
callback,
callback_params):
if callback is None:
raise ConfigError(_('callback is mandatory for the dynoptiondescription "{}"'
'').format(self.impl_get_display_name()))
def _impl_get_suffixes(self,
2017-12-19 23:11:45 +01:00
config_bag):
callback, callback_params = self.impl_get_callback()
values = carry_out_calculation(self,
2017-12-19 23:11:45 +01:00
config_bag.config,
callback,
callback_params,
None,
config_bag)
if not isinstance(values, list):
raise ValueError(_('invalid suffix "{}" for option "{}", must be a list'
'').format(values,
self.impl_get_display_name()))
if len(values) > len(set(values)):
raise ValueError(_('DynOptionDescription callback return not unique value'))
for val in values:
if not isinstance(val, str) or re.match(NAME_REGEXP, val) is None:
raise ValueError(_('invalid suffix "{}" for option "{}"'
'').format(val,
self.impl_get_display_name()))
return values