from flask import Flask
from config import Config
from sqlalchemy import MetaData
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_moment import Moment
from flask_misaka import Misaka
from flask_bootstrap import Bootstrap
import os
import logging
from logging.handlers import RotatingFileHandler
from elasticsearch import Elasticsearch

convention = {
    "ix": 'ix_%(column_0_label)s',
    "uq": "uq_%(table_name)s_%(column_0_name)s",
    "ck": "ck_%(table_name)s_%(constraint_name)s",
    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    "pk": "pk_%(table_name)s"
}

metadata = MetaData(naming_convention=convention)

db = SQLAlchemy(metadata=metadata)
migrate = Migrate()
login = LoginManager()
login.login_view = "auth.login"
moment = Moment()
md = Misaka()
bootstrap = Bootstrap()

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)

    db.init_app(app)
    with app.app_context():
        if db.engine.url.drivername == 'sqlite':
            migrate.init_app(app, db, render_as_batch=True)
        else:
            migrate.init_app(app, db)
    # migrate.init_app(app, db)
    login.init_app(app)
    moment.init_app(app)
    md.init_app(app)
    bootstrap.init_app(app)

    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    from app.cli import bp as cli_bp
    app.register_blueprint(cli_bp)

    app.elasticsearch = Elasticsearch([app.config['ELASTICSEARCH_URL']]) \
        if app.config['ELASTICSEARCH_URL'] else None

    from app import models

    if not app.debug and not app.testing:
        if not os.path.exists("logs"):
            os.mkdir("logs")
        file_handler = RotatingFileHandler(
            "logs/moviedb.log", maxBytes=10240, backupCount=10
        )
        file_handler.setFormatter(
            logging.Formatter(
                "%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]"
            )
        )
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info("Moviedb startup")
    
    return app

[((23, 11, 23, 49), 'sqlalchemy.MetaData', 'MetaData', (), '', False, 'from sqlalchemy import MetaData\n'), ((25, 5, 25, 34), 'flask_sqlalchemy.SQLAlchemy', 'SQLAlchemy', (), '', False, 'from flask_sqlalchemy import SQLAlchemy\n'), ((26, 10, 26, 19), 'flask_migrate.Migrate', 'Migrate', ({}, {}), '()', False, 'from flask_migrate import Migrate\n'), ((27, 8, 27, 22), 'flask_login.LoginManager', 'LoginManager', ({}, {}), '()', False, 'from flask_login import LoginManager\n'), ((29, 9, 29, 17), 'flask_moment.Moment', 'Moment', ({}, {}), '()', False, 'from flask_moment import Moment\n'), ((30, 5, 30, 13), 'flask_misaka.Misaka', 'Misaka', ({}, {}), '()', False, 'from flask_misaka import Misaka\n'), ((31, 12, 31, 23), 'flask_bootstrap.Bootstrap', 'Bootstrap', ({}, {}), '()', False, 'from flask_bootstrap import Bootstrap\n'), ((34, 10, 34, 25), 'flask.Flask', 'Flask', ({(34, 16, 34, 24): '__name__'}, {}), '(__name__)', False, 'from flask import Flask\n'), ((61, 24, 61, 72), 'elasticsearch.Elasticsearch', 'Elasticsearch', ({(61, 38, 61, 71): "[app.config['ELASTICSEARCH_URL']]"}, {}), "([app.config['ELASTICSEARCH_URL']])", False, 'from elasticsearch import Elasticsearch\n'), ((69, 23, 71, 9), 'logging.handlers.RotatingFileHandler', 'RotatingFileHandler', (), '', False, 'from logging.handlers import RotatingFileHandler\n'), ((67, 15, 67, 37), 'os.path.exists', 'os.path.exists', ({(67, 30, 67, 36): '"""logs"""'}, {}), "('logs')", False, 'import os\n'), ((68, 12, 68, 28), 'os.mkdir', 'os.mkdir', ({(68, 21, 68, 27): '"""logs"""'}, {}), "('logs')", False, 'import os\n'), ((73, 12, 75, 13), 'logging.Formatter', 'logging.Formatter', ({(74, 16, 74, 85): '"""%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]"""'}, {}), "(\n    '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')", False, 'import logging\n')]