Skip to content

Commit

Permalink
Add Dataverse to UI. Fixes #900 (#969)
Browse files Browse the repository at this point in the history
Add Dataverse to UI. Fixes #900
  • Loading branch information
choldgraf authored Dec 10, 2019
2 parents 089702b + 8a0cee8 commit c7b7fe4
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 6 deletions.
4 changes: 3 additions & 1 deletion binderhub/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
from .main import MainHandler, ParameterizedMainHandler, LegacyRedirectHandler
from .repoproviders import (GitHubRepoProvider, GitRepoProvider,
GitLabRepoProvider, GistRepoProvider,
ZenodoProvider, FigshareProvider)
ZenodoProvider, FigshareProvider,
DataverseProvider)
from .metrics import MetricsHandler

from .utils import ByteSpecification, url_path_join
Expand Down Expand Up @@ -393,6 +394,7 @@ def _add_slash(self, proposal):
'gl': GitLabRepoProvider,
'zenodo': ZenodoProvider,
'figshare': FigshareProvider,
'dataverse': DataverseProvider,
},
config=True,
help="""
Expand Down
3 changes: 2 additions & 1 deletion binderhub/event-schemas/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"GitLab",
"Git",
"Zenodo",
"Figshare"
"Figshare",
"Dataverse"
],
"description": "Provider for the repository being launched"
},
Expand Down
3 changes: 2 additions & 1 deletion binderhub/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"gl": "GitLab",
"git": "Git repo",
"zenodo": "Zenodo",
"figshare": "Figshare"
"figshare": "Figshare",
"dataverse": "Dataverse",
}


Expand Down
59 changes: 58 additions & 1 deletion binderhub/repoproviders.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import re
import subprocess

import escapism
from prometheus_client import Gauge

from tornado import gen
from tornado.httpclient import AsyncHTTPClient, HTTPError, HTTPRequest
from tornado.httputil import url_concat

from traitlets import Dict, Unicode, Bool, default, List, observe
from traitlets import Dict, Unicode, Bool, default, List
from traitlets.config import LoggingConfigurable

from .utils import Cache
Expand Down Expand Up @@ -294,6 +295,62 @@ def get_build_slug(self):
return "figshare-{}".format(self.record_id)


class DataverseProvider(RepoProvider):
name = Unicode("Dataverse")

@gen.coroutine
def get_resolved_ref(self):
client = AsyncHTTPClient()
req = HTTPRequest("https://doi.org/{}".format(self.spec),
user_agent="BinderHub")
r = yield client.fetch(req)

search_url = urllib.parse.urlunparse(
urllib.parse.urlparse(r.effective_url)._replace(
path="/api/datasets/:persistentId"
)
)
req = HTTPRequest(search_url, user_agent="BinderHub")
r = yield client.fetch(req)
resp = json.loads(r.body)

assert resp["status"] == "OK"

self.identifier = resp["data"]["identifier"]
self.record_id = "{datasetId}.v{major}.{minor}".format(
datasetId=resp["data"]["id"],
major=resp["data"]["latestVersion"]["versionNumber"],
minor=resp["data"]["latestVersion"]["versionMinorNumber"],
)

# NOTE: data.protocol should be potentially prepended here
# {protocol}:{authority}/{identifier}
self.resolved_spec = "{authority}/{identifier}".format(
authority=resp["data"]["authority"],
identifier=resp["data"]["identifier"],
)
self.resolved_ref_url = resp["data"]["persistentUrl"]
return self.record_id

async def get_resolved_spec(self):
if not hasattr(self, 'resolved_spec'):
await self.get_resolved_ref()
return self.resolved_spec

async def get_resolved_ref_url(self):
if not hasattr(self, 'resolved_ref_url'):
await self.get_resolved_ref()
return self.resolved_ref_url

def get_repo_url(self):
# While called repo URL, the return value of this function is passed
# as argument to repo2docker, hence we return the spec as is.
return self.spec

def get_build_slug(self):
return "dataverse-" + escapism.escape(self.identifier, escape_char="-").lower()


class GitRepoProvider(RepoProvider):
"""Bare bones git repo provider.
Expand Down
7 changes: 6 additions & 1 deletion binderhub/static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ function updateRepoText() {
$("#ref").prop("disabled", true);
$("label[for=ref]").prop("disabled", true);
}
else if (provider === "dataverse") {
text = "Dataverse DOI (10.7910/DVN/TJCLKP)";
$("#ref").prop("disabled", true);
$("label[for=ref]").prop("disabled", true);
}
$("#repository").attr('placeholder', text);
$("label[for=repository]").text(text);
$("#ref").attr('placeholder', tag_text);
Expand All @@ -112,7 +117,7 @@ function getBuildFormValues() {
}

