@@ -99,6 +99,16 @@ def get_or_set_token(production=True):
9999 print ("Tokens do not match. Please try again." )
100100
101101
102+ # Delete the saved token file.
103+ def delete_saved_token ():
104+ token_file = os .path .join (caltechdata_directory , "token.txt" )
105+ if os .path .exists (token_file ):
106+ os .remove (token_file )
107+ print ("Token deleted successfully." )
108+ else :
109+ print ("No token found to delete." )
110+
111+
102112def welcome_message ():
103113 print ("Welcome to CaltechDATA CLI" )
104114
@@ -186,6 +196,61 @@ def get_funding_details():
186196 }
187197
188198
199+ # Add profile handling functions
200+ def save_profile ():
201+ profile_file = os .path .join (caltechdata_directory , "profile.json" )
202+
203+ # Get ORCID
204+ while True :
205+ orcid = get_user_input ("Enter your ORCID identifier: " )
206+ orcid = normalize_orcid (orcid )
207+ family_name , given_name = get_names (orcid )
208+ if family_name is not None and given_name is not None :
209+ break
210+ retry = input ("Do you want to try again? (y/n): " )
211+ if retry .lower () != "y" :
212+ return None
213+
214+ # Get funding details
215+ funding_references = []
216+ num_funding_entries = get_funding_entries ()
217+ for _ in range (num_funding_entries ):
218+ funding_references .append (get_funding_details ())
219+
220+ profile_data = {
221+ "orcid" : orcid ,
222+ "family_name" : family_name ,
223+ "given_name" : given_name ,
224+ "funding_references" : funding_references ,
225+ }
226+
227+ with open (profile_file , "w" ) as f :
228+ json .dump (profile_data , f , indent = 2 )
229+
230+ print ("Profile saved successfully!" )
231+ return profile_data
232+
233+
234+ def load_profile ():
235+ profile_file = os .path .join (caltechdata_directory , "profile.json" )
236+ try :
237+ with open (profile_file , "r" ) as f :
238+ return json .load (f )
239+ except FileNotFoundError :
240+ return None
241+
242+
243+ def get_or_create_profile ():
244+ profile = load_profile ()
245+ if profile :
246+ use_saved = input ("Use saved profile? (y/n): " ).lower ()
247+ if use_saved == "y" :
248+ return profile
249+
250+ print ("Creating new profile..." )
251+ return save_profile ()
252+
253+
189254def parse_arguments ():
190255 welcome_message ()
191256 args = {}
@@ -222,23 +287,17 @@ def parse_arguments():
222287 break
223288 else :
224289 print ("Invalid input. Please enter a number between 1 and 8." )
290+ # Load or create profile
291+ profile = get_or_create_profile ()
292+ if profile :
293+ args ["orcid" ] = profile ["orcid" ]
294+ args ["family_name" ] = profile ["family_name" ]
295+ args ["given_name" ] = profile ["given_name" ]
296+ args ["fundingReferences" ] = profile ["funding_references" ]
297+ else :
298+ print ("Failed to load or create profile. Exiting." )
299+ return None
225300
226- while True :
227- orcid = get_user_input ("Enter your ORCID identifier: " )
228- family_name , given_name = get_names (orcid )
229- if family_name is not None and given_name is not None :
230- args ["orcid" ] = orcid
231- break # Break out of the loop if names are successfully retrieved
232- retry = input ("Do you want to try again? (y/n): " )
233- if retry .lower () != "y" :
234- print ("Exiting program." )
235- return
236- # Optional arguments
237- num_funding_entries = get_funding_entries ()
238- funding_references = []
239- for _ in range (num_funding_entries ):
240- funding_references .append (get_funding_details ())
241- args ["fundingReferences" ] = funding_references
242301 return args
243302
244303
@@ -410,24 +469,48 @@ def parse_args():
410469 parser .add_argument (
411470 "-test" , action = "store_true" , help = "Use test mode, sets production to False"
412471 )
472+ parser .add_argument (
473+ "--delete-token" , action = "store_true" , help = "Delete the saved token."
474+ )
413475 args = parser .parse_args ()
414476 return args
415477
416478
479+ def normalize_orcid (val ):
480+ orcid_urls = ["https://orcid.org/" , "http://orcid.org/" , "orcid.org/" ]
481+ for orcid_url in orcid_urls :
482+ if val .startswith (orcid_url ):
483+ val = val [len (orcid_url ) :]
484+ break
485+
486+ val = val .replace ("-" , "" ).replace (" " , "" )
487+ if len (val ) != 16 or not val .isdigit ():
488+ raise ValueError (f"Invalid ORCID identifier: { val } " )
489+ return "-" .join ([val [0 :4 ], val [4 :8 ], val [8 :12 ], val [12 :16 ]])
490+
491+
417492def main ():
418493 args = parse_args ()
494+ production = not args .test
495+ if args .delete_token :
496+ delete_saved_token ()
497+ while True :
498+ choice = get_user_input (
499+ "What would you like to do? (create/edit/profile/exit): "
500+ ).lower ()
419501
420- production = not args .test # Set production to False if -test flag is provided
421-
422- choice = get_user_input (
423- "Do you want to create or edit a CaltechDATA record? (create/edit): "
424- ).lower ()
425- if choice == "create" :
426- create_record (production )
427- elif choice == "edit" :
428- edit_record (production )
429- else :
430- print ("Invalid choice. Please enter 'create' or 'edit'." )
502+ if choice == "create" :
503+ create_record (production )
504+ elif choice == "edit" :
505+ edit_record (production )
506+ elif choice == "profile" :
507+ save_profile ()
508+ elif choice == "exit" :
509+ break
510+ else :
511+ print (
512+ "Invalid choice. Please enter 'create', 'edit', 'profile', or 'exit'."
513+ )
431514
432515
433516def create_record (production ):
0 commit comments