# -*- coding: utf-8 -*-


"""Configurator"""


import logging
import os

from six.moves import configparser

from imap_cli import const


app_name = os.path.splitext(os.path.basename(__file__))[0]
log = logging.getLogger(app_name)


class Ctx(object):
    # Account
    username = None
    password = None
    hostname = None
    ssl = True
    # Display
    limit = None
    format_status = None
    format_list = None


def new_context_from_file(config_filename=None):
    ctx = Ctx()
    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 = configparser.RawConfigParser()
    config.read(config_filename)
    log.debug("Reading configuration file '{}'".format(config_filename))

    # Account
    ctx.username = config.get('imap', 'username')
    ctx.password = config.get('imap', 'password')
    ctx.hostname = config.get('imap', 'hostname')
    ctx.ssl = config.getboolean('imap', 'ssl')

    # Display
    if config.has_option('display', 'limit'):
        ctx.limit = config.getint('display', 'limit')

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

    ctx.format_list = config.get('display', 'format_list') \
        if config.has_option('display', 'format_list') \
        else u'From: {mail_from:<30} To: {to:<20} Subjetc: {subject}'
    return ctx

@@ -194,230 +194,1027 @@
 %5B0%5D%0A
-log = logging.getLogger(app_name)%0A%0A%0Aclass Ctx(object):%0A    # Account%0A    username = None%0A    password = None%0A    hostname = None%0A    ssl = True%0A    # Display%0A    limit = None%0A    format_status = None%0A    format_list = None
+DEFAULT_CONFIG = %7B%0A    'hostname': 'imap.example.org',%0A    'username': 'username',%0A    'password': 'secret',%0A    'ssl': True,%0A%0A    'limit': 10,%0A    'format_list': %22%22.join(%5B%0A        %22%5Cn%22,%0A        %22ID:         %7Bmail_id%7D%5Cn%22,%0A        %22Flags:      %7Bflags%7D%5Cn%22,%0A        %22From:       %7Bmail_from%7D%5Cn%22,%0A        %22To:         %7Bto%7D%5Cn%22,%0A        %22Date:       %7Bdate%7D%5Cn%22,%0A        %22Subject:    %7Bsubject%7D%22,%0A    %5D),%0A    'format_status': %22%7Bdirectory:%3E20%7D : %7Bcount:%3E5%7D Mails - %7Bunseen:%3E5%7D Unseen - %7Brecent:%3E5%7D Recent%22%0A%7D%0Alog = logging.getLogger(app_name)%0A%0A%0Aclass Ctx(object):%0A    # Account%0A    username = None%0A    password = None%0A    hostname = None%0A    ssl = True%0A    # Display%0A    limit = None%0A    format_status = None%0A    format_list = None%0A%0A%0Adef new_context(config=None):%0A    ctx = Ctx()%0A    if config is None:%0A        log.debug(%22Loading default configuration%22)%0A        config = DEFAULT_CONFIG%0A    else:%0A        log.debug(%22Loading custom configuration%22)%0A    for key, value in config.items():%0A        setattr(ctx, key, value)%0A    return ctx
 %0A%0A%0Ad

Load context by python dict in addition to file loading