1
|
1 |
|
import logging |
2
|
1 |
|
import sys |
3
|
1 |
|
from .query_object import QueryObject as _QueryObject |
4
|
|
|
|
5
|
|
|
|
6
|
1 |
|
logger = logging.getLogger(__name__) |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
class CustomQueryObject(_QueryObject): |
10
|
1 |
|
def __init__(self, query, description=""): |
11
|
|
|
"""Create a new CustomQueryObject. |
12
|
|
|
|
13
|
|
|
Parameters |
14
|
|
|
----------- |
15
|
|
|
|
16
|
|
|
query : function |
17
|
|
|
Function that defines a custom query method. The query can have any |
18
|
|
|
signature, but input and output of the query needs to be JSON |
19
|
|
|
serializable. |
20
|
|
|
|
21
|
|
|
description : str |
22
|
|
|
The description of the custom query object |
23
|
|
|
|
24
|
|
|
""" |
25
|
1 |
|
super().__init__(description) |
26
|
|
|
|
27
|
1 |
|
self.custom_query = query |
28
|
|
|
|
29
|
1 |
|
def query(self, *args, **kwargs): |
30
|
|
|
"""Query the custom defined query method using the given input. |
31
|
|
|
|
32
|
|
|
Parameters |
33
|
|
|
---------- |
34
|
|
|
args : list |
35
|
|
|
positional arguments to the query |
36
|
|
|
|
37
|
|
|
kwargs : dict |
38
|
|
|
keyword arguments to the query |
39
|
|
|
|
40
|
|
|
Returns |
41
|
|
|
------- |
42
|
|
|
out: object. |
43
|
|
|
The results depends on the implementation of the query method. |
44
|
|
|
Typically the return value will be whatever that function returns. |
45
|
|
|
|
46
|
|
|
See Also |
47
|
|
|
-------- |
48
|
|
|
QueryObject |
49
|
|
|
""" |
50
|
|
|
# include the dependent files in sys path so that the query can run |
51
|
|
|
# correctly |
52
|
|
|
|
53
|
|
|
try: |
54
|
|
|
logger.debug( |
55
|
|
|
"Running custom query with arguments " f"({args}, {kwargs})..." |
56
|
|
|
) |
57
|
|
|
ret = self.custom_query(*args, **kwargs) |
58
|
|
|
except Exception as e: |
59
|
|
|
logger.exception( |
60
|
|
|
"Exception hit when running custom query, error: " f"{str(e)}" |
61
|
|
|
) |
62
|
|
|
raise |
63
|
|
|
|
64
|
|
|
logger.debug(f"Received response {ret}") |
65
|
|
|
try: |
66
|
|
|
return self._make_serializable(ret) |
67
|
|
|
except Exception as e: |
68
|
|
|
logger.exception( |
69
|
|
|
"Cannot properly serialize custom query result, " f"error: {str(e)}" |
70
|
|
|
) |
71
|
|
|
raise |
72
|
|
|
|
73
|
1 |
|
def get_doc_string(self): |
74
|
|
|
"""Get doc string from customized query""" |
75
|
1 |
|
default_docstring = "-- no docstring found in query function --" |
76
|
|
|
|
77
|
|
|
# TODO: fix docstring parsing on Windows systems |
78
|
|
|
# if sys.platform == 'win32': |
79
|
|
|
# return default_docstring |
80
|
1 |
|
try: |
81
|
1 |
|
ds = getattr(self.custom_query, '__doc__', None) |
82
|
1 |
|
return ds if ds and isinstance(ds, str) else default_docstring |
83
|
|
|
except Exception as e: |
84
|
|
|
print(e) |
85
|
|
|
return default_docstring |
86
|
|
|
|
87
|
1 |
|
def get_methods(self): |
88
|
1 |
|
return [self.get_query_method()] |
89
|
|
|
|
90
|
1 |
|
def get_query_method(self): |
91
|
|
|
return {"method": "query"} |
92
|
|
|
|