|
1
|
|
|
"""goblin application class and class constructor""" |
|
2
|
|
|
|
|
3
|
|
|
import collections |
|
4
|
|
|
import importlib |
|
5
|
|
|
import logging |
|
6
|
|
|
|
|
7
|
|
|
import aiogremlin # type: ignore |
|
8
|
|
|
|
|
9
|
|
|
from goblin import element, provider, session |
|
10
|
|
|
|
|
11
|
|
|
logger = logging.getLogger(__name__) |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
# Main API classes |
|
15
|
|
|
class Goblin: |
|
16
|
|
|
""" |
|
17
|
|
|
Class used to encapsulate database connection configuration and generate |
|
18
|
|
|
database connections Used as a factory to create |
|
19
|
|
|
:py:class:`Session<goblin.session.Session>` objects. |
|
20
|
|
|
|
|
21
|
|
|
:param str url: Database url |
|
22
|
|
|
:param asyncio.BaseEventLoop loop: Event loop implementation |
|
23
|
|
|
:param dict features: Vendor implementation specific database features |
|
24
|
|
|
:param dict config: Config parameters for application |
|
25
|
|
|
""" |
|
26
|
|
|
|
|
27
|
|
|
def __init__(self, |
|
28
|
|
|
cluster, |
|
29
|
|
|
*, |
|
30
|
|
|
provider=provider.TinkerGraph, |
|
31
|
|
|
get_hashable_id=None, |
|
32
|
|
|
aliases=None): |
|
33
|
|
|
self._cluster = cluster |
|
34
|
|
|
self._loop = self._cluster._loop |
|
35
|
|
|
self._cluster = cluster |
|
36
|
|
|
self._vertices = collections.defaultdict(lambda: element.GenericVertex) |
|
37
|
|
|
self._edges = collections.defaultdict(lambda: element.GenericEdge) |
|
38
|
|
|
self._vertex_properties = {} |
|
39
|
|
|
self._provider = provider |
|
40
|
|
|
if not get_hashable_id: |
|
41
|
|
|
get_hashable_id = self._provider.get_hashable_id |
|
42
|
|
|
self._get_hashable_id = get_hashable_id |
|
43
|
|
|
if aliases is None: |
|
44
|
|
|
aliases = {} |
|
45
|
|
|
self._aliases = aliases |
|
46
|
|
|
|
|
47
|
|
|
@classmethod |
|
48
|
|
|
async def open(cls, |
|
49
|
|
|
loop, |
|
50
|
|
|
*, |
|
51
|
|
|
provider=provider.TinkerGraph, |
|
52
|
|
|
get_hashable_id=None, |
|
53
|
|
|
aliases=None, |
|
54
|
|
|
**config): |
|
55
|
|
|
# App currently only supports GraphSON 1 |
|
56
|
|
|
# aiogremlin does not yet support providers |
|
57
|
|
|
cluster = await aiogremlin.Cluster.open( |
|
58
|
|
|
loop, aliases=aliases, **config) |
|
59
|
|
|
app = Goblin( |
|
60
|
|
|
cluster, |
|
61
|
|
|
provider=provider, |
|
62
|
|
|
get_hashable_id=get_hashable_id, |
|
63
|
|
|
aliases=aliases) |
|
64
|
|
|
return app |
|
65
|
|
|
|
|
66
|
|
|
@property |
|
67
|
|
|
def cluster(self): |
|
68
|
|
|
return self._cluster |
|
69
|
|
|
|
|
70
|
|
|
@property |
|
71
|
|
|
def config(self): |
|
72
|
|
|
return self.cluster.config |
|
73
|
|
|
|
|
74
|
|
|
@property |
|
75
|
|
|
def vertices(self): |
|
76
|
|
|
"""Registered vertex classes""" |
|
77
|
|
|
return self._vertices |
|
78
|
|
|
|
|
79
|
|
|
@property |
|
80
|
|
|
def vertex_properties(self): |
|
81
|
|
|
"""Registered vertex classes""" |
|
82
|
|
|
return self._vertex_properties |
|
83
|
|
|
|
|
84
|
|
|
@property |
|
85
|
|
|
def edges(self): |
|
86
|
|
|
"""Registered edge classes""" |
|
87
|
|
|
return self._edges |
|
88
|
|
|
|
|
89
|
|
|
@property |
|
90
|
|
|
def url(self): |
|
91
|
|
|
"""Database url""" |
|
92
|
|
|
return self._url |
|
93
|
|
|
|
|
94
|
|
|
def register(self, *elements): |
|
95
|
|
|
""" |
|
96
|
|
|
Register user created Element classes. |
|
97
|
|
|
|
|
98
|
|
|
:param goblin.element.Element elements: User defined Element classes |
|
99
|
|
|
""" |
|
100
|
|
|
for element in elements: |
|
101
|
|
|
if element.__type__ == 'vertex': |
|
102
|
|
|
self._vertices[element.__label__] = element |
|
103
|
|
|
if element.__type__ == 'edge': |
|
104
|
|
|
self._edges[element.__label__] = element |
|
105
|
|
|
if element.__type__ == 'vertexproperty': |
|
106
|
|
|
self._vertex_properties[element.__label__] = element |
|
107
|
|
|
|
|
108
|
|
|
def config_from_file(self, filename): |
|
109
|
|
|
""" |
|
110
|
|
|
Load configuration from from file. |
|
111
|
|
|
|
|
112
|
|
|
:param str filename: Path to the configuration file. |
|
113
|
|
|
""" |
|
114
|
|
|
self._cluster.config_from_file(filename) |
|
115
|
|
|
|
|
116
|
|
|
def config_from_yaml(self, filename): |
|
117
|
|
|
self._cluster.config_from_yaml(filename) |
|
118
|
|
|
|
|
119
|
|
|
def config_from_json(self, filename): |
|
120
|
|
|
""" |
|
121
|
|
|
Load configuration from from JSON file. |
|
122
|
|
|
|
|
123
|
|
|
:param str filename: Path to the configuration file. |
|
124
|
|
|
""" |
|
125
|
|
|
self._cluster.config_from_json(filename) |
|
126
|
|
|
|
|
127
|
|
|
def config_from_module(self, module): |
|
128
|
|
|
self._cluster.config_from_module(module) |
|
129
|
|
|
|
|
130
|
|
|
def register_from_module(self, module, *, package=None): |
|
131
|
|
|
if isinstance(module, str): |
|
132
|
|
|
module = importlib.import_module(module, package) |
|
133
|
|
|
elements = list() |
|
134
|
|
|
for item_name in dir(module): |
|
135
|
|
|
item = getattr(module, item_name) |
|
136
|
|
|
if isinstance(item, element.ElementMeta): |
|
137
|
|
|
elements.append(item) |
|
138
|
|
|
self.register(*elements) |
|
139
|
|
|
|
|
140
|
|
|
async def session(self, *, processor='', op='eval', aliases=None): |
|
141
|
|
|
""" |
|
142
|
|
|
Create a session object. |
|
143
|
|
|
|
|
144
|
|
|
:returns: :py:class:`Session<goblin.session.Session>` object |
|
145
|
|
|
""" |
|
146
|
|
|
remote_connection = await aiogremlin.DriverRemoteConnection.using( |
|
147
|
|
|
self._cluster, aliases=self._aliases) |
|
148
|
|
|
return session.Session(self, remote_connection, self._get_hashable_id) |
|
149
|
|
|
|
|
150
|
|
|
async def close(self): |
|
151
|
|
|
await self._cluster.close() |
|
152
|
|
|
|