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

import os
import sys
from datetime import timedelta

from oslo.config import cfg

CONF = cfg.CONF

CONF.register_opts([
    cfg.StrOpt('log-dir'),
    cfg.StrOpt('log-file'),
    cfg.StrOpt('debug'),
    cfg.StrOpt('verbose'),
], 'log')

CONF.register_opts([
    cfg.StrOpt('connection'),
    cfg.StrOpt('data'),
], 'DB')

CONF.register_opts([
    cfg.StrOpt('server'),
    cfg.StrOpt('port'),
    cfg.StrOpt('from_addr'),
    cfg.StrOpt('info_list'),
    cfg.StrOpt('alert_list'),
], 'MAIL')

CONF.register_opts([
    cfg.StrOpt('allow_ip'),
    cfg.StrOpt('secret_key'),
    cfg.StrOpt('env'),
    cfg.StrOpt('local_group'),
    cfg.StrOpt('acl_dir'),
    cfg.StrOpt('view_acl_group')
], 'etc')

CONF.register_opts([
    cfg.IntOpt('dnsupdater_port'),
], 'api')

CONF.register_opts([
    cfg.StrOpt('acl_groups'),
    cfg.IntOpt('cname_ttl'),
    cfg.StrOpt('view_zone')
], 'view')

CONF.register_opts([
    cfg.StrOpt('base-url',
               default='/',
               help='The url prefix of this site.'),
    cfg.StrOpt('run-mode',
               default="werkzeug",
               choices=('gunicorn', 'werkzeug'),
               help="Run server use the specify mode."),
    cfg.StrOpt('bind',
               default='0.0.0.0',
               help='The IP address to bind'),
    cfg.IntOpt('port',
               default=8080,
               help='The port to listen'),
    cfg.BoolOpt('debug',
                default=False),
], 'web')

CONF.register_opts([
    cfg.StrOpt('config',
               default=None,
               help='The path to a Gunicorn config file.'),
    cfg.StrOpt('bind',
               default='127.0.0.1:8888'),
    cfg.IntOpt('workers',
               default=0,
               help='The number of worker processes for handling requests'),
    cfg.BoolOpt('daemon',
                default=False,
                help='Daemonize the Gunicorn process'),
    cfg.StrOpt('accesslog',
               default=None,
               help='The Access log file to write to.'
                    '"-" means log to stderr.'),
    cfg.StrOpt('loglevel',
               default='info',
               help='The granularity of Error log outputs.',
               choices=('debug', 'info', 'warning', 'error', 'critical')),
    cfg.BoolOpt('ignore-healthcheck-accesslog',
                default=False),
    cfg.IntOpt('timeout',
               default=30,
               help='Workers silent for more than this many seconds are '
                    'killed and restarted.'),
    cfg.StrOpt('worker-class',
               default='sync',
               help='The type of workers to use.',
               choices=('sync', 'eventlet', 'gevent', 'tornado'))
], 'gunicorn')


def setup_config(app_env, app_kind, conf_dir):
    if "--" in sys.argv:
        args = sys.argv[sys.argv.index("--") + 1:]
    else:
        args = []

    common_config_file = os.path.join(conf_dir, "etc/{}/common.conf".format(app_env))
    default_config_files = [common_config_file]
    app_config_file = os.path.join(conf_dir, "etc/{}/{}.conf".format(app_env, app_kind))
    default_config_files.append(app_config_file)
    CONF(default_config_files=default_config_files, args=args)


class Config(object):
    def __init__(self, app_env, app_kind, conf_dir):
        # print 'conf_dir: ', conf_dir
        if "--" in sys.argv:
            args = sys.argv[sys.argv.index("--") + 1:]
        else:
            args = []

        common_config_file = os.path.join(conf_dir, "etc/{}/common.conf".format(app_env))
        default_config_files = [common_config_file]
        app_config_file = os.path.join(conf_dir, "etc/{}/{}.conf".format(app_env, app_kind))
        default_config_files.append(app_config_file)
        CONF(default_config_files=default_config_files, args=args)

        self.SECRET_KEY = os.environ.get('SECRET_KEY') or CONF.etc.secret_key
        self.SQLALCHEMY_DATABASE_URI = CONF.DB.connection
        self.SQLALCHEMY_TRACK_MODIFICATIONS = False
        self.PERMANENT_SESSION_LIFETIME = timedelta(days=1)

    # SECRET_KEY = os.environ.get('SECRET_KEY') or CONF.etc.secret_key
    # SQLALCHEMY_DATABASE_URI = CONF.DB.connection
    # SQLALCHEMY_TRACK_MODIFICATIONS = False
    # PERMANENT_SESSION_LIFETIME = timedelta(days=1)

