tiramisu/tiramisu/option/syndynoptiondescription.py

139 lines
4.8 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
# ____________________________________________________________
from typing import Optional, Iterator, Union
from ..i18n import _
from ..setting import ConfigBag, groups, undefined
#from .baseoption import BaseOption
class SynDynOptionDescription(object):
__slots__ = ('_opt',
'_subpath',
'_suffix')
def __init__(self,
opt,
subpath,
suffix):
self._opt = opt
self._subpath = subpath
self._suffix = suffix
def __getattr__(self, name):
return getattr(self._opt, name)
def impl_getopt(self):
return self._opt
def get_child(self,
name: str,
config_bag: ConfigBag,
subpath: str):
#FIXME -> Union[BaseOption, SynDynOptionDescription]:
if name.endswith(self._suffix):
oname = name[:-len(self._suffix)]
try:
child = self._children[1][self._children[0].index(oname)]
except ValueError:
# when oname not in self._children
pass
else:
return child.to_dynoption(subpath,
self._suffix)
raise AttributeError(_('unknown option "{0}" '
'in syndynoptiondescription "{1}"'
'').format(name, self.impl_getname()))
def impl_getname(self):
return self._opt.impl_getname() + self._suffix
def impl_is_dynoptiondescription(self):
return True
def get_children(self,
config_bag,
dyn=True):
children = []
subpath = self.impl_getpath()
for child in self._opt.get_children(config_bag):
yield child.to_dynoption(subpath,
self._suffix)
def get_children_recursively(self,
bytype, # FIXME : Optional[BaseOption],
byname: Optional[str],
config_bag: ConfigBag,
self_opt=None): # FIXME : BaseOption=None)
# FIXME -> Iterator[Union[BaseOption, SynDynOptionDescription]]:
return self._opt.get_children_recursively(bytype,
byname,
config_bag,
self)
def impl_getpath(self):
subpath = self._subpath
if subpath != '':
subpath += '.'
return subpath + self.impl_getname()
def getmaster(self):
master = self._opt.getmaster()
return master.to_dynoption(self.impl_getpath(),
self._suffix)
def getslaves(self):
subpath = self.impl_getpath()
for slave in self._opt.getslaves():
yield slave.to_dynoption(subpath,
self._suffix)
def impl_get_display_name(self):
return self._opt.impl_get_display_name() + self._suffix
def reset_cache(self,
path,
values,
settings,
resetted_opts):
if self.impl_get_group_type() == groups.master:
master = self.getmaster()
slaves = self.getslaves()
self._reset_cache(path,
master,
slaves,
values,
settings,
resetted_opts)
else:
self._opt.reset_cache(path,
values,
settings,
resetted_opts)
def pop(self,
*args,
**kwargs):
self._opt.pop(*args,
slaves=self.getslaves(),
**kwargs)