<commit_before>#
#  config.py
#  cauldron
#
#  Created by <NAME> on 2015-11-21.
#  Copyright 2015 <NAME>. All rights reserved.
#
"""
Basics for handling Cauldron configuration files.
"""

import os
import six
import warnings
import pkg_resources

from six.moves import configparser

from .exc import ConfigurationMissing

def default_configuration():
    """Get the default configuration object."""
    config = configparser.ConfigParser()
    with pkg_resources.resource_stream(__name__, 'data/defaults.cfg') as fp:
        config.readfp(fp)
    return config

def read_configuration(configuration_location = None, config = None):
    """Read a configuration from a filepath."""
    
    if config is None:
        config = default_configuration()
    elif not isinstance(config, configparser.ConfigParser):
        raise TypeError("'config' must be a subclass of {0!r}".format(configparser.ConfigParser))
    
    if configuration_location is None:
        return config
    else:
        configuration_location = six.text_type(configuration_location)
    
    configuration_location = os.path.abspath(os.path.expanduser(configuration_location))
    
    try:
        with open(configuration_location, 'r') as fp:<commit_msg>Better scoping for the config.py module<commit_after>#
#  config.py
#  cauldron
#
#  Created by <NAME> on 2015-11-21.
#  Copyright 2015 <NAME>. All rights reserved.
#
"""
Basics for handling Cauldron configuration files.
"""

import os
import six
import warnings
import pkg_resources

from six.moves import configparser

from .exc import ConfigurationMissing

__all__ = ['default_configuration', 'read_configuration', 'set_module_configuration', 'get_module_configuration']

def default_configuration():
    """Get the default configuration object."""
    config = configparser.ConfigParser()
    with pkg_resources.resource_stream(__name__, 'data/defaults.cfg') as fp:
        config.readfp(fp)
    return config

def read_configuration(configuration_location = None, config = None):
    """Read a configuration from a filepath."""
    
    if config is None:
        config = default_configuration()
    elif not isinstance(config, configparser.ConfigParser):
        raise TypeError("'config' must be a subclass of {0!r}".format(configparser.ConfigParser))
    
    if configuration_location is None:
        return config
    else:
        configuration_location = six.text_type(configuration_location)
    
    configuration_location = os.path.abspath(os.path.expanduser(configuration_location))
    
    try:
        with open(configuration_location, 'r') as fp:
{'fraction_non_alphanumeric': 0.09210019267822736, 'fraction_numerical': 0.01001926782273603, 'mean_word_length': 3.9166666666666665, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6049333', 'n_tokens_mistral': 734, 'n_tokens_neox': 702, 'n_words': 207}