merge from val_prop_plugin branch
[tiramisu.git] / tiramisu / config.py
1 # -*- coding: utf-8 -*-
2 "pretty small and local configuration management tool"
3 # Copyright (C) 2012-2013 Team tiramisu (see AUTHORS for all contributors)
4 #
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.
9 #
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.
14 #
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
18 #
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 # ____________________________________________________________
23 from time import time
24 from tiramisu.error import PropertiesOptionError, ConfigError
25 from tiramisu.option import OptionDescription, Option, SymLinkOption, \
26     BaseInformation
27 from tiramisu.setting import groups, Settings, default_encoding, default_storage
28 from tiramisu.value import Values
29 from tiramisu.i18n import _
30
31
32 class SubConfig(BaseInformation):
33     "sub configuration management entry"
34     __slots__ = ('_impl_context', '_impl_descr')
35
36     def __init__(self, descr, context):
37         """ Configuration option management master class
38
39         :param descr: describes the configuration schema
40         :type descr: an instance of ``option.OptionDescription``
41         :param context: the current root config
42         :type context: `Config`
43         """
44         # main option description
45         if not isinstance(descr, OptionDescription):
46             raise ValueError(_('descr must be an optiondescription, not {0}'
47                                ).format(type(descr)))
48         self._impl_descr = descr
49         # sub option descriptions
50         if not isinstance(context, SubConfig):
51             raise ValueError('context must be a SubConfig')
52         self._impl_context = context
53
54     def cfgimpl_reset_cache(self, only_expired=False, only=('values',
55                                                             'settings')):
56         self.cfgimpl_get_context().cfgimpl_reset_cache(only_expired, only)
57
58     def cfgimpl_get_home_by_path(self, path, force_permissive=False,
59                                  force_properties=None):
60         """:returns: tuple (config, name)"""
61         path = path.split('.')
62         for step in path[:-1]:
63             self = self._getattr(step,
64                                  force_permissive=force_permissive,
65                                  force_properties=force_properties)
66         return self, path[-1]
67
68     def __hash__(self):
69         return hash(self.cfgimpl_get_description().impl_getkey(self))
70
71     def __eq__(self, other):
72         "Config's comparison"
73         if not isinstance(other, Config):
74             return False
75         return self.cfgimpl_get_description().impl_getkey(self) == \
76             other.cfgimpl_get_description().impl_getkey(other)
77
78     def __ne__(self, other):
79         "Config's comparison"
80         if not isinstance(other, Config):
81             return False
82         return not self == other
83
84     # ______________________________________________________________________
85     def __iter__(self):
86         """Pythonesque way of parsing group's ordered options.
87         iteration only on Options (not OptionDescriptions)"""
88         for child in self.cfgimpl_get_description().impl_getchildren():
89             if not isinstance(child, OptionDescription):
90                 try:
91                     yield child._name, getattr(self, child._name)
92                 except GeneratorExit:
93                     raise StopIteration
94                 except PropertiesOptionError:
95                     pass  # option with properties
96
97     def iter_all(self):
98         """A way of parsing options **and** groups.
99         iteration on Options and OptionDescriptions."""
100         for child in self.cfgimpl_get_description().impl_getchildren():
101             try:
102                 yield child._name, getattr(self, child._name)
103             except GeneratorExit:
104                 raise StopIteration
105             except PropertiesOptionError:
106                 pass  # option with properties
107
108     def iter_groups(self, group_type=None):
109         """iteration on groups objects only.
110         All groups are returned if `group_type` is `None`, otherwise the groups
111         can be filtered by categories (families, or whatever).
112
113         :param group_type: if defined, is an instance of `groups.GroupType`
114                            or `groups.MasterGroupType` that lives in
115                            `setting.groups`
116         """
117         if group_type is not None:
118             if not isinstance(group_type, groups.GroupType):
119                 raise TypeError(_("unknown group_type: {0}").format(group_type))
120         for child in self.cfgimpl_get_description().impl_getchildren():
121             if isinstance(child, OptionDescription):
122                 try:
123                     if group_type is None or (group_type is not None and
124                                               child.impl_get_group_type()
125                                               == group_type):
126                         yield child._name, getattr(self, child._name)
127                 except GeneratorExit:
128                     raise StopIteration
129                 except PropertiesOptionError:
130                     pass
131     # ______________________________________________________________________
132
133     def __str__(self):
134         "Config's string representation"
135         lines = []
136         for name, grp in self.iter_groups():
137             lines.append("[{0}]".format(name))
138         for name, value in self:
139             try:
140                 lines.append("{0} = {1}".format(name, value))
141             except UnicodeEncodeError:
142                 lines.append("{0} = {1}".format(name,
143                                                 value.encode(default_encoding)))
144             except PropertiesOptionError:
145                 pass
146         return '\n'.join(lines)
147
148     __repr__ = __str__
149
150     def cfgimpl_get_context(self):
151         return self._impl_context
152
153     def cfgimpl_get_description(self):
154         if self._impl_descr is None:
155             raise ConfigError(_('no option description found for this config'
156                                 ' (may be metaconfig without meta)'))
157         else:
158             return self._impl_descr
159
160     def cfgimpl_get_settings(self):
161         return self.cfgimpl_get_context()._impl_settings
162
163     def cfgimpl_get_values(self):
164         return self.cfgimpl_get_context()._impl_values
165
166     # ____________________________________________________________
167     # attribute methods
168     def __setattr__(self, name, value):
169         "attribute notation mechanism for the setting of the value of an option"
170         if name.startswith('_impl_'):
171             #self.__dict__[name] = value
172             object.__setattr__(self, name, value)
173             return
174         self._setattr(name, value)
175
176     def _setattr(self, name, value, force_permissive=False):
177         if '.' in name:
178             homeconfig, name = self.cfgimpl_get_home_by_path(name)
179             return homeconfig.__setattr__(name, value)
180         child = getattr(self.cfgimpl_get_description(), name)
181         if not isinstance(child, SymLinkOption):
182             self.cfgimpl_get_values().setitem(child, value,
183                                               force_permissive=force_permissive)
184         else:
185             context = self.cfgimpl_get_context()
186             path = context.\
187                    cfgimpl_get_description().\
188                    impl_get_path_by_opt(child._opt)
189             context._setattr(path, value, force_permissive=force_permissive)
190
191     def __delattr__(self, name):
192         child = getattr(self.cfgimpl_get_description(), name)
193         del(self.cfgimpl_get_values()[child])
194
195     def __getattr__(self, name):
196         return self._getattr(name)
197
198     def _getattr(self, name, force_permissive=False, force_properties=None,
199                  validate=True):
200         """
201         attribute notation mechanism for accessing the value of an option
202         :param name: attribute name
203         :return: option's value if name is an option name, OptionDescription
204                  otherwise
205         """
206         # attribute access by passing a path,
207         # for instance getattr(self, "creole.general.family.adresse_ip_eth0")
208         if '.' in name:
209             homeconfig, name = self.cfgimpl_get_home_by_path(name,
210                                force_permissive=force_permissive,
211                                force_properties=force_properties)
212             return homeconfig._getattr(name, force_permissive=force_permissive,
213                                        force_properties=force_properties,
214                                        validate=validate)
215         # special attributes
216         if name.startswith('_impl_') or name.startswith('cfgimpl_') \
217                 or name.startswith('impl_'):
218             # if it were in __dict__ it would have been found already
219             return object.__getattribute__(self, name)
220         opt_or_descr = getattr(self.cfgimpl_get_description(), name)
221         # symlink options
222         if isinstance(opt_or_descr, SymLinkOption):
223             context = self.cfgimpl_get_context()
224             path = context.cfgimpl_get_description(
225                    ).impl_get_path_by_opt(opt_or_descr._opt)
226             return context._getattr(path, validate=validate,
227                                     force_properties=force_properties,
228                                     force_permissive=force_permissive)
229         elif isinstance(opt_or_descr, OptionDescription):
230             self.cfgimpl_get_settings().validate_properties(opt_or_descr,
231                  True, False,
232                  force_permissive=force_permissive,
233                  force_properties=force_properties)
234             return SubConfig(opt_or_descr, self.cfgimpl_get_context())
235         else:
236             return self.cfgimpl_get_values().getitem(opt_or_descr,
237                         validate=validate,
238                         force_properties=force_properties,
239                         force_permissive=force_permissive)
240
241     def find(self, bytype=None, byname=None, byvalue=None, type_='option'):
242         """
243             finds a list of options recursively in the config
244
245             :param bytype: Option class (BoolOption, StrOption, ...)
246             :param byname: filter by Option._name
247             :param byvalue: filter by the option's value
248             :returns: list of matching Option objects
249         """
250         return self.cfgimpl_get_context()._find(bytype, byname, byvalue,
251                                                 first=False,
252                                                 type_=type_,
253                                                 _subpath=self.cfgimpl_get_path()
254                                                 )
255
256     def find_first(self, bytype=None, byname=None, byvalue=None,
257                    type_='option', display_error=True):
258         """
259             finds an option recursively in the config
260
261             :param bytype: Option class (BoolOption, StrOption, ...)
262             :param byname: filter by Option._name
263             :param byvalue: filter by the option's value
264             :returns: list of matching Option objects
265         """
266         return self.cfgimpl_get_context()._find(bytype, byname, byvalue,
267                                           first=True,
268                                           type_=type_,
269                                           _subpath=self.cfgimpl_get_path(),
270                                           display_error=display_error)
271
272     def _find(self, bytype, byname, byvalue, first, type_='option',
273               _subpath=None, check_properties=True, display_error=True):
274         """
275         convenience method for finding an option that lives only in the subtree
276
277         :param first: return only one option if True, a list otherwise
278         :return: find list or an exception if nothing has been found
279         """
280         def _filter_by_name():
281             try:
282                 if byname is None or path == byname or \
283                                      path.endswith('.' + byname):
284                     return True
285             except IndexError:
286                 pass
287             return False
288
289         def _filter_by_value():
290             if byvalue is None:
291                 return True
292             try:
293                 value = getattr(self, path)
294                 if value == byvalue:
295                     return True
296             except PropertiesOptionError:  # a property is a restriction
297                                            # upon the access of the value
298                 pass
299             return False
300
301         def _filter_by_type():
302             if bytype is None:
303                 return True
304             if isinstance(option, bytype):
305                 return True
306             return False
307
308         #def _filter_by_attrs():
309         #    if byattrs is None:
310         #        return True
311         #    for key, val in byattrs.items():
312         #        print "----", path, key
313         #        if path == key or path.endswith('.' + key):
314         #            if value == val:
315         #                return True
316         #            else:
317         #                return False
318         #    return False
319         if type_ not in ('option', 'path', 'context', 'value'):
320             raise ValueError(_('unknown type_ type {0}'
321                                'for _find').format(type_))
322         find_results = []
323         opts, paths = self.cfgimpl_get_description()._cache_paths
324         for index in range(0, len(paths)):
325             option = opts[index]
326             if isinstance(option, OptionDescription):
327                 continue
328             path = paths[index]
329             if _subpath is not None and not path.startswith(_subpath + '.'):
330                 continue
331             if not _filter_by_name():
332                 continue
333             if not _filter_by_value():
334                 continue
335             #remove option with propertyerror, ...
336             if check_properties:
337                 try:
338                     value = getattr(self, path)
339                 except PropertiesOptionError:
340                     # a property restricts the access of the value
341                     continue
342             if not _filter_by_type():
343                 continue
344             #if not _filter_by_attrs():
345             #    continue
346             if type_ == 'value':
347                 retval = value
348             elif type_ == 'path':
349                 retval = path
350             elif type_ == 'option':
351                 retval = option
352             elif type_ == 'context':
353                 retval = self.cfgimpl_get_context()
354             if first:
355                 return retval
356             else:
357                 find_results.append(retval)
358         if find_results == []:
359             if display_error:
360                 raise AttributeError(_("no option found in config"
361                                        " with these criteria"))
362             else:
363                 #translation is slow
364                 raise AttributeError()
365         else:
366             return find_results
367
368     def make_dict(self, flatten=False, _currpath=None, withoption=None,
369                   withvalue=None):
370         """exports the whole config into a `dict`, for example:
371
372         >>> print cfg.make_dict()
373         {'od2.var4': None, 'od2.var5': None, 'od2.var6': None}
374
375
376
377         :param flatten: returns a dict(name=value) instead of a dict(path=value)
378                         ::
379
380                             >>> print cfg.make_dict(flatten=True)
381                             {'var5': None, 'var4': None, 'var6': None}
382
383         :param withoption: returns the options that are present in the very same
384                            `OptionDescription` than the `withoption` itself::
385
386                                 >>> print cfg.make_dict(withoption='var1')
387                                 {'od2.var4': None, 'od2.var5': None,
388                                 'od2.var6': None,
389                                 'od2.var1': u'value',
390                                 'od1.var1': None,
391                                 'od1.var3': None,
392                                 'od1.var2': None}
393
394         :param withvalue: returns the options that have the value `withvalue`
395                           ::
396
397                             >>> print c.make_dict(withoption='var1',
398                                                   withvalue=u'value')
399                             {'od2.var4': None,
400                             'od2.var5': None,
401                             'od2.var6': None,
402                             'od2.var1': u'value'}
403
404         :returns: dict of Option's name (or path) and values
405         """
406         pathsvalues = []
407         if _currpath is None:
408             _currpath = []
409         if withoption is None and withvalue is not None:
410             raise ValueError(_("make_dict can't filtering with value without "
411                                "option"))
412         if withoption is not None:
413             mypath = self.cfgimpl_get_path()
414             for path in self.cfgimpl_get_context()._find(bytype=Option,
415                                                          byname=withoption,
416                                                          byvalue=withvalue,
417                                                          first=False,
418                                                          type_='path',
419                                                          _subpath=mypath):
420                 path = '.'.join(path.split('.')[:-1])
421                 opt = self.cfgimpl_get_context(
422                            ).cfgimpl_get_description(
423                            ).impl_get_opt_by_path(path)
424                 if mypath is not None:
425                     if mypath == path:
426                         withoption = None
427                         withvalue = None
428                         break
429                     else:
430                         tmypath = mypath + '.'
431                         if not path.startswith(tmypath):
432                             raise AttributeError(_('unexpected path {0}, '
433                                                    'should start with {1}'
434                                                    '').format(path, mypath))
435                         path = path[len(tmypath):]
436                 self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
437         #withoption can be set to None below !
438         if withoption is None:
439             for opt in self.cfgimpl_get_description().impl_getchildren():
440                 path = opt._name
441                 self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten)
442         if _currpath == []:
443             options = dict(pathsvalues)
444             return options
445         return pathsvalues
446
447     def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten):
448         if isinstance(opt, OptionDescription):
449             try:
450                 pathsvalues += getattr(self, path).make_dict(flatten,
451                                                              _currpath +
452                                                              path.split('.'))
453             except PropertiesOptionError:
454                 pass  # this just a hidden or disabled option
455         else:
456             try:
457                 value = self._getattr(opt._name)
458                 if flatten:
459                     name = opt._name
460                 else:
461                     name = '.'.join(_currpath + [opt._name])
462                 pathsvalues.append((name, value))
463             except PropertiesOptionError:
464                 pass  # this just a hidden or disabled option
465
466     def cfgimpl_get_path(self):
467         descr = self.cfgimpl_get_description()
468         context_descr = self.cfgimpl_get_context().cfgimpl_get_description()
469         return context_descr.impl_get_path_by_opt(descr)
470
471
472 class CommonConfig(SubConfig):
473     "abstract base class for the Config and the MetaConfig"
474     __slots__ = ('_impl_values', '_impl_settings', '_impl_meta')
475
476     def _impl_build_all_paths(self):
477         self.cfgimpl_get_description().impl_build_cache()
478
479     def read_only(self):
480         "read only is a global config's setting, see `settings.py`"
481         self.cfgimpl_get_settings().read_only()
482
483     def read_write(self):
484         "read write is a global config's setting, see `settings.py`"
485         self.cfgimpl_get_settings().read_write()
486
487     def getowner(self, path):
488         """convenience method to retrieve an option's owner
489         from the config itself
490         """
491         opt = self.cfgimpl_get_description().impl_get_opt_by_path(path)
492         return self.cfgimpl_get_values().getowner(opt)
493
494     def unwrap_from_path(self, path, force_permissive=False):
495         """convenience method to extract and Option() object from the Config()
496         and it is **fast**: finds the option directly in the appropriate
497         namespace
498
499         :returns: Option()
500         """
501         if '.' in path:
502             homeconfig, path = self.cfgimpl_get_home_by_path(
503                                     path,
504                                     force_permissive=force_permissive)
505             return getattr(homeconfig.cfgimpl_get_description(), path)
506         return getattr(self.cfgimpl_get_description(), path)
507
508     def cfgimpl_get_path(self):
509         return None
510
511     def cfgimpl_get_meta(self):
512         return self._impl_meta
513
514
515 # ____________________________________________________________
516 class Config(CommonConfig):
517     "main configuration management entry"
518     __slots__ = tuple()
519
520     def __init__(self, descr):
521         """ Configuration option management master class
522
523         :param descr: describes the configuration schema
524         :type descr: an instance of ``option.OptionDescription``
525         :param context: the current root config
526         :type context: `Config`
527         """
528         config_id = str(id(self)) + str(time())
529         self._impl_settings = Settings(self, config_id, default_storage)
530         self._impl_values = Values(self, config_id, default_storage)
531         super(Config, self).__init__(descr, self)
532         self._impl_build_all_paths()
533         self._impl_meta = None
534         self._impl_informations = {}
535
536     def cfgimpl_reset_cache(self,
537                             only_expired=False,
538                             only=('values', 'settings')):
539         if 'values' in only:
540             self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
541         if 'settings' in only:
542             self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
543
544
545 class MetaConfig(CommonConfig):
546     __slots__ = ('_impl_children',)
547
548     def __init__(self, children, meta=True):
549         if not isinstance(children, list):
550             raise ValueError(_("metaconfig's children must be a list"))
551         self._impl_descr = None
552         if meta:
553             for child in children:
554                 if not isinstance(child, CommonConfig):
555                     raise ValueError(_("metaconfig's children "
556                                        "must be config, not {0}"
557                                        ).format(type(child)))
558                 if self._impl_descr is None:
559                     self._impl_descr = child.cfgimpl_get_description()
560                 elif not self._impl_descr is child.cfgimpl_get_description():
561                     raise ValueError(_('all config in metaconfig must '
562                                        'have the same optiondescription'))
563                 if child.cfgimpl_get_meta() is not None:
564                     raise ValueError(_("child has already a metaconfig's"))
565                 child._impl_meta = self
566
567         config_id = str(id(self))
568         self._impl_children = children
569         self._impl_settings = Settings(self, config_id, default_storage)
570         self._impl_values = Values(self, config_id, default_storage)
571         self._impl_meta = None
572         self._impl_informations = {}
573
574     def cfgimpl_get_children(self):
575         return self._impl_children
576
577     def cfgimpl_get_context(self):
578         "a meta config is a config wich has a setting, that is itself"
579         return self
580
581     def cfgimpl_reset_cache(self,
582                             only_expired=False,
583                             only=('values', 'settings')):
584         if 'values' in only:
585             self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
586         if 'settings' in only:
587             self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
588         for child in self._impl_children:
589             child.cfgimpl_reset_cache(only_expired=only_expired, only=only)
590
591     def set_contexts(self, path, value):
592         for child in self._impl_children:
593             try:
594                 if not isinstance(child, MetaConfig):
595                     setattr(child, path, value)
596                 else:
597                     child.set_contexts(path, value)
598             except PropertiesOptionError:
599                 pass
600
601     def find_first_contexts(self, byname=None, bypath=None, byvalue=None,
602                             type_='context', display_error=True):
603         ret = []
604         try:
605             if bypath is None and byname is not None and \
606                     self.cfgimpl_get_description() is not None:
607                 bypath = self._find(bytype=None, byvalue=None, byname=byname,
608                                     first=True, type_='path',
609                                     check_properties=False)
610         except ConfigError:
611             pass
612         for child in self._impl_children:
613             try:
614                 if not isinstance(child, MetaConfig):
615                     if bypath is not None:
616                         if byvalue is not None:
617                             if getattr(child, bypath) == byvalue:
618                                 ret.append(child)
619                         else:
620                             #not raise
621                             getattr(child, bypath)
622                             ret.append(child)
623                     else:
624                         ret.append(child.find_first(byname=byname,
625                                                     byvalue=byvalue,
626                                                     type_=type_,
627                                                     display_error=False))
628                 else:
629                     ret.extend(child.find_first_contexts(byname=byname,
630                                                          bypath=bypath,
631                                                          byvalue=byvalue,
632                                                          type_=type_,
633                                                          display_error=False))
634             except AttributeError:
635                 pass
636         return ret
637
638
639 def mandatory_warnings(config):
640     """convenience function to trace Options that are mandatory and
641     where no value has been set
642
643     :returns: generator of mandatory Option's path
644
645     """
646     #if value in cache, properties are not calculated
647     config.cfgimpl_reset_cache(only=('values',))
648     for path in config.cfgimpl_get_description(
649                 ).impl_getpaths(include_groups=True):
650         try:
651             config._getattr(path, force_properties=frozenset(('mandatory',)))
652         except PropertiesOptionError, err:
653             if err.proptype == ['mandatory']:
654                 yield path
655     config.cfgimpl_reset_cache(only=('values',))