<commit_before>from ConfigParser import SafeConfigParser, NoSectionError
import sys
import os

from .log import get_logger
log = get_logger()

try:
    server_type = open('/etc/inbox/server_type', 'r').read().strip()
except IOError:
    server_type = 'development'


def is_prod():
    return server_type == 'production'

config = dict(SERVER_TYPE=server_type)


def transform_bools(v):
    mapping = dict(true=True, false=False, yes=True, no=False)
    return mapping[v] if v in mapping else v


def load_config(filename='config.cfg'):
    if not os.path.isfile(filename):
        print >>sys.stderr, ("Configuration file {0} does not exist. Either "
            "create it or specify a different file (./inbox --config "
            "path/to/your/config.cfg).".format(filename))
        sys.exit(1)

    global config
    try:
        parser = SafeConfigParser()
        parser.read([filename])
        config.update({k.upper(): transform_bools(v) for k, v in
                       parser.items('inboxserver')})
        log.info('Loaded configuration from {0}'.format(filename))

        # This is pretty hacky...
        config['MSG_PARTS_DIRECTORY'] = os.path.expanduser(
            config['MSG_PARTS_DIRECTORY'])
        config['LOGDIR'] = os.path.expanduser(config['LOGDIR'])

    except NoSectionError:
        print >>sys.stderr, "Couldn't load configuration from {0}". \
            format(filename)<commit_msg>cleaner way to sanitize paths<commit_after>from ConfigParser import SafeConfigParser, NoSectionError
import sys
import os

from .log import get_logger
log = get_logger()

try:
    server_type = open('/etc/inbox/server_type', 'r').read().strip()
except IOError:
    server_type = 'development'


def is_prod():
    return server_type == 'production'

config = dict(SERVER_TYPE=server_type)


def transform_bools(v):
    mapping = dict(true=True, false=False, yes=True, no=False)
    return mapping[v] if v in mapping else v


def load_config(filename='config.cfg'):
    if not os.path.isfile(filename):
        print >>sys.stderr, ("Configuration file {0} does not exist. Either "
            "create it or specify a different file (./inbox --config "
            "path/to/your/config.cfg).".format(filename))
        sys.exit(1)

    global config
    try:
        parser = SafeConfigParser()
        parser.read([filename])
        config.update({k.upper(): transform_bools(v) for k, v in
                       parser.items('inboxserver')})
        log.info('Loaded configuration from {0}'.format(filename))

        # This is pretty hacky...
        paths_to_normalize = ('MSG_PARTS_DIRECTORY', 'LOGDIR', 'CACHE_BASEDIR', 'KEY_DIR')
        for p in paths_to_normalize:
            if p in config:
                config[p] = os.path.expanduser(config[p])

    except NoSectionError:
        print >>sys.stderr, "Couldn't load configuration from {0}". \
            format(filename)
{'fraction_non_alphanumeric': 0.12349501203990368, 'fraction_numerical': 0.0027519779841761265, 'mean_word_length': 2.935047361299053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6784798', 'n_tokens_mistral': 876, 'n_tokens_neox': 856, 'n_words': 225}