1010from pydantic .v1 import PrivateAttr
1111
1212from osw .custom_types import PossibleFilePath
13+ from osw .defaults import paths as default_paths
1314from osw .model .static import OswBaseModel
1415
15- CREDENTIALS_FN_DEFAULT = "credentials.pwd.yaml"
16-
1716
1817class CredentialManager (OswBaseModel ):
1918 """Handles credentials"""
@@ -253,15 +252,16 @@ def save_credentials_to_file(
253252 If True, the cred_filepath is set to the given filepath. If False, the
254253 cred_filepath of the CredentialManager is not changed.
255254 """
256- filepath_ = [filepath ]
255+ cred_filepaths = [filepath ]
256+ """The filepath to save the credentials to."""
257257 if filepath is None :
258- filepath_ = self .cred_filepath
258+ cred_filepaths = self .cred_filepath
259259 if self .cred_filepath is None :
260- filepath_ = [Path . cwd () / CREDENTIALS_FN_DEFAULT ]
260+ cred_filepaths = [default_paths . cred_fp ]
261261 if set_cred_filepath :
262262 # Creates error if file does not exist -> Using custom FilePath
263- self .cred_filepath = filepath_
264- for fp in filepath_ :
263+ self .cred_filepath = cred_filepaths
264+ for fp in cred_filepaths :
265265 file = Path (fp )
266266 if not file .parent .exists ():
267267 file .parent .mkdir (parents = True )
@@ -282,30 +282,61 @@ def save_credentials_to_file(
282282
283283 # Creating or updating .gitignore file in the working directory
284284 cwd = Path .cwd ()
285- potential_fp = [cwd / ".gitignore" , cwd .parent / ".gitignore" ]
286- write_to_fp = potential_fp [0 ]
285+ potential_fp = [
286+ cwd / ".gitignore" ,
287+ cwd .parent / ".gitignore" ,
288+ ]
289+ gitignore_fp = potential_fp [0 ]
290+ # Stops if a .gitignore file is found
287291 for fp in potential_fp :
288292 if fp .exists ():
289- write_to_fp = fp
293+ gitignore_fp = fp
290294 break
291- if not write_to_fp .exists ():
292- if not write_to_fp .parent .exists ():
293- write_to_fp .parent .mkdir (parents = True )
294- write_to_fp .touch ()
295- with open (write_to_fp , "r" ) as stream :
295+ # Creates a .gitignore file if none is found
296+ if not gitignore_fp .exists ():
297+ if not gitignore_fp .parent .exists ():
298+ gitignore_fp .parent .mkdir (parents = True )
299+ gitignore_fp .touch ()
300+ # Reads the .gitignore file
301+ with open (gitignore_fp , "r" ) as stream :
296302 content = stream .read ()
297303 comment_set = False
298- for _ii , fp in enumerate (filepath_ ):
299- if fp .name not in content :
300- print (f"Adding '{ fp .name } ' to gitignore file '{ write_to_fp } '." )
301- with open (write_to_fp , "a" ) as stream :
302- if comment_set :
304+ osw_dir_added = False
305+ # For every file path in the list of credentials file paths
306+ for _ii , fp in enumerate (cred_filepaths ):
307+ to_add = ""
308+ if default_paths .osw_files_dir in fp .parents and not osw_dir_added :
309+ print (
310+ f"Adding '{ default_paths .osw_files_dir } ' to gitignore file "
311+ f"'{ gitignore_fp } '."
312+ )
313+ containing_gitignore = gitignore_fp .parent .absolute ()
314+
315+ if containing_gitignore in default_paths .osw_files_dir .parents :
316+ # If the default_path.osw_files_dir is a subdirectory of the directory
317+ # containing the .gitignore file, add the relative path to the
318+ # .gitignore file
319+ rel = default_paths .osw_files_dir .relative_to (containing_gitignore )
320+ to_add = f"\n *{ str (rel .as_posix ())} /*"
321+ else :
322+ # Test if the default_path.osw_files_dir is a subdirectory of the
323+ # directory containing the .gitignore file
324+ to_add = f"\n *{ default_paths .osw_files_dir .absolute ().as_posix ()} /*"
325+ osw_dir_added = True
326+ elif fp .name not in content :
327+ print (f"Adding '{ fp .name } ' to gitignore file '{ gitignore_fp } '." )
328+ to_add = f"\n *{ fp .name } "
329+ # If to_add is not empty, write to .gitignore file
330+ if to_add :
331+ with open (gitignore_fp , "a" ) as stream :
332+ # Only add comment if not already set
333+ if not comment_set :
303334 stream .write (
304335 "\n # Automatically added by osw.auth.CredentialManager."
305336 "save_credentials_to_file:"
306337 )
307338 comment_set = True
308- stream .write (f" \n * { fp . name } " )
339+ stream .write (to_add )
309340
310341
311342CredentialManager .CredentialConfig .update_forward_refs ()
0 commit comments