100 lines
4.3 KiB
Python
100 lines
4.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
# 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
|
|
from .syndynoptiondescription import SynDynOptionDescription
|
|
|
|
|
|
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().__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'))
|
|
for chld in child.impl_getchildren(config_bag=undefined):
|
|
chld._setsubdyn(self)
|
|
if child.impl_is_symlinkoption():
|
|
raise ConfigError(_('cannot set symlinkoption in a '
|
|
'dynoptiondescription'))
|
|
child._setsubdyn(self)
|
|
# add callback
|
|
self._impl_set_callback(callback,
|
|
callback_params)
|
|
|
|
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,
|
|
option_bag):
|
|
callback, callback_params = self.impl_get_callback()
|
|
values = carry_out_calculation(self,
|
|
option_bag.config_bag.context,
|
|
callback,
|
|
callback_params,
|
|
None,
|
|
option_bag)
|
|
if not isinstance(values, list):
|
|
raise ValueError(_('DynOptionDescription callback for option "{}", is not a list ({})'
|
|
'').format(self.impl_get_display_name(), values))
|
|
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
|
|
|
|
def get_syndynoptiondescriptions(self, option_bag):
|
|
subpath = self.impl_getpath(option_bag.config_bag.context).rsplit('.', 1)[0]
|
|
for suffix in self._impl_get_suffixes(option_bag):
|
|
yield SynDynOptionDescription(self,
|
|
subpath,
|
|
suffix)
|