def _parse_file(file_path): """Parse the specified configuration file, returning a nested dict of key/value pairs by section. :param str file_path: The path of the file to read. :rtype: dict """ file_path = path.abspath(path.expanduser(path.expandvars(file_path))) LOGGER.debug('Reading file: %s', file_path) if not path.exists(file_path): raise exceptions.ConfigNotFound(path=file_path) parser = configparser.RawConfigParser() try: parser.read(file_path) except configparser.Error as error: LOGGER.error('Error reading file: %s', error) raise exceptions.ConfigParserError(path=file_path) config = {} for section in parser.sections(): config[section] = {} for option in parser.options(section): config[section][option] = parser.get(section, option) return config
def get_region(profile): """Return the credentials from the configured ~/.aws/credentials file following a similar behavior implemented by awscli and botocore. :param str profile: Use the optional profile for getting settings :return: region :rtype: str :raises: exceptions.ConfigNotFound """ region = os.getenv('AWS_DEFAULT_REGION', None) if region: return region file_path = os.getenv('AWS_CONFIG_FILE', '~/.aws/config') try: config = _parse_file(file_path) except exceptions.ConfigNotFound: try: return _request_region_from_instance() except (httpclient.HTTPError, OSError) as error: LOGGER.error('Error fetching from EC2 Instance Metadata (%s)', error) raise exceptions.ConfigNotFound(path=file_path) if profile not in config and 'default' not in config: raise exceptions.NoProfileError(path=file_path, profile=profile) return (config.get(profile, {}).get('region') or config.get('default', {}).get('region') or DEFAULT_REGION)
def test_raises_config_not_found_exception(self): with mock.patch('tornado_aws.client.AsyncAWSClient.fetch') as fetch: fetch.side_effect = aws_exceptions.ConfigNotFound(path='/test') with self.assertRaises(exceptions.ConfigNotFound): yield self.client.create_table(self.generic_table_definition())
def test_raises_config_not_found_exception(self): self.create_table_expecting_raise( dynamodb.ConfigNotFound, aws_exceptions.ConfigNotFound(path='/test'))