@@ -89,7 +89,6 @@ def get_or_set_token(production=True):
8989 print ("Tokens do not match. Please try again." )
9090
9191
92-
9392def welcome_message ():
9493 print ("Welcome to CaltechDATA CLI" )
9594
@@ -383,22 +382,22 @@ def upload_data_from_file():
383382 except json .JSONDecodeError as e :
384383 print (f"Error: Invalid JSON format in the file '{ filename } '. { str (e )} " )
385384
385+
386386def parse_args ():
387387 """Parse command-line arguments."""
388388 parser = argparse .ArgumentParser (description = "CaltechDATA CLI tool." )
389389 parser .add_argument (
390- "-test" ,
391- action = "store_true" ,
392- help = "Use test mode, sets production to False"
390+ "-test" , action = "store_true" , help = "Use test mode, sets production to False"
393391 )
394392 args = parser .parse_args ()
395393 return args
396394
395+
397396def main ():
398397 args = parse_args ()
399-
398+
400399 production = not args .test # Set production to False if -test flag is provided
401-
400+
402401 choice = get_user_input (
403402 "Do you want to create or edit a CaltechDATA record? (create/edit): "
404403 ).lower ()
@@ -412,7 +411,7 @@ def main():
412411
413412def create_record (production ):
414413 token = get_or_set_token (production )
415- #keep_file = input("Do you want to keep your existing files? (yes/no): ").lower() == "yes"
414+ # keep_file = input("Do you want to keep your existing files? (yes/no): ").lower() == "yes"
416415 print ("Using CaltechDATA token:" , token )
417416 while True :
418417 choice = get_user_input (
@@ -424,7 +423,11 @@ def create_record(production):
424423 if existing_data :
425424 if filepath != "" :
426425 response = caltechdata_write (
427- existing_data , token , filepath , production = production , publish = False
426+ existing_data ,
427+ token ,
428+ filepath ,
429+ production = production ,
430+ publish = False ,
428431 )
429432 elif file_link != "" :
430433 response = caltechdata_write (
@@ -501,7 +504,6 @@ def create_record(production):
501504 metadata , token , production = production , publish = False
502505 )
503506 rec_id = response
504-
505507
506508 print_upload_message (rec_id , production )
507509 with open (response + ".json" , "w" ) as file :
@@ -512,20 +514,26 @@ def create_record(production):
512514 else :
513515 print ("Invalid choice. Please enter 'existing' or 'create'." )
514516
517+
515518def print_upload_message (rec_id , production ):
516- base_url = "https://data.caltech.edu/uploads/" if production else "https://data.caltechlibrary.dev/uploads/"
519+ base_url = (
520+ "https://data.caltech.edu/uploads/"
521+ if production
522+ else "https://data.caltechlibrary.dev/uploads/"
523+ )
517524 print (
518525 f"""You can view and publish this record at
519526 { base_url } { rec_id }
520527 If you need to upload large files to S3, you can type
521528 `s3cmd put DATA_FILE s3://ini230004-bucket01/{ rec_id } /`"""
522529 )
523530
531+
524532def edit_record (production ):
525533 record_id = input ("Enter the CaltechDATA record ID: " )
526534 token = get_or_set_token (production )
527535 file_name = download_file_by_id (record_id , token )
528-
536+
529537 if file_name :
530538 try :
531539 # Read the edited metadata file
@@ -546,47 +554,58 @@ def edit_record(production):
546554 if choice == "y" :
547555 if production :
548556 API_URL_TEMPLATE = "https://data.caltech.edu/api/records/{record_id}/files"
549- API_URL_TEMPLATE_DRAFT = "https://data.caltech.edu/api/records/{record_id}/draft/files"
557+ API_URL_TEMPLATE_DRAFT = (
558+ "https://data.caltech.edu/api/records/{record_id}/draft/files"
559+ )
550560 else :
551- API_URL_TEMPLATE = "https://data.caltechlibrary.dev/api/records/{record_id}/files"
552- API_URL_TEMPLATE_DRAFT = "https://data.caltechlibrary.dev/api/records/{record_id}/draft/files"
553-
561+ API_URL_TEMPLATE = (
562+ "https://data.caltechlibrary.dev/api/records/{record_id}/files"
563+ )
564+ API_URL_TEMPLATE_DRAFT = (
565+ "https://data.caltechlibrary.dev/api/records/{record_id}/draft/files"
566+ )
567+
554568 url = API_URL_TEMPLATE .format (record_id = record_id )
555569 url_draft = API_URL_TEMPLATE_DRAFT .format (record_id = record_id )
556-
570+
557571 headers = {
558- "accept" : "application/json" ,
572+ "accept" : "application/json" ,
559573 }
560574
561575 if token :
562576 headers ["Authorization" ] = "Bearer %s" % token
563577
564578 response = requests .get (url , headers = headers )
565579 response_draft = requests .get (url_draft , headers = headers )
566-
567- #print(production, response, response_draft)
568- #print(response.status_code, response_draft.status_code)
580+
581+ # print(production, response, response_draft)
582+ # print(response.status_code, response_draft.status_code)
569583
570584 data = response .json ()
571585 data_draft = response_draft .json ()
572586
573- #print(data_draft)
587+ # print(data_draft)
574588 # Check if 'entries' exists and its length
575- if len (data .get ('entries' , [])) == 0 and len (data_draft .get ('entries' , [])) == 0 :
589+ if (
590+ len (data .get ("entries" , [])) == 0
591+ and len (data_draft .get ("entries" , [])) == 0
592+ ):
576593 keepfile = False
577594 else :
578- keepfile = input ("Do you want to keep existing files? (y/n): " ).lower () == "y"
579-
595+ keepfile = (
596+ input ("Do you want to keep existing files? (y/n): " ).lower () == "y"
597+ )
598+
580599 # if response.status_code == 404 and response_draft.status_code == 404:
581600 # keepfile = False
582601 # else:
583-
602+
584603 # keepfile = input("Do you want to keep existing files? (y/n): ").lower() == "y"
585-
604+
586605 filepath , file_link = upload_supporting_file (record_id )
587606 if file_link :
588607 print (file_link )
589-
608+
590609 if filepath != "" :
591610 response = caltechdata_edit (
592611 record_id ,
@@ -604,14 +623,11 @@ def edit_record(production):
604623 file_links = file_link ,
605624 production = production ,
606625 publish = False ,
607- keepfile = keepfile ,
626+ keepfiles = keepfile ,
608627 )
609-
628+
610629 rec_id = response
611630 print_upload_message (rec_id , production )
612-
613-
614-
615631
616632
617633def download_file_by_id (record_id , token = None ):
@@ -632,20 +648,22 @@ def download_file_by_id(record_id, token=None):
632648 url + "/draft" ,
633649 headers = headers ,
634650 )
635- if response .status_code != 200 :
651+ if response .status_code != 200 :
636652 url = f"https://data.caltechlibrary.dev/api/records/{ record_id } "
637653 response = requests .get (
638- url ,
639- headers = headers ,
640- )
654+ url ,
655+ headers = headers ,
656+ )
641657 if response .status_code != 200 :
642658 # Might have a draft
643659 response = requests .get (
644660 url + "/draft" ,
645661 headers = headers ,
646662 )
647663 if response .status_code != 200 :
648- raise Exception (f"Record { record_id } does not exist, cannot edit" )
664+ raise Exception (
665+ f"Record { record_id } does not exist, cannot edit"
666+ )
649667 file_content = response .content
650668 file_name = f"downloaded_data_{ record_id } .json"
651669 with open (file_name , "wb" ) as file :
0 commit comments