14
14
HTTP_PROTOCOL_FORMATS , WS_PROTOCOL_FORMATS ,
15
15
DEFAULT_NEO4J_USERNAME , DEFAULT_NEO4J_PASSWORD , DEFAULT_NEO4J_DATABASE ,
16
16
NEPTUNE_CONFIG_HOST_IDENTIFIERS , is_allowed_neptune_host , false_str_variants ,
17
- GRAPHBINARYV1 , GREMLIN_SERIALIZERS_HTTP ,
17
+ GRAPHBINARYV1 , GREMLIN_SERIALIZERS_HTTP , GREMLIN_SERIALIZERS_WS ,
18
+ GREMLIN_SERIALIZERS_ALL , NEPTUNE_GREMLIN_SERIALIZERS_HTTP ,
19
+ DEFAULT_GREMLIN_WS_SERIALIZER , DEFAULT_GREMLIN_HTTP_SERIALIZER ,
18
20
NEPTUNE_DB_SERVICE_NAME , NEPTUNE_ANALYTICS_SERVICE_NAME ,
19
- normalize_service_name )
21
+ normalize_service_name , normalize_protocol_name ,
22
+ normalize_serializer_class_name )
20
23
21
24
DEFAULT_CONFIG_LOCATION = os .path .expanduser ('~/graph_notebook_config.json' )
22
25
@@ -57,7 +60,8 @@ class GremlinSection(object):
57
60
"""
58
61
59
62
def __init__ (self , traversal_source : str = '' , username : str = '' , password : str = '' ,
60
- message_serializer : str = '' , connection_protocol : str = '' , include_protocol : bool = False ):
63
+ message_serializer : str = '' , connection_protocol : str = '' ,
64
+ include_protocol : bool = False , neptune_service : str = '' ):
61
65
"""
62
66
:param traversal_source: used to specify the traversal source for a Gremlin traversal, in the case that we are
63
67
connected to an endpoint that can access multiple graphs.
@@ -71,57 +75,78 @@ def __init__(self, traversal_source: str = '', username: str = '', password: str
71
75
if traversal_source == '' :
72
76
traversal_source = DEFAULT_GREMLIN_TRAVERSAL_SOURCE
73
77
74
- serializer_lower = message_serializer .lower ()
75
- # TODO: Update with untyped serializers once supported in GremlinPython
76
- # Accept TinkerPop serializer class name
77
- # https://github.com/apache/tinkerpop/blob/fd040c94a66516e473811fe29eaeaf4081cf104c/docs/src/reference/gremlin-applications.asciidoc#graphson
78
- # https://github.com/apache/tinkerpop/blob/fd040c94a66516e473811fe29eaeaf4081cf104c/docs/src/reference/gremlin-applications.asciidoc#graphbinary
79
- if serializer_lower == '' :
80
- message_serializer = DEFAULT_GREMLIN_SERIALIZER
81
- elif 'graphson' in serializer_lower :
82
- message_serializer = 'GraphSON'
83
- if 'untyped' in serializer_lower :
84
- message_serializer += 'Untyped'
85
- if 'v1' in serializer_lower :
86
- if 'untyped' in serializer_lower :
87
- message_serializer += 'MessageSerializerV1'
88
- else :
89
- message_serializer += 'MessageSerializerGremlinV1'
90
- elif 'v2' in serializer_lower :
91
- message_serializer += 'MessageSerializerV2'
78
+ invalid_serializer_input = False
79
+ if message_serializer != '' :
80
+ message_serializer , invalid_serializer_input = normalize_serializer_class_name (message_serializer )
81
+
82
+ if include_protocol :
83
+ # Neptune endpoint
84
+ invalid_protocol_input = False
85
+ if connection_protocol != '' :
86
+ connection_protocol , invalid_protocol_input = normalize_protocol_name (connection_protocol )
87
+
88
+ if neptune_service == NEPTUNE_ANALYTICS_SERVICE_NAME :
89
+ if connection_protocol != DEFAULT_HTTP_PROTOCOL :
90
+ if invalid_protocol_input :
91
+ print (f"Invalid connection protocol specified, you must use { DEFAULT_HTTP_PROTOCOL } . " )
92
+ elif connection_protocol == DEFAULT_WS_PROTOCOL :
93
+ print (f"Enforcing HTTP protocol." )
94
+ connection_protocol = DEFAULT_HTTP_PROTOCOL
95
+ # temporary restriction until GraphSON-typed and GraphBinary results are supported
96
+ if message_serializer not in NEPTUNE_GREMLIN_SERIALIZERS_HTTP :
97
+ if message_serializer not in GREMLIN_SERIALIZERS_ALL :
98
+ if invalid_serializer_input :
99
+ print (f"Invalid serializer specified, defaulting to { DEFAULT_GREMLIN_HTTP_SERIALIZER } . "
100
+ f"Valid serializers: { NEPTUNE_GREMLIN_SERIALIZERS_HTTP } " )
101
+ else :
102
+ print (f"{ message_serializer } is not currently supported for HTTP connections, "
103
+ f"defaulting to { DEFAULT_GREMLIN_HTTP_SERIALIZER } . "
104
+ f"Please use one of: { NEPTUNE_GREMLIN_SERIALIZERS_HTTP } " )
105
+ message_serializer = DEFAULT_GREMLIN_HTTP_SERIALIZER
92
106
else :
93
- message_serializer += 'MessageSerializerV3'
94
- elif 'graphbinary' in serializer_lower :
95
- message_serializer = GRAPHBINARYV1
107
+ if connection_protocol not in [DEFAULT_WS_PROTOCOL , DEFAULT_HTTP_PROTOCOL ]:
108
+ if invalid_protocol_input :
109
+ print (f"Invalid connection protocol specified, defaulting to { DEFAULT_WS_PROTOCOL } . "
110
+ f"Valid protocols: [websockets, http]." )
111
+ connection_protocol = DEFAULT_WS_PROTOCOL
112
+
113
+ if connection_protocol == DEFAULT_HTTP_PROTOCOL :
114
+ # temporary restriction until GraphSON-typed and GraphBinary results are supported
115
+ if message_serializer not in NEPTUNE_GREMLIN_SERIALIZERS_HTTP :
116
+ if message_serializer not in GREMLIN_SERIALIZERS_ALL :
117
+ if invalid_serializer_input :
118
+ print (f"Invalid serializer specified, defaulting to { DEFAULT_GREMLIN_HTTP_SERIALIZER } . "
119
+ f"Valid serializers: { NEPTUNE_GREMLIN_SERIALIZERS_HTTP } " )
120
+ else :
121
+ print (f"{ message_serializer } is not currently supported for HTTP connections, "
122
+ f"defaulting to { DEFAULT_GREMLIN_HTTP_SERIALIZER } . "
123
+ f"Please use one of: { NEPTUNE_GREMLIN_SERIALIZERS_HTTP } " )
124
+ message_serializer = DEFAULT_GREMLIN_HTTP_SERIALIZER
125
+ else :
126
+ if message_serializer not in GREMLIN_SERIALIZERS_WS :
127
+ if invalid_serializer_input :
128
+ print (f"Invalid serializer specified, defaulting to { DEFAULT_GREMLIN_WS_SERIALIZER } . "
129
+ f"Valid serializers: { GREMLIN_SERIALIZERS_WS } " )
130
+ elif message_serializer != '' :
131
+ print (f"{ message_serializer } is not currently supported by Gremlin Python driver, "
132
+ f"defaulting to { DEFAULT_GREMLIN_WS_SERIALIZER } . "
133
+ f"Valid serializers: { GREMLIN_SERIALIZERS_WS } " )
134
+ message_serializer = DEFAULT_GREMLIN_WS_SERIALIZER
135
+
136
+ self .connection_protocol = connection_protocol
96
137
else :
97
- print (f'Invalid Gremlin serializer specified, defaulting to graphsonv3. '
98
- f'Valid serializers: { GREMLIN_SERIALIZERS_HTTP } .' )
99
- message_serializer = DEFAULT_GREMLIN_SERIALIZER
138
+ # Non-Neptune database - check and set valid WebSockets serializer if invalid/empty
139
+ if message_serializer not in GREMLIN_SERIALIZERS_WS :
140
+ message_serializer = DEFAULT_GREMLIN_WS_SERIALIZER
141
+ if invalid_serializer_input :
142
+ print (f'Invalid Gremlin serializer specified, defaulting to { DEFAULT_GREMLIN_WS_SERIALIZER } . '
143
+ f'Valid serializers: { GREMLIN_SERIALIZERS_WS } .' )
100
144
101
145
self .traversal_source = traversal_source
102
146
self .username = username
103
147
self .password = password
104
148
self .message_serializer = message_serializer
105
149
106
- if include_protocol :
107
- protocol_lower = connection_protocol .lower ()
108
- if message_serializer in GREMLIN_SERIALIZERS_HTTP :
109
- connection_protocol = DEFAULT_HTTP_PROTOCOL
110
- if protocol_lower != '' and protocol_lower not in HTTP_PROTOCOL_FORMATS :
111
- print (f"Enforcing HTTP protocol usage for serializer: { message_serializer } ." )
112
- else :
113
- if protocol_lower == '' :
114
- connection_protocol = DEFAULT_GREMLIN_PROTOCOL
115
- elif protocol_lower in HTTP_PROTOCOL_FORMATS :
116
- connection_protocol = DEFAULT_HTTP_PROTOCOL
117
- elif protocol_lower in WS_PROTOCOL_FORMATS :
118
- connection_protocol = DEFAULT_WS_PROTOCOL
119
- else :
120
- print (f"Invalid connection protocol specified, defaulting to { DEFAULT_GREMLIN_PROTOCOL } . "
121
- f"Valid protocols: [websockets, http]." )
122
- connection_protocol = DEFAULT_GREMLIN_PROTOCOL
123
- self .connection_protocol = connection_protocol
124
-
125
150
def to_dict (self ):
126
151
return self .__dict__
127
152
@@ -178,8 +203,8 @@ def __init__(self, host: str, port: int,
178
203
self .auth_mode = auth_mode
179
204
self .load_from_s3_arn = load_from_s3_arn
180
205
self .aws_region = aws_region
181
- default_protocol = DEFAULT_HTTP_PROTOCOL if self ._proxy_host != '' else DEFAULT_GREMLIN_PROTOCOL
182
206
if gremlin_section is not None :
207
+ default_protocol = DEFAULT_HTTP_PROTOCOL if self ._proxy_host != '' else ''
183
208
if hasattr (gremlin_section , "connection_protocol" ):
184
209
if self ._proxy_host != '' and gremlin_section .connection_protocol != DEFAULT_HTTP_PROTOCOL :
185
210
print ("Enforcing HTTP connection protocol for proxy connections." )
@@ -189,9 +214,12 @@ def __init__(self, host: str, port: int,
189
214
else :
190
215
final_protocol = default_protocol
191
216
self .gremlin = GremlinSection (message_serializer = gremlin_section .message_serializer ,
192
- connection_protocol = final_protocol , include_protocol = True )
217
+ connection_protocol = final_protocol ,
218
+ include_protocol = True ,
219
+ neptune_service = self .neptune_service )
193
220
else :
194
- self .gremlin = GremlinSection (connection_protocol = default_protocol , include_protocol = True )
221
+ self .gremlin = GremlinSection (include_protocol = True ,
222
+ neptune_service = self .neptune_service )
195
223
self .neo4j = Neo4JSection ()
196
224
else :
197
225
self .is_neptune_config = False
@@ -331,11 +359,14 @@ def generate_default_config():
331
359
auth_mode_arg = args .auth_mode if args .auth_mode != '' else AuthModeEnum .DEFAULT .value
332
360
protocol_arg = args .gremlin_connection_protocol
333
361
include_protocol = False
362
+ gremlin_service = ''
334
363
if is_allowed_neptune_host (args .host , args .neptune_hosts ):
335
364
include_protocol = True
365
+ gremlin_service = args .neptune_service
336
366
if not protocol_arg :
337
367
protocol_arg = DEFAULT_HTTP_PROTOCOL \
338
368
if args .neptune_service == NEPTUNE_ANALYTICS_SERVICE_NAME else DEFAULT_WS_PROTOCOL
369
+
339
370
config = generate_config (args .host , int (args .port ),
340
371
AuthModeEnum (auth_mode_arg ),
341
372
args .ssl , args .ssl_verify ,
@@ -344,7 +375,7 @@ def generate_default_config():
344
375
SparqlSection (args .sparql_path , '' ),
345
376
GremlinSection (args .gremlin_traversal_source , args .gremlin_username ,
346
377
args .gremlin_password , args .gremlin_serializer ,
347
- protocol_arg , include_protocol ),
378
+ protocol_arg , include_protocol , gremlin_service ),
348
379
Neo4JSection (args .neo4j_username , args .neo4j_password ,
349
380
args .neo4j_auth , args .neo4j_database ),
350
381
args .neptune_hosts )
0 commit comments