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