Skip to content

Commit 6729d40

Browse files
Address PR comments for Python examples integration
- Add explanatory comments for Docker port configurations - Use specialized labels (person-py-ex, py-conn-ex) to isolate test data - Replace global cleanup with targeted cleanup using has_label() - Add conditional SSL certificate verification for non-localhost connections - Fix Kerberos hostname to match conftest.py approach
1 parent f3fbef3 commit 6729d40

File tree

3 files changed

+43
-45
lines changed

3 files changed

+43
-45
lines changed

gremlin-python/src/main/python/examples/basic_gremlin.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,32 @@
2626

2727

2828
def main():
29+
# if there is a port placeholder in the env var then we are running with docker so set appropriate port
2930
server_url = os.getenv('GREMLIN_SERVER_URL', 'ws://localhost:8182/gremlin').format(45940)
3031
rc = DriverRemoteConnection(server_url, 'g')
3132
g = traversal().with_remote(rc)
3233

33-
# drop existing vertices
34-
g.V().drop().iterate()
35-
3634
# basic Gremlin: adding and retrieving data
37-
v1 = g.add_v('person').property('name', 'marko').next()
38-
v2 = g.add_v('person').property('name', 'stephen').next()
39-
v3 = g.add_v('person').property('name', 'vadas').next()
35+
v1 = g.add_v('person-py-ex').property('name', 'marko').next()
36+
v2 = g.add_v('person-py-ex').property('name', 'stephen').next()
37+
v3 = g.add_v('person-py-ex').property('name', 'vadas').next()
4038

4139
# be sure to use a terminating step like next() or iterate() so that the traversal "executes"
4240
# iterate() does not return any data and is used to just generate side-effects (i.e. write data to the database)
4341
g.V(v1).add_e('knows').to(v2).property('weight', 0.75).iterate()
4442
g.V(v1).add_e('knows').to(v3).property('weight', 0.75).iterate()
4543

4644
# retrieve the data from the "marko" vertex
47-
marko = g.V().has('person', 'name', 'marko').values('name').next()
45+
marko = g.V().has('person-py-ex', 'name', 'marko').values('name').next()
4846
print("name: " + marko)
4947

5048
# find the "marko" vertex and then traverse to the people he "knows" and return their data
51-
people_marko_knows = g.V().has('person', 'name', 'marko').out('knows').values('name').to_list()
49+
people_marko_knows = g.V().has('person-py-ex', 'name', 'marko').out('knows').values('name').to_list()
5250
for person in people_marko_knows:
5351
print("marko knows " + person)
5452

5553
# clean added data
56-
g.V().drop().iterate()
54+
g.V().has_label('person-py-ex').drop().iterate()
5755

5856
rc.close()
5957

gremlin-python/src/main/python/examples/connections.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import sys
1919
import os
2020
import ssl
21+
import socket
2122

2223
sys.path.append("..")
2324

@@ -43,72 +44,70 @@ def with_remote():
4344
#
4445
# which starts it in "console" mode with an empty in-memory TinkerGraph ready to go bound to a
4546
# variable named "g" as referenced in the following line.
47+
# if there is a port placeholder in the env var then we are running with docker so set appropriate port
4648
server_url = os.getenv('GREMLIN_SERVER_URL', 'ws://localhost:8182/gremlin').format(45940)
4749
rc = DriverRemoteConnection(server_url, 'g')
4850
g = traversal().with_remote(rc)
4951

50-
# drop existing vertices
51-
g.V().drop().iterate()
52-
5352
# simple query to verify connection
54-
v = g.add_v().iterate()
55-
count = g.V().count().next()
53+
v = g.add_v('py-conn-ex').iterate()
54+
count = g.V().has_label('py-conn-ex').count().next()
5655
print("Vertex count: " + str(count))
5756

57+
# clean added data
58+
g.V().has_label('py-conn-ex').drop().iterate()
5859
# cleanup
5960
rc.close()
6061

6162

