1
+ import os
2
+ import errors
3
+
4
+
5
+ def documentation ():
6
+ pass
7
+
8
+
9
+ def create_database (database , * args ):
10
+ '''
11
+ Console command
12
+ CREATE DATABASE DataBaseName
13
+ '''
14
+
15
+ try :
16
+
17
+ os .mkdir (f"databases/{ database } " ), os .mkdir (f"databases/{ database } /files" ), os .mkdir (f"databases/{ database } /tables" )
18
+
19
+ except FileExistsError :
20
+ return "Database already exists"
21
+
22
+ return f"Database \" { database } \" was created"
23
+
24
+
25
+ def use_database (database , * args ):
26
+ '''
27
+ Console command
28
+ USE DATABASE DataBaseName
29
+ '''
30
+
31
+ if os .path .exists (f"databases/{ database } /" ):
32
+ return [f"Currently working with database \" { database } \" " , database ]
33
+
34
+ raise errors .DataBaseNotFoundError (f"Database \" { database } \" not found!" )
35
+
36
+
37
+ def create_table (database , table , values , * args ):
38
+ '''
39
+ Console command
40
+ CREATE TABLE TableName(id: int, name: str, age: float, more...)
41
+ '''
42
+
43
+ if os .path .exists (f"databases/{ database } /tables/{ table } .txt" ):
44
+ return f"Table already exists!"
45
+
46
+ table = open (f"databases/{ database } /tables/{ table } .txt" , "a+" )
47
+ table .write (f"{ values } \n \n " )
48
+ table .close ()
49
+
50
+ return f"Table \" { table } \" was created!"
51
+
52
+
53
+ def add_content_to_table (database , table , * content ):
54
+ '''
55
+ Console command
56
+
57
+ ADD TableName VALUES (
58
+ (id, name, age, more...)
59
+ (id, name, age)
60
+ );
61
+
62
+ '''
63
+
64
+ try :
65
+
66
+ with open (f"databases/{ database } /tables/{ table } .txt" , "r" ) as file :
67
+
68
+ values = [line for line in file ][0 ]
69
+ values_dictionary = {}
70
+
71
+ for item in values [1 :- 2 ].split (", " ):
72
+
73
+ key , value = item .split (": " )
74
+ values_dictionary [key ] = value
75
+
76
+ with open (f"databases/{ database } /tables/{ table } .txt" , "a+" ) as write_file :
77
+
78
+ for content_list in content :
79
+
80
+ content_dict = {}
81
+
82
+ for index , item in enumerate (values_dictionary .keys ()):
83
+
84
+ content_dict [item ] = content_list [index ]
85
+
86
+ if type (content_dict [item ]) is int and values_dictionary [item ] == "'int'" or \
87
+ type (content_dict [item ]) is str and values_dictionary [item ] == "'str'" or \
88
+ type (content_dict [item ]) is float and values_dictionary [item ] == "'float'" :
89
+ continue
90
+
91
+ raise errors .ItemValueDifferentThanTheSetValue (f"item \" { item } \" is type \' { type (content_dict [item ])} \' and it must be \' { values_dictionary [item ]} \' " )
92
+
93
+ write_file .write (f"{ content_dict } \n " )
94
+
95
+ except Exception as e :
96
+ raise e
97
+
98
+ return "Content added to table!"
99
+
100
+
101
+ def create_file (database , file_name ):
102
+ '''
103
+ Console command
104
+ CREATE FILE FileName
105
+ '''
106
+
107
+ if os .path .exists (f"databases/{ database } /files/{ file_name } .txt" ):
108
+ return "File already exists"
109
+
110
+ file = open (f"databases/{ database } /files/{ file_name } .txt" , 'x' )
111
+ file .close ()
112
+
113
+ return f"File \" { file_name } \" was created!"
114
+
115
+
116
+ def write_in_file (database , file , * content ):
117
+ '''
118
+ Console command
119
+ WRITE IN FileName:
120
+ Something isn't right.
121
+ Some Messages!
122
+ content, content, content,
123
+ content, content,
124
+ content,
125
+ content,
126
+ content;;;
127
+ '''
128
+
129
+ if os .path .exists (f"databases/{ database } /files/{ file } .txt" ):
130
+ with open (f"databases/{ database } /files/{ file } .txt" , "a+" ) as f :
131
+ for line in content :
132
+ f .write (f"{ line } \n " )
133
+
134
+ return "Content added to file!"
135
+
136
+ return f"Database \" { database } \" or File \" { file } \" not found!"
137
+
138
+
139
+ def check_table_content (database , table , * border ):
140
+ '''
141
+ Console command
142
+
143
+ without border: GET ALL TableName
144
+ with border: GET 3 TableName # border equals 3, gets the first three lines
145
+ '''
146
+
147
+ if os .path .exists (f"databases/{ database } /tables/{ table } .txt" ):
148
+ file = open (f"databases/{ database } /tables/{ table } .txt" , "r" )
149
+
150
+ if border :
151
+ if type (border [0 ]) is int :
152
+ if border [0 ] < len ([line for line in file ]):
153
+ return [line for line in file [3 :border [0 ]+ 4 ]]
154
+
155
+ return [line for line in file [3 :]]
156
+
157
+ print ("Table not found!" )
158
+ return []
159
+
160
+
161
+ def check_file_content (database , file_name , * border ):
162
+ '''
163
+ Console command
164
+
165
+ without border: GET FILE FileName
166
+ with border: GET FILE FileName 3 # border equals 3, gets the first three lines
167
+ '''
168
+
169
+ if os .path .exists (f"databases/{ database } /files/{ file_name } .txt" ):
170
+ file = open (f"databases/{ database } /tables/{ file_name } .txt" , "r" )
171
+
172
+ if border :
173
+ if type (border [0 ]) is int :
174
+ if border [0 ] < len ([line for line in file ]):
175
+ return [line for line in file [:border [0 ] + 1 ]]
176
+
177
+ return [line for line in file ]
178
+
179
+ print ("File not found!" )
180
+ return []
181
+
182
+
183
+ def drop_database (* databases ):
184
+ '''
185
+ Console command
186
+
187
+ One DataBase:
188
+ FIRST WAY: DROP DB DataBaseName
189
+ SECOND WAY: DROP DATABASE DataBaseName
190
+
191
+ More Than One DataBases:
192
+ FIRST WAY: DROP DBS FirstDataBaseName SecondDataBaseName ThirdDataBaseName...
193
+ SECOND WAY: DROP DATABASES FirstDataBaseName SecondDataBaseName ThirdDataBaseName...
194
+ '''
195
+
196
+ for db in databases :
197
+ if os .path .exists (f"databases/{ db } /" ):
198
+ os .remove (f"databases/{ db } /" )
199
+
200
+ return "Database/s dropped!"
201
+
202
+
203
+ def drop_table (database , * tables ):
204
+ '''
205
+ Console command
206
+
207
+ One Table:
208
+ DROP TABLE TableName
209
+
210
+ More Than One Table:
211
+ DROP TABLES FirstTableName SecondTableName ThirdTableName...
212
+ '''
213
+
214
+ for table in tables :
215
+ if os .path .exists (f"databases/{ database } /tables/{ table } .txt" ):
216
+ os .remove (f"databases/{ database } /tables/{ table } .txt" )
217
+
218
+ return "Table/s dropped!"
219
+
220
+
221
+ def delete_file (database , * files ):
222
+ '''
223
+ Console command
224
+
225
+ One File:
226
+ DEL FILE TableName
227
+
228
+ More Than One File:
229
+ DEL FILES FirstFileName SecondFileName ThirdFileName...
230
+ '''
231
+
232
+ for file in files :
233
+ if os .path .exists (f"databases/{ database } /files/{ file } .txt" ):
234
+ os .remove (f"databases/{ database } /files/{ file } .txt" )
235
+
236
+ return "File/s deleted!"
237
+
238
+
239
+ def code_saver (user_input , code_file , new_line ):
240
+ '''
241
+ Saves the code in the code file.
242
+ '''
243
+
244
+ file = open (f"src/{ code_file } " , "a+" )
245
+ file .write (f"{ user_input } { new_line } " )
246
+ file .close ()
247
+
248
+
249
+ def run_program ():
250
+ while True :
251
+ file = input ("Create or choose file where to save the code from your console experience:\n " )
252
+
253
+ if not os .path .exists (f"src/{ file } .txt" ):
254
+ f = open (f"src/{ file } .txt" , "x" )
255
+ f .close ()
256
+
257
+ if file :
258
+ break
259
+
260
+ file = f"{ file } .txt"
261
+
262
+ while True :
263
+
264
+ operation_code = input ()
265
+ operation = operation_code .lower ().split ()
266
+
267
+ code_saver (operation_code , file , '\n ' )
268
+
269
+ if operation_code == "END" :
270
+ break
271
+
272
+ if operation_code == "docs" :
273
+ print (documentation ().__doc__ ())
274
+
275
+ if len (operation ) >= 3 :
276
+ if operation [- 1 ]:
277
+
278
+ if operation [:- 1 ] == ["create" , "database" ]:
279
+ print (create_database (operation [- 1 ]))
280
+
281
+ elif operation [:- 1 ] == ["use" , "database" ]:
282
+
283
+ db = use_database (operation [- 1 ])
284
+ print (db [0 ])
285
+ database = db [- 1 ]
286
+
287
+ elif operation [:2 ] == ["create" , "table" ]:
288
+
289
+ table_name = ' ' .join (operation [2 :])[:' ' .join (operation [2 :]).index ("(" )]
290
+ values = ' ' .join (operation [2 :])[' ' .join (operation [2 :]).index ("(" )+ 1 :' ' .join (operation [2 :]).index (")" )]
291
+ values_tuple = values .split (", " )
292
+ values_dict = {}
293
+
294
+ for items in values_tuple :
295
+
296
+ key , value = items .split (": " )
297
+ values_dict [key ] = value
298
+
299
+ print (create_table (database , table_name , values_dict ))
300
+
301
+ elif operation [0 ] == "add" and operation [- 2 ] == "values" :
302
+
303
+ table = operation [1 ]
304
+
305
+ if operation [- 1 ] == "(" :
306
+
307
+ lst_values = []
308
+ item = input ()
309
+
310
+ while item != ");" :
311
+
312
+ code_saver (item , file , '\n ' )
313
+ items = item [1 :- 1 ].split (", " )
314
+
315
+ for index in range (len (items )):
316
+
317
+ if len (items [index ].split ("." )) == 2 :
318
+ if items [index ].split ("." )[0 ].isdigit () and items [index ].split ("." )[1 ].isdigit ():
319
+ items [index ] = float (items [index ])
320
+
321
+ elif items [index ].isdigit ():
322
+ items [index ] = int (items [index ])
323
+
324
+ lst_values .append (items )
325
+ item = input ()
326
+
327
+ code_saver (item , file , '\n \n \n ' )
328
+ print (add_content_to_table (database , table , * lst_values ))
329
+
330
+ elif operation [:- 1 ] == ["create" "file" ]:
331
+ print (create_file (database , operation [- 1 ]))
332
+
333
+ elif operation [:- 1 ] == ["write" , "in" ]:
334
+
335
+ content = []
336
+ text = input ()
337
+
338
+ while text [- 3 :] != ";;;" :
339
+
340
+ code_saver (text , file , '\n ' )
341
+ content .append (text )
342
+ text = input ()
343
+
344
+ content .append (text )
345
+
346
+ if operation [- 1 ][- 1 ] == ':' :
347
+ print (write_in_file (database , operation [- 1 ][:- 1 ], content ))
348
+
349
+ else :
350
+ print (write_in_file (database , operation [- 1 ], content ))
351
+
352
+ code_saver (content , file , '\n \n \n ' )
353
+
354
+ elif operation [0 ] == "get" and operation [1 ] == "all" :
355
+
356
+ lines = check_table_content (database , operation [1 ])
357
+ print ()
358
+ [print (line ) for line in lines ]
359
+
360
+ elif operation [0 ] == "get" and operation [1 ].isdigit ():
361
+
362
+ lines = check_table_content (database , operation [- 1 ], int (operation [1 ]))
363
+ print ()
364
+ [print (line ) for line in lines ]
365
+
366
+ elif operation [:- 1 ] == ["get" , "file" ]:
367
+
368
+ lines = check_file_content (database , operation [- 1 ])
369
+ print ()
370
+ [print (line ) for line in lines ]
371
+
372
+ elif operation [:- 2 ] == ["get" , "file" ]:
373
+
374
+ lines = check_file_content (database , operation [- 2 ], int (operation [- 1 ]))
375
+ print ()
376
+ [print (line ) for line in lines ]
377
+
378
+ elif operation [:- 1 ] == ["drop" , "db" ] or operation [:- 1 ] == ["drop" , "database" ] or operation [:2 ] == \
379
+ ["drop" , "dbs" ] or operation [:2 ] == ["drop" , "databases" ]:
380
+
381
+ dbs = operation [2 :]
382
+ print (drop_database (* dbs ))
383
+
384
+ elif operation [:2 ] == ["drop" , "table" ] or operation [:2 ] == ["drop" , "tables" ]:
385
+ print (drop_table (database , * operation [2 :]))
386
+
387
+ elif operation [:2 ] == ["del" , "file" ] or operation [:2 ] == ["del" , "files" ]:
388
+ print (delete_file (database , * operation [2 :]))
389
+
390
+ code_saver ('\n // everything bellow is made on new run.' , file , '\n ' )
391
+
392
+
393
+ run_program ()
0 commit comments