Skip to content

Commit

Permalink
Add exec open and proj open utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Apr 13, 2017
1 parent 680679e commit 501ef0e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
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
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)
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

0 comments on commit 501ef0e

Please sign in to comment.