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