11import argparse
22from textwrap import fill as tfill
33
4- __all__ = [' argparse' , ' CreateNote' , ' CreateEncryptedNote' , ' DecryptNote' , ' ModifyNote' , ' ShowNote' ,
5- ' ListAll' , ' RemoveNote' , ' RemoveAll' ]
4+ __all__ = [" argparse" , " CreateNote" , " CreateEncryptedNote" , " DecryptNote" , " ModifyNote" , " ShowNote" ,
5+ " ListAll" , " RemoveNote" , " RemoveAll" ]
66
77
88def pretty_string (string ):
@@ -19,59 +19,62 @@ def __init__(self, cod, title, text):
1919 self .date = datetime .now ().strftime ("%d/%m/%Y %H:%M:%S" )
2020
2121 def __str__ (self ):
22- return f '''
23- ID: { self . cod } { self . date }
22+ return '''
23+ ID: {} {}
2424---------------------------------------------------------
25- Title: { self . title }
25+ Title: {}
2626
27- { self . text }
27+ {}
2828---------------------------------------------------------
29- '''
29+ ''' . format ( self . cod , self . date , self . title , self . text )
3030
3131 def shorter_str (self ):
32- return f '''
33- ID: { self . cod } { self . date }
32+ return '''
33+ ID: {} {}
3434---------------------------------------------------------
35- Title: { self . title }
35+ Title: {}
3636
37- { self . text [: 77 ] } ...
37+ {} ...
3838---------------------------------------------------------
39- '''
39+ ''' . format ( self . cod , self . date , self . title , self . text [: 77 ])
4040
4141
4242class CreateNote (argparse .Action ):
4343 def __call__ (self , parser , namespace , values , option_string = None ):
4444 import shelve
4545 with shelve .open ("./data/notes" ) as notes_db :
46- notes_db [' total' ] += 1
47- note_cod = f'n { notes_db ["total" ]} '
46+ notes_db [" total" ] += 1
47+ note_cod = "n{}" . format ( notes_db ["total" ])
4848 notes_db [note_cod ] = Note (note_cod , values [0 ], values [1 ])
49- print (f' \n { pretty_string (str (notes_db [note_cod ]))} ' )
49+ print ("{}" . format ( pretty_string (str (notes_db [note_cod ]))) )
5050
5151
5252class CreateEncryptedNote (argparse .Action ):
5353 def __call__ (self , parser , namespace , values , option_string = None ):
5454 from aes_note import AESCipher , shelve
5555 cipher = AESCipher (values [2 ])
5656 with shelve .open ("./data/notes" ) as notes_db :
57- notes_db [' total' ] += 1
58- note_cod = f'e { notes_db ["total" ]} '
59- notes_db [note_cod ] = Note (note_cod , values [0 ], cipher .encrypt (values [1 ]).decode (' UTF-8' ))
60- print (f' \n { pretty_string (str (notes_db [note_cod ]))} ' )
57+ notes_db [" total" ] += 1
58+ note_cod = "e{}" . format ( notes_db ["total" ])
59+ notes_db [note_cod ] = Note (note_cod , values [0 ], cipher .encrypt (values [1 ]).decode (" UTF-8" ))
60+ print ("{}" . format ( pretty_string (str (notes_db [note_cod ]))) )
6161
6262
6363class DecryptNote (argparse .Action ):
6464 def __call__ (self , parser , namespace , values , option_string = None ):
6565 from aes_note import AESCipher
66- cipher = AESCipher (values [1 ])
67- cipher .decrypt (values [0 ])
66+ if values [0 ][0 ] == 'e' :
67+ cipher = AESCipher (values [1 ])
68+ cipher .decrypt (values [0 ])
69+ else :
70+ print ("Encrypted notes start with 'e'" )
6871
6972
7073class ModifyNote (argparse .Action ):
7174 """ Modifies an available note selected by ID """
7275 def __call__ (self , parser , namespace , value , option_string = None ):
7376 from mod_note import mod_note
74- if value [0 ] != 'e' :
77+ if value [0 ] != "e" :
7578 mod_note (value )
7679 else :
7780 print ("You can't edit an encrypted note" )
@@ -85,45 +88,45 @@ def __call__(self, parser, namespace, values, option_string=None):
8588 if len (notes_db ) > 1 :
8689 for note_cod in values :
8790 if notes_db .pop (note_cod , - 1 ) == - 1 :
88- print (f" \n No note with ID: { note_cod } " )
91+ print ("No note with ID: {}" . format ( note_cod ) )
8992 else :
90- print (f" \n Note { note_cod } removed" )
93+ print ("Note { } removed". format ( note_cod ) )
9194 else :
92- print ("\n There are no notes available" )
95+ print ("There are no notes available" )
9396
9497
9598class RemoveAll (argparse .Action ):
9699 """ Deletes every note created """
97100 def __call__ (self , parser , namespace , values , option_string = None ):
98101 import shelve
99- with shelve .open ("./data/notes" , 'n' ) as notes_db :
100- notes_db [' total' ] = 0
101- print ("\n All notes removed" )
102+ with shelve .open ("./data/notes" , "n" ) as notes_db :
103+ notes_db [" total" ] = 0
104+ print ("All notes removed" )
102105
103106
104107class ShowNote (argparse .Action ):
105108 """ Searches for a note in the database by ID, Title and string in text """
106109 def __call__ (self , parser , namespace , value , option_string = None ):
107110 import shelve
108- with shelve .open (' ./data/notes' , 'r' ) as notes_db :
111+ with shelve .open (" ./data/notes" , "r" ) as notes_db :
109112 if len (notes_db ) > 1 :
110113 if value in notes_db :
111- print (f' \n { pretty_string (str (notes_db [value ]))} ' )
114+ print ("{}" . format ( pretty_string (str (notes_db [value ]))) )
112115 else :
113- print (f" \n There is no note with ID { value } " )
116+ print ("There is no note with ID {}" . format ( value ) )
114117 else :
115- print ("\n There are no notes available" )
118+ print ("There are no notes available" )
116119
117120
118121class ListAll (argparse .Action ):
119122 """ Shows every available note """
120123 def __call__ (self , parser , namespace , values , option_string = None ):
121124 import shelve
122- with shelve .open (' ./data/notes' , 'r' ) as notes_db :
125+ with shelve .open (" ./data/notes" , "r" ) as notes_db :
123126 if len (notes_db ) > 1 :
124- print ("\n \ t Press <Enter> to show the next note" )
127+ print ("\t Press <Enter> to show the next note" )
125128 for i in list (notes_db .values ())[1 :]:
126- print (f' \n { pretty_string (i .shorter_str ())} ' )
129+ print ("{}" . format ( pretty_string (i .shorter_str ())) )
127130 input ()
128131 else :
129- print ("\n There are no notes available" )
132+ print ("There are no notes available" )
0 commit comments