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