1313from rich .text import Text
1414from rich .panel import Panel
1515from rich .prompt import Prompt , Confirm
16+
1617# For working with postgres
1718import psycopg2
1819from psycopg2 import Error
1920import json
2021import string
2122import random
23+
2224console = Console ()
2325
2426try :
3335 # If you are building something serious, use a more secure method
3436 # Like a .env file
3537 # The creds.json is only being used to make the code more readable
36- connection = psycopg2 .connect (user = str (secrets ["username" ]),
37- password = str (secrets ["password" ]),
38- host = str (secrets ["host" ]),
39- port = str (secrets ["port" ]),
40- database = str (secrets ["database" ]))
41-
38+ connection = psycopg2 .connect (
39+ user = str (secrets ["username" ]),
40+ password = str (secrets ["password" ]),
41+ host = str (secrets ["host" ]),
42+ port = str (secrets ["port" ]),
43+ database = str (secrets ["database" ]),
44+ )
45+
4246 # Get the info of the connection using the get_dsn_parameters() method
4347 info = connection .get_dsn_parameters ()
4448 except :
4549 console .print (
46- ":red_circle:Sorry, something went wrong! Maybe a wrong credential? Check stuff.json's info correctly" )
50+ ":red_circle:Sorry, something went wrong! Maybe a wrong credential? Check stuff.json's info correctly"
51+ )
4752
4853 # Define a new cursor object by calling the cursor() method
4954 # This is the object we use to interact with the database
5055 # Think of it as a pointer to a specific place in the database
5156 cursor = connection .cursor ()
5257
5358 print (
54- f'{ info ["user" ]} is connected to PostgreSQL server on { info ["host" ]} :{ info ["port" ]} ' )
59+ f'{ info ["user" ]} is connected to PostgreSQL server on { info ["host" ]} :{ info ["port" ]} '
60+ )
5561
5662 # Generate hash for assigning to a entry
5763 def hash_gen ():
@@ -69,73 +75,83 @@ def hash_gen():
6975 # The name, url, author have to be filled
7076 # Rest is auto generated by the program and the database
7177 def check_table ():
72- cursor .execute ("""
78+ cursor .execute (
79+ """
7380 CREATE TABLE IF NOT EXISTS dbview(
7481 name TEXT NOT NULL,
7582 url TEXT NOT NULL,
7683 author TEXT,
7784 hash TEXT NOT NULL,
7885 date TIMESTAMP
7986 );
80- """ )
87+ """
88+ )
8189 console .log (":green_circle: Connected to Table :man: dbview" )
8290
8391 # Insert a new entry into the table
8492 # The hash is auto generated
8593 def insert (name , url , author ):
86- cursor .execute (f"""
94+ cursor .execute (
95+ f"""
8796 INSERT INTO dbview (name, url, author, hash, date) VALUES ('{ name } ', '{ url } ', '{ author } ', '{ str (hash_gen ())} ', CURRENT_TIMESTAMP)
88- """ )
97+ """
98+ )
8999 search ("name" , name )
90100
91101 # View the table using a rich table
92102 def table ():
93103 cursor .execute ("SELECT * from dbview" )
94104 records = cursor .fetchall ()
95105 try :
96- table = Table (title = Panel (
97- f'Query DB [red]{ info ["host" ]} :{ info ["port" ]} ' ), show_header = True , header_style = "bold green" )
106+ table = Table (
107+ title = Panel (f'Query DB [red]{ info ["host" ]} :{ info ["port" ]} ' ),
108+ show_header = True ,
109+ header_style = "bold green" ,
110+ )
98111 table .add_column ("Name" , style = "purple" , width = 20 )
99112 table .add_column ("Url" , style = "yellow" )
100113 table .add_column ("Author" , style = "cyan" )
101114 table .add_column ("Hash" , style = "dim" )
102115 table .add_column ("Date" , no_wrap = True , style = "dim blue" )
103116 for record in records :
104117 table .add_row (
105- record [0 ],
106- record [1 ],
107- record [2 ],
108- record [3 ],
109- str (record [4 ])
118+ record [0 ], record [1 ], record [2 ], record [3 ], str (record [4 ])
110119 )
111120 console .print (table )
112121 except :
113122 # Some terminals don't support rich
114123 print ("Name\t \t | URL\t \t | Channel\t \t | Hash\t \t | Date" )
115124 for record in records :
116125 print (
117- f'{ record [0 ]} \t \t | { record [1 ]} \t \t | { record [2 ]} \t \t | { record [3 ]} \t \t | { str (record [4 ])} \t \t ' )
126+ f"{ record [0 ]} \t \t | { record [1 ]} \t \t | { record [2 ]} \t \t | { record [3 ]} \t \t | { str (record [4 ])} \t \t "
127+ )
118128
119129 # Edit a entry in the table
120130 def edit (key , value ):
121131 console .print (
122- "Enter The New Values to Update in the form of a name url channel" )
132+ "Enter The New Values to Update in the form of a name url channel"
133+ )
123134 new_record = str (Prompt .ask ("Enter Here: " ))
124135 new_records = new_record .split ()
125136 # Execute the query to update the table
126137 cursor .execute (
127- f"UPDATE dbview SET name='{ new_records [0 ]} ', url='{ new_records [1 ]} ', author='{ new_records [2 ]} ' WHERE { key } ='{ value } '" )
138+ f"UPDATE dbview SET name='{ new_records [0 ]} ', url='{ new_records [1 ]} ', author='{ new_records [2 ]} ' WHERE { key } ='{ value } '"
139+ )
128140 console .log (":green_circle: Successfully Updated Database" )
129141 table ()
130142
131143 # Get the hash of a entry
132144 def hash ():
133145 console .print ("Enter A Key and Value to get Hash" )
134- record_key = str (Prompt .ask ("Enter a Key" , choices = [
135- "name" , "url" , "author" , "hash" , "date" ], default = "name" ))
146+ record_key = str (
147+ Prompt .ask (
148+ "Enter a Key" ,
149+ choices = ["name" , "url" , "author" , "hash" , "date" ],
150+ default = "name" ,
151+ )
152+ )
136153 record_value = str (Prompt .ask ("Enter a Value" ))
137- cursor .execute (
138- f"SELECT * from dnview WHERE { record_key } ='{ record_value } '" )
154+ cursor .execute (f"SELECT * from dnview WHERE { record_key } ='{ record_value } '" )
139155 records = cursor .fetchone ()
140156 console .print (f"The Hash is { records [3 ]} " )
141157
@@ -149,12 +165,14 @@ def delete(key, value):
149165 # Drop the table
150166 def drop ():
151167 console .print (
152- "Are you sure you want to Drop the database? This will delete the whole table" )
168+ "Are you sure you want to Drop the database? This will delete the whole table"
169+ )
153170 confirmation = Confirm .ask ("Are you sure" )
154171 if confirmation :
155172 cursor .execute ("DROP table dbview;" )
156173 console .print (
157- "Dropped Table dbview: Restart the application to start a new main table" )
174+ "Dropped Table dbview: Restart the application to start a new main table"
175+ )
158176 else :
159177 console .print ("Skipped the drop operation" )
160178
@@ -165,30 +183,33 @@ def drop():
165183 def search (key , value ):
166184 cursor .execute (f"SELECT * from dbview WHERE { key } ='{ value } '" )
167185 records = cursor .fetchall ()
168- table = Table (title = Panel (
169- f'Query DB [red]{ info ["host" ]} :{ info ["port" ]} ' ), show_header = True , header_style = "bold green" )
186+ table = Table (
187+ title = Panel (f'Query DB [red]{ info ["host" ]} :{ info ["port" ]} ' ),
188+ show_header = True ,
189+ header_style = "bold green" ,
190+ )
170191 table .add_column ("Name" , style = "purple" , width = 20 )
171192 table .add_column ("Url" , style = "yellow" )
172193 table .add_column ("Channel" , style = "cyan" )
173194 table .add_column ("Hash" , style = "dim" )
174195 table .add_column ("Date" , no_wrap = True , style = "dim blue" )
175196 for record in records :
176- table .add_row (
177- record [0 ],
178- record [1 ],
179- record [2 ],
180- record [3 ],
181- str (record [4 ])
182- )
197+ table .add_row (record [0 ], record [1 ], record [2 ], record [3 ], str (record [4 ]))
183198 console .print (table )
199+
184200 loop = 1
185201 # 12 is a rather arbitrary number
186202 # I know. But after a while, you don't want the program to run forever
187203 while loop != 12 :
188204 check_table ()
189205 # Just the normal CRUD operations
190- action = str (Prompt .ask ("Enter your name" , choices = [
191- "view" , "new" , "edit" , "delete" , "drop" , "quit" , "hash" ], default = "view" ))
206+ action = str (
207+ Prompt .ask (
208+ "Enter your name" ,
209+ choices = ["view" , "new" , "edit" , "delete" , "drop" , "quit" , "hash" ],
210+ default = "view" ,
211+ )
212+ )
192213 if action == "view" :
193214 table ()
194215 if action == "new" :
@@ -197,15 +218,25 @@ def search(key, value):
197218 author = Prompt .ask ("Enter The Author" )
198219 insert (name , url , author )
199220 if action == "delete" :
200- record_key = str (Prompt .ask ("Enter the key" , choices = [
201- "name" , "url" , "author" , "hash" , "date" ], default = "name" ))
221+ record_key = str (
222+ Prompt .ask (
223+ "Enter the key" ,
224+ choices = ["name" , "url" , "author" , "hash" , "date" ],
225+ default = "name" ,
226+ )
227+ )
202228 record_value = str (Prompt .ask ("Enter the Value" ))
203229 delete (record_key , record_value )
204230 if action == "drop" :
205231 drop ()
206232 if action == "edit" :
207- record_key = str (Prompt .ask ("Enter the key" , choices = [
208- "name" , "url" , "author" , "hash" , "date" ], default = "name" ))
233+ record_key = str (
234+ Prompt .ask (
235+ "Enter the key" ,
236+ choices = ["name" , "url" , "author" , "hash" , "date" ],
237+ default = "name" ,
238+ )
239+ )
209240 record_value = str (Prompt .ask ("Enter the Value" ))
210241 edit (record_key , record_value )
211242 if action == "hash" :
@@ -218,8 +249,8 @@ def search(key, value):
218249 print ("Error while connecting to PostgreSQL" , error )
219250# We finally close the connection
220251finally :
221- if ( connection ) :
252+ if connection :
222253 connection .commit ()
223254 cursor .close ()
224255 connection .close ()
225- console .print (":red_heart: Thanks for using db.py" )
256+ console .print (":red_heart: Thanks for using db.py" )
0 commit comments