Skip to content
queries.py 3.23 KiB
Newer Older
Franziska Koehn's avatar
Franziska Koehn committed
from threading import Lock
requests_lock = Lock()

def get_query_methods():
    return ["AND", "OR"]

def get_operators():
    return ["LIKE", ">", "<", "<=", ">=", "="]

def download_all(results, host):
        download(r, host)

def download_async(result, host, creds, rest, dest_folder='', cb=(lambda *_: None), cb_args=()):
Franziska Koehn's avatar
Franziska Koehn committed
    from threading import Thread
    download_thread = Thread(target=download, args=(result, host, creds, rest, dest_folder, cb, cb_args))
Franziska Koehn's avatar
Franziska Koehn committed
    download_thread.start()
    return download_thread
Franziska Koehn's avatar
Franziska Koehn committed

def download(result, host, creds, rest, dest_folder='', cb=(lambda *_: None), cb_args=()):
Franziska Koehn's avatar
Franziska Koehn committed
    requests_lock.acquire()

    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)
Franziska Koehn's avatar
Franziska Koehn committed
    requests_lock.release()
    cb(*cb_args)
def download_file(url, creds, path):
    import requests
    from base64 import b64encode
    from requests.auth import HTTPBasicAuth

    if not path.endswith(".zip"):
        path += ".zip"
        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)
            for block in response.iter_content(1024):
                if not block:
                    break
                handle.write(block)
    except IOError as e:
        print "Error writing file %s, %s" % (path, e)
    except ValueError as e:
        print "Error downloading file %s, Status Code %s" % (url, e)


def search_for(host, root_element, constraints, search_fields, user, passw):
    import xsa.errors as xsa_errors

    from pyxnat.core import errors
    from httplib2 import ServerNotFoundError
    from httplib import ResponseNotReady


    from tempfile import mkdtemp
    from pyxnat import Interface
        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.") #TODO Ok?
    result = []

    try:
        result =  central.select(root_element,search_fields).where(constraints)
    except errors.DatabaseError as e:
        if '401' in str(e):
            raise xsa_errors.UnauthorizedError("Unauthorizied attempt. Check your User and Password")
    except ServerNotFoundError:
        raise xsa_errors.ServerNotFoundError("Server not found, check your host-address.")
    except ResponseNotReady:
        raise xsa_errors.ResponseNotReady("Please check your Host-Address")

    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)
    except:
        pass