-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
58 lines (46 loc) · 1.83 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys
import time
from decimal import Decimal
from neo4j import GraphDatabase
from tabulate import tabulate
from cli import queries
def print_commands():
"""
Print the input commands table with task descriptions
"""
print "* Select a query option OR Enter 0 to exit *\n"
labels = ['input', 'command']
commands = [[ind + 1, x['text']] for ind, x in enumerate(queries)]
print(tabulate(commands, headers=labels, tablefmt='psql'))
def pretty_print(result, column_labels):
"""
Pretty print the results in a tabular manner
"""
print(tabulate(result, headers=column_labels, tablefmt='psql'))
def main():
if (len(sys.argv) <= 3):
print('*** ERROR ***' + '\nUsage: python main.py <server_host>:<port> <username> <password>')
return
driver = GraphDatabase.driver("bolt://" + sys.argv[1], auth=(sys.argv[2], sys.argv[3]))
with driver.session() as session:
print("*** Database connection success ***\n")
while (True):
print_commands()
input_command = input('> ')
if (input_command <= 0):
print ('\nExiting...')
break
if (input_command > len(queries)):
print ('Invalid input.')
continue
raw_inputs = None
command_index = input_command - 1
if 'follow_up' in queries[command_index]:
raw_inputs = raw_input(queries[command_index]['follow_up'])
start_time = time.time()
result = session.read_transaction(queries[command_index]['query'], raw_inputs)
exec_time = time.time() - start_time
pretty_print(result, queries[command_index]['column_labels'])
print("\n--- Query took %s seconds ---\n" % ('{0:.3f}'.format(exec_time)))
if __name__ == '__main__':
main()