Skip to content

Commit 27a5070

Browse files
Update cli.py (#40)
Add test flag to cli
1 parent 5441c91 commit 27a5070

File tree

1 file changed

+60
-40
lines changed

1 file changed

+60
-40
lines changed

caltechdata_api/cli.py

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -375,20 +375,34 @@ def upload_data_from_file():
375375
except json.JSONDecodeError as e:
376376
print(f"Error: Invalid JSON format in the file '{filename}'. {str(e)}")
377377

378+
def parse_args():
379+
"""Parse command-line arguments."""
380+
parser = argparse.ArgumentParser(description="CaltechDATA CLI tool.")
381+
parser.add_argument(
382+
"-test",
383+
action="store_true",
384+
help="Use test mode, sets production to False"
385+
)
386+
args = parser.parse_args()
387+
return args
378388

379389
def main():
390+
args = parse_args()
391+
392+
production = not args.test # Set production to False if -test flag is provided
393+
380394
choice = get_user_input(
381395
"Do you want to create or edit a CaltechDATA record? (create/edit): "
382396
).lower()
383397
if choice == "create":
384-
create_record()
398+
create_record(production)
385399
elif choice == "edit":
386-
edit_record()
400+
edit_record(production)
387401
else:
388402
print("Invalid choice. Please enter 'create' or 'edit'.")
389403

390404

391-
def create_record():
405+
def create_record(production):
392406
token = get_or_set_token()
393407
print("Using CaltechDATA token:", token)
394408
while True:
@@ -401,7 +415,7 @@ def create_record():
401415
if existing_data:
402416
if filepath != "":
403417
response = caltechdata_write(
404-
existing_data, token, filepath, production=True, publish=False
418+
existing_data, token, filepath, production=production, publish=False
405419
)
406420
elif file_link != "":
407421
response = caltechdata_write(
@@ -414,15 +428,10 @@ def create_record():
414428
)
415429
else:
416430
response = caltechdata_write(
417-
existing_data, token, production=True, publish=False
431+
existing_data, token, production=production, publish=False
418432
)
419433
rec_id = response
420-
print(
421-
f"""You can view and publish this record at
422-
https://data.caltech.edu/uploads/{rec_id}
423-
If you need to upload large files to S3, you can type
424-
`s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/"""
425-
)
434+
print_upload_message(rec_id, production)
426435
break
427436
else:
428437
print("Going back to the main menu.")
@@ -468,27 +477,24 @@ def create_record():
468477
if confirm_upload():
469478
if filepath != "":
470479
response = caltechdata_write(
471-
metadata, token, filepath, production=True, publish=False
480+
metadata, token, filepath, production=production, publish=False
472481
)
473482
elif file_link != "":
474483
response = caltechdata_write(
475484
metadata,
476485
token,
477486
file_links=[file_link],
478-
production=True,
487+
production=production,
479488
publish=False,
480489
)
481490
else:
482491
response = caltechdata_write(
483-
metadata, token, production=True, publish=False
492+
metadata, token, production=production, publish=False
484493
)
485494
rec_id = response
486-
print(
487-
f"""You can view and publish this record at
488-
https://data.caltech.edu/uploads/{rec_id}
489-
If you need to upload large files to S3, you can type
490-
`s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/"""
491-
)
495+
496+
497+
print_upload_message(rec_id, production)
492498
with open(response + ".json", "w") as file:
493499
json.dump(metadata, file, indent=2)
494500
break
@@ -497,18 +503,27 @@ def create_record():
497503
else:
498504
print("Invalid choice. Please enter 'existing' or 'create'.")
499505

506+
def print_upload_message(rec_id, production):
507+
base_url = "https://data.caltech.edu/uploads/" if production else "https://data.caltechlibrary.dev/uploads/"
508+
print(
509+
f"""You can view and publish this record at
510+
{base_url}{rec_id}
511+
If you need to upload large files to S3, you can type
512+
`s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/`"""
513+
)
500514

501-
def edit_record():
515+
def edit_record(production):
502516
record_id = input("Enter the CaltechDATA record ID: ")
503517
token = get_or_set_token()
504518
file_name = download_file_by_id(record_id, token)
519+
505520
if file_name:
506521
try:
507522
# Read the edited metadata file
508523
with open(file_name, "r") as file:
509524
metadata = json.load(file)
510525
response = caltechdata_edit(
511-
record_id, metadata, token, production=True, publish=False
526+
record_id, metadata, token, production=production, publish=False
512527
)
513528
if response:
514529
print("Metadata edited successfully.")
@@ -518,33 +533,36 @@ def edit_record():
518533
print(f"An error occurred during metadata editing: {e}")
519534
else:
520535
print("No metadata file found.")
536+
521537
choice = get_user_input("Do you want to add files? (y/n): ").lower()
522538
if choice == "y":
523-
API_URL_TEMPLATE = "https://data.caltech.edu/api/records/{record_id}/files"
539+
if production:
540+
API_URL_TEMPLATE = "https://data.caltech.edu/api/records/{record_id}/files"
541+
API_URL_TEMPLATE_DRAFT = "https://data.caltech.edu/api/records/{record_id}/draft/files"
542+
else:
543+
API_URL_TEMPLATE = "https://data.caltechlibrary.dev/api/records/{record_id}/files"
544+
API_URL_TEMPLATE_DRAFT = "https://data.caltechlibrary.dev/api/records/{record_id}/draft/files"
545+
524546
url = API_URL_TEMPLATE.format(record_id=record_id)
525-
526-
API_URL_TEMPLATE2 = (
527-
"https://data.caltech.edu/api/records/{record_id}/draft/files"
528-
)
529-
url2 = API_URL_TEMPLATE2.format(record_id=record_id)
547+
url_draft = API_URL_TEMPLATE_DRAFT.format(record_id=record_id)
548+
530549
response = requests.get(url)
531-
response2 = requests.get(url2)
550+
response_draft = requests.get(url_draft)
551+
532552
filepath, file_link = upload_supporting_file(record_id)
533553
print(file_link)
534-
if response.status_code == 404 and response2.status_code == 404:
554+
555+
if response.status_code == 404 and response_draft.status_code == 404:
535556
keepfile = False
536557
else:
537-
keepfile = input("Do you want to keep existing files? y/n: ")
538-
if keepfile == "y":
539-
keepfile = True
540-
else:
541-
keepfile = False
558+
keepfile = input("Do you want to keep existing files? (y/n): ").lower() == "y"
559+
542560
if filepath != "":
543561
response = caltechdata_edit(
544562
record_id,
545563
token=token,
546564
files=filepath,
547-
production=True,
565+
production=production,
548566
publish=False,
549567
keepfiles=keepfile,
550568
)
@@ -554,14 +572,16 @@ def edit_record():
554572
metadata,
555573
token=token,
556574
file_links=file_link,
557-
production=True,
575+
production=production,
558576
publish=False,
559577
keepfile=keepfile,
560578
)
579+
561580
rec_id = response
562-
print(
563-
f"You can view and publish this record at https://data.caltech.edu/uploads/{rec_id}\n"
564-
)
581+
print_upload_message(rec_id, production)
582+
583+
584+
565585

566586

567587
def download_file_by_id(record_id, token=None):

0 commit comments

Comments
 (0)