1
|
|
|
import abc |
2
|
|
|
import logging |
3
|
|
|
|
4
|
|
|
from gremlin_python.process.traversal import Cardinality # type: ignore |
5
|
|
|
|
6
|
|
|
from goblin import element, exception, manager |
7
|
|
|
|
8
|
|
|
logger = logging.getLogger(__name__) |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class DataType(abc.ABC): |
12
|
|
|
""" |
13
|
|
|
Abstract base class for Goblin Data Types. All custom data types should |
14
|
|
|
inherit from :py:class:`DataType`. |
15
|
|
|
""" |
16
|
|
|
|
17
|
|
|
def __init__(self, val=None): |
18
|
|
|
if val: |
19
|
|
|
val = self.validate(val) |
20
|
|
|
self._val = val |
21
|
|
|
|
22
|
|
|
@abc.abstractmethod |
23
|
|
|
def validate(self, val): |
24
|
|
|
"""Validate property value""" |
25
|
|
|
return val |
26
|
|
|
|
27
|
|
|
@abc.abstractmethod |
28
|
|
|
def to_db(self, val=None): |
29
|
|
|
""" |
30
|
|
|
Convert property value to db compatible format. If no value passed, try |
31
|
|
|
to use default bound value |
32
|
|
|
""" |
33
|
|
|
if val is None: |
34
|
|
|
val = self._val |
35
|
|
|
return val |
36
|
|
|
|
37
|
|
|
@abc.abstractmethod |
38
|
|
|
def to_ogm(self, val): |
39
|
|
|
"""Convert property value to a Python compatible format""" |
40
|
|
|
return val |
41
|
|
|
|
42
|
|
|
def validate_vertex_prop(self, val, card, vertex_prop, data_type): |
43
|
|
|
if card == Cardinality.list_: |
44
|
|
|
if isinstance(val, list): |
45
|
|
|
val = val |
46
|
|
|
elif isinstance(val, (set, tuple)): |
47
|
|
|
val = list(val) |
48
|
|
|
else: |
49
|
|
|
val = [val] |
50
|
|
|
vertex_props = [] |
51
|
|
|
for v in val: |
52
|
|
|
vp = vertex_prop(data_type, card=card) |
53
|
|
|
vp.value = self.validate(v) |
54
|
|
|
vertex_props.append(vp) |
55
|
|
|
val = manager.ListVertexPropertyManager(data_type, vertex_prop, |
56
|
|
|
card, vertex_props) |
57
|
|
|
elif card == Cardinality.set_: |
58
|
|
|
if isinstance(val, set): |
59
|
|
|
val = val |
60
|
|
|
elif isinstance(val, (list, tuple)): |
61
|
|
|
val = set(val) |
62
|
|
|
else: |
63
|
|
|
val = set([val]) |
64
|
|
|
vertex_props = set([]) |
65
|
|
|
for v in val: |
66
|
|
|
if not isinstance(v, element.VertexProperty): |
67
|
|
|
vp = vertex_prop(data_type, card=card) |
68
|
|
|
vp.value = self.validate(v) |
69
|
|
|
else: |
70
|
|
|
vp = v |
71
|
|
|
vertex_props.add(vp) |
72
|
|
|
val = manager.SetVertexPropertyManager(data_type, vertex_prop, |
73
|
|
|
card, vertex_props) |
74
|
|
|
else: |
75
|
|
|
vp = vertex_prop(data_type) |
76
|
|
|
vp.value = self.validate(val) |
77
|
|
|
val = vp |
78
|
|
|
return val |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
class BaseProperty: |
82
|
|
|
"""Abstract base class that implements the property interface""" |
83
|
|
|
|
84
|
|
|
@property |
85
|
|
|
def data_type(self): |
86
|
|
|
raise NotImplementedError |
87
|
|
|
|