-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
30 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,37 @@ | ||
from pathlib import Path | ||
from threading import Lock | ||
from typing import NewType, Optional | ||
from typing import Optional | ||
|
||
from scitacean import Client | ||
from scitacean import Client, Dataset | ||
|
||
from .nexus import FilePath | ||
|
||
ScitaceanToken = NewType('ScitaceanToken', str) | ||
'''Token used to authenticate the scitacean client''' | ||
ScitaceanVersion = NewType('ScitaceanVersion', str) | ||
'''Version of the scitacean api''' | ||
ScitaceanClient = NewType('ScitaceanClient', Client) | ||
'''An instance of scitacean.Client that is used to fetch data from Scicat''' | ||
|
||
|
||
_locks = {} | ||
_file_download_locks = {} | ||
|
||
|
||
def download_scicat_file( | ||
client: Client, | ||
dataset_id: str, | ||
filename: str, | ||
*, | ||
client: Client, | ||
target: Optional[Path] = None, | ||
) -> FilePath: | ||
if target is None: | ||
target = Path(f'~/.cache/essreduce/{dataset_id}') | ||
key = (dataset_id, filename, target) | ||
with _locks.setdefault(key, Lock()): | ||
with _file_download_locks.setdefault(key, Lock()): | ||
dset = client.get_dataset(dataset_id) | ||
dset = client.download_files(dset, target=target, select=filename) | ||
_locks.pop(key) | ||
_file_download_locks.pop(key) | ||
return dset.files[0].local_path | ||
|
||
|
||
def get_related_dataset(client: Client, ds: Dataset, relationship: str) -> Dataset: | ||
'''Goes through the datasets related to 'ds' | ||
and finds the one with the selected relation''' | ||
for d in getattr(ds, 'relationships', ()): | ||
if d.relationship == relationship: | ||
return client.get_dataset(d.pid) | ||
raise ValueError( | ||
f'The requested relation "{relationship}" was not found in dataset {ds}' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters