<commit_before>import os
import yaml
import logging

logger = logging.getLogger(__name__)


def load_configuration(name, prefix, fields):
    """
    Load configuration data from a cascading series of locations.

    The precedence order is (highest priority last):

    1. The conda environment
       - CONDA_ENV/etc/{name}.yaml (if CONDA_ETC_env is defined)
    2. At the system level
       - /etc/{name}.yml
    3. In the user's home directory
       - ~/.config/{name}/connection.yml
    4. Environmental variables
       - {PREFIX}_{FIELD}

    where
        {name} is metadatastore
        {PREFIX} is MDS
        {FIELD} is one of {host, database, port, timezone}

    Parameters
    ----------
    name : str
        The expected base-name of the configuration files

    prefix : str
        The prefix when looking for environmental variables

    fields : iterable of strings
        The required configuration fields

    Returns
    ------
    conf : dict
        Dictionary keyed on ``fields`` with the values extracted
    """
    filenames = [
        os.path.join('/etc', name + '.yml'),
        os.path.join(os.path.expanduser('~'), '.config', name,
                     'connection.yml'),
        ]

    if 'CONDA_ETC_' in os.environ:
        filenames.insert(0, os.path.join(
            os.environ['CONDA_ETC_'], name + '.yml'))

    config = {}
    for filename in filenames:
        if os.path.isfile(filename):
            with open(filename) as f:
                config.update(yaml.load(f))
            logger.debug("Using db connection specified in config file. \n%r",
                         config)

    for field in fields:
        var_name = prefix + '_' + field.upper().replace(' ', '_')
        config[field] = os.environ.get(var_name, config.get(field, None))

    missing = [k for k, v in config.items() if v is None]
    if missing:
        raise KeyError("The configuration field(s) {0} "
                       "were not found in any file or environmental "
                       "variable.".format(missing))
    return config

connection_config = load_configuration(
    'metadatastore', 'MDS', ['host', 'database', 'port', 'timezone'])<commit_msg>FIX: Cast port to int.<commit_after>import os
import yaml
import logging

logger = logging.getLogger(__name__)


def load_configuration(name, prefix, fields):
    """
    Load configuration data from a cascading series of locations.

    The precedence order is (highest priority last):

    1. The conda environment
       - CONDA_ENV/etc/{name}.yaml (if CONDA_ETC_env is defined)
    2. At the system level
       - /etc/{name}.yml
    3. In the user's home directory
       - ~/.config/{name}/connection.yml
    4. Environmental variables
       - {PREFIX}_{FIELD}

    where
        {name} is metadatastore
        {PREFIX} is MDS
        {FIELD} is one of {host, database, port, timezone}

    Parameters
    ----------
    name : str
        The expected base-name of the configuration files

    prefix : str
        The prefix when looking for environmental variables

    fields : iterable of strings
        The required configuration fields

    Returns
    ------
    conf : dict
        Dictionary keyed on ``fields`` with the values extracted
    """
    filenames = [
        os.path.join('/etc', name + '.yml'),
        os.path.join(os.path.expanduser('~'), '.config', name,
                     'connection.yml'),
        ]

    if 'CONDA_ETC_' in os.environ:
        filenames.insert(0, os.path.join(
            os.environ['CONDA_ETC_'], name + '.yml'))

    config = {}
    for filename in filenames:
        if os.path.isfile(filename):
            with open(filename) as f:
                config.update(yaml.load(f))
            logger.debug("Using db connection specified in config file. \n%r",
                         config)

    for field in fields:
        var_name = prefix + '_' + field.upper().replace(' ', '_')
        config[field] = os.environ.get(var_name, config.get(field, None))
        # Valid values for 'port' are None or castable to int.
        if field == 'port' and config[field] is not None:
            config[field] = int(config[field])

    missing = [k for k, v in config.items() if v is None]
    if missing:
        raise KeyError("The configuration field(s) {0} "
                       "were not found in any file or environmental "
                       "variable.".format(missing))
    return config

connection_config = load_configuration(
    'metadatastore', 'MDS', ['host', 'database', 'port', 'timezone'])
{'fraction_non_alphanumeric': 0.11493748629085326, 'fraction_numerical': 0.002632156174599693, 'mean_word_length': 2.1147540983606556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4294030', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1258, 'n_words': 409}