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