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
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 __slots__ = ('_impl_context', '_impl_descr', '_impl_path')
37 def __init__(self, descr, context, subpath=None):
38 """ Configuration option management master class
40 :param descr: describes the configuration schema
41 :type descr: an instance of ``option.OptionDescription``
42 :param context: the current root config
43 :type context: `Config`
44 :type subpath: `str` with the path name
46 # main option description
47 if not isinstance(descr, OptionDescription):
48 raise TypeError(_('descr must be an optiondescription, not {0}'
49 ).format(type(descr)))
50 self._impl_descr = descr
51 # sub option descriptions
52 if not isinstance(context, weakref.ReferenceType):
53 raise ValueError('context must be a Weakref')
54 self._impl_context = context
55 self._impl_path = subpath
57 def cfgimpl_reset_cache(self, only_expired=False, only=('values',
59 self._cfgimpl_get_context().cfgimpl_reset_cache(only_expired, only)
61 def cfgimpl_get_home_by_path(self, path, force_permissive=False,
62 force_properties=None):
63 """:returns: tuple (config, name)"""
64 path = path.split('.')
65 for step in path[:-1]:
66 self = self._getattr(step,
67 force_permissive=force_permissive,
68 force_properties=force_properties)
72 return hash(self.cfgimpl_get_description().impl_getkey(self))
74 def __eq__(self, other):
76 if not isinstance(other, Config):
78 return self.cfgimpl_get_description().impl_getkey(self) == \
79 other.cfgimpl_get_description().impl_getkey(other)
81 def __ne__(self, other):
83 if not isinstance(other, Config):
85 return not self == other
87 # ______________________________________________________________________
89 """Pythonesque way of parsing group's ordered options.
90 iteration only on Options (not OptionDescriptions)"""
91 for child in self.cfgimpl_get_description().impl_getchildren():
92 if not isinstance(child, OptionDescription):
94 yield child._name, getattr(self, child._name)
97 except PropertiesOptionError:
98 pass # option with properties
101 """A way of parsing options **and** groups.
102 iteration on Options and OptionDescriptions."""
103 for child in self.cfgimpl_get_description().impl_getchildren():
105 yield child._name, getattr(self, child._name)
106 except GeneratorExit:
108 except PropertiesOptionError:
109 pass # option with properties
111 def iter_groups(self, group_type=None):
112 """iteration on groups objects only.
113 All groups are returned if `group_type` is `None`, otherwise the groups
114 can be filtered by categories (families, or whatever).
116 :param group_type: if defined, is an instance of `groups.GroupType`
117 or `groups.MasterGroupType` that lives in
120 if group_type is not None and not isinstance(group_type,
122 raise TypeError(_("unknown group_type: {0}").format(group_type))
123 for child in self.cfgimpl_get_description().impl_getchildren():
124 if isinstance(child, OptionDescription):
126 if group_type is None or (group_type is not None and
127 child.impl_get_group_type()
129 yield child._name, getattr(self, child._name)
130 except GeneratorExit:
132 except PropertiesOptionError:
134 # ______________________________________________________________________
137 "Config's string representation"
139 for name, grp in self.iter_groups():
140 lines.append("[{0}]".format(name))
141 for name, value in self:
143 lines.append("{0} = {1}".format(name, value))
144 except UnicodeEncodeError:
145 lines.append("{0} = {1}".format(name,
146 value.encode(default_encoding)))
147 except PropertiesOptionError:
149 return '\n'.join(lines)
153 def _cfgimpl_get_context(self):
154 return self._impl_context()
156 def cfgimpl_get_description(self):
157 if self._impl_descr is None:
158 raise ConfigError(_('no option description found for this config'
159 ' (may be metaconfig without meta)'))
161 return self._impl_descr
163 def cfgimpl_get_settings(self):
164 return self._cfgimpl_get_context()._impl_settings
166 def cfgimpl_get_values(self):
167 return self._cfgimpl_get_context()._impl_values
169 # ____________________________________________________________
171 def __setattr__(self, name, value):
172 "attribute notation mechanism for the setting of the value of an option"
173 if name.startswith('_impl_'):
174 object.__setattr__(self, name, value)
176 self._setattr(name, value)
178 def _setattr(self, name, value, force_permissive=False):
180 homeconfig, name = self.cfgimpl_get_home_by_path(name)
181 return homeconfig.__setattr__(name, value)
182 child = getattr(self.cfgimpl_get_description(), name)
183 if not isinstance(child, SymLinkOption):
184 if self._impl_path is None:
187 path = self._impl_path + '.' + name
188 self.cfgimpl_get_values().setitem(child, value, path,
189 force_permissive=force_permissive)
191 context = self._cfgimpl_get_context()
192 path = context.cfgimpl_get_description().impl_get_path_by_opt(
194 context._setattr(path, value, force_permissive=force_permissive)
196 def __delattr__(self, name):
197 child = getattr(self.cfgimpl_get_description(), name)
198 self.cfgimpl_get_values().__delitem__(child)
200 def __getattr__(self, name):
201 return self._getattr(name)
203 def _getattr(self, name, force_permissive=False, force_properties=None,
206 attribute notation mechanism for accessing the value of an option
207 :param name: attribute name
208 :return: option's value if name is an option name, OptionDescription
211 # attribute access by passing a path,
212 # for instance getattr(self, "creole.general.family.adresse_ip_eth0")
214 homeconfig, name = self.cfgimpl_get_home_by_path(
215 name, force_permissive=force_permissive,
216 force_properties=force_properties)
217 return homeconfig._getattr(name, force_permissive=force_permissive,
218 force_properties=force_properties,
220 opt_or_descr = getattr(self.cfgimpl_get_description(), name)
221 if self._impl_path is None:
224 subpath = self._impl_path + '.' + name
226 if isinstance(opt_or_descr, SymLinkOption):
227 context = self._cfgimpl_get_context()
228 path = context.cfgimpl_get_description().impl_get_path_by_opt(
230 return context._getattr(path, validate=validate,
231 force_properties=force_properties,
232 force_permissive=force_permissive)
233 elif isinstance(opt_or_descr, OptionDescription):
234 self.cfgimpl_get_settings().validate_properties(
235 opt_or_descr, True, False, path=subpath,
236 force_permissive=force_permissive,
237 force_properties=force_properties)
238 return SubConfig(opt_or_descr, self._impl_context, subpath)
240 return self.cfgimpl_get_values().getitem(
241 opt_or_descr, path=subpath,
243 force_properties=force_properties,
244 force_permissive=force_permissive)
246 def find(self, bytype=None, byname=None, byvalue=None, type_='option'):
248 finds a list of options recursively in the config
250 :param bytype: Option class (BoolOption, StrOption, ...)
251 :param byname: filter by Option._name
252 :param byvalue: filter by the option's value
253 :returns: list of matching Option objects
255 return self._cfgimpl_get_context()._find(bytype, byname, byvalue,
258 _subpath=self.cfgimpl_get_path()
261 def find_first(self, bytype=None, byname=None, byvalue=None,
262 type_='option', display_error=True):
264 finds an option 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(
272 bytype, byname, byvalue, first=True, type_=type_,
273 _subpath=self.cfgimpl_get_path(), display_error=display_error)
275 def _find(self, bytype, byname, byvalue, first, type_='option',
276 _subpath=None, check_properties=True, display_error=True):
278 convenience method for finding an option that lives only in the subtree
280 :param first: return only one option if True, a list otherwise
281 :return: find list or an exception if nothing has been found
283 def _filter_by_name():
285 if byname is None or path == byname or \
286 path.endswith('.' + byname):
292 def _filter_by_value():
296 value = getattr(self, path)
297 if isinstance(value, Multi):
298 return byvalue in value
300 return value == byvalue
301 except PropertiesOptionError: # a property is a restriction
302 # upon the access of the value
305 def _filter_by_type():
308 if isinstance(option, bytype):
312 if type_ not in ('option', 'path', 'value'):
313 raise ValueError(_('unknown type_ type {0}'
314 'for _find').format(type_))
316 opts, paths = self.cfgimpl_get_description()._cache_paths
317 for index in range(0, len(paths)):
319 if isinstance(option, OptionDescription):
322 if _subpath is not None and not path.startswith(_subpath + '.'):
324 if not _filter_by_name():
326 if not _filter_by_value():
328 if not _filter_by_type():
330 #remove option with propertyerror, ...
331 if byvalue is None and check_properties:
333 value = getattr(self, path)
334 except PropertiesOptionError:
335 # a property restricts the access of the value
339 elif type_ == 'path':
341 elif type_ == 'option':
346 find_results.append(retval)
347 return self._find_return_results(find_results, display_error)
349 def _find_return_results(self, find_results, display_error):
350 if find_results == []:
352 raise AttributeError(_("no option found in config"
353 " with these criteria"))
355 # translation is slow
356 raise AttributeError()
360 def make_dict(self, flatten=False, _currpath=None, withoption=None,
362 """exports the whole config into a `dict`, for example:
364 >>> print cfg.make_dict()
365 {'od2.var4': None, 'od2.var5': None, 'od2.var6': None}
369 :param flatten: returns a dict(name=value) instead of a dict(path=value)
372 >>> print cfg.make_dict(flatten=True)
373 {'var5': None, 'var4': None, 'var6': None}
375 :param withoption: returns the options that are present in the very same
376 `OptionDescription` than the `withoption` itself::
378 >>> print cfg.make_dict(withoption='var1')
379 {'od2.var4': None, 'od2.var5': None,
381 'od2.var1': u'value',
386 :param withvalue: returns the options that have the value `withvalue`
389 >>> print c.make_dict(withoption='var1',
394 'od2.var1': u'value'}
396 :returns: dict of Option's name (or path) and values
399 if _currpath is None:
401 if withoption is None and withvalue is not None:
402 raise ValueError(_("make_dict can't filtering with value without "
404 if withoption is not None:
405 mypath = self.cfgimpl_get_path()
406 for path in self._cfgimpl_get_context()._find(bytype=Option,
412 path = '.'.join(path.split('.')[:-1])
413 opt = self._cfgimpl_get_context().cfgimpl_get_description(
414 ).impl_get_opt_by_path(path)
415 if mypath is not None:
421 tmypath = mypath + '.'
422 if not path.startswith(tmypath):
423 raise AttributeError(_('unexpected path {0}, '
424 'should start with {1}'
425 '').format(path, mypath))
426 path = path[len(tmypath):]
427 self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
428 #withoption can be set to None below !
429 if withoption is None:
430 for opt in self.cfgimpl_get_description().impl_getchildren():
432 self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
434 options = dict(pathsvalues)
438 def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten):
439 if isinstance(opt, OptionDescription):
441 pathsvalues += getattr(self, path).make_dict(flatten,
444 except PropertiesOptionError:
445 pass # this just a hidden or disabled option
448 value = self._getattr(opt._name)
452 name = '.'.join(_currpath + [opt._name])
453 pathsvalues.append((name, value))
454 except PropertiesOptionError:
455 pass # this just a hidden or disabled option
457 def cfgimpl_get_path(self):
458 descr = self.cfgimpl_get_description()
459 context_descr = self._cfgimpl_get_context().cfgimpl_get_description()
460 return context_descr.impl_get_path_by_opt(descr)
463 class CommonConfig(SubConfig):
464 "abstract base class for the Config and the MetaConfig"
465 __slots__ = ('_impl_values', '_impl_settings', '_impl_meta')
467 def _impl_build_all_paths(self):
468 self.cfgimpl_get_description().impl_build_cache()
471 "read only is a global config's setting, see `settings.py`"
472 self.cfgimpl_get_settings().read_only()
474 def read_write(self):
475 "read write is a global config's setting, see `settings.py`"
476 self.cfgimpl_get_settings().read_write()
478 def getowner(self, opt):
479 """convenience method to retrieve an option's owner
480 from the config itself
482 if not isinstance(opt, Option) and not isinstance(opt, SymLinkOption):
483 raise TypeError(_('opt in getowner must be an option not {0}'
484 '').format(type(opt)))
485 return self.cfgimpl_get_values().getowner(opt)
487 def unwrap_from_path(self, path, force_permissive=False):
488 """convenience method to extract and Option() object from the Config()
489 and it is **fast**: finds the option directly in the appropriate
495 homeconfig, path = self.cfgimpl_get_home_by_path(
496 path, force_permissive=force_permissive)
497 return getattr(homeconfig.cfgimpl_get_description(), path)
498 return getattr(self.cfgimpl_get_description(), path)
500 def cfgimpl_get_path(self):
503 def cfgimpl_get_meta(self):
504 return self._impl_meta
507 def impl_set_information(self, key, value):
508 """updates the information's attribute
510 :param key: information's key (ex: "help", "doc"
511 :param value: information's value (ex: "the help string")
513 self._impl_values.set_information(key, value)
515 def impl_get_information(self, key, default=None):
516 """retrieves one information's item
518 :param key: the item string (ex: "help")
520 return self._impl_values.get_information(key, default)
523 # ____________________________________________________________
524 class Config(CommonConfig):
525 "main configuration management entry"
526 __slots__ = ('__weakref__', '_impl_test')
528 def __init__(self, descr, session_id=None, persistent=False):
529 """ Configuration option management master class
531 :param descr: describes the configuration schema
532 :type descr: an instance of ``option.OptionDescription``
533 :param context: the current root config
534 :type context: `Config`
535 :param session_id: session ID is import with persistent Config to
536 retrieve good session
537 :type session_id: `str`
538 :param persistent: if persistent, don't delete storage when leaving
539 :type persistent: `boolean`
541 settings, values = get_storages(self, session_id, persistent)
542 self._impl_settings = Settings(self, settings)
543 self._impl_values = Values(self, values)
544 super(Config, self).__init__(descr, weakref.ref(self))
545 self._impl_build_all_paths()
546 self._impl_meta = None
547 #undocumented option used only in test script
548 self._impl_test = False
550 def __getstate__(self):
551 if self._impl_meta is not None:
552 raise ConfigError('cannot serialize Config with meta')
554 for subclass in self.__class__.__mro__:
555 if subclass is not object:
556 slots.update(subclass.__slots__)
557 slots -= frozenset(['_impl_context', '__weakref__'])
561 state[slot] = getattr(self, slot)
562 except AttributeError:
564 storage = self._impl_values._p_._storage
565 if not storage.serializable:
566 raise ConfigError('this storage is not serialisable, could be a '
567 'none persistent storage')
568 state['_storage'] = {'session_id': storage.session_id,
569 'persistent': storage.persistent}
570 state['_impl_setting'] = _impl_getstate_setting()
573 def __setstate__(self, state):
574 for key, value in state.items():
575 if key not in ['_storage', '_impl_setting']:
576 setattr(self, key, value)
577 set_storage(**state['_impl_setting'])
578 self._impl_context = weakref.ref(self)
579 self._impl_settings.context = weakref.ref(self)
580 self._impl_values.context = weakref.ref(self)
581 storage = get_storage(test=self._impl_test, **state['_storage'])
582 self._impl_values._impl_setstate(storage)
583 self._impl_settings._impl_setstate(storage)
585 def cfgimpl_reset_cache(self,
587 only=('values', 'settings')):
589 self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
590 if 'settings' in only:
591 self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
594 #class MetaConfig(CommonConfig):
595 # __slots__ = ('_impl_children',)
597 # def __init__(self, children, meta=True, session_id=None, persistent=False):
598 # if not isinstance(children, list):
599 # raise ValueError(_("metaconfig's children must be a list"))
600 # self._impl_descr = None
601 # self._impl_path = None
603 # for child in children:
604 # if not isinstance(child, CommonConfig):
605 # raise TypeError(_("metaconfig's children "
606 # "must be config, not {0}"
607 # ).format(type(child)))
608 # if self._impl_descr is None:
609 # self._impl_descr = child.cfgimpl_get_description()
610 # elif not self._impl_descr is child.cfgimpl_get_description():
611 # raise ValueError(_('all config in metaconfig must '
612 # 'have the same optiondescription'))
613 # if child.cfgimpl_get_meta() is not None:
614 # raise ValueError(_("child has already a metaconfig's"))
615 # child._impl_meta = self
617 # self._impl_children = children
618 # settings, values = get_storages(self, session_id, persistent)
619 # self._impl_settings = Settings(self, settings)
620 # self._impl_values = Values(self, values)
621 # self._impl_meta = None
623 # def cfgimpl_get_children(self):
624 # return self._impl_children
626 # def cfgimpl_get_context(self):
627 # "a meta config is a config wich has a setting, that is itself"
630 # def cfgimpl_reset_cache(self,
631 # only_expired=False,
632 # only=('values', 'settings')):
633 # if 'values' in only:
634 # self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
635 # if 'settings' in only:
636 # self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
637 # for child in self._impl_children:
638 # child.cfgimpl_reset_cache(only_expired=only_expired, only=only)
640 # def set_contexts(self, path, value):
641 # for child in self._impl_children:
643 # if not isinstance(child, MetaConfig):
644 # setattr(child, path, value)
646 # child.set_contexts(path, value)
647 # except PropertiesOptionError:
650 # def find_first_contexts(self, byname=None, bypath=None, byvalue=None,
651 # type_='path', display_error=True):
654 # if bypath is None and byname is not None and \
655 # self.cfgimpl_get_description() is not None:
656 # bypath = self._find(bytype=None, byvalue=None, byname=byname,
657 # first=True, type_='path',
658 # check_properties=False,
659 # display_error=display_error)
660 # except ConfigError:
662 # for child in self._impl_children:
664 # if not isinstance(child, MetaConfig):
665 # if bypath is not None:
666 # if byvalue is not None:
667 # if getattr(child, bypath) == byvalue:
671 # getattr(child, bypath)
674 # ret.append(child.find_first(byname=byname,
677 # display_error=False))
679 # ret.extend(child.find_first_contexts(byname=byname,
683 # display_error=False))
684 # except AttributeError:
686 # return self._find_return_results(ret, display_error)
689 def mandatory_warnings(config):
690 """convenience function to trace Options that are mandatory and
691 where no value has been set
693 :returns: generator of mandatory Option's path
696 #if value in cache, properties are not calculated
697 config.cfgimpl_reset_cache(only=('values',))
698 for path in config.cfgimpl_get_description().impl_getpaths(
699 include_groups=True):
701 config._getattr(path, force_properties=frozenset(('mandatory',)))
702 except PropertiesOptionError as err:
703 if err.proptype == ['mandatory']:
705 config.cfgimpl_reset_cache(only=('values',))