148 lines
5.7 KiB
Python
148 lines
5.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright (C) 2017-2023 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
|
|
# ____________________________________________________________
|
|
"""DynOptionDescription
|
|
"""
|
|
import re
|
|
import weakref
|
|
from typing import List, Any
|
|
from itertools import chain
|
|
from ..autolib import ParamOption
|
|
|
|
|
|
from ..i18n import _
|
|
from .optiondescription import OptionDescription
|
|
from .baseoption import BaseOption
|
|
from ..setting import OptionBag, ConfigBag, undefined
|
|
from ..error import ConfigError
|
|
from ..autolib import Calculation
|
|
|
|
|
|
NAME_REGEXP = re.compile(r'^[a-zA-Z\d\-_]*$')
|
|
|
|
|
|
class DynOptionDescription(OptionDescription):
|
|
"""dyn option description
|
|
"""
|
|
__slots__ = ('_suffixes',)
|
|
|
|
def __init__(self,
|
|
name: str,
|
|
doc: str,
|
|
children: List[BaseOption],
|
|
suffixes: Calculation,
|
|
properties=None,
|
|
) -> None:
|
|
# pylint: disable=too-many-arguments
|
|
super().__init__(name,
|
|
doc,
|
|
children,
|
|
properties,
|
|
)
|
|
# check children + set relation to this dynoptiondescription
|
|
wself = weakref.ref(self)
|
|
for child in children:
|
|
child._setsubdyn(wself)
|
|
# add suffixes
|
|
if __debug__ and not isinstance(suffixes, Calculation):
|
|
raise ConfigError(_('suffixes in dynoptiondescription has to be a calculation'))
|
|
for param in chain(suffixes.params.args, suffixes.params.kwargs.values()):
|
|
if isinstance(param, ParamOption):
|
|
param.option._add_dependency(self,
|
|
is_suffix=True,
|
|
)
|
|
self._suffixes = suffixes
|
|
|
|
def convert_suffix_to_path(self,
|
|
suffix: Any,
|
|
) -> str:
|
|
"""convert suffix to use it to a path
|
|
"""
|
|
if suffix is None:
|
|
return None
|
|
if not isinstance(suffix, str):
|
|
suffix = str(suffix)
|
|
if '.' in suffix:
|
|
suffix = suffix.replace('.', '_')
|
|
return suffix
|
|
|
|
def get_suffixes(self,
|
|
config_bag: ConfigBag,
|
|
) -> List[str]:
|
|
"""get dynamic suffixes
|
|
"""
|
|
option_bag = OptionBag(self,
|
|
None,
|
|
config_bag,
|
|
properties=None,
|
|
)
|
|
values = self._suffixes.execute(option_bag)
|
|
if values is None:
|
|
values = []
|
|
values_ = []
|
|
if __debug__:
|
|
if not isinstance(values, list):
|
|
raise ValueError(_('DynOptionDescription suffixes for '
|
|
f'option "{self.impl_get_display_name()}", is not '
|
|
f'a list ({values})'))
|
|
for val in values:
|
|
cval = self.convert_suffix_to_path(val)
|
|
if not isinstance(cval, str) or re.match(NAME_REGEXP, cval) is None:
|
|
if __debug__ and cval is not None:
|
|
raise ValueError(_('invalid suffix "{}" for option "{}"'
|
|
'').format(cval,
|
|
self.impl_get_display_name()))
|
|
else:
|
|
values_.append(val)
|
|
if __debug__ and len(values_) > len(set(values_)):
|
|
raise ValueError(_(f'DynOptionDescription "{self._name}" suffixes return a list with '
|
|
f'same values "{values_}"'''))
|
|
return values_
|
|
|
|
def impl_is_dynoptiondescription(self) -> bool:
|
|
return True
|
|
|
|
def get_sub_children(self,
|
|
option,
|
|
config_bag,
|
|
index=None,
|
|
properties=undefined,
|
|
):
|
|
if option == self:
|
|
rootpath = self.impl_getpath().rsplit('.', 1)[0]
|
|
else:
|
|
rootpath = self.impl_getpath()
|
|
subpath = option.impl_getpath()[len(rootpath):].rsplit('.', 1)[0]
|
|
for suffix in self.get_suffixes(config_bag):
|
|
path_suffix = self.convert_suffix_to_path(suffix)
|
|
if option == self:
|
|
parent_path = rootpath
|
|
else:
|
|
parent_path = rootpath + path_suffix + subpath
|
|
doption_bag = OptionBag(option.to_dynoption(parent_path,
|
|
suffix,
|
|
self,
|
|
),
|
|
index,
|
|
config_bag,
|
|
properties=properties,
|
|
ori_option=option
|
|
)
|
|
yield doption_bag
|