1 # -*- coding: utf-8 -*-
2 "options handler global entry point"
3 # Copyright (C) 2012-2013 Team tiramisu (see AUTHORS for all contributors)
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # The original `Config` design model is unproudly borrowed from
20 # the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
21 # the whole pypy projet is under MIT licence
22 # ____________________________________________________________
24 from tiramisu.error import PropertiesOptionError, ConfigError
25 from tiramisu.option import OptionDescription, Option, SymLinkOption, \
27 from tiramisu.setting import groups, Settings, default_encoding, storage_type
28 from tiramisu.value import Values
29 from tiramisu.i18n import _
33 return str(id(config)) + str(time())
36 class SubConfig(BaseInformation):
37 "sub configuration management entry"
38 __slots__ = ('_impl_context', '_impl_descr', '_impl_path')
40 def __init__(self, descr, context, subpath=None):
41 """ Configuration option management master class
43 :param descr: describes the configuration schema
44 :type descr: an instance of ``option.OptionDescription``
45 :param context: the current root config
46 :type context: `Config`
47 :type subpath: `str` with the path name
49 # main option description
50 if not isinstance(descr, OptionDescription):
51 raise ValueError(_('descr must be an optiondescription, not {0}'
52 ).format(type(descr)))
53 self._impl_descr = descr
54 # sub option descriptions
55 if not isinstance(context, SubConfig):
56 raise ValueError('context must be a SubConfig')
57 self._impl_context = context
58 self._impl_path = subpath
60 def cfgimpl_reset_cache(self, only_expired=False, only=('values',
62 self.cfgimpl_get_context().cfgimpl_reset_cache(only_expired, only)
64 def cfgimpl_get_home_by_path(self, path, force_permissive=False,
65 force_properties=None):
66 """:returns: tuple (config, name)"""
67 path = path.split('.')
68 for step in path[:-1]:
69 self = self._getattr(step,
70 force_permissive=force_permissive,
71 force_properties=force_properties)
75 return hash(self.cfgimpl_get_description().impl_getkey(self))
77 def __eq__(self, other):
79 if not isinstance(other, Config):
81 return self.cfgimpl_get_description().impl_getkey(self) == \
82 other.cfgimpl_get_description().impl_getkey(other)
84 def __ne__(self, other):
86 if not isinstance(other, Config):
88 return not self == other
90 # ______________________________________________________________________
92 """Pythonesque way of parsing group's ordered options.
93 iteration only on Options (not OptionDescriptions)"""
94 for child in self.cfgimpl_get_description().impl_getchildren():
95 if not isinstance(child, OptionDescription):
97 yield child._name, getattr(self, child._name)
100 except PropertiesOptionError:
101 pass # option with properties
104 """A way of parsing options **and** groups.
105 iteration on Options and OptionDescriptions."""
106 for child in self.cfgimpl_get_description().impl_getchildren():
108 yield child._name, getattr(self, child._name)
109 except GeneratorExit:
111 except PropertiesOptionError:
112 pass # option with properties
114 def iter_groups(self, group_type=None):
115 """iteration on groups objects only.
116 All groups are returned if `group_type` is `None`, otherwise the groups
117 can be filtered by categories (families, or whatever).
119 :param group_type: if defined, is an instance of `groups.GroupType`
120 or `groups.MasterGroupType` that lives in
123 if group_type is not None:
124 if not isinstance(group_type, groups.GroupType):
125 raise TypeError(_("unknown group_type: {0}").format(group_type))
126 for child in self.cfgimpl_get_description().impl_getchildren():
127 if isinstance(child, OptionDescription):
129 if group_type is None or (group_type is not None and
130 child.impl_get_group_type()
132 yield child._name, getattr(self, child._name)
133 except GeneratorExit:
135 except PropertiesOptionError:
137 # ______________________________________________________________________
140 "Config's string representation"
142 for name, grp in self.iter_groups():
143 lines.append("[{0}]".format(name))
144 for name, value in self:
146 lines.append("{0} = {1}".format(name, value))
147 except UnicodeEncodeError:
148 lines.append("{0} = {1}".format(name,
149 value.encode(default_encoding)))
150 except PropertiesOptionError:
152 return '\n'.join(lines)
156 def cfgimpl_get_context(self):
157 return self._impl_context
159 def cfgimpl_get_description(self):
160 if self._impl_descr is None:
161 raise ConfigError(_('no option description found for this config'
162 ' (may be metaconfig without meta)'))
164 return self._impl_descr
166 def cfgimpl_get_settings(self):
167 return self.cfgimpl_get_context()._impl_settings
169 def cfgimpl_get_values(self):
170 return self.cfgimpl_get_context()._impl_values
172 # ____________________________________________________________
174 def __setattr__(self, name, value):
175 "attribute notation mechanism for the setting of the value of an option"
176 if name.startswith('_impl_'):
177 #self.__dict__[name] = value
178 object.__setattr__(self, name, value)
180 self._setattr(name, value)
182 def _setattr(self, name, value, force_permissive=False):
184 homeconfig, name = self.cfgimpl_get_home_by_path(name)
185 return homeconfig.__setattr__(name, value)
186 child = getattr(self.cfgimpl_get_description(), name)
187 if not isinstance(child, SymLinkOption):
188 if self._impl_path is None:
191 path = self._impl_path + '.' + name
192 self.cfgimpl_get_values().setitem(child, value, path,
193 force_permissive=force_permissive)
195 context = self.cfgimpl_get_context()
196 path = context.cfgimpl_get_description().impl_get_path_by_opt(
198 context._setattr(path, value, force_permissive=force_permissive)
200 def __delattr__(self, name):
201 child = getattr(self.cfgimpl_get_description(), name)
202 self.cfgimpl_get_values().__delitem__(child)
204 def __getattr__(self, name):
205 return self._getattr(name)
207 def _getattr(self, name, force_permissive=False, force_properties=None,
210 attribute notation mechanism for accessing the value of an option
211 :param name: attribute name
212 :return: option's value if name is an option name, OptionDescription
215 # attribute access by passing a path,
216 # for instance getattr(self, "creole.general.family.adresse_ip_eth0")
218 homeconfig, name = self.cfgimpl_get_home_by_path(
219 name, force_permissive=force_permissive,
220 force_properties=force_properties)
221 return homeconfig._getattr(name, force_permissive=force_permissive,
222 force_properties=force_properties,
225 if name.startswith('_impl_') or name.startswith('cfgimpl_') \
226 or name.startswith('impl_'):
227 # if it were in __dict__ it would have been found already
228 return object.__getattribute__(self, name)
229 opt_or_descr = getattr(self.cfgimpl_get_description(), name)
231 if self._impl_path is None:
234 subpath = self._impl_path + '.' + name
235 if isinstance(opt_or_descr, SymLinkOption):
236 context = self.cfgimpl_get_context()
237 path = context.cfgimpl_get_description().impl_get_path_by_opt(
239 return context._getattr(path, validate=validate,
240 force_properties=force_properties,
241 force_permissive=force_permissive)
242 elif isinstance(opt_or_descr, OptionDescription):
243 self.cfgimpl_get_settings().validate_properties(
244 opt_or_descr, True, False, path=subpath,
245 force_permissive=force_permissive,
246 force_properties=force_properties)
247 return SubConfig(opt_or_descr, self.cfgimpl_get_context(), subpath)
249 return self.cfgimpl_get_values().getitem(
250 opt_or_descr, path=subpath,
252 force_properties=force_properties,
253 force_permissive=force_permissive)
255 def find(self, bytype=None, byname=None, byvalue=None, type_='option'):
257 finds a list of options recursively in the config
259 :param bytype: Option class (BoolOption, StrOption, ...)
260 :param byname: filter by Option._name
261 :param byvalue: filter by the option's value
262 :returns: list of matching Option objects
264 return self.cfgimpl_get_context()._find(bytype, byname, byvalue,
267 _subpath=self.cfgimpl_get_path()
270 def find_first(self, bytype=None, byname=None, byvalue=None,
271 type_='option', display_error=True):
273 finds an option recursively in the config
275 :param bytype: Option class (BoolOption, StrOption, ...)
276 :param byname: filter by Option._name
277 :param byvalue: filter by the option's value
278 :returns: list of matching Option objects
280 return self.cfgimpl_get_context()._find(
281 bytype, byname, byvalue, first=True, type_=type_,
282 _subpath=self.cfgimpl_get_path(), display_error=display_error)
284 def _find(self, bytype, byname, byvalue, first, type_='option',
285 _subpath=None, check_properties=True, display_error=True):
287 convenience method for finding an option that lives only in the subtree
289 :param first: return only one option if True, a list otherwise
290 :return: find list or an exception if nothing has been found
292 def _filter_by_name():
294 if byname is None or path == byname or \
295 path.endswith('.' + byname):
301 def _filter_by_value():
305 value = getattr(self, path)
308 except PropertiesOptionError: # a property is a restriction
309 # upon the access of the value
313 def _filter_by_type():
316 if isinstance(option, bytype):
320 #def _filter_by_attrs():
321 # if byattrs is None:
323 # for key, val in byattrs.items():
324 # print "----", path, key
325 # if path == key or path.endswith('.' + key):
331 if type_ not in ('option', 'path', 'context', 'value'):
332 raise ValueError(_('unknown type_ type {0}'
333 'for _find').format(type_))
335 opts, paths = self.cfgimpl_get_description()._cache_paths
336 for index in range(0, len(paths)):
338 if isinstance(option, OptionDescription):
341 if _subpath is not None and not path.startswith(_subpath + '.'):
343 if not _filter_by_name():
345 if not _filter_by_value():
347 #remove option with propertyerror, ...
350 value = getattr(self, path)
351 except PropertiesOptionError:
352 # a property restricts the access of the value
354 if not _filter_by_type():
356 #if not _filter_by_attrs():
360 elif type_ == 'path':
362 elif type_ == 'option':
364 elif type_ == 'context':
365 retval = self.cfgimpl_get_context()
369 find_results.append(retval)
370 if find_results == []:
372 raise AttributeError(_("no option found in config"
373 " with these criteria"))
376 raise AttributeError()
380 def make_dict(self, flatten=False, _currpath=None, withoption=None,
382 """exports the whole config into a `dict`, for example:
384 >>> print cfg.make_dict()
385 {'od2.var4': None, 'od2.var5': None, 'od2.var6': None}
389 :param flatten: returns a dict(name=value) instead of a dict(path=value)
392 >>> print cfg.make_dict(flatten=True)
393 {'var5': None, 'var4': None, 'var6': None}
395 :param withoption: returns the options that are present in the very same
396 `OptionDescription` than the `withoption` itself::
398 >>> print cfg.make_dict(withoption='var1')
399 {'od2.var4': None, 'od2.var5': None,
401 'od2.var1': u'value',
406 :param withvalue: returns the options that have the value `withvalue`
409 >>> print c.make_dict(withoption='var1',
414 'od2.var1': u'value'}
416 :returns: dict of Option's name (or path) and values
419 if _currpath is None:
421 if withoption is None and withvalue is not None:
422 raise ValueError(_("make_dict can't filtering with value without "
424 if withoption is not None:
425 mypath = self.cfgimpl_get_path()
426 for path in self.cfgimpl_get_context()._find(bytype=Option,
432 path = '.'.join(path.split('.')[:-1])
433 opt = self.cfgimpl_get_context().cfgimpl_get_description(
434 ).impl_get_opt_by_path(path)
435 if mypath is not None:
441 tmypath = mypath + '.'
442 if not path.startswith(tmypath):
443 raise AttributeError(_('unexpected path {0}, '
444 'should start with {1}'
445 '').format(path, mypath))
446 path = path[len(tmypath):]
447 self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
448 #withoption can be set to None below !
449 if withoption is None:
450 for opt in self.cfgimpl_get_description().impl_getchildren():
452 self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
454 options = dict(pathsvalues)
458 def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten):
459 if isinstance(opt, OptionDescription):
461 pathsvalues += getattr(self, path).make_dict(flatten,
464 except PropertiesOptionError:
465 pass # this just a hidden or disabled option
468 value = self._getattr(opt._name)
472 name = '.'.join(_currpath + [opt._name])
473 pathsvalues.append((name, value))
474 except PropertiesOptionError:
475 pass # this just a hidden or disabled option
477 def cfgimpl_get_path(self):
478 descr = self.cfgimpl_get_description()
479 context_descr = self.cfgimpl_get_context().cfgimpl_get_description()
480 return context_descr.impl_get_path_by_opt(descr)
483 class CommonConfig(SubConfig):
484 "abstract base class for the Config and the MetaConfig"
485 __slots__ = ('_impl_values', '_impl_settings', '_impl_meta')
487 def _init_storage(self, config_id, is_persistent):
488 if config_id is None:
489 config_id = gen_id(self)
490 import_lib = 'tiramisu.storage.{0}.storage'.format(storage_type)
491 return __import__(import_lib, globals(), locals(), ['Storage'],
492 -1).Storage(config_id, is_persistent)
494 def _impl_build_all_paths(self):
495 self.cfgimpl_get_description().impl_build_cache()
498 "read only is a global config's setting, see `settings.py`"
499 self.cfgimpl_get_settings().read_only()
501 def read_write(self):
502 "read write is a global config's setting, see `settings.py`"
503 self.cfgimpl_get_settings().read_write()
505 def getowner(self, path):
506 """convenience method to retrieve an option's owner
507 from the config itself
509 opt = self.cfgimpl_get_description().impl_get_opt_by_path(path)
510 return self.cfgimpl_get_values().getowner(opt)
512 def unwrap_from_path(self, path, force_permissive=False):
513 """convenience method to extract and Option() object from the Config()
514 and it is **fast**: finds the option directly in the appropriate
520 homeconfig, path = self.cfgimpl_get_home_by_path(
521 path, force_permissive=force_permissive)
522 return getattr(homeconfig.cfgimpl_get_description(), path)
523 return getattr(self.cfgimpl_get_description(), path)
525 def cfgimpl_get_path(self):
528 def cfgimpl_get_meta(self):
529 return self._impl_meta
532 # ____________________________________________________________
533 class Config(CommonConfig):
534 "main configuration management entry"
537 def __init__(self, descr, config_id=None, is_persistent=False):
538 """ Configuration option management master class
540 :param descr: describes the configuration schema
541 :type descr: an instance of ``option.OptionDescription``
542 :param context: the current root config
543 :type context: `Config`
545 storage = self._init_storage(config_id, is_persistent)
546 self._impl_settings = Settings(self, storage)
547 self._impl_values = Values(self, storage)
548 super(Config, self).__init__(descr, self)
549 self._impl_build_all_paths()
550 self._impl_meta = None
551 self._impl_informations = {}
553 def cfgimpl_reset_cache(self,
555 only=('values', 'settings')):
557 self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
558 if 'settings' in only:
559 self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
562 class MetaConfig(CommonConfig):
563 __slots__ = ('_impl_children',)
565 def __init__(self, children, meta=True, config_id=None, is_persistent=False):
566 if not isinstance(children, list):
567 raise ValueError(_("metaconfig's children must be a list"))
568 self._impl_descr = None
569 self._impl_path = None
571 for child in children:
572 if not isinstance(child, CommonConfig):
573 raise ValueError(_("metaconfig's children "
574 "must be config, not {0}"
575 ).format(type(child)))
576 if self._impl_descr is None:
577 self._impl_descr = child.cfgimpl_get_description()
578 elif not self._impl_descr is child.cfgimpl_get_description():
579 raise ValueError(_('all config in metaconfig must '
580 'have the same optiondescription'))
581 if child.cfgimpl_get_meta() is not None:
582 raise ValueError(_("child has already a metaconfig's"))
583 child._impl_meta = self
585 if config_id is None:
586 config_id = gen_id(self)
587 self._impl_children = children
588 storage = self._init_storage(config_id, is_persistent)
589 self._impl_settings = Settings(self, storage)
590 self._impl_values = Values(self, storage)
591 self._impl_meta = None
592 self._impl_informations = {}
594 def cfgimpl_get_children(self):
595 return self._impl_children
597 def cfgimpl_get_context(self):
598 "a meta config is a config wich has a setting, that is itself"
601 def cfgimpl_reset_cache(self,
603 only=('values', 'settings')):
605 self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
606 if 'settings' in only:
607 self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
608 for child in self._impl_children:
609 child.cfgimpl_reset_cache(only_expired=only_expired, only=only)
611 def set_contexts(self, path, value):
612 for child in self._impl_children:
614 if not isinstance(child, MetaConfig):
615 setattr(child, path, value)
617 child.set_contexts(path, value)
618 except PropertiesOptionError:
621 def find_first_contexts(self, byname=None, bypath=None, byvalue=None,
622 type_='context', display_error=True):
625 if bypath is None and byname is not None and \
626 self.cfgimpl_get_description() is not None:
627 bypath = self._find(bytype=None, byvalue=None, byname=byname,
628 first=True, type_='path',
629 check_properties=False)
632 for child in self._impl_children:
634 if not isinstance(child, MetaConfig):
635 if bypath is not None:
636 if byvalue is not None:
637 if getattr(child, bypath) == byvalue:
641 getattr(child, bypath)
644 ret.append(child.find_first(byname=byname,
647 display_error=False))
649 ret.extend(child.find_first_contexts(byname=byname,
653 display_error=False))
654 except AttributeError:
659 def mandatory_warnings(config):
660 """convenience function to trace Options that are mandatory and
661 where no value has been set
663 :returns: generator of mandatory Option's path
666 #if value in cache, properties are not calculated
667 config.cfgimpl_reset_cache(only=('values',))
668 for path in config.cfgimpl_get_description().impl_getpaths(
669 include_groups=True):
671 config._getattr(path, force_properties=frozenset(('mandatory',)))
672 # XXX raise Exception("ca passe ici")
673 # XXX depuis l'exterieur on donne un paht maintenant ! perturbant !
674 except PropertiesOptionError, err:
675 if err.proptype == ['mandatory']:
677 config.cfgimpl_reset_cache(only=('values',))