-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabasehandling.py
More file actions
295 lines (261 loc) · 9.41 KB
/
databasehandling.py
File metadata and controls
295 lines (261 loc) · 9.41 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from head import errors, core_engine
import os
import re
_db_header = {
}
_db_values = {
}
#Ex ==> Extended
def startDatabase(name, Kvars):
if (name[0] == '"'):
name = name.replace('"', '')
name = name.replace('\n', '')
elif (name[0] == "'"):
name = name.replace("'", "")
name = name.replace('\n', '')
else:
name = name.replace(' ', '')
name = name.rstrip('\n')
if name in Kvars:
name = Kvars[name][0]
else:
print(errors['var_not_found'])
name = name + '{}'.format('.db')
core_engine['databaseValue'] = name
with open(name, 'a') as f:
pass
def table_analysis(toEval, Kvars, indexing):
table_index = 0
values = toEval.split(',')
with open('config/config.init', 'r') as f:
filedata = f.read()
for line in f:
cmng = line.split(' ')
if cmng[0] == 'current_index':
table_index = cmng[2]
filedata = filedata.replace('current_index = {}'.format(str(table_index)), 'current_index = {}'.format(str(table_index + 1)))
with open('config/config.init', 'w') as f:
f.write(filedata)
_db_header[indexing] = ['@{}'.format(str(table_index + 1))] #Falta Implementar ==> Index Memory
print(_db_header)
print(_db_values)
for i in range(len(values)):
try:
values[i] = values[i].replace(' ', '')
except:
pass
if "'" in (values[i]):
values[i] = values[i].replace("'", '')
elif '"' in (values[i]):
values[i] = values[i].replace('"', '')
else:
if values[i] in Kvars:
values[i] = Kvars[values[i]][0]
_db_header[indexing].append(values[i])
def seekOnFile(file, len_values, values, old_values):
with open(core_engine['database'], 'r') as f:
filedata = f.read()
for i in range(len_values):
filedata = replace(old_values[i], values[i])
with open(core_engine['database'], 'w') as f:
f.write(filedata)
#
#with open('file.txt', 'r') as file :
# filedata = file.read()
# Replace the target string
#filedata = filedata.replace('ram', 'abcd')
# Write the file out again
#with open('file.txt', 'w') as file:
# file.write(filedata)
def table_append_analysis(line, Kvars, indexing):
gate = line.index("(") + 1
backdoor = line.index(")")
toEval = line[gate:backdoor]
values = toEval.split(',')
_db_values[_db_header[indexing][0]] = [] #Creates a empty list
for i in range(len(values)):
try:
values[i] = values[i].replace(' ', '')
except:
pass
if "'" in (values[i]):
values[i] = values[i].replace("'", '')
elif '"' in (values[i]):
values[i] = values[i].replace('"', '')
elif values[i] in Kvars:
values[i] = Kvars[values[i]][0]
try:
values[i] = values[i].replace('\n', '')
except:
pass
elif 'key=' in (values[i]):
pass
else:
pass
_db_values[_db_header[indexing][0]].append(values[i])
strln = indexing + ' '
#print(values)
if len(values) == (len(_db_header[indexing]) - 1):
for i in range(len(values)):
strln += str(values[i]) + ' '
handshake = False
with open(core_engine['databaseValue'], 'r') as f:
if strln in f.read():
handshake = True
if (handshake == True):
pass
else:
with open(core_engine['databaseValue'], 'a') as f:
f.write(strln)
f.write('\n')
else:
print(errors['error3'])
print(_db_header)
print(_db_values)
#-----------------------------------------------------------------
# The following functions are the only ones being used on 'body.py'
# The functions down bellow lack any key finding duplicate method!
def create_table_function(line, Kvars, table_name, internal_clock):
gate = line.index("(") + 1
backdoor = line.index(")")
toEval = line[gate:backdoor]
values = toEval.split(',')
_db_header[table_name] = ['@{}'.format(str(internal_clock))]
for i in range(len(values)):
try:
values[i] = values[i].replace(' ', '')
except:
pass
if ("'" in values[i]):
values[i] = values[i].replace("'", '')
elif ('"' in values[i]):
values[i] = values[i].replace('"', '')
else:
if values[i] in Kvars:
values[i] = Kvars[values[i]][0]
_db_header[table_name].append(values[i])
def check_global_key(body_line):
body_line = 'Null'
with open(core_engine['databaseValue'], 'r') as f:
for line in f:
elements = line.split(' ')
def Exadd_to_table_function_remove_temporary_file(tmp):
with open(tmp) as f:
with open(core_engine['databaseValue'], "w") as m:
for line in f:
m.write(line)
os.remove(tmp)
def Exadd_to_table_function_remove_duplicates():
tmp = core_engine['databaseValue'] + '{}'.format('.tmp')
lines_seen = set() # holds lines already seen
outfile = open(tmp, "w")
for line in open(core_engine['databaseValue'], "r"):
if (line not in lines_seen): # not a duplicate
outfile.write(line)
lines_seen.add(line)
outfile.close()
Exadd_to_table_function_remove_temporary_file(tmp)
def Exadd_to_table_function(_db_header, _db_values, values, table_name):
strln = table_name + ' '
if len(values) == (len(_db_header[table_name]) - 1):
for i in range(len(values)):
strln += str(values[i]) + ' '
with open(core_engine['databaseValue'], 'a') as f:
f.write(strln)
f.write('\n')
Exadd_to_table_function_remove_duplicates()
def add_to_table_function(line, Kvars, table_name):
gate = line.index("(") + 1
backdoor = line.index(")")
toEval = line[gate:backdoor]
values = toEval.split(',')
_db_values[_db_header[table_name][0]] = [] #Creates a empty list
for i in range(len(values)):
try:
values[i] = values[i].replace(' ', '')
except:
pass
if "'" in (values[i]):
values[i] = values[i].replace("'", '')
elif '"' in (values[i]):
values[i] = values[i].replace('"', '')
elif values[i] in Kvars:
values[i] = Kvars[values[i]][0]
try:
values[i] = values[i].replace('\n', '')
except:
pass
else:
errors['var_not_found'] #In the future the number of the line should be returned and the exact error!
_db_values[_db_header[table_name][0]].append(values[i])
Exadd_to_table_function(_db_header,_db_values,values, table_name)
#----------------------------------------------------------------------------------------------------------------
# I stopped Here! on 11/01/2019
def update_table_function(line, table_name, Kvars):
gate = line.index("(") + 1
backdoor = line.index(")")
entry_door = line.index("{")
garden_door = line.index(",")
key = line[entry_door+1:garden_door]
toChange = line[gate:backdoor]
print(toChange,',',key)
print(key)
try:
key = key.replace(' ', '')
except:
pass
try:
valuestChange = toChange.Replace('"', '')
except:
pass
try:
valuestChange = toChange.replace("'", '')
except:
pass
try:
valuestChange = valuestChange.split(',')
except:
pass
print(valuestChange)
for e in range(len(valuestChange)):
try:
currentPairOFvalues = valuestChange[e].split('=')
except:
print('Error - not defined!')
for K in Kvars:
if (re.search(r'{}'.format(K), currentPairOFvalues[1])):
currentPairOFvalues[1] = Kvars[K][0]
try:
currentPairOFvalues[1] = currentPairOFvalues[1].replace('\n', '')
except:
pass
try:
currentPairOFvalues[0] = currentPairOFvalues[0].replace(' ', '')
except:
pass
if currentPairOFvalues[0] in _db_header[table_name]:
log = [] # list on the go!
with open(core_engine['databaseValue'], 'r') as f:
for line in f:
values = line.split(' ')
print(values)
for e in range(len(values)):
if (values[0] == table_name):
pass
print(currentPairOFvalues)
#if (values[0] == )
#print('-----------------')
#print(currentPairOFvalues)
print('#------#')
print(_db_header)
print(_db_values)
#(re.search(r'{}'.format(K), toEval))
'''
if "'" in (key[0]):
key[0] = key[0].replace("'", '')
elif '"' in (key[0]):
key[0] = key[0].replace('"', '')
elif key[0] in Kvars:
key[0] = Kvars[key[0]][0]
'''
#print(key)