2c354f4318fdeda9d118dd079a7c264d0d9ceb8b
[tiramisu.git] / tiramisu / option / optiondescription.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2014 Team tiramisu (see AUTHORS for all contributors)
3 #
4 # This program is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Lesser General Public License as published by the
6 # Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # The original `Config` design model is unproudly borrowed from
18 # the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
19 # the whole pypy projet is under MIT licence
20 # ____________________________________________________________
21 from copy import copy
22 import re
23
24
25 from ..i18n import _
26 from ..setting import groups, undefined, owners  # , log
27 from .baseoption import BaseOption, SymLinkOption
28 from . import MasterSlaves
29 from ..error import ConfigError, ConflictError
30 from ..storage import get_storages_option
31 from ..autolib import carry_out_calculation
32
33
34 StorageOptionDescription = get_storages_option('optiondescription')
35
36 name_regexp = re.compile(r'^[a-zA-Z\d\-_]*$')
37
38 import sys
39 if sys.version_info[0] >= 3:  # pragma: optional cover
40     xrange = range
41 del(sys)
42
43
44 class OptionDescription(BaseOption, StorageOptionDescription):
45     """Config's schema (organisation, group) and container of Options
46     The `OptionsDescription` objects lives in the `tiramisu.config.Config`.
47     """
48     __slots__ = tuple()
49
50     def __init__(self, name, doc, children, requires=None, properties=None):
51         """
52         :param children: a list of options (including optiondescriptions)
53
54         """
55         super(OptionDescription, self).__init__(name, doc=doc,
56                                                 requires=requires,
57                                                 properties=properties,
58                                                 callback=False)
59         child_names = []
60         dynopt_names = []
61         for child in children:
62             name = child.impl_getname()
63             child_names.append(name)
64             if isinstance(child, DynOptionDescription):
65                 dynopt_names.append(name)
66
67         #better performance like this
68         valid_child = copy(child_names)
69         valid_child.sort()
70         old = None
71         for child in valid_child:
72             if child == old:  # pragma: optional cover
73                 raise ConflictError(_('duplicate option name: '
74                                       '{0}').format(child))
75             if dynopt_names:
76                 for dynopt in dynopt_names:
77                     if child != dynopt and child.startswith(dynopt):
78                         raise ConflictError(_('option must not start as '
79                                               'dynoptiondescription'))
80             old = child
81         self._add_children(child_names, children)
82         _setattr = object.__setattr__
83         _setattr(self, '_cache_consistencies', None)
84         # the group_type is useful for filtering OptionDescriptions in a config
85         _setattr(self, '_group_type', groups.default)
86
87     def impl_getdoc(self):
88         return self.impl_get_information('doc')
89
90     def impl_validate(self, *args, **kwargs):
91         """usefull for OptionDescription"""
92         pass
93
94     def impl_getpaths(self, include_groups=False, _currpath=None):
95         """returns a list of all paths in self, recursively
96            _currpath should not be provided (helps with recursion)
97         """
98         return _impl_getpaths(self, include_groups, _currpath)
99
100     def impl_build_cache(self, config, path='', _consistencies=None,
101                          cache_option=None, force_store_values=None):
102         """validate duplicate option and set option has readonly option
103         """
104         if cache_option is None:
105             init = True
106             _consistencies = {}
107             cache_option = []
108             force_store_values = []
109         else:
110             init = False
111         for option in self._impl_getchildren(dyn=False):
112             #FIXME specifique id for sqlalchemy?
113             #FIXME avec sqlalchemy ca marche le multi parent ? (dans des configs diffĂ©rentes)
114             cache_option.append(option._get_id())
115             if path == '':
116                 subpath = option.impl_getname()
117             else:
118                 subpath = path + '.' + option.impl_getname()
119             if isinstance(option, OptionDescription):
120                 option._set_readonly(False)
121                 option.impl_build_cache(config, subpath, _consistencies,
122                                         cache_option, force_store_values)
123                 #cannot set multi option as OptionDescription requires
124             else:
125                 option._set_readonly(True)
126                 is_multi = option.impl_is_multi()
127                 if not isinstance(option, SymLinkOption) and 'force_store_value' in option.impl_getproperties():
128                     force_store_values.append((subpath, option))
129                 for func, all_cons_opts, params in option._get_consistencies():
130                     option._valid_consistencies(all_cons_opts[1:])
131                     if is_multi:
132                         is_slave = option.impl_is_master_slaves()
133                         if not is_slave:
134                             raise ValueError(_('malformed consistency option {0} '
135                                                'must be a master/slaves').format(
136                                                    option.impl_getname()))
137                         masterslaves = option.impl_get_master_slaves()
138                     for opt in all_cons_opts:
139                         if is_multi:
140                             if not opt.impl_is_master_slaves():
141                                 raise ValueError(_('malformed consistency option {0} '
142                                                    'must not be a multi for {1}').format(
143                                                        option.impl_getname(), opt.impl_getname()))
144                             elif masterslaves != opt.impl_get_master_slaves():
145                                 raise ValueError(_('malformed consistency option {0} '
146                                                    'must be in same master/slaves for {1}').format(
147                                                        option.impl_getname(), opt.impl_getname()))
148                         _consistencies.setdefault(opt,
149                                                   []).append((func,
150                                                              all_cons_opts,
151                                                              params))
152                 is_slave = None
153                 if is_multi:
154                     all_requires = option.impl_getrequires()
155                     if all_requires != tuple():
156                         for requires in all_requires:
157                             for require in requires:
158                                 #if option in require is a multi:
159                                 # * option in require must be a master or a slave
160                                 # * current option must be a slave (and only a slave)
161                                 # * option in require and current option must be in same master/slaves
162                                 require_opt = require[0]
163                                 if require_opt.impl_is_multi():
164                                     if is_slave is None:
165                                         is_slave = option.impl_is_master_slaves('slave')
166                                         if is_slave:
167                                             masterslaves = option.impl_get_master_slaves()
168                                     if is_slave and require_opt.impl_is_master_slaves():
169                                         if masterslaves != require_opt.impl_get_master_slaves():
170                                             raise ValueError(_('malformed requirements option {0} '
171                                                                'must be in same master/slaves for {1}').format(
172                                                                    require_opt.impl_getname(), option.impl_getname()))
173                                     else:
174                                         raise ValueError(_('malformed requirements option {0} '
175                                                            'must not be a multi for {1}').format(
176                                                                require_opt.impl_getname(), option.impl_getname()))
177         if init:
178             if len(cache_option) != len(set(cache_option)):
179                 for idx in xrange(1, len(cache_option) + 1):
180                     opt = cache_option.pop(0)
181                     if opt in cache_option:
182                         raise ConflictError(_('duplicate option: {0}').format(opt))
183             if _consistencies != {}:
184                 self._cache_consistencies = {}
185                 for opt, cons in _consistencies.items():
186                     if opt._get_id() not in cache_option:  # pragma: optional cover
187                         raise ConfigError(_('consistency with option {0} '
188                                             'which is not in Config').format(
189                                                 opt.impl_getname()))
190                     self._cache_consistencies[opt] = tuple(cons)
191             self._set_readonly(False)
192             for subpath, option in force_store_values:
193                 value = config.cfgimpl_get_values()._get_cached_value(option,
194                                                                       path=subpath,
195                                                                       validate=False,
196                                                                       trusted_cached_properties=False,
197                                                                       validate_properties=True)
198                 if option.impl_is_master_slaves('slave'):
199                     # problem with index
200                     raise ConfigError(_('a slave ({0}) cannot have '
201                                         'force_store_value property').format(subpath))
202                 if option._is_subdyn():
203                     raise ConfigError(_('a dynoption ({0}) cannot have '
204                                         'force_store_value property').format(subpath))
205                 config._impl_values._p_.setvalue(subpath, value,
206                                                  owners.forced, None)
207
208     # ____________________________________________________________
209     def impl_set_group_type(self, group_type):
210         """sets a given group object to an OptionDescription
211
212         :param group_type: an instance of `GroupType` or `MasterGroupType`
213                               that lives in `setting.groups`
214         """
215         if self._group_type != groups.default:  # pragma: optional cover
216             raise TypeError(_('cannot change group_type if already set '
217                             '(old {0}, new {1})').format(self._group_type,
218                                                          group_type))
219         if isinstance(group_type, groups.GroupType):
220             self._group_type = group_type
221             if isinstance(group_type, groups.MasterGroupType):
222                 MasterSlaves(self.impl_getname(), self.impl_getchildren())
223         else:  # pragma: optional cover
224             raise ValueError(_('group_type: {0}'
225                                ' not allowed').format(group_type))
226
227     def _impl_getstate(self, descr=None):
228         """enables us to export into a dict
229         :param descr: parent :class:`tiramisu.option.OptionDescription`
230         """
231         if descr is None:
232             self.impl_build_cache_option()
233             descr = self
234         super(OptionDescription, self)._impl_getstate(descr)
235         self._state_group_type = str(self._group_type)
236         for option in self._impl_getchildren():
237             option._impl_getstate(descr)
238
239     def __getstate__(self):
240         """special method to enable the serialization with pickle
241         """
242         stated = True
243         try:
244             # the `_state` attribute is a flag that which tells us if
245             # the serialization can be performed
246             self._stated
247         except AttributeError:
248             # if cannot delete, _impl_getstate never launch
249             # launch it recursivement
250             # _stated prevent __getstate__ launch more than one time
251             # _stated is delete, if re-serialize, re-lauch _impl_getstate
252             self._impl_getstate()
253             stated = False
254         return super(OptionDescription, self).__getstate__(stated)
255
256     def _impl_setstate(self, descr=None):
257         """enables us to import from a dict
258         :param descr: parent :class:`tiramisu.option.OptionDescription`
259         """
260         if descr is None:
261             self._cache_consistencies = None
262             self.impl_build_cache_option()
263             descr = self
264         self._group_type = getattr(groups, self._state_group_type)
265         if isinstance(self._group_type, groups.MasterGroupType):
266             MasterSlaves(self.impl_getname(), self.impl_getchildren(),
267                          validate=False)
268         del(self._state_group_type)
269         super(OptionDescription, self)._impl_setstate(descr)
270         for option in self._impl_getchildren(dyn=False):
271             option._impl_setstate(descr)
272
273     def __setstate__(self, state):
274         super(OptionDescription, self).__setstate__(state)
275         try:
276             self._stated
277         except AttributeError:
278             self._impl_setstate()
279
280     def _impl_get_suffixes(self, context):
281         callback, callback_params = self.impl_get_callback()
282         values = carry_out_calculation(self, context=context,
283                                        callback=callback,
284                                        callback_params=callback_params)
285         if len(values) > len(set(values)):
286             raise ConfigError(_('DynOptionDescription callback return not uniq value'))
287         for val in values:
288             if not isinstance(val, str) or re.match(name_regexp, val) is None:
289                 raise ValueError(_("invalid suffix: {0} for option").format(val))
290         return values
291
292     def _impl_search_dynchild(self, name=undefined, context=undefined):
293         ret = []
294         for child in self._impl_st_getchildren(context, only_dyn=True):
295             cname = child.impl_getname()
296             if name is undefined or name.startswith(cname):
297                 path = cname
298                 for value in child._impl_get_suffixes(context):
299                     if name is undefined:
300                         ret.append(SynDynOptionDescription(child, cname + value, path + value, value))
301                     elif name == cname + value:
302                         return SynDynOptionDescription(child, name, path + value, value)
303         return ret
304
305     def _impl_get_dynchild(self, child, suffix):
306         name = child.impl_getname() + suffix
307         path = self.impl_getname() + suffix + '.' + name
308         if isinstance(child, OptionDescription):
309             return SynDynOptionDescription(child, name, path, suffix)
310         else:
311             return child._impl_to_dyn(name, path)
312
313     def _impl_getchildren(self, dyn=True, context=undefined):
314         for child in self._impl_st_getchildren(context):
315             cname = child.impl_getname()
316             if dyn and child.impl_is_dynoptiondescription():
317                 path = cname
318                 for value in child._impl_get_suffixes(context):
319                     yield SynDynOptionDescription(child,
320                                                   cname + value,
321                                                   path + value, value)
322             else:
323                 yield child
324
325     def impl_getchildren(self):
326         return list(self._impl_getchildren())
327
328     def __getattr__(self, name, context=undefined):
329         if name.startswith('_'):  # or name.startswith('impl_'):
330             return object.__getattribute__(self, name)
331         if '.' in name:
332             path = name.split('.')[0]
333             subpath = '.'.join(name.split('.')[1:])
334             return self.__getattr__(path, context=context).__getattr__(subpath, context=context)
335         return self._getattr(name, context=context)
336
337
338 class DynOptionDescription(OptionDescription):
339     def __init__(self, name, doc, children, requires=None, properties=None,
340                  callback=None, callback_params=None):
341         super(DynOptionDescription, self).__init__(name, doc, children,
342                                                    requires, properties)
343         for child in children:
344             if isinstance(child, OptionDescription):
345                 if child.impl_get_group_type() != groups.master:
346                     raise ConfigError(_('cannot set optiondescription in a '
347                                         'dynoptiondescription'))
348                 for chld in child._impl_getchildren():
349                     chld._impl_setsubdyn(self)
350             if isinstance(child, SymLinkOption):
351                 raise ConfigError(_('cannot set symlinkoption in a '
352                                     'dynoptiondescription'))
353             if isinstance(child, SymLinkOption):
354                 raise ConfigError(_('cannot set symlinkoption in a '
355                                     'dynoptiondescription'))
356             child._impl_setsubdyn(self)
357         self.impl_set_callback(callback, callback_params)
358         self.commit()
359
360     def _validate_callback(self, callback, callback_params):
361         if callback is None:
362             raise ConfigError(_('callback is mandatory for dynoptiondescription'))
363
364
365 class SynDynOptionDescription(object):
366     __slots__ = ('_opt', '_name', '_path', '_suffix')
367
368     def __init__(self, opt, name, path, suffix):
369         self._opt = opt
370         self._name = name
371         self._path = path
372         self._suffix = suffix
373
374     def __getattr__(self, name, context=undefined):
375         if name in dir(self._opt):
376             return getattr(self._opt, name)
377         return self._opt._getattr(name, suffix=self._suffix, context=context)
378
379     def impl_getname(self):
380         return self._name
381
382     def _impl_getchildren(self, dyn=True, context=undefined):
383         children = []
384         for child in self._opt._impl_getchildren():
385             children.append(self._opt._impl_get_dynchild(child, self._suffix))
386         return children
387
388     def impl_getchildren(self):
389         return self._impl_getchildren()
390
391     def impl_getpath(self, context):
392         return self._path
393
394     def impl_getpaths(self, include_groups=False, _currpath=None):
395         return _impl_getpaths(self, include_groups, _currpath)
396
397     def _impl_getopt(self):
398         return self._opt
399
400
401 def _impl_getpaths(klass, include_groups, _currpath):
402         """returns a list of all paths in klass, recursively
403            _currpath should not be provided (helps with recursion)
404         """
405         if _currpath is None:
406             _currpath = []
407         paths = []
408         for option in klass._impl_getchildren():
409             attr = option.impl_getname()
410             if option.impl_is_optiondescription():
411                 if include_groups:
412                     paths.append('.'.join(_currpath + [attr]))
413                 paths += option.impl_getpaths(include_groups=include_groups,
414                                               _currpath=_currpath + [attr])
415             else:
416                 paths.append('.'.join(_currpath + [attr]))
417         return paths