-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.py
163 lines (133 loc) · 4.8 KB
/
start.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import sys
from parser import *
from linker import Linker
from graph_structure import GraphStructure
from graph_storage import GraphStorage
import networkx as nx
from utilities import Utilities
from BatchExecute import BatchExecute
from error_checking import Error_Checking
import readline
class StartDatabase:
"""
Class runs the entire graph database in the terminal and manages
the entire interaction from user input to printing out data.
"""
def __init__(self, flag, verbose):
"""
Initializes the graph database by reading persisted data on disk
and setting up the internal in-memory graph file.
"""
print "Welcome to microDB!"
self.gs = GraphStructure()
self.gstorage = GraphStorage(self.gs)
# Loads data on disk
self.graph_file = 'graph_file'
self.id_file = 'id_file'
self.load_persistent_data()
# Stores verbose flag
self.verbose = verbose
# If flag is set, need to execute commands in file that user passed.
if flag:
print "Loading batch file..."
BatchExecute(self.gs, sys.argv[1])
print "Done executing batch file!"
def load_persistent_data(self):
"""
Loads persisted data on disk, if it exists, into our GraphStructure
object.
"""
print "Loading database from disk..."
if Utilities.files_exist(self.graph_file, self.id_file):
self.gstorage.load_graph(self.graph_file, self.id_file)
print "Finished loading database from disk."
else:
print "No files to load from."
def has_Errors(self, parser):
"""
This method checks the command entered by the user
for any errors and if there are any, don't create
a linker.
@type parser: Object
@param parser: Parser instance used to check for errors
@rtype: Boolean
@return: Boolean indicating whether errors exist
"""
# First check if state machine produced any error
# If errors exist, then don't create linker
if (parser.get_Errors()):
print "State machine Error"
return True
# Create error class instance
errorCheck = Error_Checking(parser.get_object_list())
# If there are errors, don't create linker
if errorCheck.check_commands():
print "Command state Error"
return True
return False
def run(self):
"""
Keeps the graph database and continously running in the terminal
and parses the input.
"""
while True:
if verbose:
# Prints out the graph structure for verbose output
self.gs.display()
commands = []
#sys.stdout.write(">>> ")
command = raw_input(">>> ")
while True:
#command = raw_input(">>> ")
if (len(command) != 0 and command[-1] == ";"):
commands.append(command)
break
commands.append(command)
command = raw_input("... ")
command_str = " ".join(commands)
# Start the parser and parse the commands
if (command_str[-1] == ";"):
real_command = command_str[:-1] + " ;" # need to add space for parser
parser = Parser(real_command)
parser.run()
else:
print "ERROR INVALID QUERY"
# Check if user entered any errors in query.
# If there are no errors, then create linker
if (not(self.has_Errors(parser))):
linker = Linker(parser.get_object_list(), self.gs)
linker.execute()
# Else, print the error
else:
print "Invalid Query"
def exit(self):
"""
Persists the graph data and exits the graph database.
"""
print
self.persist_data()
print "Exiting microDB..."
def persist_data(self):
"""
Persists the GraphStructure object onto disk.
"""
print "Writing database back to disk..."
self.gstorage.write_graph(self.graph_file, self.id_file)
# Start our main programs
if __name__ == "__main__":
batch_flag = False
verbose = False
# Initialize options supplied by user for extra options
if "-b" in sys.argv:
# Executes batch file into our database
batch_flag = True
if "-v" in sys.argv:
# Displays show for every command
verbose = True
# Do all necessary initialization on start-up.
start = StartDatabase(batch_flag, verbose)
# Then run the interpreter.
try:
start.run()
except KeyboardInterrupt:
start.exit()