<commit_before># -*- coding: utf-8 -*-


"""Configurator"""


import codecs
import itertools
import logging
import os

from six.moves import configparser

from imap_cli import const


app_name = os.path.splitext(os.path.basename(__file__))[0]
DEFAULT_CONFIG = {
    u'hostname': u'imap.example.org',
    u'username': u'username',
    u'password': u'<PASSWORD>',
    u'ssl': True,

    u'limit': 10,
    u'format_list': u''.join([
        u'\n',
        u'ID:         {uid}\n',
        u'Flags:      {flags}\n',
        u'From:       {mail_from}\n',
        u'To:         {to}\n',
        u'Date:       {date}\n',
        u'Subject:    {subject}',
    ]),
    u'format_status': u'{directory:>20} : {count:>5} Mails - {unseen:>5} Unseen - {recent:>5} Recent'
}
log = logging.getLogger(app_name)


def new_context(config=None):
    if config is None:
        log.debug(u'Loading default configuration')
        config = DEFAULT_CONFIG
    else:
        log.debug(u'Loading custom configuration')

    return dict(
        (key, value)
        for key, value in itertools.chain(DEFAULT_CONFIG.items(), config.items())
    )


def new_context_from_file(config_filename=None, encoding='utf-8', section=None):
    if config_filename is None:
        config_filename = const.DEFAULT_CONFIG_FILE
    config_filename = os.path.abspath(os.path.expanduser(os.path.expandvars(config_filename)))

    config_reader = configparser.RawConfigParser()
    config_reader.readfp(codecs.open(config_filename, 'r', encoding))
    log.debug(u'Reading configuration file \'{}\''.format(config_filename))

    config = {}
    if section is None or section == 'imap':
        # Account
        config['username'] = config_reader.get('imap', 'username')
        config['password'] = config_reader.get('imap', 'password')
        config['hostname'] = config_reader.get('imap', 'hostname')
        config['ssl'] = config_reader.getboolean('imap', 'ssl')

    if section is None or section == 'display':
        # Display
        if config_reader.has_option('display', 'limit'):
            config['limit'] = config_reader.getint('display', 'limit')

        config['format_status'] = config_reader.get('display', 'format_status') \
            if config_reader.has_option('display', 'format_status') \
            else u'{directory}:{unseen} Unseen - {count} Mails - {recent} Recent'

        config['format_list'] = config_reader.get('display', 'format_list') \
            if config_reader.has_option('display', 'format_list') \<commit_msg>Open file using 'with', move debug message<commit_after># -*- coding: utf-8 -*-


"""Configurator"""


import codecs
import itertools
import logging
import os

from six.moves import configparser

from imap_cli import const


app_name = os.path.splitext(os.path.basename(__file__))[0]
DEFAULT_CONFIG = {
    u'hostname': u'imap.example.org',
    u'username': u'username',
    u'password': u'<PASSWORD>',
    u'ssl': True,

    u'limit': 10,
    u'format_list': u''.join([
        u'\n',
        u'ID:         {uid}\n',
        u'Flags:      {flags}\n',
        u'From:       {mail_from}\n',
        u'To:         {to}\n',
        u'Date:       {date}\n',
        u'Subject:    {subject}',
    ]),
    u'format_status': u'{directory:>20} : {count:>5} Mails - {unseen:>5} Unseen - {recent:>5} Recent'
}
log = logging.getLogger(app_name)


def new_context(config=None):
    if config is None:
        log.debug(u'Loading default configuration')
        config = DEFAULT_CONFIG
    else:
        log.debug(u'Loading custom configuration')

    return dict(
        (key, value)
        for key, value in itertools.chain(DEFAULT_CONFIG.items(), config.items())
    )


def new_context_from_file(config_filename=None, encoding='utf-8', section=None):
    if config_filename is None:
        config_filename = const.DEFAULT_CONFIG_FILE
    config_filename = os.path.abspath(os.path.expanduser(os.path.expandvars(config_filename)))
    log.debug(u'Reading configuration file \'{}\''.format(config_filename))

    config_reader = configparser.RawConfigParser()
    with codecs.open(config_filename, 'r', encoding) as config_file:
        config_reader.readfp(config_file)

    config = {}
    if section is None or section == 'imap':
        # Account
        config['username'] = config_reader.get('imap', 'username')
        config['password'] = config_reader.get('imap', 'password')
        config['hostname'] = config_reader.get('imap', 'hostname')
        config['ssl'] = config_reader.getboolean('imap', 'ssl')

    if section is None or section == 'display':
        # Display
        if config_reader.has_option('display', 'limit'):
            config['limit'] = config_reader.getint('display', 'limit')

        config['format_status'] = config_reader.get('display', 'format_status') \
            if config_reader.has_option('display', 'format_status') \
            else u'{directory}:{unseen} Unseen - {count} Mails - {recent} Recent'

        config['format_list'] = config_reader.get('display', 'format_list') \
            if config_reader.has_option('display', 'format_list') \
{'fraction_non_alphanumeric': 0.1510989010989011, 'fraction_numerical': 0.003924646781789639, 'mean_word_length': 3.120452708164915, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2051380', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1599, 'n_words': 326}