-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
102 lines (89 loc) · 3.36 KB
/
console.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os.path
from graph_db.access import db
import sys
import readline
from graph_db.engine.error import GraphEngineError
from graph_db.engine.types import DFS_CONFIG_PATH
GREETING = """
Welcome to not4oundGraph DB. (c) Ilya Borovik, Artur Khayaliev, Boris Makaev
Use `help` to see query examples.
Use `exit` to close database connection.
"""
HELP = """
Query examples:
create graph: label
create node: label
create node: label key:value
create node: label key:value key:value key:value
create relationship: label from label1 to label2
create relationship: label from label1 to label2 key:value
create relationship: label from id:0 to id:1 key:value
match node: label
match node: id:0
match relationship: label
match relationship: id:1
match node: key=value
match node: key<value
match relationship: key>=value
match graph: label
delete node: id:0
delete relationship: id:0
"""
class ConsoleReader:
def __init__(self, base_dir: str = 'temp_db/'):
print("Application is starting...")
self.base_dir = base_dir
print('Setting up connections with worker machines...')
self.db = db.connect(base_dir)
print('Connection has been set up!\n')
self.cursor = self.db.cursor()
def read_query(self):
print(GREETING)
while True:
user_input = input("/")
if len(user_input) != 0:
if 'help' in user_input:
print(HELP)
continue
if user_input == 'exit':
self.db.close()
break
if 'create graph' not in user_input and self.db.get_graph() is None:
print('You have to create a graph firstly using `create graph: label`')
continue
else:
if not self.db.get_graph():
try:
self.cursor.execute(user_input)
result = self.cursor.fetch_one()
print(f"You have created graph called '{result.get_name()}' ")
except (SyntaxError, GraphEngineError) as e:
print(e)
else:
if 'create graph' in user_input:
print(f"You have already created a graph called '{self.db.get_graph().get_name()}'")
continue
try:
self.cursor.execute(user_input)
result = self.cursor.fetch_all()
print(f'Message: {result.get_message()}')
for r in result:
print(r)
except (SyntaxError, GraphEngineError) as e:
print(e)
def run():
config_path = DFS_CONFIG_PATH
if len(sys.argv) > 1:
config_path = sys.argv[1]
if not os.path.isfile(config_path) and not os.path.isfile('../../../' + config_path):
print(f'Path `{config_path}` to DFS configuration file is incorrect')
return
try:
reader = ConsoleReader(config_path)
except ConnectionError:
print('Error: Connection was not set up. Either configuration file, or database files are damaged.\n'
'Please, try again later.')
return
reader.read_query()
if __name__ == '__main__':
run()