Skip to content

Commit

Permalink
Merge pull request #37 from dereklu888/nf-external-links
Browse files Browse the repository at this point in the history
Add external links to files (if available) and display in context menu
  • Loading branch information
yarikoptic committed Mar 14, 2023
2 parents da42437 + 6bffdaf commit 400d2c4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ dist/
venvs/
fixtures/
.idea
.hypothesis
23 changes: 21 additions & 2 deletions datalad_deprecated/ls_webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from datalad_deprecated.utils import safe_print
from datalad.utils import with_pathsep
from datalad.utils import Path
import datalad.api as dl

import datalad_deprecated.metadata.extractors.datalad_core as dc

from datalad_deprecated.metadata.consts import (
OLDMETADATA_DIR,
Expand Down Expand Up @@ -199,7 +202,8 @@ def fs_traverse(path, repo, parent=None,
render=True,
recurse_datasets=False,
recurse_directories=False,
json=None, basepath=None):
json=None, basepath=None,
file_metadata=None):
"""Traverse path through its nodes and returns a dictionary of relevant
attributes attached to each node
Expand All @@ -216,6 +220,9 @@ def fs_traverse(path, repo, parent=None,
render: bool
To render from within function or not. Set to false if results to be
manipulated before final render
file_metadata: dict
Extracted metadata of all files in the dataset being traversed in.
This will be used to attach external urls to files if they have them.
Returns
-------
Expand Down Expand Up @@ -298,6 +305,8 @@ def fs_traverse(path, repo, parent=None,
subdir = fs_extract(nodepath,
repo,
basepath=basepath or path)
if file_metadata and nodepath in file_metadata:
subdir['external_urls'] = file_metadata[nodepath]
# append child metadata to list
children.extend([subdir])

Expand Down Expand Up @@ -352,6 +361,15 @@ def ds_traverse(rootds, parent=None, json=None,
fsparent = fs_extract(parent.path, parent.repo, basepath=rootds.path) \
if parent else None

# from my understanding, this constructs a file metadata map that maps file path to external urls found
# (the map is passed to fs_traverse)
metadata_raw = dl.extract_metadata(types=["datalad_core"], dataset=rootds)
metadata_map = {}

for file_metadata in metadata_raw:
if file_metadata['type'] == 'file' and 'datalad_core' in file_metadata['metadata'] and 'url' in file_metadata['metadata']['datalad_core']:
metadata_map[file_metadata['path']] = file_metadata['metadata']['datalad_core']['url']

# (recursively) traverse file tree of current dataset
fs = fs_traverse(
rootds.path, rootds.repo,
Expand All @@ -361,7 +379,8 @@ def ds_traverse(rootds, parent=None, json=None,
# XXX note that here I kinda flipped the notions!
recurse_datasets=recurse_datasets,
recurse_directories=recurse_directories,
json=json
json=json,
file_metadata=metadata_map
)

# BUT if we are recurse_datasets but not recurse_directories
Expand Down
18 changes: 18 additions & 0 deletions datalad_deprecated/resources/website/assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,24 @@ function directory(jQuery, md5) {
menu += '<li class="context-option external"><a href="' + data.url + '">Open External Link</a></li>';
}

// add in external urls for any files that have them
if (data.external_urls) {
if (data.external_urls.length == 1) {
menu += '<li class="context-option external"><a href="' + data.external_urls[0] + '">' + 'Open External URL' + '</a></li>';
}
// if there are multiple external urls, list at most 5 (currently just gets the first 5)
else if (data.external_urls.length > 1) {
menu += '<li class="context-option separator">External URLs</li>';
for (var i = 0; i < Math.min(5, data.external_urls.length); i++) {
var link = document.createElement("a");
link.href = data.external_urls[i];
link.protocol = "https";
menu += '<li class="context-option service"><a href="' + data.external_urls[i] + '">' + link.hostname + '</a></li>';
}
}

}

if (data.type === 'dir' || data.type === 'git' || data.type === 'annex') {
var folderUrl = traverse.next
if (folderUrl.indexOf("?dir=") > -1){
Expand Down
2 changes: 1 addition & 1 deletion datalad_deprecated/tests/test_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def generate_nii(f):

def verify_nii(f, mode="r"):
ni = nib.load(f)
assert_array_equal(ni.get_data(), d)
assert_array_equal(ni.get_fdata(), d)

_test_proxying_open(generate_nii, verify_nii)

Expand Down

0 comments on commit 400d2c4

Please sign in to comment.