"""Miscellaneous tools for Heroku."""

import pexpect
from psiturk.psiturk_config import PsiturkConfig
import subprocess


def app_name(id):
    """Convert a UUID to a valid Heroku app name."""
    return "dlgr-" + id[0:8]


def log_in():
    """Ensure that the user is logged in to Heroku."""
    p = pexpect.spawn("heroku auth:whoami")
    p.interact()


def scale_up_dynos(app):
    """Scale up the Heroku dynos."""
    # Load psiTurk configuration.
    config = PsiturkConfig()
    config.load_config()

    dyno_type = config.get('Server Parameters', 'dyno_type')
    num_dynos_web = config.get('Server Parameters', 'num_dynos_web')
    num_dynos_worker = config.get('Server Parameters', 'num_dynos_worker')

    subprocess.call(
        "heroku ps:scale web=" + str(num_dynos_web) + ":" +
        str(dyno_type) + " --app " + app, shell=True)

    subprocess.call(
        "heroku ps:scale worker=" + str(num_dynos_worker) + ":" +
        str(dyno_type) + " --app " + app, shell=True)

    clock_on = config.getboolean('Server Parameters', 'clock_on')
    if clock_on:
        subprocess.call(
            "heroku ps:scale clock=1:" + dyno_type + " --app " + app,
            shell=True)

"""Miscellaneous tools for Heroku."""

import pexpect
import subprocess

import dallinger as dlgr


def app_name(id):
    """Convert a UUID to a valid Heroku app name."""
    return "dlgr-" + id[0:8]


def log_in():
    """Ensure that the user is logged in to Heroku."""
    p = pexpect.spawn("heroku auth:whoami")
    p.interact()


def scale_up_dynos(app):
    """Scale up the Heroku dynos."""
    dyno_type = dlgr.config.server_parameters.dyno_type

    num_dynos = {
        "web": dlgr.config.server_parameters.num_dynos_web,
        "worker": dlgr.config.server_parameters.num_dynos_worker
    }

    for process in ["web", "worker"]:
        subprocess.call([
            "heroku",
            "ps:scale",
            "{}={}:{}".format(process, num_dynos[process], dyno_type),
            "--app",
            app,
        ])

    if dlgr.config.server_parameters.clock_on:
        subprocess.call([
            "heroku",
            "ps:scale",
            "clock=1:{}".format(dyno_type),
            "--app",
            app,
        ])

Simplify configuration