var ref = $('#ref').val().trim() || 'master';
if (providerPrefix === 'zenodo' || providerPrefix === 'figshare') {
if (providerPrefix === 'zenodo' || providerPrefix === 'figshare' || providerPrefix === 'dataverse') {
ref = "";
}
var path = $('#filepath').val().trim();
Expand Down
1 change: 1 addition & 0 deletions binderhub/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ <h4 id="form-header" class='row'>Build and launch a repository</h4>
<li class="dropdown-item" value="git"><a href="#">Git repository</a></li>
<li class="dropdown-item" value="zenodo"><a href="#">Zenodo DOI</a></li>
<li class="dropdown-item" value="figshare"><a href="#">Figshare DOI</a></li>
<li class="dropdown-item" value="dataverse"><a href="#">Dataverse DOI</a></li>
</ul>
</div>
<input class="form-control" type="text" id="repository" data-lpignore="true" placeholder="GitHub repository name or link"/>
Expand Down
27 changes: 26 additions & 1 deletion binderhub/tests/test_repoproviders.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from binderhub.repoproviders import (
tokenize_spec, strip_suffix, GitHubRepoProvider, GitRepoProvider,
GitLabRepoProvider, GistRepoProvider, ZenodoProvider, FigshareProvider
GitLabRepoProvider, GistRepoProvider, ZenodoProvider, FigshareProvider,
DataverseProvider
)


Expand Down Expand Up @@ -98,6 +99,30 @@ async def test_figshare(spec, resolved_spec, resolved_ref, resolved_ref_url, bui
assert spec == resolved_spec


@pytest.mark.parametrize('spec,resolved_spec,resolved_ref,resolved_ref_url,build_slug', [
['10.7910/DVN/TJCLKP',
'10.7910/DVN/TJCLKP',
'3035124.v3.0',
'https://doi.org/10.7910/DVN/TJCLKP',
'dataverse-dvn-2ftjclkp'],
])
async def test_dataverse(spec, resolved_spec, resolved_ref, resolved_ref_url, build_slug):
provider = DataverseProvider(spec=spec)

# have to resolve the ref first
ref = await provider.get_resolved_ref()
assert ref == resolved_ref

slug = provider.get_build_slug()
assert slug == build_slug
repo_url = provider.get_repo_url()
assert repo_url == spec
ref_url = await provider.get_resolved_ref_url()
assert ref_url == resolved_ref_url
spec = await provider.get_resolved_spec()
assert spec == resolved_spec


@pytest.mark.github_api
def test_github_ref():
namespace = 'jupyterhub/zero-to-jupyterhub-k8s'
Expand Down
2 changes: 2 additions & 0 deletions doc/developer/repoproviders.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Currently supported providers, their prefixes and specs are:
+------------+--------------------+-------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Figshare | ``figshare`` | ``<figshare-DOI>`` | `FigShare <https://figshare.com/>`_ is company that offers hosting for scholarly artifacts (such as code repositories). |
+------------+--------------------+-------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Dataverse | ``dataverse`` | ``<dataverse-DOI>`` | `Dataverse <https://dataverse.org/>`_ is open source research data repository software installed all over the world. |
+------------+--------------------+-------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Git | ``git`` | ``<url-escaped-url>/<commit-sha>`` | A generic repository provider for URLs that point directly to a git repository. |
+------------+--------------------+-------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+

Expand Down
7 changes: 7 additions & 0 deletions doc/reference/repoproviders.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ Module: :mod:`binderhub.repoproviders`
:members:


:class:`DataverseProvider`
---------------------------

.. autoconfigurable:: DataverseProvider
:members:


:class:`GitRepoProvider`
---------------------------

Expand Down

0 comments on commit c7b7fe4

Please sign in to comment.