queries.py 3.1 KB
Newer Older
Franziska Koehn's avatar
Franziska Koehn committed
1
2
3
from threading import Lock
requests_lock = Lock()

4
5
6
7
8
9
def get_query_methods():
    return ["AND", "OR"]

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

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

16
def download(result, host, creds, rest, dest_folder='', cb=(lambda *_: None), cb_args=()):
Franziska Koehn's avatar
Franziska Koehn committed
17
    requests_lock.acquire()
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

    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)

    download_file(url, creds, path)
Franziska Koehn's avatar
Franziska Koehn committed
34
35
    requests_lock.release()
    cb(*cb_args)
36

37
def download_file(url, creds, path):
Franziska Koehn's avatar
Franziska Koehn committed
38
39
40
41
    import requests
    from base64 import b64encode
    from requests.auth import HTTPBasicAuth

42
43
    if not path.endswith(".zip"):
        path += ".zip"
Franziska Koehn's avatar
Franziska Koehn committed
44

45
    user, passw = creds
Franziska Koehn's avatar
Franziska Koehn committed
46
47

    try:
48
        with open(path, 'wb') as handle:
Franziska Koehn's avatar
Franziska Koehn committed
49
50
51
52
53
54
55
56
            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:
57
        print "Error writing file %s, %s" % (path, e)
Franziska Koehn's avatar
Franziska Koehn committed
58
59
60
61
    except ValueError as e:
        print "Error downloading file %s, Status Code %s" % (url, e)


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

65
66
    from pyxnat.core import errors
    from httplib2 import ServerNotFoundError
67
68
69
    from httplib import ResponseNotReady

    from tempfile import mkdtemp
70
    tmp_dir=mkdtemp()
71

72
    from pyxnat import Interface
73
    try:
74
75
76
77
78
        central = Interface(server=host,
                            user=user,
                            password=passw,
                            cachedir=tmp_dir)
    except IndexError as e:
Franziska Koehn's avatar
Franziska Koehn committed
79
        raise xsa_errors.ServerNotFoundError("Server not found, check your host-address.")
80

81
82
83
84
    result = []

    try:
        result =  central.select(root_element,search_fields).where(constraints)
85
86
87
    except errors.DatabaseError as e:
        if '401' in str(e):
            raise xsa_errors.UnauthorizedError("Unauthorizied attempt. Check your User and Password")
88
    except ServerNotFoundError:
89
90
91
92
93
94
        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.")
95
96
97
98
99
100
101
102
103
104
105

    try:
        central.disonnect()
    except AttributeError:
        print "can\'t close connection (wrong pyxnat version?)"

    try:
        from shutil import rmtree
        rmtree(tmp_dir)
    except:
        pass
106

107
    return result