def _validate(cp): """ Validate configuration file against the following criteria: - There must a valid url for the default calendar - Each calendar must have a valid url defined - If log level is provided the level must be valid and file must be provided. - If log file is provided but log level is optional. Log level will default to DEBUG """ if cp.has_option(section='general', option='default_calendar'): general_cal_url = cp.get(section='general', option='default_calendar') if not is_uri(general_cal_url): raise configparser.ParsingError( F'{general_cal_url} is not a valid url') else: raise configparser.NoOptionError('Missing default_calendar') for section in cp.sections(): for auth_field in ['username', 'password']: has_cal_auth = cp.has_option(section=section, option=auth_field) \ or cp.has_option(section=section, option=F'{auth_field}.eval') has_global_auth = cp.has_option(section='general', option=auth_field) \ or cp.has_option(section='general', option=F'{auth_field}.eval') if has_global_auth is None and has_cal_auth is None: raise configparser.NoOptionError( F'Cannot find a {auth_field} to authenticate {section} calendar' ) if cp.has_option(section=section, option='url'): cal_url = cp.get(section=section, option='url') if not is_uri(cal_url): raise configparser.ParsingError( F'{cal_url} is not a valid url') else: raise configparser.NoOptionError( F'Missing calendar url for {section}')
def remove_comment(self, section, option=None): """ Remove the comment from a section or option. @type section: str @param section: The section to remove from. @type option: str or None @param option: The option to remove from or None to remove a section's comment. @rtype: bool @return: True if a comment was removed and False if no comment was removed. """ if not section: section = configparser.DEFAULTSECT elif not self.has_section(section) and \ section != configparser.DEFAULTSECT: raise configparser.NoSectionError(section) if option: option = self.optionxform(option) if not self.has_option(section, option): raise configparser.NoOptionError(option, section) else: # setting section comment option = '__name__' del self._comments[section][option]
def has_comment(self, section, option=None): """ Return if the given section heading or option within a section has an associated comment. @type section: str @type option: str or None @rtype: bool @raise NoSectionError: The provided section does not exist """ if not section: section = configparser.DEFAULTSECT if not self.has_section(section) and \ section != configparser.DEFAULTSECT: raise configparser.NoSectionError(section) if option: option = self.optionxform(option) if not self.has_option(section, option): raise configparser.NoOptionError(option, section) else: # looking for section comment option = '__name__' return bool( self._comments.get(section, False) and self._comments[section].get(option, False))
def set_comment(self, comment, section, option=None): """ Set the comment for a section or option @type comment: str or None @type section: str @type option: str or None """ if not section: section = configparser.DEFAULTSECT if not self.has_section(section) and \ section != configparser.DEFAULTSECT: raise configparser.NoSectionError(section) if section not in self._comments: self._comments[section] = self._dict() if option: option = self.optionxform(option) if not self.has_option(section, option): raise configparser.NoOptionError(option, section) else: # setting section comment option = '__name__' self._comments[section][option] = comment
def test_empty_render_ignore_no_option(self): self.config.get.side_effect = ConfigParser.NoOptionError( 'files', 'render_ignore') s = Settings(self.interface, self.config) self.assertEqual(s.render_ignore_globs(), [])
def do_get(section, option): val = ConfigParser.SafeConfigParser.get(self, section, option, raw=raw) if len(val.strip()) == 0: raise ConfigParser.NoOptionError(option, section) return val
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 get(self, section, option, **kwargs): fallback = kwargs.pop('fallback', NotImplemented) for cfg in self.configs: try: return cfg.get(section, option, **kwargs) except configparser.NoOptionError: pass if fallback is not NotImplemented: return fallback raise configparser.NoOptionError((section, option))
def get_provider_ssl_args(self, protocol): """ Extract the ssl arguments :type cfg: :class:`configparser.SafeConfigParser` :param cfg: Configuration :type protocol_prefix: :class:`str` :param protocol_prefix: Prefix of the protocol configuration :rtype: :class:`dict` :return: SSL arguments for this protocol configuration """ ssl_args = {} # Try to get the certdir from environment variable or .certdir option certdir = os.environ.get('CERTDIR') if certdir is None: try: certdir_key = '%s.%s.%s.certdir' % ( EndpointSection.PROTOCOL_PREFIX, protocol, EndpointSection.PROTOCOL_SSL_SUFFIX) certdir = self._get_value(Sections.ENDPOINT, certdir_key) except configparser.NoOptionError: # But don't complain yet pass for ssl_key in ('ca_certs', 'certfile', 'keyfile'): option = '%s.%s.%s.%s' % ( EndpointSection.PROTOCOL_PREFIX, protocol, EndpointSection.PROTOCOL_SSL_SUFFIX, ssl_key) file_name = None try: file_name = self._get_value(Sections.ENDPOINT, option) except configparser.NoOptionError: # ca_certs, certfile and keyfile are optional, so don't complain # if they are not present pass if file_name is not None: if certdir is None: # If one of ca_certs, certfile, keyfile is specified # and certdir is not specified, then raise an error logger.error( 'Specify certificate absolute directory path ' 'either by setting environment variable ' 'CERTDIR or by setting %s.certdir in the ' 'properties file', EndpointSection.PROTOCOL_SSL_SUFFIX) raise configparser.NoOptionError( '%s.certdir' % EndpointSection.PROTOCOL_SSL_SUFFIX, Sections.ENDPOINT) else: file_path = os.path.join(certdir, file_name) check_file_exists(file_path) ssl_args[ssl_key] = file_path return ssl_args
def test_get_data_db_client_parsing_error(self, mock_parser, mock_get_client): _mock_parser = mock.Mock() mock_parser.return_value = _mock_parser mock_parser.NoOptionError = configparser.NoOptionError mock_get_client.side_effect = configparser.NoOptionError( 'option', 'section') with self.assertRaises(configparser.NoOptionError): influx.get_data_db_client() _mock_parser.read.assert_called_once_with(constants.CONF_FILE) mock_get_client.assert_called_once_with(_mock_parser)
def get_location(self, section, option): """Get location information for an option value in a given section. Returns a tuple (filename, line_number) if location information exists, and None otherwise. """ if not self.has_section(section): raise configparser.NoSectionError(section) elif not self.has_option(section, option): raise configparser.NoOptionError(option, section) option = self.optionxform(option) loc = self._option_lines[section].get(option) if loc is None and option in self._defaults: return self._option_lines[configparser.DEFAULTSECT].get(option) return loc
def set_location(self, section, option, location): """Explicitly set location information for an option value in a given section. `location' must be a tuple of (filename, line_number). """ if not self.has_section(section): raise configparser.NoSectionError(section) elif not self.has_option(section, option): raise configparser.NoOptionError(option, section) if location is not None: try: filename, lineno = location except ValueError: err = ValueError("location must be (filename, lineno) or None") six.raise_from(err, None) option = self.optionxform(option) self._option_lines[section][option] = location
def get_comment(self, section, option=None): """ Get the comment for a section[.option] @type section: str @param section: Section heading to check for a comment, or the section heading that the target option is located under. @type option: str or None @param option: If checking for an option's comment, this is the option under the given section to check. If checking for a section's comment, this should be None. @rtype: str or None @return: The section or option comment if there is one, or None if there is no comment for the specified section or option. """ if not section: section = configparser.DEFAULTSECT if not self.has_section(section) and \ section != configparser.DEFAULTSECT: raise configparser.NoSectionError(section) if option: option = self.optionxform(option) if not self.has_option(section, option): raise configparser.NoOptionError(option, section) else: # looking for section comment option = '__name__' # Combined statement to handle both section and option requests. return (self._comments.get(section, None) and self._comments[section].get(option, None))
def get(self, section, option): if self.config_parser: return self.config_parser.get(section, option) raise configparser.NoOptionError(section, option)