import click

from six.moves.urllib.parse import urlparse
from six.moves.urllib.parse import urlunparse

from dallinger.command_line.utils import header
from dallinger.command_line.utils import log
from dallinger.command_line.utils import Output
from dallinger.command_line.utils import require_exp_directory
from dallinger.config import get_config
from dallinger.deployment import DevelopmentDeployment
from dallinger.utils import open_browser


BASE_URL = "http://127.0.0.1:7000/"


def ad_url(config):
    return BASE_URL + "ad?generate_tokens=true&recruiter=hotair"


def dashboard_url(config):
    parsed = list(urlparse(BASE_URL + "dashboard/develop"))
    parsed[1] = "{}:{}@{}".format(
        config.get("dashboard_user"),
        config.get("dashboard_password"),
        parsed[1],
    )
    return urlunparse(parsed)


valid_routes = {
    "ad": ad_url,
    "dashboard": dashboard_url,
}


@click.group()
def develop():
    pass


@develop.command()
@require_exp_directory
def bootstrap(exp_config=None):
    """Run the experiment locally."""
    bootstrapper = DevelopmentDeployment(Output(), exp_config)
    log(header, chevrons=False)
    bootstrapper.run()


@develop.command()
@click.option("--route", default=None, help="Route name")
def browser(route=None):
    """Open one of the supported routes with appropriate path and URL parameters"""
    config = get_config()
    config.load()
    url_factory = valid_routes.get(route)
    if url_factory is not None:
        open_browser(url_factory(config))
    else:
        click.echo(
            "Supported routes are:\n\t{}".format("\n\t".join(valid_routes.keys()))
        )

@@ -473,12 +473,10 @@
 0.1:
-7000
+%7B%7D
 /%22%0A%0A
@@ -489,24 +489,30 @@
 d_url(config
+, port
 ):%0A    retur
@@ -517,24 +517,37 @@
 urn BASE_URL
+.format(port)
  + %22ad?gener
@@ -606,16 +606,22 @@
 l(config
+, port
 ):%0A    p
@@ -650,16 +650,29 @@
 BASE_URL
+.format(port)
  + %22dash
@@ -1237,16 +1237,21 @@
 .option(
+%0A    
 %22--route
@@ -1252,16 +1252,20 @@
 -route%22,
+%0A   
  default
@@ -1270,16 +1270,20 @@
 lt=None,
+%0A   
  help=%22R
@@ -1291,16 +1291,155 @@
 ute name
+ (valid routes are: %7B%7D)%22.format(%22, %22.join(valid_routes.keys())),%0A)%0A@click.option(%22--port%22, default=5000, help=%22The port Flask is running on
 %22)%0Adef b
@@ -1451,24 +1451,35 @@
 r(route=None
+, port=5000
 ):%0A    %22%22%22Op
@@ -1710,16 +1710,22 @@
 y(config
+, port
 ))%0A    e

Allow specifying a port to the browser command, since it can be set to a non-default when running the run.sh script