Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project and execution opening commands #16

Merged
merged 6 commits into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tests/commands/execution/test_open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import webbrowser

from tests.commands.execution.utils import get_execution_data_mock
from tests.utils import make_call_stub
from valohai_cli.commands.execution.open import open


def test_open(monkeypatch, runner, logged_in_and_linked):
call_stub = make_call_stub()
monkeypatch.setattr(webbrowser, 'open', call_stub)
with get_execution_data_mock():
runner.invoke(open, ['7'], catch_exceptions=False)
assert call_stub.calls
17 changes: 17 additions & 0 deletions tests/commands/project/test_open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import webbrowser

import requests_mock

from tests.fixture_data import PROJECT_DATA
from tests.utils import make_call_stub
from valohai_cli.commands.project.open import open


def test_open(monkeypatch, runner, logged_in_and_linked):
call_stub = make_call_stub()
monkeypatch.setattr(webbrowser, 'open', call_stub)
with requests_mock.mock() as m:
project_data = dict(PROJECT_DATA)
m.get('https://app.valohai.com/api/v0/projects/{id}/'.format(id=project_data['id']), json=project_data)
runner.invoke(open, catch_exceptions=False)
assert call_stub.calls
9 changes: 6 additions & 3 deletions tests/fixture_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
'owner': 1,
'ctime': '2016-12-16T12:25:52.718310Z',
'mtime': '2017-01-20T14:35:02.196871Z',
'urls': {
'display': 'https://app.valohai.com/p/nyan/nyan/',
}
}

execution_id = str(uuid.uuid4())
Expand All @@ -35,9 +38,9 @@
'step': 'run training',
'url': 'https://app.valohai.com/api/v0/executions/{id}/'.format(id=execution_id),
'urls': {
'copy': '/api/v0/executions/34/copy/',
'display': '/p/test/mnist/execution/34/',
'stop': '/api/v0/executions/34/stop/',
'copy': 'https://app.valohai.com/api/v0/executions/34/copy/',
'display': 'https://app.valohai.com/p/test/mnist/execution/34/',
'stop': 'https://app.valohai.com/api/v0/executions/34/stop/',
},
'events': [
{
Expand Down
11 changes: 11 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ def get_project_data(n_projects):
for i in range(n_projects)
],
}


def make_call_stub(retval=None):
calls = []

def call_stub(*args, **kwargs):
calls.append({'args': args, 'kwargs': kwargs})
return retval

call_stub.calls = calls
return call_stub
2 changes: 2 additions & 0 deletions valohai_cli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,6 @@ def request(method, url, **kwargs):
:rtype: requests.Response
"""
session = _get_current_api_session()
if url.startswith(session.base_url):
url = url[len(session.base_url):]
return session.request(method, url, **kwargs)
14 changes: 14 additions & 0 deletions valohai_cli/commands/execution/open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import click

from valohai_cli.ctx import get_project
from valohai_cli.utils import open_browser


@click.command()
@click.argument('counter')
def open(counter):
"""
Open an execution in a web browser.
"""
execution = get_project(require=True).get_execution_from_counter(counter=counter, detail=True)
open_browser(execution)
15 changes: 15 additions & 0 deletions valohai_cli/commands/project/open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import click

from valohai_cli.api import request
from valohai_cli.ctx import get_project
from valohai_cli.utils import open_browser


@click.command()
def open():
"""
Open the project's view in a web browser.
"""
project = get_project(require=True)
project_data = request('get', '/api/v0/projects/{id}/'.format(id=project.id)).json()
open_browser(project_data)
8 changes: 7 additions & 1 deletion valohai_cli/commands/project/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def status(summary, incomplete):
project = get_project(require=True)
project_data = request('get', '/api/v0/projects/{id}/'.format(id=project.id)).json()

click.secho('# Project %s\n' % click.style(project.name, underline=True), bold=True)
click.secho('# Project %s' % click.style(project.name, underline=True), bold=True)
if 'urls' in project_data:
click.secho(' %s' % project_data['urls']['display'])
click.secho('')

if summary:
print_execution_summary(project_data)
Expand All @@ -42,6 +45,9 @@ def print_execution_summary(project_data):
if not execution_summary:
return
total = execution_summary.pop('count')
if not total:
click.secho('No executions yet.', fg='cyan')
return
click.secho('## Summary of %d executions\n' % total, bold=True)
print_table(
[
Expand Down
11 changes: 11 additions & 0 deletions valohai_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import random
import re
import string
import webbrowser

import click
import six


Expand Down Expand Up @@ -109,3 +111,12 @@ def find_scripts(directory):
interpreter = extension_to_interpreter.get(os.path.splitext(filename.lower())[1])
if interpreter:
yield (interpreter, os.path.basename(filename))


def open_browser(object, url_name='display'):
if 'urls' not in object:
return False
url = object['urls'][url_name]
click.echo('Opening {} ...'.format(click.style(url, bold=True)))
webbrowser.open(url)
return True