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