#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- mode: python -*-

import aiopg
import psycopg2
import tornado.locks
from tornado.options import define, options
from tornado.web import Application

from app.application import Application

define('port', default=8080, help="listening port")
define('bind_address', default="", help="bind address")
define("db_host", default="127.0.0.1", help="database host")
define("db_port", default=5432, help="database port")
define("db_database", default="tornado", help="database name")
define("db_user", default="tornado", help="database user")
define("db_password", default="tornado", help="database password")


async def maybe_create_tables(db):
    try:
        with (await db.cursor()) as cur:
            await cur.execute("SELECT COUNT(*) FROM schema LIMIT 1")
            await cur.fetchone()
    except psycopg2.ProgrammingError:
        print("Database error!")


async def main():
    options.parse_command_line()

    async with aiopg.create_pool(
            host=options.db_host,
            port=options.db_port,
            user=options.db_user,
            password=options.db_password,
            dbname=options.db_database) as db:
        await maybe_create_tables(db)
        app = Application(db)
        app.listen(options.port, options.bind_address, xheaders=True)
        print("Listening on http://%s:%i" % (options.bind_address, options.port))
        shutdown_event = tornado.locks.Event()
        await shutdown_event.wait()

[((242, 293), 'tornado.options.define', 'define', (['"""port"""'], {'default': '(8080)', 'help': '"""listening port"""'}), "('port', default=8080, help='listening port')\n", (248, 293), False, 'from tornado.options import define, options\n'), ((294, 349), 'tornado.options.define', 'define', (['"""bind_address"""'], {'default': '""""""', 'help': '"""bind address"""'}), "('bind_address', default='', help='bind address')\n", (300, 349), False, 'from tornado.options import define, options\n'), ((350, 410), 'tornado.options.define', 'define', (['"""db_host"""'], {'default': '"""127.0.0.1"""', 'help': '"""database host"""'}), "('db_host', default='127.0.0.1', help='database host')\n", (356, 410), False, 'from tornado.options import define, options\n'), ((411, 464), 'tornado.options.define', 'define', (['"""db_port"""'], {'default': '(5432)', 'help': '"""database port"""'}), "('db_port', default=5432, help='database port')\n", (417, 464), False, 'from tornado.options import define, options\n'), ((465, 527), 'tornado.options.define', 'define', (['"""db_database"""'], {'default': '"""tornado"""', 'help': '"""database name"""'}), "('db_database', default='tornado', help='database name')\n", (471, 527), False, 'from tornado.options import define, options\n'), ((528, 586), 'tornado.options.define', 'define', (['"""db_user"""'], {'default': '"""tornado"""', 'help': '"""database user"""'}), "('db_user', default='tornado', help='database user')\n", (534, 586), False, 'from tornado.options import define, options\n'), ((587, 653), 'tornado.options.define', 'define', (['"""db_password"""'], {'default': '"""tornado"""', 'help': '"""database password"""'}), "('db_password', default='tornado', help='database password')\n", (593, 653), False, 'from tornado.options import define, options\n'), ((938, 966), 'tornado.options.options.parse_command_line', 'options.parse_command_line', ([], {}), '()\n', (964, 966), False, 'from tornado.options import define, options\n'), ((983, 1129), 'aiopg.create_pool', 'aiopg.create_pool', ([], {'host': 'options.db_host', 'port': 'options.db_port', 'user': 'options.db_user', 'password': 'options.db_password', 'dbname': 'options.db_database'}), '(host=options.db_host, port=options.db_port, user=options.\n    db_user, password=options.db_password, dbname=options.db_database)\n', (1000, 1129), False, 'import aiopg\n'), ((1245, 1260), 'app.application.Application', 'Application', (['db'], {}), '(db)\n', (1256, 1260), False, 'from app.application import Application\n')]