Home | History | Annotate | Download | only in suite_scheduler
      1 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import ConfigParser
      6 
      7 
      8 def forgive_config_error(func):
      9     """A decorator to make ConfigParser get*() functions return None on fail."""
     10     def wrapper(*args, **kwargs):
     11         try:
     12             return func(*args, **kwargs)
     13         except ConfigParser.Error:
     14             return None
     15     return wrapper
     16 
     17 
     18 class ForgivingConfigParser(ConfigParser.SafeConfigParser):
     19     """A SafeConfigParser that returns None on any error in get*().
     20 
     21     Also implements reread(), which allows any already-read-in configs to be
     22     reloaded from disk on-demand.
     23 
     24     Note that I can't use super() here, as ConfigParser.SafeConfigParser
     25     isn't a new-style class.
     26 
     27     @var _cached_config_file_names: the names of the config files last read().
     28     """
     29 
     30 
     31     def __init__(self):
     32         ConfigParser.SafeConfigParser.__init__(self)
     33         self._cached_config_file_names = ''
     34 
     35 
     36     def read(self, filenames):
     37         """Caches filenames, then performs normal read() functionality.
     38 
     39         @param filenames: string or iterable.  The files to read.
     40         @return list of files that could not be read, as per super class.
     41         """
     42         to_return = ConfigParser.SafeConfigParser.read(self, filenames)
     43         self._cached_config_file_names = filenames
     44         return to_return
     45 
     46 
     47     def reread(self):
     48         """Clear all sections, re-read configs from disk."""
     49         for section in self.sections():
     50             self.remove_section(section)
     51         return ConfigParser.SafeConfigParser.read(
     52             self, self._cached_config_file_names)
     53 
     54 
     55     @forgive_config_error
     56     def getstring(self, section, option):
     57         """Can't override get(), as it breaks the other getters to have get()
     58         return None sometimes."""
     59         return ConfigParser.SafeConfigParser.get(self, section, option)
     60 
     61 
     62     @forgive_config_error
     63     def getint(self, section, option):
     64         return ConfigParser.SafeConfigParser.getint(self, section, option)
     65 
     66 
     67     @forgive_config_error
     68     def getfloat(self, section, option):
     69         return ConfigParser.SafeConfigParser.getfloat(self, section, option)
     70 
     71 
     72     @forgive_config_error
     73     def getboolean(self, section, option):
     74         return ConfigParser.SafeConfigParser.getboolean(self, section, option)
     75