[(12, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""log-dir"""'], {}), False, 'from oslo.config import cfg\n'), (13, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""log-file"""'], {}), False, 'from oslo.config import cfg\n'), (14, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""debug"""'], {}), False, 'from oslo.config import cfg\n'), (15, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""verbose"""'], {}), False, 'from oslo.config import cfg\n'), (19, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""connection"""'], {}), False, 'from oslo.config import cfg\n'), (20, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""data"""'], {}), False, 'from oslo.config import cfg\n'), (24, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""server"""'], {}), False, 'from oslo.config import cfg\n'), (25, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""port"""'], {}), False, 'from oslo.config import cfg\n'), (26, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""from_addr"""'], {}), False, 'from oslo.config import cfg\n'), (27, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""info_list"""'], {}), False, 'from oslo.config import cfg\n'), (28, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""alert_list"""'], {}), False, 'from oslo.config import cfg\n'), (32, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""allow_ip"""'], {}), False, 'from oslo.config import cfg\n'), (33, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""secret_key"""'], {}), False, 'from oslo.config import cfg\n'), (34, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""env"""'], {}), False, 'from oslo.config import cfg\n'), (35, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""local_group"""'], {}), False, 'from oslo.config import cfg\n'), (36, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""acl_dir"""'], {}), False, 'from oslo.config import cfg\n'), (37, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""view_acl_group"""'], {}), False, 'from oslo.config import cfg\n'), (41, 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (['"""dnsupdater_port"""'], {}), False, 'from oslo.config import cfg\n'), (45, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""acl_groups"""'], {}), False, 'from oslo.config import cfg\n'), (46, 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (['"""cname_ttl"""'], {}), False, 'from oslo.config import cfg\n'), (47, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""view_zone"""'], {}), False, 'from oslo.config import cfg\n'), (51, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""base-url"""'], {'default': '"""/"""', 'help': '"""The url prefix of this site."""'}), False, 'from oslo.config import cfg\n'), (54, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""run-mode"""'], {'default': '"""werkzeug"""', 'choices': "('gunicorn', 'werkzeug')", 'help': '"""Run server use the specify mode."""'}), False, 'from oslo.config import cfg\n'), (58, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""bind"""'], {'default': '"""0.0.0.0"""', 'help': '"""The IP address to bind"""'}), False, 'from oslo.config import cfg\n'), (61, 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (['"""port"""'], {'default': '(8080)', 'help': '"""The port to listen"""'}), False, 'from oslo.config import cfg\n'), (64, 'oslo.config.cfg.BoolOpt', 'cfg.BoolOpt', (['"""debug"""'], {'default': '(False)'}), False, 'from oslo.config import cfg\n'), (69, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""config"""'], {'default': 'None', 'help': '"""The path to a Gunicorn config file."""'}), False, 'from oslo.config import cfg\n'), (72, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""bind"""'], {'default': '"""127.0.0.1:8888"""'}), False, 'from oslo.config import cfg\n'), (74, 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (['"""workers"""'], {'default': '(0)', 'help': '"""The number of worker processes for handling requests"""'}), False, 'from oslo.config import cfg\n'), (77, 'oslo.config.cfg.BoolOpt', 'cfg.BoolOpt', (['"""daemon"""'], {'default': '(False)', 'help': '"""Daemonize the Gunicorn process"""'}), False, 'from oslo.config import cfg\n'), (80, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""accesslog"""'], {'default': 'None', 'help': '"""The Access log file to write to."-" means log to stderr."""'}), False, 'from oslo.config import cfg\n'), (84, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""loglevel"""'], {'default': '"""info"""', 'help': '"""The granularity of Error log outputs."""', 'choices': "('debug', 'info', 'warning', 'error', 'critical')"}), False, 'from oslo.config import cfg\n'), (88, 'oslo.config.cfg.BoolOpt', 'cfg.BoolOpt', (['"""ignore-healthcheck-accesslog"""'], {'default': '(False)'}), False, 'from oslo.config import cfg\n'), (90, 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (['"""timeout"""'], {'default': '(30)', 'help': '"""Workers silent for more than this many seconds are killed and restarted."""'}), False, 'from oslo.config import cfg\n'), (94, 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""worker-class"""'], {'default': '"""sync"""', 'help': '"""The type of workers to use."""', 'choices': "('sync', 'eventlet', 'gevent', 'tornado')"}), False, 'from oslo.config import cfg\n'), (131, 'datetime.timedelta', 'timedelta', ([], {'days': '(1)'}), False, 'from datetime import timedelta\n'), (128, 'os.environ.get', 'os.environ.get', (['"""SECRET_KEY"""'], {}), False, 'import os\n'), (103, 'sys.argv.index', 'sys.argv.index', (['"""--"""'], {}), False, 'import sys\n'), (118, 'sys.argv.index', 'sys.argv.index', (['"""--"""'], {}), False, 'import sys\n')]