<commit_before>'''
The Emma2 configuration module reads several config files to build its final
set of settings. It searches for the file 'emma2.cfg' in several locations with
different priorities:
1. $CWD/emma2.cfg
2. /etc/emma2.cfg
3. ~/emma2.cfg
4. $PYTHONPATH/Emma2/emma2.cfg

The default values are stored in later file to ensure these values are always
defined. This is preferred over hardcoding them somewhere in the python code.

Created on 31.03.2014

@author: marscher
'''
import ConfigParser
import os
import pkg_resources

__all__ = ['configParser', 'used_filenames', 'AttribStore']

configParser = None

class AttribStore(dict):
    def __getattr__(self, name):
        return self[name]

    def __setattr__(self, name, value):
        self[name] = value

def readConfiguration():
    global configParser, used_filenames
    """ this filenames are being tried to read to obtain basic configuration values 
        for the logging system."""
    cfg = 'emma2.cfg'
    ultimate_backup = pkg_resources.resource_filename('emma2', os.path.join('..', 'emma2.cfg'))
    filenames = [cfg, # config in current dir
                '/etc/' + cfg, # config in global installation
                os.path.join(os.path.expanduser('~'), os.path.sep, cfg), # config in user dir
                # This should always be last, since it provides all default values
                # and things will horribly fail, if this file can not be found.
                ultimate_backup
                ]
    
    configParser = ConfigParser.SafeConfigParser()
    """ this is a list of used configuration filenames during parsing the configuration"""
    used_filenames = configParser.read(filenames)
    
    if ultimate_backup not in used_filenames:
        raise RuntimeError('Default configuration values could not be red!')


readConfiguration()<commit_msg>[config] fix: user class pathes are always first. User values are no more overwritten by defaults<commit_after>'''
The Emma2 configuration module reads several config files to build its final
set of settings. It searches for the file 'emma2.cfg' in several locations with
different priorities:
1. $CWD/emma2.cfg
2. /etc/emma2.cfg
3. ~/emma2.cfg
4. $PYTHONPATH/Emma2/emma2.cfg

The default values are stored in later file to ensure these values are always
defined. This is preferred over hardcoding them somewhere in the python code.

Created on 31.03.2014

@author: marscher
'''
import ConfigParser
import os
import pkg_resources

__all__ = ['configParser', 'used_filenames', 'AttribStore']

configParser = None

class AttribStore(dict):
    def __getattr__(self, name):
        return self[name]

    def __setattr__(self, name, value):
        self[name] = value

def readConfiguration():
    global configParser, used_filenames
    """ this filenames are being tried to read to obtain basic configuration values 
        for the logging system."""
    cfg = 'emma2.cfg'
    
    # read defaults from ultimate_backup first, 
    # then pass them as defaults to safeconfig parser
    defParser = ConfigParser.RawConfigParser()
    ultimate_backup = pkg_resources.resource_filename('emma2', os.path.join('..', 'emma2.cfg'))
    if defParser.read(ultimate_backup) == []:
        raise RuntimeError('Default configuration values could not be red!')
    defaults = {}
    for section in defParser.sections():
        for item in defParser.items(section):
            defaults[item[0]] = item[1]
    
    # use these files to extend/overwrite the config.
    # Last red files always overwrites existing values!
    filenames = [cfg, # config in current dir
                '/etc/' + cfg, # config in global installation
                os.path.join(os.path.expanduser('~' + os.path.sep), cfg), # config in user dir
                ]
    
    configParser = ConfigParser.SafeConfigParser(defaults)
    """ this is a list of used configuration filenames during parsing the configuration"""
    used_filenames = configParser.read(filenames)

readConfiguration()
{'fraction_non_alphanumeric': 0.08697825543614096, 'fraction_numerical': 0.011497125718570358, 'mean_word_length': 3.2986036519871105, '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': '1507643', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1106, 'n_words': 411}