1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2012-2013 Team tiramisu (see AUTHORS for all contributors)
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # The original `Config` design model is unproudly borrowed from
19 # the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
20 # the whole pypy projet is under MIT licence
21 # ____________________________________________________________
22 "options handler global entry point"
24 from tiramisu.error import PropertiesOptionError, ConfigError
25 from tiramisu.option import OptionDescription, Option, SymLinkOption
26 from tiramisu.setting import groups, Settings, default_encoding
27 from tiramisu.storage import get_storages, get_storage, set_storage, \
28 _impl_getstate_setting
29 from tiramisu.value import Values, Multi
30 from tiramisu.i18n import _
33 class SubConfig(object):
34 """Sub configuration management entry.
35 Tree if OptionDescription's responsability. SubConfig are generated
36 on-demand. A Config is also a SubConfig.
37 Root Config is call context below
39 __slots__ = ('_impl_context', '_impl_descr', '_impl_path')
41 def __init__(self, descr, context, subpath=None):
42 """ Configuration option management master class
44 :param descr: describes the configuration schema
45 :type descr: an instance of ``option.OptionDescription``
46 :param context: the current root config
47 :type context: `Config`
48 :type subpath: `str` with the path name
50 # main option description
51 if descr is not None and not isinstance(descr, OptionDescription):
52 raise TypeError(_('descr must be an optiondescription, not {0}'
53 ).format(type(descr)))
54 self._impl_descr = descr
55 # sub option descriptions
56 if not isinstance(context, weakref.ReferenceType):
57 raise ValueError('context must be a Weakref')
58 self._impl_context = context
59 self._impl_path = subpath
61 def cfgimpl_reset_cache(self, only_expired=False, only=('values',
63 "remove cache (in context)"
64 self._cfgimpl_get_context().cfgimpl_reset_cache(only_expired, only)
66 def cfgimpl_get_home_by_path(self, path, force_permissive=False,
67 force_properties=None):
68 """:returns: tuple (config, name)"""
69 path = path.split('.')
70 for step in path[:-1]:
71 self = self._getattr(step,
72 force_permissive=force_permissive,
73 force_properties=force_properties)
77 return hash(self.cfgimpl_get_description().impl_getkey(self))
79 def __eq__(self, other):
81 if not isinstance(other, Config):
83 return self.cfgimpl_get_description().impl_getkey(self) == \
84 other.cfgimpl_get_description().impl_getkey(other)
86 def __ne__(self, other):
88 if not isinstance(other, Config):
90 return not self == other
92 # ______________________________________________________________________
94 """Pythonesque way of parsing group's ordered options.
95 iteration only on Options (not OptionDescriptions)"""
96 for child in self.cfgimpl_get_description().impl_getchildren():
97 if not isinstance(child, OptionDescription):
99 yield child._name, getattr(self, child._name)
100 except GeneratorExit:
102 except PropertiesOptionError:
103 pass # option with properties
106 """A way of parsing options **and** groups.
107 iteration on Options and OptionDescriptions."""
108 for child in self.cfgimpl_get_description().impl_getchildren():
110 yield child._name, getattr(self, child._name)
111 except GeneratorExit:
113 except PropertiesOptionError:
114 pass # option with properties
116 def iter_groups(self, group_type=None):
117 """iteration on groups objects only.
118 All groups are returned if `group_type` is `None`, otherwise the groups
119 can be filtered by categories (families, or whatever).
121 :param group_type: if defined, is an instance of `groups.GroupType`
122 or `groups.MasterGroupType` that lives in
125 if group_type is not None and not isinstance(group_type,
127 raise TypeError(_("unknown group_type: {0}").format(group_type))
128 for child in self.cfgimpl_get_description().impl_getchildren():
129 if isinstance(child, OptionDescription):
131 if group_type is None or (group_type is not None and
132 child.impl_get_group_type()
134 yield child._name, getattr(self, child._name)
135 except GeneratorExit:
137 except PropertiesOptionError:
139 # ______________________________________________________________________
142 "Config's string representation"
144 for name, grp in self.iter_groups():
145 lines.append("[{0}]".format(name))
146 for name, value in self:
148 lines.append("{0} = {1}".format(name, value))
149 except UnicodeEncodeError:
150 lines.append("{0} = {1}".format(name,
151 value.encode(default_encoding)))
152 except PropertiesOptionError:
154 return '\n'.join(lines)
158 def _cfgimpl_get_context(self):
159 """context could be None, we need to test it
160 context is None only if all reference to `Config` object is deleted
161 (for example we delete a `Config` and we manipulate a reference to
162 old `SubConfig`, `Values`, `Multi` or `Settings`)
164 context = self._impl_context()
166 raise ConfigError(_('the context does not exist anymore'))
169 def cfgimpl_get_description(self):
170 if self._impl_descr is None:
171 raise ConfigError(_('no option description found for this config'
172 ' (may be GroupConfig)'))
174 return self._impl_descr
176 def cfgimpl_get_settings(self):
177 return self._cfgimpl_get_context()._impl_settings
179 def cfgimpl_get_values(self):
180 return self._cfgimpl_get_context()._impl_values
182 # ____________________________________________________________
184 def __setattr__(self, name, value):
185 "attribute notation mechanism for the setting of the value of an option"
186 if name.startswith('_impl_'):
187 object.__setattr__(self, name, value)
189 self._setattr(name, value)
191 def _setattr(self, name, value, force_permissive=False):
193 homeconfig, name = self.cfgimpl_get_home_by_path(name)
194 return homeconfig.__setattr__(name, value)
195 child = getattr(self.cfgimpl_get_description(), name)
196 if isinstance(child, OptionDescription):
197 raise TypeError(_("can't assign to an OptionDescription"))
198 elif not isinstance(child, SymLinkOption):
199 if self._impl_path is None:
202 path = self._impl_path + '.' + name
203 self.cfgimpl_get_values().setitem(child, value, path,
204 force_permissive=force_permissive)
206 context = self._cfgimpl_get_context()
207 path = context.cfgimpl_get_description().impl_get_path_by_opt(
209 context._setattr(path, value, force_permissive=force_permissive)
211 def __delattr__(self, name):
212 child = getattr(self.cfgimpl_get_description(), name)
213 self.cfgimpl_get_values().__delitem__(child)
215 def __getattr__(self, name):
216 return self._getattr(name)
218 def _getattr(self, name, force_permissive=False, force_properties=None,
221 attribute notation mechanism for accessing the value of an option
222 :param name: attribute name
223 :return: option's value if name is an option name, OptionDescription
226 # attribute access by passing a path,
227 # for instance getattr(self, "creole.general.family.adresse_ip_eth0")
229 homeconfig, name = self.cfgimpl_get_home_by_path(
230 name, force_permissive=force_permissive,
231 force_properties=force_properties)
232 return homeconfig._getattr(name, force_permissive=force_permissive,
233 force_properties=force_properties,
235 opt_or_descr = getattr(self.cfgimpl_get_description(), name)
236 if self._impl_path is None:
239 subpath = self._impl_path + '.' + name
241 if isinstance(opt_or_descr, SymLinkOption):
242 context = self._cfgimpl_get_context()
243 path = context.cfgimpl_get_description().impl_get_path_by_opt(
245 return context._getattr(path, validate=validate,
246 force_properties=force_properties,
247 force_permissive=force_permissive)
248 elif isinstance(opt_or_descr, OptionDescription):
249 self.cfgimpl_get_settings().validate_properties(
250 opt_or_descr, True, False, path=subpath,
251 force_permissive=force_permissive,
252 force_properties=force_properties)
253 return SubConfig(opt_or_descr, self._impl_context, subpath)
255 return self.cfgimpl_get_values().getitem(
256 opt_or_descr, path=subpath,
258 force_properties=force_properties,
259 force_permissive=force_permissive)
261 def find(self, bytype=None, byname=None, byvalue=None, type_='option',
262 check_properties=True):
264 finds a list of options recursively in the config
266 :param bytype: Option class (BoolOption, StrOption, ...)
267 :param byname: filter by Option._name
268 :param byvalue: filter by the option's value
269 :returns: list of matching Option objects
271 return self._cfgimpl_get_context()._find(bytype, byname, byvalue,
274 _subpath=self.cfgimpl_get_path(),
275 check_properties=check_properties)
277 def find_first(self, bytype=None, byname=None, byvalue=None,
278 type_='option', display_error=True, check_properties=True):
280 finds an option recursively in the config
282 :param bytype: Option class (BoolOption, StrOption, ...)
283 :param byname: filter by Option._name
284 :param byvalue: filter by the option's value
285 :returns: list of matching Option objects
287 return self._cfgimpl_get_context()._find(
288 bytype, byname, byvalue, first=True, type_=type_,
289 _subpath=self.cfgimpl_get_path(), display_error=display_error,
290 check_properties=check_properties)
292 def _find(self, bytype, byname, byvalue, first, type_='option',
293 _subpath=None, check_properties=True, display_error=True):
295 convenience method for finding an option that lives only in the subtree
297 :param first: return only one option if True, a list otherwise
298 :return: find list or an exception if nothing has been found
300 def _filter_by_name():
302 if byname is None or path == byname or \
303 path.endswith('.' + byname):
309 def _filter_by_value():
313 value = getattr(self, path)
314 if isinstance(value, Multi):
315 return byvalue in value
317 return value == byvalue
318 except PropertiesOptionError: # a property is a restriction
319 # upon the access of the value
322 def _filter_by_type():
325 if isinstance(option, bytype):
329 if type_ not in ('option', 'path', 'value'):
330 raise ValueError(_('unknown type_ type {0}'
331 'for _find').format(type_))
333 opts, paths = self.cfgimpl_get_description()._cache_paths
334 for index in range(0, len(paths)):
336 if isinstance(option, OptionDescription):
339 if _subpath is not None and not path.startswith(_subpath + '.'):
341 if not _filter_by_name():
343 if not _filter_by_value():
345 if not _filter_by_type():
347 #remove option with propertyerror, ...
348 if byvalue is None and check_properties:
350 value = getattr(self, path)
351 except PropertiesOptionError:
352 # a property restricts the access of the value
356 elif type_ == 'path':
358 elif type_ == 'option':
363 find_results.append(retval)
364 return self._find_return_results(find_results, display_error)
366 def _find_return_results(self, find_results, display_error):
367 if find_results == []:
369 raise AttributeError(_("no option found in config"
370 " with these criteria"))
372 # translation is slow
373 raise AttributeError()
377 def make_dict(self, flatten=False, _currpath=None, withoption=None,
379 """exports the whole config into a `dict`, for example:
381 >>> print cfg.make_dict()
382 {'od2.var4': None, 'od2.var5': None, 'od2.var6': None}
386 :param flatten: returns a dict(name=value) instead of a dict(path=value)
389 >>> print cfg.make_dict(flatten=True)
390 {'var5': None, 'var4': None, 'var6': None}
392 :param withoption: returns the options that are present in the very same
393 `OptionDescription` than the `withoption` itself::
395 >>> print cfg.make_dict(withoption='var1')
396 {'od2.var4': None, 'od2.var5': None,
398 'od2.var1': u'value',
403 :param withvalue: returns the options that have the value `withvalue`
406 >>> print c.make_dict(withoption='var1',
411 'od2.var1': u'value'}
413 :returns: dict of Option's name (or path) and values
416 if _currpath is None:
418 if withoption is None and withvalue is not None:
419 raise ValueError(_("make_dict can't filtering with value without "
421 if withoption is not None:
422 mypath = self.cfgimpl_get_path()
423 for path in self._cfgimpl_get_context()._find(bytype=Option,
429 path = '.'.join(path.split('.')[:-1])
430 opt = self._cfgimpl_get_context().cfgimpl_get_description(
431 ).impl_get_opt_by_path(path)
432 if mypath is not None:
438 tmypath = mypath + '.'
439 if not path.startswith(tmypath):
440 raise AttributeError(_('unexpected path {0}, '
441 'should start with {1}'
442 '').format(path, mypath))
443 path = path[len(tmypath):]
444 self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
445 #withoption can be set to None below !
446 if withoption is None:
447 for opt in self.cfgimpl_get_description().impl_getchildren():
449 self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
451 options = dict(pathsvalues)
455 def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten):
456 if isinstance(opt, OptionDescription):
458 pathsvalues += getattr(self, path).make_dict(flatten,
461 except PropertiesOptionError:
462 pass # this just a hidden or disabled option
465 value = self._getattr(opt._name)
469 name = '.'.join(_currpath + [opt._name])
470 pathsvalues.append((name, value))
471 except PropertiesOptionError:
472 pass # this just a hidden or disabled option
474 def cfgimpl_get_path(self):
475 descr = self.cfgimpl_get_description()
476 context_descr = self._cfgimpl_get_context().cfgimpl_get_description()
477 return context_descr.impl_get_path_by_opt(descr)
480 class _CommonConfig(SubConfig):
481 "abstract base class for the Config, GroupConfig and the MetaConfig"
482 __slots__ = ('_impl_values', '_impl_settings', '_impl_meta', '_impl_test')
484 def _impl_build_all_paths(self):
485 self.cfgimpl_get_description().impl_build_cache()
488 "read only is a global config's setting, see `settings.py`"
489 self.cfgimpl_get_settings().read_only()
491 def read_write(self):
492 "read write is a global config's setting, see `settings.py`"
493 self.cfgimpl_get_settings().read_write()
495 def getowner(self, opt):
496 """convenience method to retrieve an option's owner
497 from the config itself
499 if not isinstance(opt, Option) and not isinstance(opt, SymLinkOption):
500 raise TypeError(_('opt in getowner must be an option not {0}'
501 '').format(type(opt)))
502 return self.cfgimpl_get_values().getowner(opt)
504 def unwrap_from_path(self, path, force_permissive=False):
505 """convenience method to extract and Option() object from the Config()
506 and it is **fast**: finds the option directly in the appropriate
512 homeconfig, path = self.cfgimpl_get_home_by_path(
513 path, force_permissive=force_permissive)
514 return getattr(homeconfig.cfgimpl_get_description(), path)
515 return getattr(self.cfgimpl_get_description(), path)
517 def cfgimpl_get_path(self):
520 def cfgimpl_get_meta(self):
521 if self._impl_meta is not None:
522 return self._impl_meta()
525 def impl_set_information(self, key, value):
526 """updates the information's attribute
528 :param key: information's key (ex: "help", "doc"
529 :param value: information's value (ex: "the help string")
531 self._impl_values.set_information(key, value)
533 def impl_get_information(self, key, default=None):
534 """retrieves one information's item
536 :param key: the item string (ex: "help")
538 return self._impl_values.get_information(key, default)
541 def __getstate__(self):
542 if self._impl_meta is not None:
543 #FIXME _impl_meta est un weakref => faut pas sauvegarder mais faut bien savoir si c'est un méta ou pas au final ...
544 #en fait il faut ne pouvoir sérialisé que depuis une MetaConfig ... et pas directement comme pour les options
545 raise ConfigError('cannot serialize Config with MetaConfig')
547 for subclass in self.__class__.__mro__:
548 if subclass is not object:
549 slots.update(subclass.__slots__)
550 slots -= frozenset(['_impl_context', '_impl_meta', '__weakref__'])
554 state[slot] = getattr(self, slot)
555 except AttributeError:
557 storage = self._impl_values._p_._storage
558 if not storage.serializable:
559 raise ConfigError('this storage is not serialisable, could be a '
560 'none persistent storage')
561 state['_storage'] = {'session_id': storage.session_id,
562 'persistent': storage.persistent}
563 state['_impl_setting'] = _impl_getstate_setting()
566 def __setstate__(self, state):
567 for key, value in state.items():
568 if key not in ['_storage', '_impl_setting']:
569 setattr(self, key, value)
570 set_storage(**state['_impl_setting'])
571 self._impl_context = weakref.ref(self)
572 self._impl_settings.context = weakref.ref(self)
573 self._impl_values.context = weakref.ref(self)
574 storage = get_storage(test=self._impl_test, **state['_storage'])
575 self._impl_values._impl_setstate(storage)
576 self._impl_settings._impl_setstate(storage)
577 self._impl_meta = None
580 # ____________________________________________________________
581 class Config(_CommonConfig):
582 "main configuration management entry"
583 __slots__ = ('__weakref__',)
585 def __init__(self, descr, session_id=None, persistent=False):
586 """ Configuration option management master class
588 :param descr: describes the configuration schema
589 :type descr: an instance of ``option.OptionDescription``
590 :param context: the current root config
591 :type context: `Config`
592 :param session_id: session ID is import with persistent Config to
593 retrieve good session
594 :type session_id: `str`
595 :param persistent: if persistent, don't delete storage when leaving
596 :type persistent: `boolean`
598 settings, values = get_storages(self, session_id, persistent)
599 self._impl_settings = Settings(self, settings)
600 self._impl_values = Values(self, values)
601 super(Config, self).__init__(descr, weakref.ref(self))
602 self._impl_build_all_paths()
603 self._impl_meta = None
604 #undocumented option used only in test script
605 self._impl_test = False
607 def cfgimpl_reset_cache(self,
609 only=('values', 'settings')):
611 self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
612 if 'settings' in only:
613 self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
616 class GroupConfig(_CommonConfig):
617 __slots__ = ('_impl_children', '__weakref__')
619 def __init__(self, children, session_id=None, persistent=False,
621 if not isinstance(children, list):
622 raise ValueError(_("metaconfig's children must be a list"))
623 self._impl_children = children
624 settings, values = get_storages(self, session_id, persistent)
625 self._impl_settings = Settings(self, settings)
626 self._impl_values = Values(self, values)
627 super(GroupConfig, self).__init__(_descr, weakref.ref(self))
628 self._impl_meta = None
629 #undocumented option used only in test script
630 self._impl_test = False
632 def cfgimpl_get_children(self):
633 return self._impl_children
635 #def cfgimpl_get_context(self):
636 # "a meta config is a config which has a setting, that is itself"
639 def cfgimpl_reset_cache(self,
641 only=('values', 'settings')):
643 self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
644 if 'settings' in only:
645 self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
646 for child in self._impl_children:
647 child.cfgimpl_reset_cache(only_expired=only_expired, only=only)
649 def setattrs(self, path, value):
650 """Setattr not in current GroupConfig, but in each children
652 for child in self._impl_children:
654 if not isinstance(child, GroupConfig):
655 setattr(child, path, value)
657 child.setattrs(path, value)
658 except PropertiesOptionError:
661 def find_firsts(self, byname=None, bypath=None, byvalue=None,
662 type_='path', display_error=True):
663 """Find first not in current GroupConfig, but in each children
666 #if MetaConfig, all children have same OptionDescription as context
667 #so search only one time for all children
669 if bypath is None and byname is not None and \
670 isinstance(self, MetaConfig):
671 bypath = self._find(bytype=None, byvalue=None, byname=byname,
672 first=True, type_='path',
673 check_properties=False,
674 display_error=display_error)
676 except AttributeError:
678 for child in self._impl_children:
680 if not isinstance(child, MetaConfig):
681 if bypath is not None:
682 #if byvalue is None, try if not raise
683 value = getattr(child, bypath)
684 if byvalue is not None:
685 if isinstance(value, Multi):
694 ret.append(child.find_first(byname=byname,
697 display_error=False))
699 ret.extend(child.find_firsts(byname=byname,
703 display_error=False))
704 except AttributeError:
706 return self._find_return_results(ret, display_error)
709 class MetaConfig(GroupConfig):
712 def __init__(self, children, session_id=None, persistent=False):
714 for child in children:
715 if not isinstance(child, _CommonConfig):
716 raise TypeError(_("metaconfig's children "
717 "should be config, not {0}"
718 ).format(type(child)))
719 if child.cfgimpl_get_meta() is not None:
720 raise ValueError(_("child has already a metaconfig's"))
722 descr = child.cfgimpl_get_description()
723 elif not descr is child.cfgimpl_get_description():
724 raise ValueError(_('all config in metaconfig must '
725 'have the same optiondescription'))
726 child._impl_meta = weakref.ref(self)
728 super(MetaConfig, self).__init__(children, session_id, persistent, descr)
731 def mandatory_warnings(config):
732 """convenience function to trace Options that are mandatory and
733 where no value has been set
735 :returns: generator of mandatory Option's path
738 #if value in cache, properties are not calculated
739 config.cfgimpl_reset_cache(only=('values',))
740 for path in config.cfgimpl_get_description().impl_getpaths(
741 include_groups=True):
743 config._getattr(path, force_properties=frozenset(('mandatory',)))
744 except PropertiesOptionError as err:
745 if err.proptype == ['mandatory']:
749 config.cfgimpl_reset_cache(only=('values',))