# -*- 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)

[((149, 170), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""log-dir"""'], {}), "('log-dir')\n", (159, 170), False, 'from oslo.config import cfg\n'), ((176, 198), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""log-file"""'], {}), "('log-file')\n", (186, 198), False, 'from oslo.config import cfg\n'), ((204, 223), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""debug"""'], {}), "('debug')\n", (214, 223), False, 'from oslo.config import cfg\n'), ((229, 250), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""verbose"""'], {}), "('verbose')\n", (239, 250), False, 'from oslo.config import cfg\n'), ((288, 312), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""connection"""'], {}), "('connection')\n", (298, 312), False, 'from oslo.config import cfg\n'), ((318, 336), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""data"""'], {}), "('data')\n", (328, 336), False, 'from oslo.config import cfg\n'), ((373, 393), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""server"""'], {}), "('server')\n", (383, 393), False, 'from oslo.config import cfg\n'), ((399, 417), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""port"""'], {}), "('port')\n", (409, 417), False, 'from oslo.config import cfg\n'), ((423, 446), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""from_addr"""'], {}), "('from_addr')\n", (433, 446), False, 'from oslo.config import cfg\n'), ((452, 475), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""info_list"""'], {}), "('info_list')\n", (462, 475), False, 'from oslo.config import cfg\n'), ((481, 505), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""alert_list"""'], {}), "('alert_list')\n", (491, 505), False, 'from oslo.config import cfg\n'), ((544, 566), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""allow_ip"""'], {}), "('allow_ip')\n", (554, 566), False, 'from oslo.config import cfg\n'), ((572, 596), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""secret_key"""'], {}), "('secret_key')\n", (582, 596), False, 'from oslo.config import cfg\n'), ((602, 619), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""env"""'], {}), "('env')\n", (612, 619), False, 'from oslo.config import cfg\n'), ((625, 650), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""local_group"""'], {}), "('local_group')\n", (635, 650), False, 'from oslo.config import cfg\n'), ((656, 677), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""acl_dir"""'], {}), "('acl_dir')\n", (666, 677), False, 'from oslo.config import cfg\n'), ((683, 711), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""view_acl_group"""'], {}), "('view_acl_group')\n", (693, 711), False, 'from oslo.config import cfg\n'), ((748, 777), 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (['"""dnsupdater_port"""'], {}), "('dnsupdater_port')\n", (758, 777), False, 'from oslo.config import cfg\n'), ((815, 839), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""acl_groups"""'], {}), "('acl_groups')\n", (825, 839), False, 'from oslo.config import cfg\n'), ((845, 868), 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (['"""cname_ttl"""'], {}), "('cname_ttl')\n", (855, 868), False, 'from oslo.config import cfg\n'), ((874, 897), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""view_zone"""'], {}), "('view_zone')\n", (884, 897), False, 'from oslo.config import cfg\n'), ((935, 1007), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""base-url"""'], {'default': '"""/"""', 'help': '"""The url prefix of this site."""'}), "('base-url', default='/', help='The url prefix of this site.')\n", (945, 1007), False, 'from oslo.config import cfg\n'), ((1043, 1164), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""run-mode"""'], {'default': '"""werkzeug"""', 'choices': "('gunicorn', 'werkzeug')", 'help': '"""Run server use the specify mode."""'}), "('run-mode', default='werkzeug', choices=('gunicorn', 'werkzeug'),\n    help='Run server use the specify mode.')\n", (1053, 1164), False, 'from oslo.config import cfg\n'), ((1211, 1279), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""bind"""'], {'default': '"""0.0.0.0"""', 'help': '"""The IP address to bind"""'}), "('bind', default='0.0.0.0', help='The IP address to bind')\n", (1221, 1279), False, 'from oslo.config import cfg\n'), ((1315, 1374), 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (['"""port"""'], {'default': '(8080)', 'help': '"""The port to listen"""'}), "('port', default=8080, help='The port to listen')\n", (1325, 1374), False, 'from oslo.config import cfg\n'), ((1410, 1445), 'oslo.config.cfg.BoolOpt', 'cfg.BoolOpt', (['"""debug"""'], {'default': '(False)'}), "('debug', default=False)\n", (1421, 1445), False, 'from oslo.config import cfg\n'), ((1499, 1577), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""config"""'], {'default': 'None', 'help': '"""The path to a Gunicorn config file."""'}), "('config', default=None, help='The path to a Gunicorn config file.')\n", (1509, 1577), False, 'from oslo.config import cfg\n'), ((1613, 1657), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""bind"""'], {'default': '"""127.0.0.1:8888"""'}), "('bind', default='127.0.0.1:8888')\n", (1623, 1657), False, 'from oslo.config import cfg\n'), ((1678, 1776), 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (['"""workers"""'], {'default': '(0)', 'help': '"""The number of worker processes for handling requests"""'}), "('workers', default=0, help=\n    'The number of worker processes for handling requests')\n", (1688, 1776), False, 'from oslo.config import cfg\n'), ((1807, 1882), 'oslo.config.cfg.BoolOpt', 'cfg.BoolOpt', (['"""daemon"""'], {'default': '(False)', 'help': '"""Daemonize the Gunicorn process"""'}), "('daemon', default=False, help='Daemonize the Gunicorn process')\n", (1818, 1882), False, 'from oslo.config import cfg\n'), ((1920, 2027), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""accesslog"""'], {'default': 'None', 'help': '"""The Access log file to write to."-" means log to stderr."""'}), '(\'accesslog\', default=None, help=\n    \'The Access log file to write to."-" means log to stderr.\')\n', (1930, 2027), False, 'from oslo.config import cfg\n'), ((2081, 2233), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""loglevel"""'], {'default': '"""info"""', 'help': '"""The granularity of Error log outputs."""', 'choices': "('debug', 'info', 'warning', 'error', 'critical')"}), "('loglevel', default='info', help=\n    'The granularity of Error log outputs.', choices=('debug', 'info',\n    'warning', 'error', 'critical'))\n", (2091, 2233), False, 'from oslo.config import cfg\n'), ((2275, 2333), 'oslo.config.cfg.BoolOpt', 'cfg.BoolOpt', (['"""ignore-healthcheck-accesslog"""'], {'default': '(False)'}), "('ignore-healthcheck-accesslog', default=False)\n", (2286, 2333), False, 'from oslo.config import cfg\n'), ((2355, 2474), 'oslo.config.cfg.IntOpt', 'cfg.IntOpt', (['"""timeout"""'], {'default': '(30)', 'help': '"""Workers silent for more than this many seconds are killed and restarted."""'}), "('timeout', default=30, help=\n    'Workers silent for more than this many seconds are killed and restarted.')\n", (2365, 2474), False, 'from oslo.config import cfg\n'), ((2528, 2666), 'oslo.config.cfg.StrOpt', 'cfg.StrOpt', (['"""worker-class"""'], {'default': '"""sync"""', 'help': '"""The type of workers to use."""', 'choices': "('sync', 'eventlet', 'gevent', 'tornado')"}), "('worker-class', default='sync', help=\n    'The type of workers to use.', choices=('sync', 'eventlet', 'gevent',\n    'tornado'))\n", (2538, 2666), False, 'from oslo.config import cfg\n'), ((4030, 4047), 'datetime.timedelta', 'timedelta', ([], {'days': '(1)'}), '(days=1)\n', (4039, 4047), False, 'from datetime import timedelta\n'), ((3826, 3854), 'os.environ.get', 'os.environ.get', (['"""SECRET_KEY"""'], {}), "('SECRET_KEY')\n", (3840, 3854), False, 'import os\n'), ((2816, 2836), 'sys.argv.index', 'sys.argv.index', (['"""--"""'], {}), "('--')\n", (2830, 2836), False, 'import sys\n'), ((3380, 3400), 'sys.argv.index', 'sys.argv.index', (['"""--"""'], {}), "('--')\n", (3394, 3400), False, 'import sys\n')]