-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathfromtestkit.py
244 lines (222 loc) · 8.08 KB
/
fromtestkit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [https://neo4j.com]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from datetime import timedelta
import pytz
import neo4j
from neo4j import (
NotificationDisabledCategory,
NotificationMinimumSeverity,
Query,
)
from neo4j.auth_management import ClientCertificate
from neo4j.spatial import (
CartesianPoint,
WGS84Point,
)
from neo4j.time import (
Date,
DateTime,
Duration,
Time,
)
from ._preview_imports import NotificationDisabledClassification
def to_cypher_and_params(data):
from .backend import Request
params = data.get("params")
# Optional
if params is None:
return data["cypher"], None
# Transform the params to Python native
params_dict = {k: to_param(v) for k, v in params.items()}
if isinstance(params, Request):
params.mark_all_as_read()
return data["cypher"], params_dict
def to_tx_kwargs(data):
from .backend import Request
kwargs = {}
if "txMeta" in data:
metadata = data["txMeta"]
kwargs["metadata"] = metadata
if metadata is not None:
kwargs["metadata"] = {k: to_param(v) for k, v in metadata.items()}
if isinstance(metadata, Request):
metadata.mark_all_as_read()
if "timeout" in data:
kwargs["timeout"] = data["timeout"]
if kwargs["timeout"] is not None:
kwargs["timeout"] /= 1000
return kwargs
def to_query_and_params(data):
cypher, param = to_cypher_and_params(data)
tx_kwargs = to_tx_kwargs(data)
query = Query(cypher, **tx_kwargs)
return query, param
def to_param(m):
# Convert testkit parameter format to driver (python) parameter.
data = m["data"]
name = m["name"]
if name == "CypherNull":
if data["value"] is not None:
raise ValueError("CypherNull should be None")
return None
if name == "CypherString":
return str(data["value"])
if name == "CypherBool":
return bool(data["value"])
if name == "CypherInt":
return int(data["value"])
if name == "CypherFloat":
return float(data["value"])
if name == "CypherString":
return str(data["value"])
if name == "CypherBytes":
return bytearray([int(byte, 16) for byte in data["value"].split()])
if name == "CypherList":
return [to_param(v) for v in data["value"]]
if name == "CypherMap":
return {k: to_param(data["value"][k]) for k in data["value"]}
if name == "CypherPoint":
coords = [data["x"], data["y"]]
if data.get("z") is not None:
coords.append(data["z"])
if data["system"] == "cartesian":
return CartesianPoint(coords)
if data["system"] == "wgs84":
return WGS84Point(coords)
raise ValueError("Unknown point system: {}".format(data["system"]))
if name == "CypherDate":
return Date(data["year"], data["month"], data["day"])
if name == "CypherTime":
tz = None
utc_offset_s = data.get("utc_offset_s")
if utc_offset_s is not None:
utc_offset_m = utc_offset_s // 60
if utc_offset_m * 60 != utc_offset_s:
raise ValueError(
"the used timezone library only supports UTC offsets by "
"minutes"
)
tz = pytz.FixedOffset(utc_offset_m)
return Time(
data["hour"],
data["minute"],
data["second"],
data["nanosecond"],
tzinfo=tz,
)
if name == "CypherDateTime":
datetime = DateTime(
data["year"],
data["month"],
data["day"],
data["hour"],
data["minute"],
data["second"],
data["nanosecond"],
)
utc_offset_s = data["utc_offset_s"]
timezone_id = data["timezone_id"]
if timezone_id is not None:
utc_offset = timedelta(seconds=utc_offset_s)
tz = pytz.timezone(timezone_id)
try:
localized_datetime = tz.localize(datetime, is_dst=None)
if localized_datetime.utcoffset() == utc_offset:
return localized_datetime
except pytz.AmbiguousTimeError:
localized_datetime = tz.localize(datetime, is_dst=False)
if localized_datetime.utcoffset() == utc_offset:
return localized_datetime
localized_datetime = tz.localize(datetime, is_dst=True)
if localized_datetime.utcoffset() == utc_offset:
return localized_datetime
except pytz.NonExistentTimeError as e:
raise ValueError(
f"local datetime {datetime} does not exist in timezone "
f"{timezone_id}"
) from e
raise ValueError(
f"cannot localize datetime {datetime} to timezone "
f"{timezone_id} with UTC offset {utc_offset}"
)
elif utc_offset_s is not None:
utc_offset_m = utc_offset_s // 60
if utc_offset_m * 60 != utc_offset_s:
raise ValueError(
"the used timezone library only supports UTC offsets by "
"minutes"
)
tz = pytz.FixedOffset(utc_offset_m)
return tz.localize(datetime)
return datetime
if name == "CypherDuration":
return Duration(
months=data["months"],
days=data["days"],
seconds=data["seconds"],
nanoseconds=data["nanoseconds"],
)
raise ValueError("Unknown param type " + name)
def to_auth_token(data, key):
if data[key] is None:
return None
auth_token = data[key]["data"]
data[key].mark_item_as_read_if_equals("name", "AuthorizationToken")
scheme = auth_token["scheme"]
if scheme == "basic":
auth = neo4j.basic_auth(
auth_token["principal"],
auth_token["credentials"],
realm=auth_token.get("realm", None),
)
elif scheme == "kerberos":
auth = neo4j.kerberos_auth(auth_token["credentials"])
elif scheme == "bearer":
auth = neo4j.bearer_auth(auth_token["credentials"])
else:
auth = neo4j.custom_auth(
auth_token["principal"],
auth_token["credentials"],
auth_token["realm"],
auth_token["scheme"],
**auth_token.get("parameters") or {},
)
if "parameters" in auth_token:
auth_token.mark_item_as_read("parameters", recursive=True)
return auth
def to_client_cert(data, key) -> ClientCertificate | None:
if data[key] is None:
return None
data[key].mark_item_as_read_if_equals("name", "ClientCertificate")
cert_data = data[key]["data"]
return ClientCertificate(
cert_data["certfile"], cert_data["keyfile"], cert_data["password"]
)
def set_notifications_config(config, data):
if "notificationsMinSeverity" in data:
config["notifications_min_severity"] = NotificationMinimumSeverity[
data["notificationsMinSeverity"]
]
if "notificationsDisabledCategories" in data:
config["notifications_disabled_categories"] = [
NotificationDisabledCategory[c]
for c in data["notificationsDisabledCategories"]
]
if "notificationsDisabledClassifications" in data:
config["notifications_disabled_classifications"] = [
NotificationDisabledClassification[c]
for c in data["notificationsDisabledClassifications"]
]