tiramisu/tiramisu/option/syndynoptiondescription.py

130 lines
4.5 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 ..i18n import _
from ..setting import groups, undefined
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 impl_getchild(self,
name,
config_bag,
subpath):
try:
if name.endswith(self._suffix):
oname = name[:-len(self._suffix)]
child = self._children[1][self._children[0].index(oname)]
return self.impl_get_dynchild(child,
self._suffix,
subpath)
except ValueError:
# when oname not in self._children
pass
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_getchildren(self,
config_bag,
dyn=True):
children = []
subpath = self.impl_getpath()
for child in self._opt.impl_getchildren(config_bag):
yield self._opt.impl_get_dynchild(child,
self._suffix,
subpath)
def impl_get_options(self,
bytype,
byname,
config_bag):
return self._opt.impl_get_options(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.impl_get_dynoption(self.impl_getpath(),
self._suffix)
def getslaves(self):
subpath = self.impl_getpath()
for slave in self._opt.getslaves():
yield slave.impl_get_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)