def check_config(interpret): sections = globals() for section in options.sections: if not section in sections: raise ConfigParser.NoSectionError(section) for opt_section, option in options.booleans: if opt_section == section and option not in sections[section]: raise ConfigParser.NoOptionError(option, section) for opt_section, option in options.floats: if opt_section == section and option not in sections[section]: raise ConfigParser.NoOptionError(option, section) for opt_section, option in options.integers: if opt_section == section and option not in sections[section]: raise ConfigParser.NoOptionError(option, section) for opt_section, option in options.lists: if opt_section == section and option not in sections[section]: raise ConfigParser.NoOptionError(option, section) for opt_section, option in options.strings: if opt_section == section and option not in sections[section]: raise ConfigParser.NoOptionError(option, section) if interpret: __interpret_config()
def get(self, section, option, *args, **kwargs): try: return ConfigParser.ConfigParser.get(self, section, option, *args, **kwargs) except ConfigParser.NoSectionError: # plugins are used to only catch NoOptionError raise ConfigParser.NoOptionError(option, section)
def load_haproxy_configuration(): """ Load haproxy configuration file CONFIGURATION_PATH and returns configuration :raises: HaError when neither haproxy systemV file of systemd name is configured """ # ----------- Loading configuration ----- config = ConfigParser.ConfigParser(allow_no_value=True) config.read(CONFIGURATION_FILE) # -- Looking for system V or systemd daemon systemv_init_path = None systemd_service_name = None try: systemv_init_path = config.get('haproxy', 'systemv_init_path') if systemv_init_path == '': raise ConfigParser.NoOptionError() except ConfigParser.NoOptionError: logging.info('No "systemv_init_path" configured.') try: systemd_service_name = config.get('haproxy', 'systemd_service_name') except ConfigParser.NoOptionError: logging.info('No "systemd_service_name" configured.') else: logging.info('systemv_init_path {0} will be used'.format( systemd_service_name)) else: logging.info( 'systemv_init_path {0} will be used'.format(systemv_init_path)) return config, systemv_init_path, systemd_service_name
def initialise(config_file): """Initialise configuration module""" global cfg, required_options try: cfg = ConfigParser.ConfigParser() cfg.readfp(open(config_file)) #Check for required section if (not cfg.has_section('dbAlerter')): raise ConfigParser.NoSectionError('dbAlerter') #Initialise configuration defaults for option, value in default_options.items(): if (not cfg.has_option('dbAlerter', option)): cfg.set('dbAlerter', option, value) #Check for required options for option in required_options: if (not cfg.has_option('dbAlerter', option)): raise ConfigParser.NoOptionError(option, 'dbAlerter') except IOError: notify.notify("Error", 'Failed to open config file: ' + config_file, 'Failed to open config file: ' + config_file) sys.exit(1) except ConfigParser.NoSectionError: sys.exit(1) except ConfigParser.NoOptionError: sys.exit(1)
def ask(option, question, set_globally=False, secret=False, is_valid=None, reuse_existing=True): gitflow = GitFlow() git = gitflow.repo.git answer = None try: if not reuse_existing: raise ConfigParser.NoOptionError(option, 'Not using the existing value') answer = gitflow.get(option) if isinstance(answer, basestring): answer = answer.replace(MAGIC_STRING, '-') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): while True: if secret: answer = getpass.getpass(question) else: answer = raw_input(question) if is_valid is None or is_valid(answer): break raw_answer = answer.replace('-', MAGIC_STRING) if set_globally: git.config('--global', option, raw_answer) else: gitflow.set(option, raw_answer) return answer
def readConf(self): PluginConfig.readConf(self) self.baseComputersDN = self.get("main", "baseComputersDN") # Handle deprecated config option and correct the NoOptionError exception to the new option try: if self.has_option("main", "defaultSharesPath"): self.defaultSharesPath = self.get("main", "defaultSharesPath") else: self.defaultSharesPath = self.get("main", "sharespath") except ConfigParser.NoOptionError: raise ConfigParser.NoOptionError("defaultSharesPath", "main") try: self.samba_conf_file = self.get("main", "sambaConfFile") except: pass try: self.samba_init_script = self.get("main", "sambaInitScript") except: pass try: self.av_so = self.get("main", "sambaAvSo") except: pass try: listSharePaths = self.get("main", "authorizedSharePaths") self.authorizedSharePaths = listSharePaths.replace(' ', '').split(',') except: self.authorizedSharePaths = [self.defaultSharesPath]
def set(self, section, option, value): if not self.static_conf.has_section(section): raise configparser.NoSectionError(section) if not self.static_conf.has_option(section, option): raise configparser.NoOptionError(option, section) if not self.dynamic_conf.has_section(section): self.dynamic_conf.add_section(section) self.dynamic_conf.set(section, option, value)
def option_source(self, section, option): option = self.optionxform(option) try: return self._origin[(section, option)] except KeyError: if not self.has_section(section): raise ConfigParser.NoSectionError(section) raise ConfigParser.NoOptionError(option, section)
def get(self, section, setting=None, dummy=False): if setting == None: # parse as xpath return self._xml.findall(section) else: try: return self._settings[section][setting] except KeyError: raise ConfigParser.NoOptionError(setting, section)
def get_value(self, section, option): for cfg in self.configs: try: return cfg.get_value(section, option) except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): pass if not self.has_section(section): raise ConfigParser.NoSectionError(section) raise ConfigParser.NoOptionError(option, section)
def config_whine(conf, setting, section): ''' whine if a setting is missing from a section ''' if not conf.has_option(section, setting): sys.stderr.write( "The mandatory setting '{setting}' in the section '{section}' was not defined.".format( setting=setting, section=section)) raise ConfigParser.NoOptionError(section, setting)
def test_nooptionerror(self): import pickle e1 = ConfigParser.NoOptionError('option', 'section') pickled = pickle.dumps(e1) e2 = pickle.loads(pickled) self.assertEqual(e1.message, e2.message) self.assertEqual(e1.args, e2.args) self.assertEqual(e1.section, e2.section) self.assertEqual(e1.option, e2.option) self.assertEqual(repr(e1), repr(e2))
def test_notifications_opt_is_missing(self): import ConfigParser mock_config = self.mocker.mock() mock_config.getboolean('manager', 'notifications') self.mocker.throw(ConfigParser.NoOptionError('manager', 'notifications')) self.mocker.replay() notifications = health.NotificationsOption(mock_config) self.assertFalse(notifications())
def test_nooptionerror(self): import pickle e1 = ConfigParser.NoOptionError('option', 'section') for proto in range(pickle.HIGHEST_PROTOCOL + 1): pickled = pickle.dumps(e1, proto) e2 = pickle.loads(pickled) self.assertEqual(e1.message, e2.message) self.assertEqual(e1.args, e2.args) self.assertEqual(e1.section, e2.section) self.assertEqual(e1.option, e2.option) self.assertEqual(repr(e1), repr(e2))
def get(self, section, option, index = 0): """ Returns the indexth value of an option in section. section and option are case-insensitive. """ section = self.sectionxform(section) try: sectdict = self.__sections[section].copy() except KeyError: raise ConfigParser.NoSectionError(section) option = self.optionxform(option) try: rawval = sectdict[option][index][0] except KeyError: raise ConfigParser.NoOptionError(option, section) except IndexError: raise ConfigParser.NoOptionError(option, section) return rawval
def certificate(self): try: certificate_path = os.path.realpath( self.config.get('proxy', 'certificate').strip()) if not certificate_path: raise ConfigParser.NoOptionError('certificate', 'proxy') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): return self._check_file_permissions(certificate_path, 'r') return certificate_path
def private_key(self): try: key_path = os.path.realpath( self.config.get('proxy', 'private_key').strip()) if not key_path: raise ConfigParser.NoOptionError('private_key', 'proxy') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): return self._check_file_permissions(key_path, 'r') return key_path
def get(self, section, option): try: _section = self._sections[section] except KeyError: raise ConfigParser.NoSectionError(section) try: _option = _section[option] except KeyError: raise ConfigParser.NoOptionError(option, section) return _option
def get(self, section, option, *args, **kwargs): """ Return a configuration value as a string. """ try: value = ConfigParser.ConfigParser.get(self, section, option, *args, **kwargs) if value is None: return "" return value except ConfigParser.NoSectionError: # plugins are used to only catch NoOptionError raise ConfigParser.NoOptionError(option, section)
def test_cmdline_logger(self, setup_mock, warn): with mock.patch("luigi.interface.core") as env_params: env_params.return_value.logging_conf_file = None luigi.run(['SomeTask', '--n', '7', '--local-scheduler', '--no-lock']) self.assertEqual([mock.call(None)], setup_mock.call_args_list) with mock.patch("luigi.configuration.get_config") as getconf: getconf.return_value.get.side_effect = ConfigParser.NoOptionError(section='foo', option='bar') getconf.return_value.getint.return_value = 0 luigi.interface.setup_interface_logging.call_args_list = [] luigi.run(['SomeTask', '--n', '42', '--local-scheduler', '--no-lock']) self.assertEqual([], setup_mock.call_args_list)
def locate_varname(self, varname, strict=True): varname = varname.lower() sections = self.config.sections() section_name = "" var_name = "" for section in sections: if varname.startswith( section.lower()) and len(section) > len(section_name): section_name = section.lower() var_name = varname.replace(section_name, "")[1:] if strict and not self.config.has_option(section_name, var_name): raise ConfigParser.NoOptionError(var_name, section_name) return (section_name, var_name)
def read_settings(self): config = ConfigParser.SafeConfigParser() ini_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), katello_ini ) config.read(ini_path) section = 'default' for param in param_list: if config.has_option( section, param): if param == 'host_collections': setattr(self, param, config.get( section, param).split(',')) else: setattr(self, param, config.get(section, param)) else: raise ConfigParser.NoOptionError(param,section)
def test_conf_get_option_exec(open_mock, write_mock, set_mock, read_mock, set_config_mock, set_vars_mock): set_vars_mock.return_value = None set_config_mock.return_value = None e = configparser.NoOptionError("Test Error", "Error Section") read_mock.side_effect = e result = Config().conf_get('test_section', 'test_option', 'test_value') assert read_mock.call_count == 1 assert set_mock.call_count == 1 assert open_mock.call_count == 1 assert write_mock.call_count == 1 assert result == 'test_value'
def pick(option, title, source, reuse_existing=True): # Try to get the option from config first. gitflow = GitFlow() try: if not reuse_existing: raise ConfigParser.NoOptionError(option, 'Not using the existing value') opt = gitflow.get(option) if isinstance(opt, basestring): opt = opt.replace(MAGIC_STRING, '-') return opt except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): pass # Get the suggestions list. suggestions = source() if len(suggestions) == 0: raise SystemExit('No {0} available, exiting...'.format(title)) # If that fails, ask the user to pick up one of the values. print print 'Please choose one of the following %s:' % title msg = ' Loading...' sys.stdout.write(msg) sys.stdout.flush() sys.stdout.write('\r' * len(msg)) answer = None while not answer: i = 0 for sid, sname in suggestions: i += 1 print ' [%d] %s' % (i, sname) inpt = raw_input("Insert the sequence number (or 'q' to quit): ") if inpt == 'q': raise SystemExit('Operation canceled.') try: a = int(inpt) except ValueError: print 'Please specify a number betweet 1 and %i:' % i continue if a >= 1 and a <= i: answer = suggestions[a - 1][0] value = answer if isinstance(answer, basestring): value = answer.replace('-', MAGIC_STRING) gitflow.set(option, value) return answer
def get(self, section, option, default=None): stage_section = self.get_stage_section(section) if self.config.has_section(stage_section): if self.config.has_option(stage_section, option): return self.config.get(stage_section, option) if self.config.has_section(section): if self.config.has_option(section, option): return self.config.get(section, option) if default is None: raise ConfigParser.NoOptionError(option, section) else: return default
def test_cmdline_logger(self, setup_mock, warn): with mock.patch("luigi.interface.EnvironmentParamsContainer.env_params" ) as env_params: env_params.return_value.logging_conf_file = None luigi.run(['Task', '--local-scheduler']) self.assertEqual([mock.call(None)], setup_mock.call_args_list) with mock.patch("luigi.configuration.get_config") as getconf: getconf.return_value.get.side_effect = ConfigParser.NoOptionError( section='foo', option='bar') getconf.return_value.get_boolean.return_value = True luigi.interface.setup_interface_logging.call_args_list = [] luigi.run(['Task', '--local-scheduler']) self.assertEqual([], setup_mock.call_args_list)
def get(self, key): section, option = key.split('.') if section not in self._parser.sections(): raise ConfigParser.NoSectionError(section) opts = self._parser.options(section) for _opt in opts: if '!' not in _opt: typ = 'str' else: opt, typ = _opt.split('!', 1) if opt == option: return self._parser._get(section, CONV_FUNC[typ], _opt) else: raise ConfigParser.NoOptionError(option)
def elasticsearch_nodes(self): try: nodes = [] for node in self.config.get('proxy', 'elasticsearch').split(','): node = node.lstrip().rstrip(' /') if '://' not in node: node = 'http://' + node nodes.append(node) if not nodes: raise ConfigParser.NoOptionError('elasticsearch', 'proxy') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): self._exit( 'It is mandatory to provide at least one elasticsearch node.') return nodes
def input_plugin(self): plugin_args = dict() for arg_name in self.config.options(self._INPUT_PLUGIN): plugin_args[arg_name] = self.config.get(self._INPUT_PLUGIN, arg_name) class_name = plugin_args.pop('class_name', None) if not class_name: raise ConfigParser.NoOptionError("class_name", self._INPUT_PLUGIN) Plugin = get_input_plugin(class_name) if not Plugin: raise ConfigException("No such plugin: %s" % class_name) try: return Plugin(**plugin_args) except TypeError, e: raise ConfigException("Input plugin __init__ missing one of: %s" % self.get_missing_args(Plugin), e)
def database_config_section_to_connection_pool_set(self, section_name, pool_name): """Read the given section name from the private config parser and create a corresponding connection pool. This pool does not try to connect to the database immediately (with minconn=0) """ if not pool_name: pool_name = section_name if not self.cp.has_section(section_name) : raise ConfigParser.NoSectionError(section_name) hosts = self.cp.get_default(section_name, 'host', 'localhost').split(',') port = int( self.cp.get_default(section_name, 'port', 5432) ) dbname = self.cp.get_default(section_name, 'dbname', 'postgres') common_dsn = ' '.join( '{0[0]}={0[1]}'.format(item) for item in self.cp.items(section_name) if item[0].lower() not in ('host','dsn','encoding','statement_timeout') ) host_dsn_list = [ ( host, 'host={0} {1}'.format(host.strip(), common_dsn) ) for host in hosts if host.strip() ] if not host_dsn_list : raise ConfigParser.NoOptionError('host', section_name) try : encoding=self.cp.get(section_name, 'encoding') except ConfigParser.NoOptionError : encoding=None try : statement_timeout=self.cp.get(section_name, 'statement_timeout') statement_timeout=int(statement_timeout) except ConfigParser.NoOptionError : statement_timeout=None except ValueError : statement_timeout=None self.logger.warning("Database configuration section {0} contains option 'statement_timeout' that should be numeric, but the specified value is '{1}', skipping this option.".format(section_name, statement_timeout) ) for i, ( host, dsn ) in enumerate( host_dsn_list ): pool_name_i = '{0}_{1}'.format(pool_name, i) if len(host_dsn_list) > 1 else pool_name connection_pool = PersistentConnectionPoolWithContext(pool_name_i, minconn=0, maxconn=5, host=host, port=port, dbname=dbname, encoding=encoding, statement_timeout=statement_timeout, dsn=dsn) self.logger.debug('Created a connection pool to {0} database with dsn: {1}'.format(pool_name_i, dsn)) atexit.register(self._atexit_close_pool, connection_pool) yield connection_pool