Commit d8a3b253 authored by Franziska Koehn's avatar Franziska Koehn
Browse files

download via rest-interface, defined in type-json-file

parent b28cf5da
{"root-type":"xnat:mrScanData",
{
"root-type":"xnat:mrScanData",
"REST-API":"/data/archive/projects/{xnat_mrsessiondata_project}/subjects/{xnat_mrsessiondata_subject_id}/experiments/{xnat_mrsessiondata_session_id}/scans/{id}/resources/DICOM/files?format=zip",
"fields":[
{"label":"type", "field":"xnat:mrScanData/TYPE", "required":true},
{"label":"Type", "field":"xnat:mrScanData/TYPE", "required":true},
{"label":"Orientation", "field":"xnat:mrScanData/PARAMETERS_ORIENTATION", "required":false},
{"label":"fov_x", "field":"xnat:mrScanData/PARAMETERS_FOV_X", "required":false},
{"label":"fov_y", "field":"xnat:mrScanData/PARAMETERS_FOV_Y", "required":false},
{"label":"tr", "field": "xnat:mrScanData/PARAMETERS_TR", "required":false},
{"label":"te", "field": "xnat:mrScanData/PARAMETERS_TE", "required":false},
{"label":"ti", "field": "xnat:mrScanData/PARAMETERS_TI", "required":false},
{"label":"flip", "field": "xnat:mrScanData/PARAMETERS_FLIP", "required":false},
{"label":"voxel_res_x", "field":"xnat:mrScanData/PARAMETERS_VOXELRES_X", "required":false},
{"label":"voxel_res_y", "field":"xnat:mrScanData/PARAMETERS_VOXELRES_Y", "required":false},
{"label":"voxel_res_z", "field":"xnat:mrScanData/PARAMETERS_VOXELRES_Z", "required":false},
{"label":"Project-ID", "field":"xnat:mrSessionData/PROJECT", "required":true},
{"label":"Subject-ID", "field":"xnat:mrSessionData/SUBJECT_ID", "required":true},
{"label":"Session-ID", "field":"xnat:mrSessionData/SESSION_ID", "required":true},
{"label":"Sequence", "field":"xnat:mrScanData/PARAMETERS_SEQUENCE", "required":false},
{"label":"Image-Type", "field":"xnat:mrScanData/PARAMETERS_IMAGETYPE", "required":false},
{"label":"TR", "field": "xnat:mrScanData/PARAMETERS_TR", "required":false},
{"label":"TE", "field": "xnat:mrScanData/PARAMETERS_TE", "required":false},
{"label":"TI", "field": "xnat:mrScanData/PARAMETERS_TI", "required":false},
{"label":"Flip", "field": "xnat:mrScanData/PARAMETERS_FLIP", "required":false},
{"label":"Voxel_res_x", "field":"xnat:mrScanData/PARAMETERS_VOXELRES_X", "required":false},
{"label":"Voxel_res_y", "field":"xnat:mrScanData/PARAMETERS_VOXELRES_Y", "required":false},
{"label":"Voxel_res_z", "field":"xnat:mrScanData/PARAMETERS_VOXELRES_Z", "required":false},
{"label":"Project-ID", "field":"xnat:mrSessionData/PROJECT", "required":true, "key":"xnat_mrsessiondata_project"},
{"label":"Subject-ID", "field":"xnat:mrSessionData/SUBJECT_ID", "required":true, "key": "xnat_mrsessiondata_subject_id"},
{"label":"Session-ID", "field":"xnat:mrSessionData/SESSION_ID", "required":true, "key": "xnat_mrsessiondata_session_id"},
{"label":"Scan-ID", "field":"xnat:mrScanData/ID", "required":true, "key":"id"}
]
}
......@@ -182,6 +182,7 @@ class QueryController(gobject.GObject):
def event_clicked_download(*_):
import gobject
import xsalogic.queries as queries
import xsalogic.datatypereader as type_reader
dialog = gtk.FileChooserDialog( "Open..",
None,
......@@ -192,6 +193,7 @@ class QueryController(gobject.GObject):
response = dialog.run()
if response == gtk.RESPONSE_OK:
rest = type_reader.get_rest(self.root)
enabled = (d for d in self.resultsview.get_store() if d[0])
for d in enabled:
def stop_spinner(_row):
......@@ -201,6 +203,8 @@ class QueryController(gobject.GObject):
d[2] = True # Show Spinner
queries.download_async( d[1],
self.host,
self.credentials_tuple,
rest,
dialog.get_filename(),
cb=stop_spinner,
cb_args=(d,)
......
......@@ -20,6 +20,10 @@ def get_all(force=False):
return get_all()
get_all.cache = None
def get_rest(type):
for data in get_all():
if data['root-type'] == type:
return data['REST-API']
def get_data_types():
result = []
......
......@@ -25,38 +25,47 @@ def download_all(results, host):
for r in results:
download(r, host)
def download_async(result, host, dest_folder='', cb=(lambda *_: None), cb_args=()):
def download_async(result, host, creds, rest, dest_folder='', cb=(lambda *_: None), cb_args=()):
from threading import Thread
download_thread = Thread(target=download, args=(result, host, dest_folder, cb, cb_args))
download_thread = Thread(target=download, args=(result, host, creds, rest, dest_folder, cb, cb_args))
download_thread.start()
return download_thread
def download(result, host, dest_folder='', cb=(lambda *_: None), cb_args=()):
def download(result, host, creds, rest, dest_folder='', cb=(lambda *_: None), cb_args=()):
requests_lock.acquire()
print(result)
subject = result['xnat_mrsessiondata_subject_id'] #TODO download andere typen
experiment = result['xnat_mrsessiondata_session_id']
project = result['xnat_mrsessiondata_project']
scan = result['id']
file_name = "%s/%s-%s-%s-%s" % (dest_folder, project, subject, experiment, scan)
download_file(host, project, subject, experiment, scan, file_name)
import re
import os
names=[]
def subfunc(f):
key = f.group(0).strip("{}")
r = result[key]
names.append(r)
return r
ret = re.sub('\{\w+\}', subfunc, rest)
path = os.path.join(dest_folder, '-'.join(names))
url = "%s%s" % (host, ret)
print (path, url, creds)
download_file(url, creds, path)
requests_lock.release()
cb(*cb_args)
def download_file(host, project, subject, experiment, scan, file_name):
def download_file(url, creds, path):
import requests
from base64 import b64encode
from requests.auth import HTTPBasicAuth
if not file_name.endswith(".zip"):
file_name += ".zip"
url= "%s/data/archive/projects/%s/subjects/%s/experiments/%s/scans/%s/resources/DICOM/files?format=zip" % (host, project, subject, experiment, scan)
if not path.endswith(".zip"):
path += ".zip"
user, passw = get_credentials()
user, passw = creds
try:
with open(file_name, 'wb') as handle:
with open(path, 'wb') as handle:
response = requests.get(url, stream=True, auth=HTTPBasicAuth(user, passw))
if not response.ok:
raise ValueError(response.status_code)
......@@ -65,7 +74,7 @@ def download_file(host, project, subject, experiment, scan, file_name):
break
handle.write(block)
except IOError as e:
print "Error writing file %s, %s" % (file_name, e)
print "Error writing file %s, %s" % (path, e)
except ValueError as e:
print "Error downloading file %s, Status Code %s" % (url, e)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment