<commit_before># Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


import os

import yaml

from os_client_config import cloud_config
from os_client_config import defaults_dict
from os_client_config import exceptions
from os_client_config import vendors

CONFIG_HOME = os.path.join(os.path.expanduser(
    os.environ.get('XDG_CONFIG_HOME', os.path.join('~', '.config'))),
    'openstack')
CONFIG_SEARCH_PATH = [os.getcwd(), CONFIG_HOME, '/etc/openstack']
CONFIG_FILES = [
    os.path.join(d, 'clouds.yaml') for d in CONFIG_SEARCH_PATH]
BOOL_KEYS = ('insecure', 'cache')
REQUIRED_VALUES = ('auth_url', 'username', 'password', 'project_id')


def get_boolean(value):
    if value.lower() == 'true':
        return True
    return False


class OpenStackConfig(object):

    def __init__(self, config_files=None):
        self._config_files = config_files or CONFIG_FILES

        defaults = defaults_dict.DefaultsDict()
        defaults.add('username')
        defaults.add('user_domain_name')
        defaults.add('password')
        defaults.add('project_id', defaults['username'], also='tenant_name')
        defaults.add('project_domain_name')
        defaults.add('auth_url')
        defaults.add('region_name')
        defaults.add('cache')
        defaults.add('auth_token')
        defaults.add('insecure')
        defaults.add('endpoint_type')
        defaults.add('cacert')

        self.defaults = defaults

        # use a config file if it exists where expected
        self.cloud_config = self._load_config_file()
        if not self.cloud_config:
            self.cloud_config = dict(
                clouds=dict(openstack=dict(self.defaults)))

    @classmethod
    def get_services(klass):
        return SERVICES

    def _load_config_file(self):
        for path in self._config_files:
            if os.path.exists(path):
                return yaml.load(open(path, 'r'))

    def _get_regions(self, cloud):
        return self.cloud_config['clouds'][cloud]['region_name']

    def _get_region(self, cloud):
        return self._get_regions(cloud).split(',')[0]

    def _get_cloud_sections(self):
        return self.cloud_config['clouds'].keys()

    def _get_base_cloud_config(self, name):
        cloud = dict()
        if name in self.cloud_config['clouds']:
            our_cloud = self.cloud_config['clouds'][name]
        else:
            our_cloud = dict()

        # yes, I know the next line looks silly
        if 'cloud' in our_cloud:
            cloud.update(vendors.CLOUD_DEFAULTS[our_cloud['cloud']])<commit_msg>Handle null region<commit_after># Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


import os

import yaml

from os_client_config import cloud_config
from os_client_config import defaults_dict
from os_client_config import exceptions
from os_client_config import vendors

CONFIG_HOME = os.path.join(os.path.expanduser(
    os.environ.get('XDG_CONFIG_HOME', os.path.join('~', '.config'))),
    'openstack')
CONFIG_SEARCH_PATH = [os.getcwd(), CONFIG_HOME, '/etc/openstack']
CONFIG_FILES = [
    os.path.join(d, 'clouds.yaml') for d in CONFIG_SEARCH_PATH]
BOOL_KEYS = ('insecure', 'cache')
REQUIRED_VALUES = ('auth_url', 'username', 'password', 'project_id')


def get_boolean(value):
    if value.lower() == 'true':
        return True
    return False


class OpenStackConfig(object):

    def __init__(self, config_files=None):
        self._config_files = config_files or CONFIG_FILES

        defaults = defaults_dict.DefaultsDict()
        defaults.add('username')
        defaults.add('user_domain_name')
        defaults.add('password')
        defaults.add('project_id', defaults['username'], also='tenant_name')
        defaults.add('project_domain_name')
        defaults.add('auth_url')
        defaults.add('region_name')
        defaults.add('cache')
        defaults.add('auth_token')
        defaults.add('insecure')
        defaults.add('endpoint_type')
        defaults.add('cacert')
        defaults.add('auth_type')

        self.defaults = defaults

        # use a config file if it exists where expected
        self.cloud_config = self._load_config_file()
        if not self.cloud_config:
            self.cloud_config = dict(
                clouds=dict(openstack=dict(self.defaults)))

    @classmethod
    def get_services(klass):
        return SERVICES

    def _load_config_file(self):
        for path in self._config_files:
            if os.path.exists(path):
                return yaml.load(open(path, 'r'))

    def _get_regions(self, cloud):
        try:
            return self.cloud_config['clouds'][cloud]['region_name']
        except KeyError:
            # No region configured
            return ''

    def _get_region(self, cloud):
        return self._get_regions(cloud).split(',')[0]

    def _get_cloud_sections(self):
        return self.cloud_config['clouds'].keys()

    def _get_base_cloud_config(self, name):
        cloud = dict()
        if name in self.cloud_config['clouds']:
            our_cloud = self.cloud_config['clouds'][name]
        else:
            our_cloud = dict()

        # yes, I know the next line looks silly
        if 'cloud' in our_cloud:
            cloud.update(vendors.CLOUD_DEFAULTS[our_cloud['cloud']])
{'fraction_non_alphanumeric': 0.10834645669291339, 'fraction_numerical': 0.0028346456692913387, 'mean_word_length': 3.140156453715776, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3596190', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1828, 'n_words': 491}