def _get_urlfp(self, url): log.debug("Loading url: %s" % url) try: fp = urllib.urlopen(url) if fp.getcode() == 404: raise exception.ConfigError("url %s does not exist" % url) fp.name = url return fp except IOError, e: raise exception.ConfigError( "error loading config from url %s\n%s" % (url, e))
def _get_fp(self, cfg_file): log.debug("Loading file: %s" % cfg_file) if os.path.exists(cfg_file): if not os.path.isfile(cfg_file): raise exception.ConfigError( 'config %s exists but is not a regular file' % cfg_file) else: raise self.exception_class(cfg_file,self) return open(cfg_file)
def _get_int(self, config, section, option): try: opt = config.getint(section, option) return opt except ConfigParser.NoSectionError: pass except ConfigParser.NoOptionError: pass except ValueError: raise exception.ConfigError( "Expected integer value for setting %s in section [%s]" % (option, section))
def _get_bool(self, config, section, option): try: opt = config.getboolean(section, option) return opt except ConfigParser.NoSectionError: pass except ConfigParser.NoOptionError: pass except ValueError: raise exception.ConfigError( "Expected True/False value for setting %s in section [%s]" % (option, section))
def __load_config(self): fp = self._cfg_file if utils.is_url(fp): cfg = self._get_urlfp(fp) else: cfg = self._get_fp(fp) try: cp = ConfigParser.ConfigParser() cp.readfp(cfg) return cp except ConfigParser.MissingSectionHeaderError: raise exception.ConfigHasNoSections(cfg.name) except ConfigParser.ParsingError, e: raise exception.ConfigError(e)
def _check_required(self, section_name, settings, store): """ Check that all required settings were specified in the config. Raises ConfigError otherwise. Note that if a setting specified has required=True and default is not None then this method will not raise an error because a default was given. In short, if a setting is required you must provide None as the 'default' value. """ section_conf = store for setting in settings: requirements = settings[setting] required = requirements[1] value = section_conf.get(setting) if value is None and required: raise exception.ConfigError( 'missing required option %s in section "%s"' % (setting, section_name))
def _load_settings(self, section_name, settings, store): """ Load section settings into a dictionary """ section = self.config._sections.get(section_name) if not section: raise exception.ConfigSectionMissing( 'Missing section %s in config' % section_name) store.update(section) section_conf = store for setting in settings: requirements = settings[setting] func, required, default, options = requirements func = self.type_validators.get(func) value = func(self.config, section_name, setting) if value is not None: if options and not value in options: raise exception.ConfigError( '"%s" setting in section "%s" must be one of: %s' % (setting, section_name, ', '.join([str(o) for o in options]))) section_conf[setting] = value