<reponame>butzopower/flash
"""The main Flask application."""

import logging
from datetime import datetime, date, timedelta
from os import getenv

from flash_services import blueprint
from flask import Flask, jsonify, render_template, request

from .parse import parse_config

logger = logging.getLogger(__name__)

app = Flask(__name__)
app.secret_key = getenv('FLASK_SECRET_KEY', 'youwillneverguessit')
app.register_blueprint(blueprint, url_prefix='/flash_services')

CACHE = {}


CONFIG = parse_config()


@app.route('/')
def home():
    """Home page route."""
    return render_template(
        'home.html',
        config=CONFIG,
        data=_get_data(),
        title='Flash',
    )


@app.route('/scratchpad')
def scratchpad():
    """Dummy page for styling tests."""
    return render_template(
        'demo.html',
        config=dict(
            project_name='Scratchpad',
            style=request.args.get('style', 'default'),
        ),
        title='Style Scratchpad',
    )


@app.route('/_services')
def services():
    """AJAX route for accessing services."""
    return jsonify(_get_data())


def _get_data():
    service_map = CONFIG['services']
    return {name: update_service(name, service_map) for name in service_map}


def update_service(name, service_map):
    """Get an update from the specified service.

    Arguments:
      name (:py:class:`str`): The name of the service.
      service_map (:py:class:`dict`): A mapping of service names to
        :py:class:`flash.service.core.Service` instances.

    Returns:
      :py:class:`dict`: The updated data.

    """
    if name in service_map:
        service = service_map[name]
        data = service.update()
        if not data:
            logger.warning('no data received for service: %s', name)
        else:
            data['service_name'] = service.service_name
            CACHE[name] = dict(data=data, updated=datetime.now())
    else:
        logger.warning('service not found: %s', name)
    if name in CACHE:
        return add_time(CACHE[name])
    return {}


def add_time(data):
    """And a friendly update time to the supplied data.

    Arguments:
      data (:py:class:`dict`): The response data and its update time.

    Returns:
      :py:class:`dict`: The data with a friendly update time.

    """
    payload = data['data']
    updated = data['updated'].date()
    if updated == date.today():
        payload['last_updated'] = data['updated'].strftime('today at %H:%M:%S')
    elif updated >= (date.today() - timedelta(days=1)):
        payload['last_updated'] = 'yesterday'
    elif updated >= (date.today() - timedelta(days=7)):
        payload['last_updated'] = updated.strftime('on %A')
    else:
        payload['last_updated'] = updated.strftime('%Y-%m-%d')
    return payload

"""The main Flask application."""

import logging
from datetime import datetime, date, timedelta
from os import getenv

from flash_services import blueprint
from flask import Flask, jsonify, render_template, request

from .parse import parse_config

logger = logging.getLogger(__name__)

app = Flask(__name__)
app.secret_key = getenv('FLASK_SECRET_KEY', 'youwillneverguessit')
app.register_blueprint(blueprint, url_prefix='/flash_services')

CACHE = {}


CONFIG = parse_config()


@app.route('/')
def home():
    """Home page route."""
    return render_template(
        'home.html',
        config=CONFIG,
        data=_get_data(),
        title='Flash',
    )


@app.route('/scratchpad')
def scratchpad():
    """Dummy page for styling tests."""
    return render_template(
        'demo.html',
        config=dict(
            project_name='Scratchpad',
            style=request.args.get('style', 'default'),
        ),
        title='Style Scratchpad',
    )


@app.route('/_services')
def services():
    """AJAX route for accessing services."""
    return jsonify(_get_data())


def _get_data():
    service_map = CONFIG['services']
    return {name: update_service(name, service_map) for name in service_map}


def update_service(name, service_map):
    """Get an update from the specified service.

    Arguments:
      name (:py:class:`str`): The name of the service.
      service_map (:py:class:`dict`): A mapping of service names to
        :py:class:`flash.service.core.Service` instances.

    Returns:
      :py:class:`dict`: The updated data.

    """
    if name in service_map:
        service = service_map[name]
        data = service.update()
        if not data:
            logger.warning('no data received for service: %s', name)
        else:
            data['service_name'] = service.service_name
            CACHE[name] = dict(data=data, updated=datetime.now())
    else:
        logger.warning('service not found: %s', name)
    if name in CACHE:
        return add_time(CACHE[name])
    return {}


def add_time(data):
    """And a friendly update time to the supplied data.

    Arguments:
      data (:py:class:`dict`): The response data and its update time.

    Returns:
      :py:class:`dict`: The data with a friendly update time.

    """
    payload = data['data']
    updated = data['updated'].date()
    if updated == date.today():
        payload['last_updated'] = data['updated'].strftime('today at %H:%M:%S')
    elif updated >= (date.today() - timedelta(days=1)):
        payload['last_updated'] = 'yesterday'
    elif updated >= (date.today() - timedelta(days=7)):
        payload['last_updated'] = updated.strftime('on %A')
    else:
        payload['last_updated'] = updated.strftime('%Y-%m-%d')
    return payload
The main Flask application. Home page route. Dummy page for styling tests. AJAX route for accessing services. Get an update from the specified service.      Arguments:       name (:py:class:`str`): The name of the service.       service_map (:py:class:`dict`): A mapping of service names to         :py:class:`flash.service.core.Service` instances.      Returns:       :py:class:`dict`: The updated data. And a friendly update time to the supplied data.      Arguments:       data (:py:class:`dict`): The response data and its update time.      Returns:       :py:class:`dict`: The data with a friendly update time.