# -*- 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, 4, 12, 25), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(12, 15, 12, 24): '"""log-dir"""'}, {}), "('log-dir')", False, 'from oslo.config import cfg\n'), ((13, 4, 13, 26), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(13, 15, 13, 25): '"""log-file"""'}, {}), "('log-file')", False, 'from oslo.config import cfg\n'), ((14, 4, 14, 23), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(14, 15, 14, 22): '"""debug"""'}, {}), "('debug')", False, 'from oslo.config import cfg\n'), ((15, 4, 15, 25), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(15, 15, 15, 24): '"""verbose"""'}, {}), "('verbose')", False, 'from oslo.config import cfg\n'), ((19, 4, 19, 28), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(19, 15, 19, 27): '"""connection"""'}, {}), "('connection')", False, 'from oslo.config import cfg\n'), ((20, 4, 20, 22), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(20, 15, 20, 21): '"""data"""'}, {}), "('data')", False, 'from oslo.config import cfg\n'), ((24, 4, 24, 24), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(24, 15, 24, 23): '"""server"""'}, {}), "('server')", False, 'from oslo.config import cfg\n'), ((25, 4, 25, 22), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(25, 15, 25, 21): '"""port"""'}, {}), "('port')", False, 'from oslo.config import cfg\n'), ((26, 4, 26, 27), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(26, 15, 26, 26): '"""from_addr"""'}, {}), "('from_addr')", False, 'from oslo.config import cfg\n'), ((27, 4, 27, 27), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(27, 15, 27, 26): '"""info_list"""'}, {}), "('info_list')", False, 'from oslo.config import cfg\n'), ((28, 4, 28, 28), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(28, 15, 28, 27): '"""alert_list"""'}, {}), "('alert_list')", False, 'from oslo.config import cfg\n'), ((32, 4, 32, 26), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(32, 15, 32, 25): '"""allow_ip"""'}, {}), "('allow_ip')", False, 'from oslo.config import cfg\n'), ((33, 4, 33, 28), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(33, 15, 33, 27): '"""secret_key"""'}, {}), "('secret_key')", False, 'from oslo.config import cfg\n'), ((34, 4, 34, 21), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(34, 15, 34, 20): '"""env"""'}, {}), "('env')", False, 'from oslo.config import cfg\n'), ((35, 4, 35, 29), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(35, 15, 35, 28): '"""local_group"""'}, {}), "('local_group')", False, 'from oslo.config import cfg\n'), ((36, 4, 36, 25), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(36, 15, 36, 24): '"""acl_dir"""'}, {}), "('acl_dir')", False, 'from oslo.config import cfg\n'), ((37, 4, 37, 32), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(37, 15, 37, 31): '"""view_acl_group"""'}, {}), "('view_acl_group')", False, 'from oslo.config import cfg\n'), ((41, 4, 41, 33), 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', ({(41, 15, 41, 32): '"""dnsupdater_port"""'}, {}), "('dnsupdater_port')", False, 'from oslo.config import cfg\n'), ((45, 4, 45, 28), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(45, 15, 45, 27): '"""acl_groups"""'}, {}), "('acl_groups')", False, 'from oslo.config import cfg\n'), ((46, 4, 46, 27), 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', ({(46, 15, 46, 26): '"""cname_ttl"""'}, {}), "('cname_ttl')", False, 'from oslo.config import cfg\n'), ((47, 4, 47, 27), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', ({(47, 15, 47, 26): '"""view_zone"""'}, {}), "('view_zone')", False, 'from oslo.config import cfg\n'), ((51, 4, 53, 51), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (), '', False, 'from oslo.config import cfg\n'), ((54, 4, 57, 55), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (), '', False, 'from oslo.config import cfg\n'), ((58, 4, 60, 45), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (), '', False, 'from oslo.config import cfg\n'), ((61, 4, 63, 41), 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (), '', False, 'from oslo.config import cfg\n'), ((64, 4, 65, 30), 'oslo.config.cfg.BoolOpt', 'cfg.BoolOpt', (), '', False, 'from oslo.config import cfg\n'), ((69, 4, 71, 58), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (), '', False, 'from oslo.config import cfg\n'), ((72, 4, 73, 40), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (), '', False, 'from oslo.config import cfg\n'), ((74, 4, 76, 75), 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (), '', False, 'from oslo.config import cfg\n'), ((77, 4, 79, 54), 'oslo.config.cfg.BoolOpt', 'cfg.BoolOpt', (), '', False, 'from oslo.config import cfg\n'), ((80, 4, 83, 47), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (), '', False, 'from oslo.config import cfg\n'), ((84, 4, 87, 73), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (), '', False, 'from oslo.config import cfg\n'), ((88, 4, 89, 30), 'oslo.config.cfg.BoolOpt', 'cfg.BoolOpt', (), '', False, 'from oslo.config import cfg\n'), ((90, 4, 93, 44), 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (), '', False, 'from oslo.config import cfg\n'), ((94, 4, 97, 65), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (), '', False, 'from oslo.config import cfg\n'), ((131, 42, 131, 59), 'datetime.timedelta', 'timedelta', (), '', False, 'from datetime import timedelta\n'), ((128, 26, 128, 54), 'os.environ.get', 'os.environ.get', ({(128, 41, 128, 53): '"""SECRET_KEY"""'}, {}), "('SECRET_KEY')", False, 'import os\n'), ((103, 24, 103, 44), 'sys.argv.index', 'sys.argv.index', ({(103, 39, 103, 43): '"""--"""'}, {}), "('--')", False, 'import sys\n'), ((118, 28, 118, 48), 'sys.argv.index', 'sys.argv.index', ({(118, 43, 118, 47): '"""--"""'}, {}), "('--')", False, 'import sys\n')]