f4fabdd58c0fdefa87dd12d625209a933ed792a8
[tiramisu.git] / config.py
1 # -*- coding: utf-8 -*-
2 "pretty small and local configuration management tool"
3 # Copyright (C) 2012 Team tiramisu (see README 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 error import (HiddenOptionError, ConfigError, NotFoundError, 
24                 AmbigousOptionError, ConflictConfigError, NoMatchingOptionFound, 
25                             SpecialOwnersError, MandatoryError, MethodCallError, 
26                                            DisabledOptionError, ModeOptionError)
27 from option import (OptionDescription, Option, SymLinkOption, group_types, 
28                     Multi, apply_requires, modes)
29 from autolib import special_owners, special_owner_factory
30 # ______________________________________________________________________
31 # generic owner. 'default' is the general config owner after init time
32 default_owner = 'user'
33 # ____________________________________________________________
34 class Config(object):
35     _cfgimpl_hidden = True
36     _cfgimpl_disabled = True
37     _cfgimpl_mandatory = True
38     _cfgimpl_frozen = False
39     _cfgimpl_owner = default_owner
40     _cfgimpl_toplevel = None
41     _cfgimpl_mode = 'normal'
42     
43     def __init__(self, descr, parent=None, **overrides):
44         self._cfgimpl_descr = descr
45         self._cfgimpl_value_owners = {}
46         self._cfgimpl_parent = parent
47         # `Config()` indeed takes care of the `Option()`'s values
48         self._cfgimpl_values = {}
49         self._cfgimpl_previous_values = {}
50         # XXX warnings are a great idea, let's make up a better use of it 
51         self._cfgimpl_warnings = []
52         self._cfgimpl_toplevel = self._cfgimpl_get_toplevel()
53         # `freeze()` allows us to carry out this calculation again if necessary 
54         self._cfgimpl_frozen = self._cfgimpl_toplevel._cfgimpl_frozen 
55         self._cfgimpl_build(overrides)
56
57     def _validate_duplicates(self, children):
58         duplicates = []
59         for dup in children:
60             if dup._name not in duplicates:
61                 duplicates.append(dup._name)
62             else:
63                 raise ConflictConfigError('duplicate option name: ' 
64                     '{0}'.format(dup._name))
65
66     def _cfgimpl_build(self, overrides):
67         self._validate_duplicates(self._cfgimpl_descr._children)
68         for child in self._cfgimpl_descr._children:
69             if isinstance(child, Option):
70                 if child.is_multi():
71                     childdef = Multi(child.getdefault(), config=self, 
72                                      child=child)
73                     self._cfgimpl_values[child._name] = childdef
74                     self._cfgimpl_previous_values[child._name] = childdef
75                 else:
76                     childdef = child.getdefault()
77                     self._cfgimpl_values[child._name] = childdef
78                     self._cfgimpl_previous_values[child._name] = childdef 
79                 if child.getcallback() is not None:
80                     if child._is_hidden():
81                         self._cfgimpl_value_owners[child._name] = 'auto'
82                     else:
83                         self._cfgimpl_value_owners[child._name] = 'fill'
84                 else:
85                     if child.is_multi():
86                         self._cfgimpl_value_owners[child._name] = ['default' \
87                             for i in range(len(child.getdefault() ))]
88                     else:
89                         self._cfgimpl_value_owners[child._name] = 'default'
90             elif isinstance(child, OptionDescription):
91                 self._validate_duplicates(child._children)
92                 self._cfgimpl_values[child._name] = Config(child, parent=self)
93         self.override(overrides)
94
95     def cfgimpl_update(self):
96         "dynamically adds `Option()` or `OptionDescription()`"
97         # Nothing is static. Everything evolve.
98         # FIXME this is an update for new options in the schema only 
99         #┬ásee the update_child() method of the descr object 
100         for child in self._cfgimpl_descr._children:
101             if isinstance(child, Option):
102                 if child._name not in self._cfgimpl_values:
103                     self._cfgimpl_values[child._name] = child.getdefault()
104                     # FIXME and ['default', ...] if is_multi() ?
105                     self._cfgimpl_value_owners[child._name] = 'default'
106             elif isinstance(child, OptionDescription):
107                 if child._name not in self._cfgimpl_values:
108                     self._cfgimpl_values[child._name] = Config(child, parent=self)
109
110     def override(self, overrides):
111         for name, value in overrides.iteritems():
112             homeconfig, name = self._cfgimpl_get_home_by_path(name)
113             #  if there are special_owners, impossible to override
114             if homeconfig._cfgimpl_value_owners[name] in special_owners:
115                 raise SpecialOwnersError("cannot override option: {0} because "
116                                             "of its special owner".format(name))
117             homeconfig.setoption(name, value, 'default')
118
119     def cfgimpl_set_owner(self, owner):
120         self._cfgimpl_owner = owner
121         for child in self._cfgimpl_descr._children:
122             if isinstance(child, OptionDescription):
123                 self._cfgimpl_values[child._name].cfgimpl_set_owner(owner)
124     # ____________________________________________________________
125     def cfgimpl_hide(self):
126         if self._cfgimpl_parent != None:
127             raise MethodCallError("this method root_hide() shall not be"
128                                            "used with non-root Config() object") 
129         rootconfig = self._cfgimpl_get_toplevel()
130         rootconfig._cfgimpl_hidden = True
131
132     def cfgimpl_show(self):
133         if self._cfgimpl_parent != None:
134             raise MethodCallError("this method root_hide() shall not be"
135                                            "used with non-root Config() object") 
136         rootconfig = self._cfgimpl_get_toplevel()
137         rootconfig._cfgimpl_hidden = False
138     # ____________________________________________________________
139     def cfgimpl_disable(self):
140         if self._cfgimpl_parent != None:
141             raise MethodCallError("this method root_hide() shall not be"
142                                            "used with non-root Confit() object") 
143         rootconfig = self._cfgimpl_get_toplevel()
144         rootconfig._cfgimpl_disabled = True
145
146     def cfgimpl_enable(self):
147         if self._cfgimpl_parent != None:
148             raise MethodCallError("this method root_hide() shall not be"
149                                            "used with non-root Confit() object") 
150         rootconfig = self._cfgimpl_get_toplevel()
151         rootconfig._cfgimpl_disabled = False
152     # ____________________________________________________________
153     def __setattr__(self, name, value):
154         if '.' in name:
155             homeconfig, name = self._cfgimpl_get_home_by_path(name)
156             return setattr(homeconfig, name, value)
157
158         if name.startswith('_cfgimpl_'):
159             self.__dict__[name] = value
160             return
161         if self._cfgimpl_frozen and getattr(self, name) != value:
162             raise TypeError("trying to change a value in a frozen config"
163                                                 ": {0} {1}".format(name, value))
164         if type(getattr(self._cfgimpl_descr, name)) != SymLinkOption:
165             self._validate(name, getattr(self._cfgimpl_descr, name))
166         self.setoption(name, value, self._cfgimpl_owner)
167         
168     def _validate(self, name, opt_or_descr):
169         apply_requires(opt_or_descr, self) 
170         if not type(opt_or_descr) == OptionDescription:
171             # hidden options
172             if self._cfgimpl_toplevel._cfgimpl_hidden and \
173                 (opt_or_descr._is_hidden() or self._cfgimpl_descr._is_hidden()):
174                 raise HiddenOptionError("trying to access to a hidden option:"
175                                                            " {0}".format(name))
176             # disabled options
177             if self._cfgimpl_toplevel._cfgimpl_disabled and \
178             (opt_or_descr._is_disabled() or self._cfgimpl_descr._is_disabled()):
179                 raise DisabledOptionError("this option is disabled:"
180                                                             " {0}".format(name))
181             # expert options 
182             # XXX currently doesn't look at the group, is it really necessary ?
183             if self._cfgimpl_toplevel._cfgimpl_mode != 'normal':
184                 if opt_or_descr.get_mode() != 'normal':
185                     raise ModeOptionError("this option's mode is not normal:"
186                                                             " {0}".format(name))
187
188     def __getattr__(self, name):
189         # attribute access by passing a path, 
190         # for instance getattr(self, "creole.general.family.adresse_ip_eth0") 
191         if '.' in name:
192             homeconfig, name = self._cfgimpl_get_home_by_path(name)
193             return getattr(homeconfig, name)
194         opt_or_descr = getattr(self._cfgimpl_descr, name)
195         # symlink options 
196         if type(opt_or_descr) == SymLinkOption:
197             return getattr(self, opt_or_descr.path)
198         self._validate(name, opt_or_descr)
199         # special attributes
200         if name.startswith('_cfgimpl_'):
201             # if it were in __dict__ it would have been found already
202             return self.__dict__[name]
203             raise AttributeError("%s object has no attribute %s" %
204                                  (self.__class__, name))
205         if name not in self._cfgimpl_values:
206             raise AttributeError("%s object has no attribute %s" %
207                                  (self.__class__, name))
208         if name in self._cfgimpl_value_owners:
209             owner = self._cfgimpl_value_owners[name]
210             if owner in special_owners:
211                 value = self._cfgimpl_values[name]
212                 if value != None:
213                     if opt_or_descr.is_multi():
214                         if owner == 'fill' and None not in value:
215                             return value
216                     else:
217                         if owner == 'fill' and value != None:
218                             return value
219                 result = special_owner_factory(name, owner, 
220                             value=value,
221                             callback=opt_or_descr.getcallback(),
222                             callback_params=opt_or_descr.getcallback_params(),
223                             config=self._cfgimpl_get_toplevel())
224                 # this result **shall not** be a list 
225                 # for example, [1, 2, 3, None] -> [1, 2, 3, result]
226                 if type(result) == list:
227                     raise ConfigError('invalid calculated value returned'
228                         ' for option {0} : shall not be a list'.format(name))
229                 if result != None and not opt_or_descr._validate(result):
230                     raise ConfigError('invalid calculated value returned'
231                         ' for option {0}'.format(name))
232                 if opt_or_descr.is_multi():
233                     if value == []:
234                         _result = [result]
235                     else:
236                         _result = []
237                         for val in value:
238                             if val == None:
239                                 val = result
240                             _result.append(val)
241                 else:
242                     _result = result
243                 return _result
244         # mandatory options
245         if not isinstance(opt_or_descr, OptionDescription):
246             homeconfig = self._cfgimpl_get_toplevel()
247             mandatory = homeconfig._cfgimpl_mandatory
248             if opt_or_descr.is_mandatory() and mandatory:
249                 if self._cfgimpl_values[name] == None\
250                   and opt_or_descr.getdefault() == None:
251                     raise MandatoryError("option: {0} is mandatory " 
252                                           "and shall have a value".format(name))
253         # for multi types with None in the value (ex: ["egg", None, "spam"])
254         # None is replaced by default_multi
255         if not isinstance(opt_or_descr, OptionDescription):
256             if opt_or_descr.is_multi() and None in self._cfgimpl_values[name]:
257                 newvalue = []
258                 for val in self._cfgimpl_values[name]:
259                     if val is None:
260                         newvalue.append(opt_or_descr.default_multi)
261                     else:
262                         newvalue.append(val)
263                 return newvalue
264         return self._cfgimpl_values[name]
265         
266     def __dir__(self):
267         #from_type = dir(type(self))
268         from_dict = list(self.__dict__)
269         extras = list(self._cfgimpl_values)
270         return sorted(set(extras + from_dict))
271
272     def unwrap_from_name(self, name):
273         # didn't have to stoop so low: `self.get()` must be the proper method
274         # **and it is slow**: it recursively searches into the namespaces
275         paths = self.getpaths(allpaths=True)
276         opts = dict([(path, self.unwrap_from_path(path)) for path in paths])
277         all_paths = [p.split(".") for p in self.getpaths()]
278         for pth in all_paths:
279             if name in pth:
280                 return opts[".".join(pth)]
281         raise NotFoundError("name: {0} not found".format(name))
282         
283     def unwrap_from_path(self, path):
284         # didn't have to stoop so low, `geattr(self, path)` is much better
285         # **fast**: finds the option directly in the appropriate namespace
286         if '.' in path:
287             homeconfig, path = self._cfgimpl_get_home_by_path(path)
288             return getattr(homeconfig._cfgimpl_descr, path)
289         return getattr(self._cfgimpl_descr, path)
290                                             
291     def __delattr__(self, name):
292         # if you use delattr you are responsible for all bad things happening
293         if name.startswith('_cfgimpl_'):
294             del self.__dict__[name]
295             return
296         self._cfgimpl_value_owners[name] = 'default'
297         opt = getattr(self._cfgimpl_descr, name)
298         if isinstance(opt, OptionDescription):
299             raise AttributeError("can't option subgroup")
300         self._cfgimpl_values[name] = getattr(opt, 'default', None)
301
302     def setoption(self, name, value, who=None):
303         #who is **not necessarily** a owner, because it cannot be a list
304         child = getattr(self._cfgimpl_descr, name)
305         if who == None:
306             if child.is_multi():
307                 newowner = [self._cfgimpl_owner for i in range(len(value))] 
308             else:
309                 newowner = self._cfgimpl_owner
310         else:
311             if type(child) != SymLinkOption:
312                 if child.is_multi():
313                     if type(value) != list:
314                         raise ConfigError("invalid value for option:"
315                                        " {0} that is set to multi".format(name))
316                     newowner = [who for i in range(len(value))]
317                 else:
318                     newowner = who 
319         if type(child) != SymLinkOption:
320             if name not in self._cfgimpl_values:
321                 raise AttributeError('unknown option %s' % (name,))
322             # special owners, a value with a owner *auto* cannot be changed
323             oldowner = self._cfgimpl_value_owners[child._name]
324             if oldowner == 'auto':
325                 if who == 'auto':
326                     raise ConflictConfigError('cannot override value to %s for '
327                                           'option %s' % (value, name))
328             if oldowner == who:
329                 oldvalue = getattr(self, name)
330                 if oldvalue == value: #or who in ("default",):
331                     return
332             child.setoption(self, value, who)
333             # if the value owner is 'auto', set the option to hidden 
334             if who == 'auto':
335                 if not child._is_hidden():
336                     child.hide()
337             if (value is None and who != 'default' and not child.is_multi()):
338                 child.setowner(self, 'default')
339                 self._cfgimpl_values[name] = child.getdefault()
340             elif (value == [] and who != 'default' and child.is_multi()):
341                 child.setowner(self, ['default' for i in range(len(child.getdefault()))])
342                 self._cfgimpl_values[name] = child.getdefault()
343             else:         
344                 child.setowner(self, newowner)
345         else:
346             homeconfig = self._cfgimpl_get_toplevel()
347             child.setoption(homeconfig, value, who)
348
349     def set(self, **kwargs):
350         all_paths = [p.split(".") for p in self.getpaths(allpaths=True)]
351         for key, value in kwargs.iteritems():
352             key_p = key.split('.')
353             candidates = [p for p in all_paths if p[-len(key_p):] == key_p]
354             if len(candidates) == 1:
355                 name = '.'.join(candidates[0])
356                 homeconfig, name = self._cfgimpl_get_home_by_path(name)
357                 try:
358                     getattr(homeconfig, name)
359                 except MandatoryError:
360                     pass
361                 except Exception, e:
362                     raise e # HiddenOptionError or DisabledOptionError
363                 homeconfig.setoption(name, value, self._cfgimpl_owner)
364             elif len(candidates) > 1:
365                 raise AmbigousOptionError(
366                     'more than one option that ends with %s' % (key, ))
367             else:
368                 raise NoMatchingOptionFound(
369                     'there is no option that matches %s' 
370                     ' or the option is hidden or disabled'% (key, ))
371
372     def get(self, name):
373         paths = self.getpaths(allpaths=True)
374         pathsvalues = []
375         for path in paths:
376             pathname = path.split('.')[-1]
377             if pathname == name:
378                 try:
379                     value = getattr(self, path)            
380                     return value 
381                 except Exception, e:
382                     raise e
383         raise NotFoundError("option {0} not found in config".format(name))                    
384
385     def _cfgimpl_get_home_by_path(self, path):
386         """returns tuple (config, name)"""
387         path = path.split('.')
388         
389         for step in path[:-1]:
390             self = getattr(self, step)
391         return self, path[-1]
392
393     def _cfgimpl_get_toplevel(self):
394         while self._cfgimpl_parent is not None:
395             self = self._cfgimpl_parent
396         return self
397
398     def cfgimpl_previous_value(self, path):
399         home, name = self._cfgimpl_get_home_by_path(path)
400         return home._cfgimpl_previous_values[name]
401     
402     def get_previous_value(self, name):
403         return self._cfgimpl_previous_values[name]
404              
405     def add_warning(self, warning):
406         self._cfgimpl_get_toplevel()._cfgimpl_warnings.append(warning)
407
408     def get_warnings(self):
409         return self._cfgimpl_get_toplevel()._cfgimpl_warnings
410     # ____________________________________________________________
411     # freeze and read-write statuses
412     def cfgimpl_freeze(self):
413         rootconfig = self._cfgimpl_get_toplevel()
414         rootconfig._cfgimpl_frozen = True
415         self._cfgimpl_frozen = True
416
417     def cfgimpl_unfreeze(self):
418         rootconfig = self._cfgimpl_get_toplevel()
419         rootconfig._cfgimpl_frozen = False
420         self._cfgimpl_frozen = False
421
422     def is_frozen(self):
423         # it should be the same value as self._cfgimpl_frozen...
424         rootconfig = self._cfgimpl_get_toplevel()
425         return rootconfig.__dict__['_cfgimpl_frozen']
426
427     def cfgimpl_read_only(self):
428         # hung up on freeze, hidden and disabled concepts 
429         self.cfgimpl_freeze()
430         rootconfig = self._cfgimpl_get_toplevel()
431         rootconfig._cfgimpl_hidden = False
432         rootconfig._cfgimpl_disabled = True
433         rootconfig._cfgimpl_mandatory = True
434
435     def cfgimpl_set_mode(self, mode):
436         # normal or expert mode
437         rootconfig = self._cfgimpl_get_toplevel()
438         if mode not in modes:
439             raise ConfigError("mode {0} not available".format(mode))
440         rootconfig._cfgimpl_mode = mode
441     
442     def cfgimpl_read_write(self):
443         # hung up on freeze, hidden and disabled concepts
444         self.cfgimpl_unfreeze()
445         rootconfig = self._cfgimpl_get_toplevel()
446         rootconfig._cfgimpl_hidden = True
447         rootconfig._cfgimpl_disabled = False
448         rootconfig._cfgimpl_mandatory = False
449     # ____________________________________________________________
450     def getkey(self):
451         return self._cfgimpl_descr.getkey(self)
452
453     def __hash__(self):
454         return hash(self.getkey())
455
456     def __eq__(self, other):
457         return self.getkey() == other.getkey()
458
459     def __ne__(self, other):
460         return not self == other
461
462     def __iter__(self):
463         # iteration only on Options (not OptionDescriptions)
464         for child in self._cfgimpl_descr._children:
465             if isinstance(child, Option):
466                 try:
467                     yield child._name, getattr(self, child._name)
468                 except:
469                     pass # hidden, disabled option group
470
471     def iter_groups(self, group_type=None):
472         "iteration on OptionDescriptions"
473         if group_type == None:
474             groups = group_types
475         else:
476             if group_type not in group_types:
477                 raise TypeError("Unknown group_type: {0}".format(group_type))
478             groups = [group_type]
479         for child in self._cfgimpl_descr._children:
480             if isinstance(child, OptionDescription):
481                     try:
482                         if child.get_group_type() in groups: 
483                             yield child._name, getattr(self, child._name)
484                     except:
485                         pass # hidden, disabled option
486                     
487     def __str__(self, indent=""):
488         lines = []
489         children = [(child._name, child)
490                     for child in self._cfgimpl_descr._children]
491         children.sort()
492         for name, child in children:
493             if self._cfgimpl_value_owners.get(name, None) == 'default':
494                 continue
495             value = getattr(self, name)
496             if isinstance(value, Config):
497                 substr = value.__str__(indent + "    ")
498             else:
499                 substr = "%s    %s = %s" % (indent, name, value)
500             if substr:
501                 lines.append(substr)
502         if indent and not lines:
503             return ''   # hide subgroups with all default values
504         lines.insert(0, "%s[%s]" % (indent, self._cfgimpl_descr._name,))
505         return '\n'.join(lines)
506
507     def getpaths(self, include_groups=False, allpaths=False):
508         """returns a list of all paths in self, recursively, taking care of 
509         the context (hidden/disabled)
510         """
511         paths = []
512         for path in self._cfgimpl_descr.getpaths(include_groups=include_groups):
513             try: 
514                 value = getattr(self, path)
515             except Exception, e:
516                 if not allpaths:
517                     pass # hidden or disabled option
518                 else:
519                     paths.append(path) # hidden or disabled option added
520             else:
521                 paths.append(path)
522         return paths 
523         
524 def make_dict(config, flatten=False):
525     paths = config.getpaths()
526     pathsvalues = []
527     for path in paths:
528         if flatten:
529             pathname = path.split('.')[-1]
530         else:
531             pathname = path
532         try:
533             value = getattr(config, path)            
534             pathsvalues.append((pathname, value))      
535         except:
536             pass # this just a hidden or disabled option  
537     options = dict(pathsvalues)
538     return options
539 # ____________________________________________________________
540