55import pyperclip
66
77from .pw_json_client import SecretsDataJSONClient
8- from .pw_utils import generate_random_password
8+ from .pw_utils import generate_random_password , find_key
99from crypto .pw_encryption import SynchronousEncryption
1010
1111
@@ -18,27 +18,40 @@ def __init__(self, pw_client: SecretsDataJSONClient,
1818 self .args = args
1919
2020 def get_all_sections (self ) -> List [str ]:
21- return self .pw_client .get_sections ()
21+ """Get all sections of the secrets data file (json)."""
22+ return [pw for pw in self .pw_client .pw_dict .keys ()]
23+ # yield from self.pw_dict.keys()
2224
2325 def print_keys_of_section (self ):
26+ """Output all available keys of a section to the console."""
2427 if self .args .section is None :
2528 self .args .section = 'main'
26- self .pw_client .print_keys_of_section (self .args .section )
29+ for key in self .pw_client .pw_dict [self .args .section ].keys ():
30+ print (key )
2731
2832 def create_section (self ):
29- return self .pw_client .create_section (self .args .section )
33+ """Creates a new section."""
34+
35+ if self ._check_existence_of_section (self .args .section ):
36+ return print (f'Section { self .args .section } exists already.' )
37+
38+ self .pw_client .pw_dict [self .args .section ] = {}
39+ self .pw_client .save_dict_to_file ()
40+ print (f'Created a new section: "{ self .args .section } ".' )
41+
42+ def _check_existence_of_section (self , section : str ):
43+ if section in self .pw_client .pw_dict :
44+ return True
3045
3146 def add_new_secrets_data (self ):
32- args = self .args
33-
3447 secrets_data = {}
3548
36- if args .set_password :
37- new_password = args .set_password
49+ if self . args .set_password :
50+ new_password = self . args .set_password
3851 else :
3952 new_password = generate_random_password (
40- password_length = args .random_password_length ,
41- special_characters = args .no_special_characters )
53+ password_length = self . args .random_password_length ,
54+ special_characters = self . args .no_special_characters )
4255 pyperclip .copy (new_password )
4356 encrypted_password = self .crypto .encrypt (new_password )
4457 secrets_data ['password' ] = encrypted_password
@@ -63,39 +76,65 @@ def add_new_secrets_data(self):
6376 encrypted_value = self .crypto .encrypt (value )
6477 secrets_data [key ] = encrypted_value
6578
66- self .pw_client .add_new_secrets_data (entity = args .new_secrets_data ,
67- secrets_data = secrets_data ,
68- section = args .section ,
69- overwrite = args .overwrite )
79+ if self .args .section is None :
80+ self .args .section = 'main'
81+
82+ if not self ._check_existence_of_section (self .args .section ):
83+ self .create_section ()
84+
85+ if (
86+ self .args .entity in self .pw_client .pw_dict [self .args .section ]
87+ and self .args .overwrite is False
88+ ):
89+ print ('Entity is already there. Nothing happened.' )
90+ print ('Use the -ow / --overwrite option to update existing secrets data.' )
91+ return False
7092
93+ entity = self .args .new_secrets_data
94+ new_secrets_data = {entity : secrets_data }
95+
96+ print (f'Created new password for "{ entity } ".' )
97+ print ('' )
98+
99+ self .pw_client .pw_dict [self .args .section ].update (new_secrets_data )
100+ self .pw_client .save_dict_to_file ()
71101 return True
72102
73103 def update_secrets_data (self ):
74- """`pw -u <key>=<value> <entity>`"""
104+ """`pw -u <key>=<value> (-s <section>="main") <entity>`"""
75105 k , v = self .args .update .split ('=' )
76106 new_data = {k : self .crypto .encrypt (v )}
77- self .pw_client .update_secrets_data (
78- entity = self .args .entity ,
79- section = self .args .section ,
80- new_data = new_data )
107+
108+ if self .args .section is None :
109+ self .args .section = 'main'
110+
111+ self .pw_client .pw_dict [self .args .section ][self .args .entity ].update (new_data )
112+ self .pw_client .save_dict_to_file ()
81113
82114 def get_secrets_data (self ):
83- secrets_data = self .pw_client . get_secrets_data (
84- entity = self .args .entity ,
85- section = self .args .section )
115+ if self .args . section is None :
116+ self .args .section = 'main'
117+ secrets_data = self .pw_client . pw_dict [ self . args .section ][ self . args . entity ]
86118 return secrets_data
87119
88120 def remove_secrets_data (self ):
89121 key = self .args .remove_entity
90- self .pw_client .remove_secrets_data (key , self .args .section )
122+ section = self .args .section or 'main'
123+ self .pw_client .pw_dict [section ].pop (key )
124+ self .pw_client .save_dict_to_file ()
125+ print (f'Deleted { key } from { section } ' )
126+ print ('' )
91127 return True
92128
93129 def remove_section (self ):
94- self .pw_client .remove_section (self .args .remove_section )
130+ section = self .args .remove_section
131+ self .pw_client .pw_dict .pop (section )
132+ self .pw_client .save_dict_to_file ()
133+ print (f'Removed Section: "{ section } "' )
95134 return True
96135
97136 def find_secrets_data (self ):
98- results_as_generator = self . pw_client . find_key (self .args .find )
137+ results_as_generator = find_key (self .args .find , self . pw_client . pw_dict )
99138 results = list (results_as_generator )
100139 for result in results :
101140 print (f'Found "{ result ["entity" ]} " in section "{ result ["section" ]} ".' )
0 commit comments