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

method for using permanent connection implemented

parent 22f5a5df
......@@ -137,6 +137,33 @@ def download_file(url, creds, path):
return False
def get_xnat_server_connection(force=False, host="", user="", passw=""):
from pyxnat import Interface
from tempfile import mkdtemp
import xsa.errors
if get_xnat_server_connection.cache and not force:
return get_xnat_server_connection.cache
elif force and host=="":
raise xsa.errors.ServerNotFoundError("The host was not defined.")
elif force and (user=="" or passw==""):
raise xsa.errors.UnauthorizedError("The user-name and/or the user-password were not defined.")
disconnect_xnat_server()
try:
get_xnat_server_connection.cache = Interface(server=host, user=user, password=passw, cachedir=mkdtemp())
except IndexError as e:
raise xsa.errors.ServerNotFoundError("Server not found, check your host-address.")
return get_xnat_server_connection()
get_xnat_server_connection.cache = None
def disconnect_xnat_server():
import xsa.errors
try:
get_xnat_server_connection.cache.disconnect()
get_xnat_server_connection.cache = None
except:
return
def search_for(host, root_element, constraints, search_fields, user, passw):
"""
Does a search for given values. raises xsa -Exceptions
......@@ -155,7 +182,6 @@ def search_for(host, root_element, constraints, search_fields, user, passw):
:xsa.errors.ResponseNotReady:
:xsa.errors.QueryError:
"""
import xsa.errors
......@@ -166,14 +192,7 @@ def search_for(host, root_element, constraints, search_fields, user, passw):
from tempfile import mkdtemp
tmp_dir=mkdtemp()
from pyxnat import Interface
try:
central = Interface(server=host,
user=user,
password=passw,
cachedir=tmp_dir)
except IndexError as e:
raise xsa.errors.ServerNotFoundError("Server not found, check your host-address.")
central = get_xnat_server_connection(host=host, user=user, passw=passw)
result = []
......@@ -192,11 +211,6 @@ def search_for(host, root_element, constraints, search_fields, user, passw):
if result == []:
raise xsa.errors.QueryError("Please check your query.")
try:
central.disonnect()
except AttributeError:
print "can\'t close connection (wrong pyxnat version?)"
try:
from shutil import rmtree
rmtree(tmp_dir)
......
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