deposit-python 2.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- deposit/__init__.py +15 -0
- deposit/_batch.py +307 -0
- deposit/_exchange.py +524 -0
- deposit/_exchange_transport.py +411 -0
- deposit/_gateway.py +612 -0
- deposit/_meta.py +2 -0
- deposit/_paging.py +645 -0
- deposit/_procedure_manifest.json +3799 -0
- deposit/_resource_buffer.py +211 -0
- deposit/_resources.py +187 -0
- deposit/_version.py +24 -0
- deposit/client.py +535 -0
- deposit/entities.py +351 -0
- deposit/errors.py +61 -0
- deposit/exchange.py +5 -0
- deposit/networkx.py +325 -0
- deposit/store.py +2142 -0
- deposit/values.py +510 -0
- deposit_python-2.0.1.dist-info/METADATA +81 -0
- deposit_python-2.0.1.dist-info/RECORD +23 -0
- deposit_python-2.0.1.dist-info/WHEEL +5 -0
- deposit_python-2.0.1.dist-info/licenses/LICENSE +21 -0
- deposit_python-2.0.1.dist-info/top_level.txt +1 -0
deposit/values.py
ADDED
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
"""Lossless public Deposit value representations and declaration-aware codecs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from datetime import date, datetime, time
|
|
7
|
+
import math
|
|
8
|
+
import os
|
|
9
|
+
from typing import Any, Mapping
|
|
10
|
+
|
|
11
|
+
from neo4j.spatial import CartesianPoint, Point, WGS84Point
|
|
12
|
+
from neo4j.time import Date as NeoDate, DateTime as NeoDateTime, Duration, Time as NeoTime
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
INT64_MIN = -(2**63)
|
|
16
|
+
INT64_MAX = 2**63 - 1
|
|
17
|
+
_TYPES = {
|
|
18
|
+
"String", "Integer", "Float", "Boolean", "Date", "Time", "LocalTime",
|
|
19
|
+
"DateTime", "LocalDateTime", "Duration", "Point", "List", "DDict", "DGeometry", "DResource",
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True, slots=True)
|
|
24
|
+
class TypedValue:
|
|
25
|
+
"""Explicitly typed detached input; it does not imply a schema write."""
|
|
26
|
+
|
|
27
|
+
type: str
|
|
28
|
+
value: Any
|
|
29
|
+
|
|
30
|
+
def to_dict(self, *, format: str = "procedure") -> dict[str, Any]:
|
|
31
|
+
if format not in {"procedure", "exchange"}:
|
|
32
|
+
raise ValueError("format must be 'procedure' or 'exchange'")
|
|
33
|
+
encoded = encode_value(self.type, self.value)
|
|
34
|
+
if format == "exchange": encoded = _encode_exchange(self.type, encoded)
|
|
35
|
+
return {"type": self.type, "value": encoded}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def typed(type_name: str, value: Any) -> TypedValue:
|
|
39
|
+
if type_name not in _TYPES:
|
|
40
|
+
raise TypeError(f"Unsupported Deposit type {type_name!r}")
|
|
41
|
+
# Validate before any Store request.
|
|
42
|
+
encode_value(type_name, value)
|
|
43
|
+
return TypedValue(type_name, value)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class DType:
|
|
47
|
+
"""Base class for detached typed values."""
|
|
48
|
+
|
|
49
|
+
def __init__(self, *, from_dict: Mapping[str, Any] | None = None):
|
|
50
|
+
if type(self) is DType:
|
|
51
|
+
raise TypeError("DType is abstract")
|
|
52
|
+
# Public Deposit type names intentionally retain the D prefix for
|
|
53
|
+
# structured values (DDict, DGeometry, DResource).
|
|
54
|
+
self._type = type(self).__name__
|
|
55
|
+
if from_dict is None:
|
|
56
|
+
self._value = None
|
|
57
|
+
else:
|
|
58
|
+
if not isinstance(from_dict, Mapping) or set(from_dict) != {"type", "value"}:
|
|
59
|
+
raise TypeError("from_dict must contain exactly type and value")
|
|
60
|
+
if from_dict["type"] != self._type:
|
|
61
|
+
raise TypeError("typed value does not match this DType")
|
|
62
|
+
if self._type in {"DGeometry", "DResource"}:
|
|
63
|
+
if not isinstance(from_dict["value"], Mapping):
|
|
64
|
+
raise TypeError(f"invalid {self._type} payload")
|
|
65
|
+
self._value = dict(from_dict["value"])
|
|
66
|
+
else:
|
|
67
|
+
self._value = decode_value(self._type, from_dict["value"])
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def value(self) -> Any:
|
|
71
|
+
return self._value
|
|
72
|
+
|
|
73
|
+
def to_dict(self, *, format: str = "procedure") -> dict[str, Any]:
|
|
74
|
+
if format not in {"procedure", "exchange"}:
|
|
75
|
+
raise ValueError("format must be 'procedure' or 'exchange'")
|
|
76
|
+
encoded = encode_value(self._type, self._value)
|
|
77
|
+
if format == "exchange":
|
|
78
|
+
encoded = _encode_exchange(self._type, encoded)
|
|
79
|
+
return {"type": self._type, "value": encoded}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class DDict(DType):
|
|
83
|
+
def __init__(self, data: Mapping[str, Any] | None = None, *, from_dict: Mapping[str, Any] | None = None):
|
|
84
|
+
if data is not None and from_dict is not None:
|
|
85
|
+
raise TypeError("supply data or from_dict, not both")
|
|
86
|
+
super().__init__(from_dict=from_dict)
|
|
87
|
+
if data is not None:
|
|
88
|
+
self._value = dict(data)
|
|
89
|
+
encode_value("DDict", self._value)
|
|
90
|
+
|
|
91
|
+
def __getitem__(self, key: str) -> Any:
|
|
92
|
+
return self._value[key]
|
|
93
|
+
|
|
94
|
+
def __setitem__(self, key: str, value: Any) -> None:
|
|
95
|
+
if not isinstance(key, str):
|
|
96
|
+
raise TypeError("DDict keys must be strings")
|
|
97
|
+
candidate = dict(self._value)
|
|
98
|
+
candidate[key] = value
|
|
99
|
+
encode_value("DDict", candidate)
|
|
100
|
+
self._value = candidate
|
|
101
|
+
|
|
102
|
+
def __delitem__(self, key: str) -> None:
|
|
103
|
+
del self._value[key]
|
|
104
|
+
|
|
105
|
+
def __iter__(self):
|
|
106
|
+
return iter(self._value)
|
|
107
|
+
|
|
108
|
+
def __len__(self) -> int:
|
|
109
|
+
return len(self._value)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class DGeometry(DType):
|
|
113
|
+
"""Detached geometry metadata. Geometry conversion remains server-owned."""
|
|
114
|
+
|
|
115
|
+
def __init__(self, *, wkt=None, coordinates=None, geometry_type=None, srid=None,
|
|
116
|
+
vertical_srid=None, axis_order="xy", dimensions=2, from_dict=None):
|
|
117
|
+
if from_dict is not None and any(v is not None for v in (wkt, coordinates, geometry_type, srid, vertical_srid)):
|
|
118
|
+
raise TypeError("from_dict cannot be combined with geometry fields")
|
|
119
|
+
super().__init__(from_dict=from_dict)
|
|
120
|
+
self._geometry_id = None
|
|
121
|
+
if from_dict is None:
|
|
122
|
+
supplied = sum(v is not None for v in (wkt, coordinates, from_dict))
|
|
123
|
+
if supplied != 1:
|
|
124
|
+
raise TypeError("supply exactly one of wkt, coordinates, or from_dict")
|
|
125
|
+
if coordinates is not None and not geometry_type:
|
|
126
|
+
raise TypeError("coordinates require geometry_type")
|
|
127
|
+
self._value = {"wkt": wkt, "coordinates": coordinates, "geometryType": geometry_type,
|
|
128
|
+
"srid": srid, "verticalSrid": vertical_srid, "axisOrder": axis_order,
|
|
129
|
+
"dimensions": dimensions}
|
|
130
|
+
encode_value("DGeometry", self._value)
|
|
131
|
+
if isinstance(self._value, Mapping):
|
|
132
|
+
self._geometry_id = self._value.get("geometryId")
|
|
133
|
+
|
|
134
|
+
@property
|
|
135
|
+
def geometry_id(self):
|
|
136
|
+
return self._geometry_id
|
|
137
|
+
|
|
138
|
+
def __getattr__(self, name):
|
|
139
|
+
aliases = {"geometry_type": "geometryType", "vertical_srid": "verticalSrid",
|
|
140
|
+
"axis_order": "axisOrder", "dimensions": "dimensions"}
|
|
141
|
+
if name in {"wkt", "coordinates", "srid", "geometry_type", "geometryType",
|
|
142
|
+
"vertical_srid", "verticalSrid", "axis_order", "axisOrder", "dimensions"}:
|
|
143
|
+
key = aliases.get(name, name)
|
|
144
|
+
if key in {"wkt", "coordinates", "geometryType"} and self._value.get(key) is None:
|
|
145
|
+
raise ValueError(f"Geometry {key} is unavailable until resolve(store)")
|
|
146
|
+
return self._value.get(key)
|
|
147
|
+
raise AttributeError(name)
|
|
148
|
+
|
|
149
|
+
def __setattr__(self, name, value):
|
|
150
|
+
aliases = {"geometry_type": "geometryType", "vertical_srid": "verticalSrid",
|
|
151
|
+
"axis_order": "axisOrder", "dimensions": "dimensions"}
|
|
152
|
+
prop = aliases.get(name, name)
|
|
153
|
+
if name in {"geometry_id", "geometry_type", "geometryType"} or prop == "geometryId":
|
|
154
|
+
raise AttributeError(f"{name} is read-only")
|
|
155
|
+
if prop in {"wkt", "coordinates", "srid", "verticalSrid", "axisOrder", "dimensions"} and hasattr(self, "_value"):
|
|
156
|
+
candidate = dict(self._value)
|
|
157
|
+
if prop == "wkt":
|
|
158
|
+
candidate["wkt"] = value
|
|
159
|
+
candidate["coordinates"] = None
|
|
160
|
+
candidate["geometryType"] = None
|
|
161
|
+
elif prop == "coordinates":
|
|
162
|
+
if not candidate.get("geometryType"):
|
|
163
|
+
raise ValueError("coordinates require a geometry_type")
|
|
164
|
+
candidate["coordinates"] = value
|
|
165
|
+
candidate["wkt"] = None
|
|
166
|
+
else:
|
|
167
|
+
candidate[prop] = value
|
|
168
|
+
encode_value("DGeometry", candidate)
|
|
169
|
+
self._value = candidate
|
|
170
|
+
return
|
|
171
|
+
object.__setattr__(self, name, value)
|
|
172
|
+
|
|
173
|
+
def resolve(self, store):
|
|
174
|
+
if self._value.get("wkt") is None and self._value.get("coordinates") is None:
|
|
175
|
+
raise ValueError("Geometry has neither WKT nor coordinates")
|
|
176
|
+
# The Server conversion procedures accept only these spatial reference
|
|
177
|
+
# options. Axis order and dimensions are supplied by the authoritative
|
|
178
|
+
# conversion result when available, otherwise the caller metadata is
|
|
179
|
+
# retained locally for the subsequent descriptor write.
|
|
180
|
+
options = {"srid": self._value.get("srid"), "verticalSrid": self._value.get("verticalSrid")}
|
|
181
|
+
if self._value.get("wkt") is None:
|
|
182
|
+
result = store._client.coordinates_to_wkt(store._token, self._value["geometryType"],
|
|
183
|
+
self._value["coordinates"], options)
|
|
184
|
+
if not isinstance(result, Mapping) or not isinstance(result.get("wkt"), str):
|
|
185
|
+
raise TypeError("Server returned no geometry WKT")
|
|
186
|
+
self._merge_server_metadata(result)
|
|
187
|
+
self._value["wkt"] = result["wkt"]
|
|
188
|
+
else:
|
|
189
|
+
result = store._client.wkt_to_coordinates(store._token, self._value["wkt"], options)
|
|
190
|
+
if not isinstance(result, Mapping) or "coordinates" not in result:
|
|
191
|
+
raise TypeError("Server returned no geometry coordinates")
|
|
192
|
+
self._merge_server_metadata(result)
|
|
193
|
+
self._value["coordinates"] = result["coordinates"]
|
|
194
|
+
return self
|
|
195
|
+
|
|
196
|
+
def _merge_server_metadata(self, result: Mapping[str, Any]) -> None:
|
|
197
|
+
"""Retain authoritative conversion metadata without guessing from WKT."""
|
|
198
|
+
identity = result.get("geometryId")
|
|
199
|
+
if identity is not None and self._geometry_id is not None and identity != self._geometry_id:
|
|
200
|
+
raise ValueError("Geometry conversion changed geometry identity")
|
|
201
|
+
for key in ("geometryType", "srid", "verticalSrid", "axisOrder", "dimensions"):
|
|
202
|
+
if key in result:
|
|
203
|
+
self._value[key] = result[key]
|
|
204
|
+
if identity is not None:
|
|
205
|
+
self._geometry_id = identity
|
|
206
|
+
self._value["geometryId"] = identity
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
class DResource(DType):
|
|
210
|
+
"""Detached resource reference or a local source awaiting upload."""
|
|
211
|
+
|
|
212
|
+
def __init__(self, *, path=None, stream=None, filename=None, from_dict: Mapping[str, Any] | None = None):
|
|
213
|
+
if from_dict is not None and any(value is not None for value in (path, stream, filename)):
|
|
214
|
+
raise TypeError("from_dict cannot be combined with a local resource source")
|
|
215
|
+
if path is not None and stream is not None:
|
|
216
|
+
raise TypeError("supply path or stream, not both")
|
|
217
|
+
super().__init__(from_dict=from_dict)
|
|
218
|
+
self._path = None
|
|
219
|
+
self._stream = None
|
|
220
|
+
if from_dict is None:
|
|
221
|
+
if path is None and stream is None:
|
|
222
|
+
self._value = {}
|
|
223
|
+
return
|
|
224
|
+
if path is not None:
|
|
225
|
+
path = os.fspath(path)
|
|
226
|
+
if not isinstance(path, str):
|
|
227
|
+
raise TypeError("resource path must resolve to a string path")
|
|
228
|
+
self._path = path
|
|
229
|
+
inferred = os.path.basename(path)
|
|
230
|
+
else:
|
|
231
|
+
if not callable(getattr(stream, "read", None)):
|
|
232
|
+
raise TypeError("resource stream must be readable")
|
|
233
|
+
self._stream = stream
|
|
234
|
+
inferred = None
|
|
235
|
+
selected = filename if filename is not None else inferred
|
|
236
|
+
if selected is not None and (not isinstance(selected, str) or not selected):
|
|
237
|
+
raise ValueError("resource filename must be a nonempty string")
|
|
238
|
+
self._value = {"filename": selected}
|
|
239
|
+
|
|
240
|
+
@property
|
|
241
|
+
def filename(self):
|
|
242
|
+
return self._value.get("filename")
|
|
243
|
+
|
|
244
|
+
@filename.setter
|
|
245
|
+
def filename(self, value):
|
|
246
|
+
if not isinstance(value, str) or not value:
|
|
247
|
+
raise ValueError("resource filename must be a nonempty string")
|
|
248
|
+
self._value["filename"] = value
|
|
249
|
+
|
|
250
|
+
@property
|
|
251
|
+
def resource_id(self):
|
|
252
|
+
return self._value.get("resourceId")
|
|
253
|
+
|
|
254
|
+
@property
|
|
255
|
+
def is_image(self):
|
|
256
|
+
value = self._value.get("isImage")
|
|
257
|
+
return value if type(value) is bool else None
|
|
258
|
+
|
|
259
|
+
def open(self, store):
|
|
260
|
+
"""Open a checked, closable chunked reader for a stored resource."""
|
|
261
|
+
return store._open_resource(self)
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def encode_value(type_name: str, value: Any, *, item_type: str | None = None) -> Any:
|
|
265
|
+
"""Encode one public value as the server's declared-type payload."""
|
|
266
|
+
if type_name not in _TYPES:
|
|
267
|
+
raise TypeError(f"Unsupported Deposit type {type_name!r}")
|
|
268
|
+
if value is None:
|
|
269
|
+
return None
|
|
270
|
+
if type_name == "String":
|
|
271
|
+
if not isinstance(value, str): raise TypeError("String requires str")
|
|
272
|
+
return value
|
|
273
|
+
if type_name == "Boolean":
|
|
274
|
+
if type(value) is not bool: raise TypeError("Boolean requires bool")
|
|
275
|
+
return value
|
|
276
|
+
if type_name == "Integer":
|
|
277
|
+
if type(value) is not int or not INT64_MIN <= value <= INT64_MAX:
|
|
278
|
+
raise TypeError("Integer requires a signed 64-bit int")
|
|
279
|
+
return value
|
|
280
|
+
if type_name == "Float":
|
|
281
|
+
if type(value) not in (int, float): raise TypeError("Float requires int or float")
|
|
282
|
+
converted = float(value)
|
|
283
|
+
if not math.isfinite(converted): raise TypeError("Float must be finite")
|
|
284
|
+
if type(value) is int and int(converted) != value:
|
|
285
|
+
raise TypeError("Integer cannot be represented exactly as Float")
|
|
286
|
+
return converted
|
|
287
|
+
if type_name in {"Date", "Time", "LocalTime", "DateTime", "LocalDateTime"}:
|
|
288
|
+
expected = {"Date": (date, NeoDate), "Time": (time, NeoTime), "LocalTime": (time, NeoTime),
|
|
289
|
+
"DateTime": (datetime, NeoDateTime), "LocalDateTime": (datetime, NeoDateTime)}[type_name]
|
|
290
|
+
if not isinstance(value, expected) or (type_name == "Date" and isinstance(value, datetime)) or (
|
|
291
|
+
type_name in {"DateTime", "Time"} and not _has_zone(value)
|
|
292
|
+
) or (
|
|
293
|
+
type_name in {"LocalDateTime", "LocalTime"} and _has_zone(value)
|
|
294
|
+
):
|
|
295
|
+
raise TypeError(f"{type_name} requires a compatible temporal value")
|
|
296
|
+
return value.iso_format() if hasattr(value, "iso_format") else value.isoformat()
|
|
297
|
+
if type_name == "Duration":
|
|
298
|
+
if not isinstance(value, Duration): raise TypeError("Duration requires neo4j.time.Duration")
|
|
299
|
+
return {"months": int(value.months), "days": int(value.days), "seconds": int(value.seconds), "nanoseconds": int(value.nanoseconds)}
|
|
300
|
+
if type_name == "Point":
|
|
301
|
+
if not isinstance(value, Point): raise TypeError("Point requires neo4j.spatial.Point")
|
|
302
|
+
coordinates = [float(x) for x in value]
|
|
303
|
+
if not all(math.isfinite(x) for x in coordinates): raise TypeError("Point coordinates must be finite")
|
|
304
|
+
srid = getattr(value, "srid", None)
|
|
305
|
+
if type(srid) is not int:
|
|
306
|
+
raise TypeError("Point must retain an explicit SRID")
|
|
307
|
+
return {"srid": srid, "coordinates": coordinates}
|
|
308
|
+
if type_name == "List":
|
|
309
|
+
if not isinstance(value, (list, tuple)): raise TypeError("List requires list or tuple")
|
|
310
|
+
if item_type is None:
|
|
311
|
+
explicit = value.item_type if isinstance(value, TypedList) else None
|
|
312
|
+
if explicit is None:
|
|
313
|
+
types = {_infer_type(x) for x in value if x is not None}
|
|
314
|
+
if len(types) != 1:
|
|
315
|
+
raise TypeError("List requires an explicit item type when empty, mixed, or all-null")
|
|
316
|
+
explicit = types.pop()
|
|
317
|
+
item_type = explicit
|
|
318
|
+
if item_type not in _TYPES - {"DDict", "DGeometry", "DResource"}:
|
|
319
|
+
raise TypeError("List item type is unsupported")
|
|
320
|
+
encoded = []
|
|
321
|
+
for item in value:
|
|
322
|
+
if isinstance(item, (DGeometry, DResource)):
|
|
323
|
+
raise TypeError("Geometry and Resource values cannot be nested in List")
|
|
324
|
+
if isinstance(item, TypedValue):
|
|
325
|
+
if item.type != item_type: raise TypeError("typed list item has the wrong type")
|
|
326
|
+
item = item.value
|
|
327
|
+
encoded.append(encode_value(item_type, item))
|
|
328
|
+
return {"itemType": item_type, "items": encoded}
|
|
329
|
+
if type_name == "DDict":
|
|
330
|
+
if isinstance(value, DDict): value = value._value
|
|
331
|
+
if not isinstance(value, Mapping) or any(not isinstance(k, str) for k in value):
|
|
332
|
+
raise TypeError("DDict requires a string-keyed mapping")
|
|
333
|
+
return {k: _encode_ddict_entry(v) for k, v in value.items()}
|
|
334
|
+
if type_name == "DGeometry":
|
|
335
|
+
if isinstance(value, DGeometry): value = value._value
|
|
336
|
+
if not isinstance(value, Mapping): raise TypeError("DGeometry requires a geometry mapping")
|
|
337
|
+
return _encode_structure(value)
|
|
338
|
+
if type_name == "DResource":
|
|
339
|
+
if isinstance(value, DResource): value = value._value
|
|
340
|
+
if not isinstance(value, Mapping): raise TypeError("DResource requires a resource mapping")
|
|
341
|
+
return _encode_structure(value)
|
|
342
|
+
raise TypeError(f"Unsupported Deposit type {type_name!r}")
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
class TypedList(list):
|
|
346
|
+
def __init__(self, values=(), *, item_type: str):
|
|
347
|
+
if item_type not in _TYPES: raise ValueError("unknown List item type")
|
|
348
|
+
super().__init__(values)
|
|
349
|
+
self.item_type = item_type
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def decode_value(type_name: str, value: Any) -> Any:
|
|
353
|
+
if value is None: return None
|
|
354
|
+
if type_name == "String":
|
|
355
|
+
if not isinstance(value, str): raise TypeError("invalid String payload")
|
|
356
|
+
return value
|
|
357
|
+
if type_name == "Boolean":
|
|
358
|
+
if type(value) is not bool: raise TypeError("invalid Boolean payload")
|
|
359
|
+
return value
|
|
360
|
+
if type_name == "Integer":
|
|
361
|
+
if type(value) is not int or not INT64_MIN <= value <= INT64_MAX: raise TypeError("invalid Integer payload")
|
|
362
|
+
return value
|
|
363
|
+
if type_name == "Float":
|
|
364
|
+
if type(value) not in (int, float) or not math.isfinite(value): raise TypeError("invalid Float payload")
|
|
365
|
+
return float(value)
|
|
366
|
+
if type_name == "List":
|
|
367
|
+
if not isinstance(value, Mapping) or set(value) != {"itemType", "items"}: raise TypeError("invalid List payload")
|
|
368
|
+
kind, items = value["itemType"], value["items"]
|
|
369
|
+
if kind not in _TYPES - {"DGeometry", "DResource"} or not isinstance(items, list): raise TypeError("invalid List items")
|
|
370
|
+
return TypedList((decode_value(kind, x) for x in items), item_type=kind)
|
|
371
|
+
if type_name == "DDict":
|
|
372
|
+
if not isinstance(value, Mapping) or any(not isinstance(k, str) for k in value):
|
|
373
|
+
raise TypeError("invalid DDict payload")
|
|
374
|
+
decoded = {}
|
|
375
|
+
for key, item in value.items():
|
|
376
|
+
if not isinstance(item, Mapping) or set(item) != {"type", "value"} or item["type"] not in _TYPES:
|
|
377
|
+
raise TypeError("invalid typed DDict entry")
|
|
378
|
+
if item["type"] in {"DGeometry", "DResource"}:
|
|
379
|
+
raise TypeError("Geometry and Resource values cannot be nested in DDict")
|
|
380
|
+
# A bare None loses the declaration needed to faithfully encode a
|
|
381
|
+
# nullable DDict entry again. Keep nulls explicitly typed.
|
|
382
|
+
decoded[key] = (typed(item["type"], None) if item["value"] is None
|
|
383
|
+
else decode_value(item["type"], item["value"]))
|
|
384
|
+
return DDict(decoded)
|
|
385
|
+
if type_name in {"Date", "Time", "LocalTime", "DateTime", "LocalDateTime"}:
|
|
386
|
+
if not isinstance(value, str): raise TypeError(f"invalid {type_name} payload")
|
|
387
|
+
if type_name == "Date": return NeoDate.from_iso_format(value)
|
|
388
|
+
if type_name in {"Time", "DateTime"} and value.endswith("Z"):
|
|
389
|
+
value = value[:-1] + "+00:00"
|
|
390
|
+
if type_name in {"Time", "LocalTime"}: return NeoTime.from_iso_format(value)
|
|
391
|
+
parsed = NeoDateTime.from_iso_format(value)
|
|
392
|
+
if type_name == "DateTime" and not _has_zone(parsed): raise TypeError("invalid DateTime offset")
|
|
393
|
+
if type_name == "LocalDateTime" and _has_zone(parsed): raise TypeError("invalid LocalDateTime offset")
|
|
394
|
+
return parsed
|
|
395
|
+
if type_name == "Duration":
|
|
396
|
+
if not isinstance(value, Mapping) or set(value) != {"months", "days", "seconds", "nanoseconds"}:
|
|
397
|
+
raise TypeError("invalid Duration payload")
|
|
398
|
+
if any(type(value[k]) is not int for k in value): raise TypeError("invalid Duration components")
|
|
399
|
+
return Duration(months=value["months"], days=value["days"], seconds=value["seconds"], nanoseconds=value["nanoseconds"])
|
|
400
|
+
if type_name == "Point":
|
|
401
|
+
if not isinstance(value, Mapping) or set(value) != {"srid", "coordinates"}:
|
|
402
|
+
raise TypeError("invalid Point payload")
|
|
403
|
+
srid, coordinates = value["srid"], value["coordinates"]
|
|
404
|
+
if type(srid) is not int or not isinstance(coordinates, list) or any(type(c) not in {int, float} or not math.isfinite(c) for c in coordinates):
|
|
405
|
+
raise TypeError("invalid Point coordinates")
|
|
406
|
+
point_type = {4326: WGS84Point, 4979: WGS84Point, 7203: CartesianPoint, 9157: CartesianPoint}.get(srid)
|
|
407
|
+
if point_type is None: raise TypeError(f"Unsupported Neo4j Point SRID {srid}")
|
|
408
|
+
point = point_type(coordinates)
|
|
409
|
+
if point.srid != srid: raise TypeError(f"Point SRID {srid} does not match supported dimension")
|
|
410
|
+
return point
|
|
411
|
+
if type_name == "DGeometry":
|
|
412
|
+
if not isinstance(value, Mapping): raise TypeError("invalid DGeometry payload")
|
|
413
|
+
return DGeometry(from_dict={"type": "DGeometry", "value": dict(value)})
|
|
414
|
+
if type_name == "DResource":
|
|
415
|
+
if not isinstance(value, Mapping): raise TypeError("invalid DResource payload")
|
|
416
|
+
return DResource(from_dict={"type": "DResource", "value": dict(value)})
|
|
417
|
+
return value
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
def encode_value_any(value: Any, *, collection=False) -> Any:
|
|
421
|
+
if isinstance(value, TypedValue):
|
|
422
|
+
if collection and value.type in {"DGeometry", "DResource"}: raise TypeError("Geometry and Resource cannot be nested in collections")
|
|
423
|
+
return value.to_dict()
|
|
424
|
+
if isinstance(value, DGeometry):
|
|
425
|
+
if collection: raise TypeError("Geometry cannot be nested in collections")
|
|
426
|
+
return encode_value("DGeometry", value)
|
|
427
|
+
if isinstance(value, DResource):
|
|
428
|
+
if collection: raise TypeError("Resource cannot be nested in collections")
|
|
429
|
+
return encode_value("DResource", value)
|
|
430
|
+
if isinstance(value, Mapping):
|
|
431
|
+
if any(not isinstance(key, str) for key in value): raise TypeError("map keys must be strings")
|
|
432
|
+
return {k: encode_value_any(v, collection=True) for k, v in value.items()}
|
|
433
|
+
if isinstance(value, (list, tuple)):
|
|
434
|
+
return encode_value("List", value)
|
|
435
|
+
kind = _infer_type(value)
|
|
436
|
+
return encode_value(kind, value)
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
def _infer_type(value: Any) -> str:
|
|
440
|
+
if value is None: raise TypeError("Null requires an explicit declared or typed type")
|
|
441
|
+
if type(value) is bool: return "Boolean"
|
|
442
|
+
if type(value) is int: return "Integer"
|
|
443
|
+
if type(value) is float: return "Float"
|
|
444
|
+
if isinstance(value, str): return "String"
|
|
445
|
+
if isinstance(value, (datetime, NeoDateTime)): return "DateTime" if _has_zone(value) else "LocalDateTime"
|
|
446
|
+
if isinstance(value, (time, NeoTime)): return "Time" if _has_zone(value) else "LocalTime"
|
|
447
|
+
if isinstance(value, (date, NeoDate)): return "Date"
|
|
448
|
+
if isinstance(value, Duration): return "Duration"
|
|
449
|
+
if isinstance(value, Point): return "Point"
|
|
450
|
+
if isinstance(value, DDict): return "DDict"
|
|
451
|
+
if isinstance(value, DGeometry): return "DGeometry"
|
|
452
|
+
if isinstance(value, DResource): return "DResource"
|
|
453
|
+
if isinstance(value, Mapping): return "DDict"
|
|
454
|
+
if isinstance(value, (list, tuple)): return "List"
|
|
455
|
+
raise TypeError(f"Unsupported Deposit value type {type(value).__name__}")
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def _encode_structure(value: Any) -> Any:
|
|
459
|
+
"""Validate explicit geometry/resource structure without guessing null types."""
|
|
460
|
+
if value is None or type(value) in {bool, int, str}:
|
|
461
|
+
return value
|
|
462
|
+
if type(value) is float:
|
|
463
|
+
if not math.isfinite(value): raise TypeError("structured values must be finite")
|
|
464
|
+
return value
|
|
465
|
+
if isinstance(value, TypedValue):
|
|
466
|
+
return value.to_dict()
|
|
467
|
+
if isinstance(value, (DGeometry, DResource)):
|
|
468
|
+
raise TypeError("geometry and resource values cannot be nested in collections")
|
|
469
|
+
if isinstance(value, Mapping):
|
|
470
|
+
if any(not isinstance(k, str) for k in value): raise TypeError("map keys must be strings")
|
|
471
|
+
return {k: _encode_structure(v) for k, v in value.items()}
|
|
472
|
+
if isinstance(value, (list, tuple)):
|
|
473
|
+
return [_encode_structure(v) for v in value]
|
|
474
|
+
raise TypeError(f"Unsupported structured value {type(value).__name__}")
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
def _encode_ddict_entry(value: Any) -> dict[str, Any]:
|
|
478
|
+
if isinstance(value, TypedValue):
|
|
479
|
+
if value.type in {"DGeometry", "DResource"}:
|
|
480
|
+
raise TypeError("Geometry and Resource values cannot be nested in DDict")
|
|
481
|
+
return value.to_dict()
|
|
482
|
+
if isinstance(value, DType):
|
|
483
|
+
if value._type in {"DGeometry", "DResource"}:
|
|
484
|
+
raise TypeError("Geometry and Resource values cannot be nested in DDict")
|
|
485
|
+
return value.to_dict()
|
|
486
|
+
kind = _infer_type(value)
|
|
487
|
+
if kind in {"DGeometry", "DResource"}:
|
|
488
|
+
raise TypeError("Geometry and Resource values cannot be nested in DDict")
|
|
489
|
+
return {"type": kind, "value": encode_value(kind, value)}
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
def _has_zone(value: Any) -> bool:
|
|
493
|
+
if getattr(value, "tzinfo", None) is None:
|
|
494
|
+
return False
|
|
495
|
+
try: return value.utcoffset() is not None
|
|
496
|
+
except Exception: return False
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
def _encode_exchange(type_name: str, payload: Any) -> Any:
|
|
500
|
+
"""Convert exact integers to the JSON-package decimal-string form."""
|
|
501
|
+
if payload is None: return None
|
|
502
|
+
if type_name == "Integer": return str(payload)
|
|
503
|
+
if type_name == "List":
|
|
504
|
+
item_type = payload["itemType"]
|
|
505
|
+
return {"itemType": item_type, "items": [_encode_exchange(item_type, item) for item in payload["items"]]}
|
|
506
|
+
if type_name == "DDict":
|
|
507
|
+
return {k: ({"type": v["type"], "value": _encode_exchange(v["type"], v["value"])}
|
|
508
|
+
if isinstance(v, Mapping) and set(v) == {"type", "value"} else v)
|
|
509
|
+
for k, v in payload.items()}
|
|
510
|
+
return payload
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: deposit-python
|
|
3
|
+
Version: 2.0.1
|
|
4
|
+
Summary: Python driver for Deposit - Graph database for scientific data collection
|
|
5
|
+
Author-email: Peter Demján <peter.demjan@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: homepage, https://github.com/demjanp/deposit-python
|
|
8
|
+
Project-URL: Documentation, https://github.com/demjanp/deposit-python/blob/main/doc/quickstart.md
|
|
9
|
+
Project-URL: Issues, https://github.com/demjanp/deposit-python/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/demjanp/deposit-python/releases
|
|
11
|
+
Keywords: database,graph
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Topic :: Database
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Operating System :: Microsoft :: Windows :: Windows 10
|
|
16
|
+
Requires-Python: <3.15,>=3.14
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: neo4j<7,>=6.1
|
|
20
|
+
Requires-Dist: networkx<4,>=3.4
|
|
21
|
+
Provides-Extra: test
|
|
22
|
+
Requires-Dist: pytest<9,>=8.3; extra == "test"
|
|
23
|
+
Requires-Dist: build<2,>=1.2; extra == "test"
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# deposit-python
|
|
27
|
+
|
|
28
|
+
**deposit-python** is the synchronous Python wrapper for **Deposit**. The Python distribution is named `deposit-python` and the import is `deposit`. It gives Python applications access to the scientific records, relationships, and attached files managed by [deposit-server](https://github.com/demjanp/deposit-server).
|
|
29
|
+
|
|
30
|
+
## Features
|
|
31
|
+
|
|
32
|
+
- **Connections and sessions.** `Client` uses a Neo4j driver to discover the server and open independently authenticated `Store` sessions. Each Store works in one Namespace.
|
|
33
|
+
- **Python records and values.** `DClass` and `DObject` represent Classes and Objects. `DGeometry` and `DResource` represent spatial values and attached files.
|
|
34
|
+
- **Relationships.** Directed, labeled links connect Classes to Classes and Objects to Objects.
|
|
35
|
+
- **Queries and edits.** Run Deposit Query Language queries, page through large results, and group related edits into atomic batches. Revision checks identify conflicting changes.
|
|
36
|
+
- **Import and exchange.** Import data using Patterns, move portable packages between Stores, clone Namespaces, and convert graphs to and from NetworkX. Inspect import plans before applying changes.
|
|
37
|
+
- **Files and tracked operations.** Transfer files through verified streams, inspect operation outcomes, and request cancellation. Use `Store.call()` to access additional server procedures.
|
|
38
|
+
|
|
39
|
+
## Platform
|
|
40
|
+
|
|
41
|
+
The current support target is Python 3.14 on Windows x64 with Deposit Server 2.0.1. Pip installs the Neo4j Python driver and NetworkX as dependencies.
|
|
42
|
+
|
|
43
|
+
## Getting started
|
|
44
|
+
|
|
45
|
+
From a full Git checkout, follow the [quickstart](https://github.com/demjanp/deposit-python/blob/main/doc/quickstart.md) to install the wrapper, open a Store, and query the Deposit Server sample data. If you need a server, follow the [Deposit Server 2.0.1 checkout instructions](https://github.com/demjanp/deposit-server/blob/2.0.1/doc/installation.md#run-from-a-git-checkout).
|
|
46
|
+
|
|
47
|
+
For a local wheel or a published 2.x package, see the [quickstart installation options](https://github.com/demjanp/deposit-python/blob/main/doc/quickstart.md#install). The wrapper generates operation IDs for tracked calls. See [operation IDs](https://github.com/demjanp/deposit-python/blob/main/doc/quickstart.md#operation-ids) to choose an ID before submission and look up the outcome.
|
|
48
|
+
|
|
49
|
+
See the [Python API reference](https://github.com/demjanp/deposit-python/blob/main/doc/api.md) for wrapper behavior.
|
|
50
|
+
|
|
51
|
+
## Build the release wheel
|
|
52
|
+
|
|
53
|
+
On Windows x64, open PowerShell at the root of a clean, full Git checkout. Check out the release commit with its stable `X.Y.Z` tag, such as `2.0.1`. Use a fresh build directory alongside the checkout.
|
|
54
|
+
|
|
55
|
+
```powershell
|
|
56
|
+
py -3.14 -m venv ..\deposit-python-release\venv
|
|
57
|
+
..\deposit-python-release\venv\Scripts\python.exe -m pip install build
|
|
58
|
+
..\deposit-python-release\venv\Scripts\python.exe -m build --wheel --outdir ..\deposit-python-release\dist
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The builder writes the wheel to the sibling build directory. The wheel version comes from the Git tag. The wheel contains the wrapper and its procedure catalog, and pip resolves the runtime dependencies during installation.
|
|
62
|
+
|
|
63
|
+
## Citation
|
|
64
|
+
|
|
65
|
+
Please use this citation when referencing the software.
|
|
66
|
+
|
|
67
|
+
> Demján, P. (2026). *deposit-python* [Computer software]. [GitHub repository](https://github.com/demjanp/deposit-python).
|
|
68
|
+
|
|
69
|
+
## Acknowledgements
|
|
70
|
+
|
|
71
|
+
Funding for the server component is listed in the [Deposit Server acknowledgements](https://github.com/demjanp/deposit-server#acknowledgements).
|
|
72
|
+
|
|
73
|
+
## Contact
|
|
74
|
+
|
|
75
|
+
Peter Demján ([peter.demjan@gmail.com](mailto:peter.demjan@gmail.com))
|
|
76
|
+
|
|
77
|
+
Institute of Archaeology of the Czech Academy of Sciences, Prague, v.v.i.
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
Distributed under the [MIT License](https://opensource.org/license/mit). See the repository’s [LICENSE](https://github.com/demjanp/deposit-python/blob/main/LICENSE) file.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
deposit/__init__.py,sha256=4UoVU4t8WumnX6o0rPbur_UadFhA5EbZQnD91RMcVPI,1029
|
|
2
|
+
deposit/_batch.py,sha256=aROG5Jp8-x8lyEqve90T15a6v28gAcC9lOGHgjSXlcM,15382
|
|
3
|
+
deposit/_exchange.py,sha256=RIJ45sNiSEbRYE9sGGRMikigA3hlE7zaF_gwTGVczYE,21746
|
|
4
|
+
deposit/_exchange_transport.py,sha256=Q0T9V1JAjTzZXewrIYTHTHWqw2bzI_f_O9caCQGtG1c,19937
|
|
5
|
+
deposit/_gateway.py,sha256=mAurLqaQZccemZOYNdhJkOS7XH8zjlCf5MSSVRJeI1Q,24287
|
|
6
|
+
deposit/_meta.py,sha256=98O7w_flsWq6ukvU00tBDUZIWMrKaGgqzEEa5LzNsW4,43
|
|
7
|
+
deposit/_paging.py,sha256=g_wMoK1wNLLtAIS34gjsGn7AkefQJModhgS_8l0PjNw,26564
|
|
8
|
+
deposit/_procedure_manifest.json,sha256=06QVnp9ikNMqWBhMQpuDueG_e42JmKynUEJ-xPuYEtA,86624
|
|
9
|
+
deposit/_resource_buffer.py,sha256=A374o4v_XwniocEce6eVMER6Y_upAAZ5bE-8_Rm0sY0,9433
|
|
10
|
+
deposit/_resources.py,sha256=dvIUKGAyivQCgAB3f_YUsPkq_Nh2gx9lR-LbvUhwT4E,7908
|
|
11
|
+
deposit/_version.py,sha256=9MP4TWM2x1S_cAdHgLBx6v-_vsJBZtMsxHyivC6dVp4,552
|
|
12
|
+
deposit/client.py,sha256=j-qU1HPvx7Gu2xaUhJVtR2hqEimyR_EYz9pZLCLo9Do,25858
|
|
13
|
+
deposit/entities.py,sha256=k_p7bm9ocJsr2OdYs5jm8Pm5qQ94zfDlXWFCzuGGWIQ,14027
|
|
14
|
+
deposit/errors.py,sha256=jBntTr7Cfz94-cgsWjbhHglvh2ZwF-3QLQBEc5_6p7c,2368
|
|
15
|
+
deposit/exchange.py,sha256=d9lwNZE16vC4caJ-Vxdn8ut-A_GoNGlw2oR9DKKIHrA,138
|
|
16
|
+
deposit/networkx.py,sha256=DQkxSfuSuTOKluS5x9XMieBRcGcrmp3GJU9YAmuI-5A,16537
|
|
17
|
+
deposit/store.py,sha256=kzI8sRn1mkmhh9dA7yBS-6L9xOlaKYT70XWIeYDtJbw,111324
|
|
18
|
+
deposit/values.py,sha256=GdzqoX5MGWe2HX-aLhrAF-Nj8y5cl5Kc2bbdOzJKqDw,25839
|
|
19
|
+
deposit_python-2.0.1.dist-info/licenses/LICENSE,sha256=zro-lj-K8f0RtDl_AzDde4nIjJSZoAIpf0zckXAtJds,1089
|
|
20
|
+
deposit_python-2.0.1.dist-info/METADATA,sha256=pAmGvT18GtfTUPFbkqXGiy0YfsaYQV8RPmNtkfh_LRM,4948
|
|
21
|
+
deposit_python-2.0.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
22
|
+
deposit_python-2.0.1.dist-info/top_level.txt,sha256=6jY8Dh3yPuqPiIdmBxxS6m58OyoMmFGu6bdfGb8EJ4k,8
|
|
23
|
+
deposit_python-2.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Peter Demján
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
deposit
|