11import os
22import shutil
33from colorama import Fore
4+ from documentation import documentation
45import errors
56
67
7- def documentation ():
8- return Fore .GREEN + f'''
9- Current Functionalities:
10-
11- - Creating DataBase
12- - Using/Changing DataBase
13- - Creating Tables and Files
14- - Writing in Tables and Files
15- - Checking the content of Tables and Files
16- - Deleting DataBases, Tables and Files
17- - Saves all the code in File.
18-
19- { Fore .MAGENTA + "All commands can be in upper or lower case!" }
20-
21- { Fore .GREEN + "1.How to create DataBase:" }
22- - At first, you'll be asked to use or create database,
23- - if you choose to create database, it'll just ask you for the name.
24- - Otherwise, if you want to create database while working,
25- - Use the current command: CREATE DATABASE DataBaseName
26-
27- 2.How to use/change database:
28- - At first, you'll be asked to use or create database,
29- - if you choose to use database, it'll just ask you for the name.
30- - Otherwise, if you want to change the database you're working with,
31- - Use the current command: USE DATABASE DataBaseName
32-
33- 3.How to create a table and save information in it:
34- - To create a table, you need to use the main keywords "CREATE TABLE",
35- - And then, the name of the table, containing information about the storage,
36- - Example: TableName(id: int, name: str)
37- - Console command: CREATE TABLE TableName(id: int, name: str, age: float, more...)
38- - To write in table, you can see this Example:
39-
40- { Fore .CYAN + "ADD TableName VALUES (" }
41- (id, name, age, more...)
42- (id, name, age)
43- );
44-
45- { Fore .GREEN + "4.How to create file and write in it:" }
46- - To create a file, use the following command: CREATE FILE FileName
47- - To write in file, you need to start with the keywords "WRITE IN FileName:",
48- - And then, write whatever you want on every new line.
49- - To stop writing, you need to use ";;;" at the end
50- "WARNING": """The ;;; get also saved in the txt file, so if you don't want them attached to your text,
51- you might write them on new line!
52- """
53- - Write Example:
54-
55-
56- { Fore .CYAN + "WRITE IN FileName:" }
57- Something isn't right.
58- Some Messages!
59- content, content, content,
60- content, content,
61- content,
62- content,
63- content;;;
64-
65- { Fore .GREEN + "5.How to see the content of my Tables and Files:" }
66- - Use this command for Tables: GET ALL TableName
67- - Use this command for Files: GET FILE FileName
68-
69- 6.How to delete DataBases, Tables and Files:
70- - Delete DataBase/s:
71-
72- { Fore .MAGENTA + "One DataBase:" }
73- FIRST WAY: DROP DB DataBaseName
74- SECOND WAY: DROP DATABASE DataBaseName
75-
76- More Than One DataBases:
77- FIRST WAY: DROP DBS FirstDataBaseName SecondDataBaseName ThirdDataBaseName...
78- SECOND WAY: DROP DATABASES FirstDataBaseName SecondDataBaseName ThirdDataBaseName...
79-
80- { Fore .GREEN + "- Delete Tables:" }
81-
82- { Fore .MAGENTA + "One Table:" }
83- DROP TABLE TableName
84-
85- More Than One Table:
86- DROP TABLES FirstTableName SecondTableName ThirdTableName...
87-
88- { Fore .GREEN + "- Delete Files:" }
89-
90- { Fore .MAGENTA + "One File:" }
91- DEL FILE FileName
92-
93- More Than One File:
94- DEL FILES FirstFileName SecondFileName ThirdFileName...
95-
96-
97-
98- { Fore .LIGHTGREEN_EX + "7.How to save the code?" }
99- - The code is saving by itself in the chosen at the beginning by you file, to change the file
100- you must stop the program and rerun it. The file can be found in the same directory "src/filename"
101-
102-
103- Submit issues and questions here: https://github.com/MitkoVtori/Python-ConsoleSQL/issues/new
104-
105- '''
106- 107- 1088def create_database (database , * args ):
1099 '''
11010 Console command
@@ -116,9 +16,9 @@ def create_database(database, *args):
11616 os .mkdir (f"databases/{ database } " ), os .mkdir (f"databases/{ database } /files" ), os .mkdir (f"databases/{ database } /tables" )
11717
11818 except FileExistsError :
119- return Fore .WHITE + "Database already exists"
19+ return Fore .LIGHTWHITE_EX + "Database already exists"
12020
121- return Fore .WHITE + f"Database \" { database } \" was created"
21+ return Fore .LIGHTWHITE_EX + f"Database \" { database } \" was created"
12222
12323
12424def use_database (database , * args ):
@@ -128,9 +28,9 @@ def use_database(database, *args):
12828 '''
12929
13030 if os .path .exists (f"databases/{ database } /" ):
131- return [Fore .WHITE + f"Currently working with database \" { database } \" " , database ]
31+ return [Fore .LIGHTWHITE_EX + f"Currently working with database \" { database } \" " , database ]
13232
133- raise errors .DataBaseNotFoundError (Fore . WHITE + f"Database \" { database } \" not found!" )
33+ raise errors .DataBaseNotFoundError (f"Database \" { database } \" not found!" )
13434
13535
13636def create_table (database , table , values , * args ):
@@ -140,13 +40,13 @@ def create_table(database, table, values, *args):
14040 '''
14141
14242 if os .path .exists (f"databases/{ database } /tables/{ table } .txt" ):
143- return Fore .WHITE + f"Table already exists!"
43+ return Fore .LIGHTWHITE_EX + f"Table already exists!"
14444
14545 table = open (f"databases/{ database } /tables/{ table } .txt" , "a+" )
14646 table .write (f"{ values } \n \n " )
14747 table .close ()
14848
149- return Fore .WHITE + f"Table \" { table } \" was created!"
49+ return Fore .LIGHTWHITE_EX + f"Table \" { table . name . split ( '/' )[ - 1 ][: - 4 ] } \" was created!"
15050
15151
15252def add_content_to_table (database , table , * content ):
@@ -194,7 +94,7 @@ def add_content_to_table(database, table, *content):
19494 except Exception as e :
19595 raise e
19696
197- return Fore .WHITE + "Content added to table!"
97+ return Fore .LIGHTWHITE_EX + "Content added to table!"
19898
19999
200100def create_file (database , file_name ):
@@ -204,35 +104,38 @@ def create_file(database, file_name):
204104 '''
205105
206106 if os .path .exists (f"databases/{ database } /files/{ file_name } .txt" ):
207- return Fore .WHITE + "File already exists"
107+ return Fore .LIGHTWHITE_EX + "File already exists"
208108
209109 file = open (f"databases/{ database } /files/{ file_name } .txt" , 'x' )
210110 file .close ()
211111
212- return Fore .WHITE + f"File \" { file_name } \" was created!"
112+ return Fore .LIGHTWHITE_EX + f"File \" { file_name } \" was created!"
213113
214114
215115def write_in_file (database , file , * content ):
216116 '''
217117 Console command
218118 WRITE IN FileName:
219119 Something isn't right.
120+
220121 Some Messages!
221122 content, content, content,
123+
222124 content, content,
223125 content,
224126 content,
225- content;;;
127+ content
128+ ;;;
226129 '''
227130
228131 if os .path .exists (f"databases/{ database } /files/{ file } .txt" ):
229132 with open (f"databases/{ database } /files/{ file } .txt" , "a+" ) as f :
230133 for line in content :
231134 f .write (f"{ line } \n " )
232135
233- return Fore .WHITE + "Content added to file!"
136+ return Fore .LIGHTWHITE_EX + "Content added to file!"
234137
235- return Fore .WHITE + f"Database \" { database } \" or File \" { file } \" not found!"
138+ return Fore .LIGHTWHITE_EX + f"Database \" { database } \" or File \" { file } \" not found!"
236139
237140
238141def check_table_content (database , table , * args ):
@@ -244,9 +147,9 @@ def check_table_content(database, table, *args):
244147 if os .path .exists (f"databases/{ database } /tables/{ table } .txt" ):
245148 file = open (f"databases/{ database } /tables/{ table } .txt" , "r" )
246149
247- return [Fore .WHITE + line for line in file ][2 :]
150+ return [Fore .LIGHTWHITE_EX + line for line in file ][2 :]
248151
249- print (Fore .WHITE + "Table not found!" )
152+ print (Fore .LIGHTWHITE_EX + "Table not found!" )
250153 return []
251154
252155
@@ -259,12 +162,59 @@ def check_file_content(database, file_name, *border):
259162 if os .path .exists (f"databases/{ database } /files/{ file_name } .txt" ):
260163 file = open (f"databases/{ database } /files/{ file_name } .txt" , "r" )
261164
262- return [Fore .WHITE + line for line in file ]
165+ return [Fore .LIGHTWHITE_EX + line for line in file ]
263166
264- print ("File not found!" )
167+ print (Fore . LIGHTWHITE_EX + "File not found!" )
265168 return []
266169
267170
171+ def delete_lines (database , path , file_name , * lines ):
172+ '''
173+ Console command
174+ DEL TABLE/FILE FileName LINES firstline secondline seventhline
175+ '''
176+ 177+ if path == "table" :
178+ 179+ if os .path .exists (f"databases/{ database } /tables/{ file_name } .txt" ):
180+ 181+ file = [line [:- 1 ] if line [- 1 ] == '\n ' else line for line in open (f"databases/{ database } /tables/{ file_name } .txt" , "r" )]
182+ 183+ for num , line in enumerate (lines ):
184+ if 0 <= (line + 1 )- num < len (file ):
185+ file .pop ((line + 1 )- num )
186+ 187+ os .remove (f"databases/{ database } /tables/{ file_name } .txt" )
188+ f = open (f"databases/{ database } /tables/{ file_name } .txt" , "a+" )
189+ 190+ for line in file :
191+ f .write (f"{ line } \n " )
192+ 193+ f .close ()
194+ 195+ elif path == "file" :
196+ if os .path .exists (f"databases/{ database } /files/{ file_name } .txt" ):
197+ 198+ file = [line [:- 1 ] if line [- 1 ] == '\n ' else line for line in open (f"databases/{ database } /files/{ file_name } .txt" , "r" )]
199+ 200+ for num , line in enumerate (lines ):
201+ if 0 <= (line - 1 ) - num < len (file ):
202+ file .pop ((line - 1 ) - num )
203+ 204+ os .remove (f"databases/{ database } /files/{ file_name } .txt" )
205+ f = open (f"databases/{ database } /files/{ file_name } .txt" , "a+" )
206+ 207+ for line in file :
208+ f .write (f"{ line } \n " )
209+ 210+ f .close ()
211+ 212+ else :
213+ return Fore .LIGHTWHITE_EX + f"Invalid path name '{ path } '"
214+ 215+ return Fore .LIGHTWHITE_EX + "lines removed!"
216+ 217+ 268218def drop_database (* databases ):
269219 '''
270220 Console command
@@ -282,7 +232,7 @@ def drop_database(*databases):
282232 if os .path .exists (f"databases/{ db } /" ):
283233 shutil .rmtree (f"databases/{ db } /" )
284234
285- return Fore .WHITE + "Database/s dropped!"
235+ return Fore .LIGHTWHITE_EX + "Database/s dropped!"
286236
287237
288238def drop_table (database , * tables ):
@@ -300,7 +250,7 @@ def drop_table(database, *tables):
300250 if os .path .exists (f"databases/{ database } /tables/{ table } .txt" ):
301251 os .remove (f"databases/{ database } /tables/{ table } .txt" )
302252
303- return Fore .WHITE + "Table/s dropped!"
253+ return Fore .LIGHTWHITE_EX + "Table/s dropped!"
304254
305255
306256def delete_file (database , * files ):
@@ -318,7 +268,7 @@ def delete_file(database, *files):
318268 if os .path .exists (f"databases/{ database } /files/{ file } .txt" ):
319269 os .remove (f"databases/{ database } /files/{ file } .txt" )
320270
321- return Fore .WHITE + "File/s deleted!"
271+ return Fore .LIGHTWHITE_EX + "File/s deleted!"
322272
323273
324274def code_saver (user_input , code_file , new_line ):
@@ -332,28 +282,28 @@ def code_saver(user_input, code_file, new_line):
332282
333283
334284def run_program ():
335- see_documentation = input (Fore .WHITE + "Wanna see the documentation? 'yes' or 'no': " )
285+ see_documentation = input (Fore .LIGHTWHITE_EX + "Wanna see the documentation? 'yes' or 'no': " )
336286
337287 if see_documentation .lower () == "yes" :
338288 print (documentation ())
339289
340290 while True :
341- db = input (Fore .WHITE + "create or use database: " )
291+ db = input (Fore .LIGHTWHITE_EX + "create or use database: " )
342292
343293 if db == 'create' :
344- create_db = input (Fore .WHITE + "database name: " )
294+ create_db = input (Fore .LIGHTWHITE_EX + "database name: " )
345295 create_database (create_db )
346296 d = use_database (create_db )
347297 break
348298
349299 elif db == "use" :
350- d = use_database (input (Fore .WHITE + "database name: " ))[- 1 ]
300+ d = use_database (input (Fore .LIGHTWHITE_EX + "database name: " ))[- 1 ]
351301 break
352302
353303 database = d
354304
355305 while True :
356- file = input (Fore .WHITE + "Create or choose file where to save the code from your console experience:\n " )
306+ file = input (Fore .LIGHTWHITE_EX + "Create or choose file where to save the code from your console experience:\n " )
357307
358308 if not os .path .exists (f"src/{ file } .txt" ):
359309 f = open (f"src/{ file } .txt" , "x" )
@@ -448,15 +398,13 @@ def run_program():
448398 content .append (text )
449399 text = input ()
450400
451- content .append (text )
452- 453401 if operation [- 1 ][- 1 ] == ':' :
454402 print (write_in_file (database , operation [- 1 ][:- 1 ], * content ))
455403
456404 else :
457405 print (write_in_file (database , operation [- 1 ], * content ))
458406
459- code_saver (content [ - 1 ], file , '\n \n \n ' )
407+ code_saver (text [ - 3 : ], file , '\n \n \n ' )
460408
461409 elif operation [0 ] == "get" and operation [1 ] == "all" :
462410
@@ -470,6 +418,15 @@ def run_program():
470418 print ()
471419 [print (line ) for line in lines ]
472420
421+ elif len (operation ) >= 5 :
422+ if operation [0 ] == "del" and operation [3 ] == "lines" :
423+ try :
424+ 425+ print (delete_lines (database , operation [1 ], operation [2 ], * [int (l ) for l in operation [4 :]]))
426+ 427+ except ValueError :
428+ raise errors .InvalidLineError ("line must be integer" )
429+ 473430 elif operation [:- 1 ] == ["drop" , "db" ] or operation [:- 1 ] == ["drop" , "database" ] or operation [:2 ] == \
474431 ["drop" , "dbs" ] or operation [:2 ] == ["drop" , "databases" ]:
475432
@@ -480,6 +437,6 @@ def run_program():
480437 print (drop_table (database , * operation [2 :]))
481438
482439 elif operation [:2 ] == ["del" , "file" ] or operation [:2 ] == ["del" , "files" ]:
483- print (delete_file (database , * operation [2 :]))
440+ if "lines" not in operation :
441+ print (delete_file (database , * operation [2 :]))
484442
485- code_saver ('\n // everything bellow is made on new run.' , file , '\n ' )
0 commit comments