import logging
import os
import sys
import urllib
import urlparse

import redis
from flask_mail import Mail
from flask_limiter import Limiter
from flask_limiter.util import get_ipaddr
from flask_migrate import Migrate
from statsd import StatsClient

from . import settings
from .app import create_app  # noqa

__version__ = '8.0.0-beta'


if os.environ.get("REMOTE_DEBUG"):
    import ptvsd
    ptvsd.enable_attach(address=('0.0.0.0', 5678))


def setup_logging():
    handler = logging.StreamHandler(sys.stdout if settings.LOG_STDOUT else sys.stderr)
    formatter = logging.Formatter(settings.LOG_FORMAT)
    handler.setFormatter(formatter)
    logging.getLogger().addHandler(handler)
    logging.getLogger().setLevel(settings.LOG_LEVEL)

    # Make noisy libraries less noisy
    if settings.LOG_LEVEL != "DEBUG":
        for name in ["passlib", "requests.packages.urllib3", "snowflake.connector", "apiclient"]:
            logging.getLogger(name).setLevel("ERROR")


def create_redis_connection():
    logging.debug("Creating Redis connection (%s)", settings.REDIS_URL)
    redis_url = urlparse.urlparse(settings.REDIS_URL)

    if redis_url.scheme == 'redis+socket':
        qs = urlparse.parse_qs(redis_url.query)
        if 'virtual_host' in qs:
            db = qs['virtual_host'][0]
        else:
            db = 0

        client = redis.StrictRedis(unix_socket_path=redis_url.path, db=db)
    else:
        use_ssl = redis_url.scheme == 'rediss'

        if redis_url.path:
            redis_db = redis_url.path[1]
        else:
            redis_db = 0
        # Redis passwords might be quoted with special characters
        redis_password = redis_url.password and urllib.unquote(redis_url.password)
        client = redis.StrictRedis(
            host=redis_url.hostname, port=redis_url.port, db=redis_db, password=redis_password,
            ssl=use_ssl)

    return client


setup_logging()

redis_connection = create_redis_connection()

mail = Mail()

migrate = Migrate()

statsd_client = StatsClient(host=settings.STATSD_HOST, port=settings.STATSD_PORT, prefix=settings.STATSD_PREFIX)

limiter = Limiter(key_func=get_ipaddr, storage_uri=settings.LIMITER_STORAGE)

import logging
import os
import sys
import urllib
import urlparse

import redis
from flask_mail import Mail
from flask_limiter import Limiter
from flask_limiter.util import get_ipaddr
from flask_migrate import Migrate
from statsd import StatsClient

from . import settings
from .app import create_app  # noqa

__version__ = '8.0.0-beta'


if os.environ.get("REMOTE_DEBUG"):
    import ptvsd
    ptvsd.enable_attach(address=('0.0.0.0', 5678))


def setup_logging():
    handler = logging.StreamHandler(sys.stdout if settings.LOG_STDOUT else sys.stderr)
    formatter = logging.Formatter(settings.LOG_FORMAT)
    handler.setFormatter(formatter)
    logging.getLogger().addHandler(handler)
    logging.getLogger().setLevel(settings.LOG_LEVEL)

    # Make noisy libraries less noisy
    if settings.LOG_LEVEL != "DEBUG":
        for name in ["passlib", "requests.packages.urllib3", "snowflake.connector", "apiclient"]:
            logging.getLogger(name).setLevel("ERROR")


setup_logging()

redis_connection = redis.from_url(settings.REDIS_URL)
mail = Mail()
migrate = Migrate()
statsd_client = StatsClient(host=settings.STATSD_HOST, port=settings.STATSD_PORT, prefix=settings.STATSD_PREFIX)
limiter = Limiter(key_func=get_ipaddr, storage_uri=settings.LIMITER_STORAGE)

Remove custom Redis connection code in favor of redis.from_url (#3992)