<commit_before># Copyright (c) 2015 Midokura SARL, All Rights Reserved.
#
# @author: <NAME> <<EMAIL>>, Midokura

import ConfigParser
import logging
from StringIO import StringIO

from os.path import isfile
from midonet_sandbox.utils import Singleton

log = logging.getLogger('midonet-sandbox.configuration')

DEFAULT_SETTINGS = {
    'extra_flavours': None,
    'extra_components': None,
    'docker_socket': 'unix://var/run/docker.sock'
}


@Singleton
class Config(object):
    """
    Read and parse the configuration file. The configuration file format is:
    ---------------
    [sandbox]
    extra_flavours = <path_to_extra_flavours_directory>
    extra_components = <path_to_extra_flavours_directory>
    docker_socket = unix://var/run/docker.sock
    -----------------
    """

    def __init__(self, config_file):
        self._config = ConfigParser.SafeConfigParser(defaults=DEFAULT_SETTINGS)

        if isfile(config_file):
            log.info('Loading configuration file: {}'.format(config_file))
            self._config.read(config_file)
        else:
            self._config.add_section('sandbox')
            if not isfile(config_file):
                log.info('Cannot read {}'.format(config_file))
            log.info('Using default settings')

        log.debug('Settings: \n{}'.format(self.dump_config(self._config)))

    def get_sandbox_value(self, param):
        return self._config.get('sandbox', param)

    def dump_config(self, config):
        dump = StringIO()
        config.write(dump)
        return dump.getvalue()<commit_msg>Fix bug reading default configuration file. The ~ char was not expanded<commit_after># Copyright (c) 2015 Midokura SARL, All Rights Reserved.
#
# @author: <NAME> <<EMAIL>>, Midokura

import ConfigParser
import logging
from StringIO import StringIO

import os
from os.path import isfile
from midonet_sandbox.utils import Singleton

log = logging.getLogger('midonet-sandbox.configuration')

DEFAULT_SETTINGS = {
    'extra_flavours': None,
    'extra_components': None,
    'docker_socket': 'unix://var/run/docker.sock'
}


@Singleton
class Config(object):
    """
    Read and parse the configuration file. The configuration file format is:
    ---------------
    [sandbox]
    extra_flavours = <path_to_extra_flavours_directory>
    extra_components = <path_to_extra_flavours_directory>
    docker_socket = unix://var/run/docker.sock
    -----------------
    """

    def __init__(self, config_file):
        self._config = ConfigParser.SafeConfigParser(defaults=DEFAULT_SETTINGS)
        config_file = os.path.expanduser(config_file)

        if isfile(config_file):
            log.info('Loading configuration file: {}'.format(config_file))
            self._config.read(config_file)
        else:
            self._config.add_section('sandbox')
            if not isfile(config_file):
                log.info('Cannot read {}'.format(config_file))
            log.info('Using default settings')

        log.debug('Settings: \n{}'.format(self.dump_config(self._config)))

    def get_sandbox_value(self, param):
        return self._config.get('sandbox', param)

    def dump_config(self, config):
        dump = StringIO()
        config.write(dump)
        return dump.getvalue()
{'fraction_non_alphanumeric': 0.1270769230769231, 'fraction_numerical': 0.0024615384615384616, 'mean_word_length': 3.0036945812807883, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '393400', 'n_tokens_mistral': 993, 'n_tokens_neox': 948, 'n_words': 209}