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