6263
# connecting with plain text authentication
6364
def with_auth():
65+
# if there is a port placeholder in the env var then we are running with docker so set appropriate port
6466
server_url = os.getenv('GREMLIN_SERVER_BASIC_AUTH_URL', 'ws://localhost:8182/gremlin').format(45941)
65-
# turn off certificate verification for testing purposes only
66-
ssl_opts = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
67-
ssl_opts.check_hostname = False
68-
ssl_opts.verify_mode = ssl.CERT_NONE
69-
rc = DriverRemoteConnection(server_url, 'g', username='stephen', password='password',
70-
transport_factory=lambda: AiohttpTransport(ssl_options=ssl_opts))
67+
68+
# disable SSL certificate verification for remote hosts in test environments
69+
if 'localhost' not in server_url:
70+
ssl_opts = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
71+
ssl_opts.check_hostname = False
72+
ssl_opts.verify_mode = ssl.CERT_NONE
73+
rc = DriverRemoteConnection(server_url, 'g', username='stephen', password='password',
74+
transport_factory=lambda: AiohttpTransport(ssl_options=ssl_opts))
75+
else:
76+
rc = DriverRemoteConnection(server_url, 'g', username='stephen', password='password')
77+
7178
g = traversal().with_remote(rc)
7279

73-
# drop existing vertices
74-
g.V().drop().iterate()
75-
76-
v = g.add_v().iterate()
77-
count = g.V().count().next()
80+
v = g.add_v('py-conn-ex').iterate()
81+
count = g.V().has_label('py-conn-ex').count().next()
7882
print("Vertex count: " + str(count))
7983

8084
# clean added data
81-
g.V().drop().iterate()
85+
g.V().has_label('py-conn-ex').drop().iterate()
8286
rc.close()
8387

8488

8589
# connecting with Kerberos SASL authentication
8690
def with_kerberos():
91+
# if there is a port placeholder in the env var then we are running with docker so set appropriate port
8792
server_url = os.getenv('GREMLIN_SERVER_URL', 'ws://localhost:8182/gremlin').format(45942)
88-
kerberos_hostname = os.getenv('KRB_HOSTNAME', 'gremlin-server-test')
93+
kerberos_hostname = os.getenv('KRB_HOSTNAME', socket.gethostname())
8994
kerberized_service = f'test-service@{kerberos_hostname}'
9095

91-
try:
92-
rc = DriverRemoteConnection(server_url, 'g', kerberized_service=kerberized_service)
93-
g = traversal().with_remote(rc)
94-
95-
# drop existing vertices
96-
g.V().drop().iterate()
96+
rc = DriverRemoteConnection(server_url, 'g', kerberized_service=kerberized_service)
97+
g = traversal().with_remote(rc)
9798

98-
v = g.add_v().iterate()
99-
count = g.V().count().next()
100-
print("Vertex count: " + str(count))
99+
v = g.add_v('py-conn-ex').iterate()
100+
count = g.V().has_label('py-conn-ex').count().next()
101+
print("Vertex count: " + str(count))
101102

102-
# clean added data
103-
g.V().drop().iterate()
104-
rc.close()
105-
except Exception as e:
106-
print(f"Kerberos authentication failed (expected in test environment): {e}")
107-
# This is expected to fail in CI without proper Kerberos setup
103+
# clean added data
104+
g.V().has_label('py-conn-ex').drop().iterate()
105+
rc.close()
108106

109107

110108
# connecting with customized configurations
111109
def with_configs():
110+
# if there is a port placeholder in the env var then we are running with docker so set appropriate port
112111
server_url = os.getenv('GREMLIN_SERVER_URL', 'ws://localhost:8182/gremlin').format(45940)
113112
rc = DriverRemoteConnection(
114113
server_url, 'g',
@@ -119,12 +118,12 @@ def with_configs():
119118
)
120119
g = traversal().with_remote(rc)
121120

122-
v = g.add_v().iterate()
123-
count = g.V().count().next()
121+
v = g.add_v('py-conn-ex').iterate()
122+
count = g.V().has_label('py-conn-ex').count().next()
124123
print("Vertex count: " + str(count))
125124

126125
# clean added data
127-
g.V().drop().iterate()
126+
g.V().has_label('py-conn-ex').drop().iterate()
128127

129128
rc.close()
130129

gremlin-python/src/main/python/examples/modern_traversals.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def main():
3232
# This example requires the Modern toy graph to be preloaded upon launching the Gremlin server.
3333
# For details, see https://tinkerpop.apache.org/docs/current/reference/#gremlin-server-docker-image and use
3434
# conf/gremlin-server-modern.yaml.
35+
# if there is a port placeholder in the env var then we are running with docker so set appropriate port
3536
server_url = os.getenv('GREMLIN_SERVER_URL', 'ws://localhost:8182/gremlin').format(45940)
3637

3738
# CI uses port 45940 with gmodern binding, local uses 8182 with g binding

0 commit comments

Comments
 (0)