Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 60 additions & 40 deletions caltechdata_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,20 +375,34 @@ def upload_data_from_file():
except json.JSONDecodeError as e:
print(f"Error: Invalid JSON format in the file '{filename}'. {str(e)}")

def parse_args():
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(description="CaltechDATA CLI tool.")
parser.add_argument(
"-test",
action="store_true",
help="Use test mode, sets production to False"
)
args = parser.parse_args()
return args

def main():
args = parse_args()

production = not args.test # Set production to False if -test flag is provided

choice = get_user_input(
"Do you want to create or edit a CaltechDATA record? (create/edit): "
).lower()
if choice == "create":
create_record()
create_record(production)
elif choice == "edit":
edit_record()
edit_record(production)
else:
print("Invalid choice. Please enter 'create' or 'edit'.")


def create_record():
def create_record(production):
token = get_or_set_token()
print("Using CaltechDATA token:", token)
while True:
Expand All @@ -401,7 +415,7 @@ def create_record():
if existing_data:
if filepath != "":
response = caltechdata_write(
existing_data, token, filepath, production=True, publish=False
existing_data, token, filepath, production=production, publish=False
)
elif file_link != "":
response = caltechdata_write(
Expand All @@ -414,15 +428,10 @@ def create_record():
)
else:
response = caltechdata_write(
existing_data, token, production=True, publish=False
existing_data, token, production=production, publish=False
)
rec_id = response
print(
f"""You can view and publish this record at
https://data.caltech.edu/uploads/{rec_id}
If you need to upload large files to S3, you can type
`s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/"""
)
print_upload_message(rec_id, production)
break
else:
print("Going back to the main menu.")
Expand Down Expand Up @@ -468,27 +477,24 @@ def create_record():
if confirm_upload():
if filepath != "":
response = caltechdata_write(
metadata, token, filepath, production=True, publish=False
metadata, token, filepath, production=production, publish=False
)
elif file_link != "":
response = caltechdata_write(
metadata,
token,
file_links=[file_link],
production=True,
production=production,
publish=False,
)
else:
response = caltechdata_write(
metadata, token, production=True, publish=False
metadata, token, production=production, publish=False
)
rec_id = response
print(
f"""You can view and publish this record at
https://data.caltech.edu/uploads/{rec_id}
If you need to upload large files to S3, you can type
`s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/"""
)


print_upload_message(rec_id, production)
with open(response + ".json", "w") as file:
json.dump(metadata, file, indent=2)
break
Expand All @@ -497,18 +503,27 @@ def create_record():
else:
print("Invalid choice. Please enter 'existing' or 'create'.")

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

def edit_record():
def edit_record(production):
record_id = input("Enter the CaltechDATA record ID: ")
token = get_or_set_token()
file_name = download_file_by_id(record_id, token)

if file_name:
try:
# Read the edited metadata file
with open(file_name, "r") as file:
metadata = json.load(file)
response = caltechdata_edit(
record_id, metadata, token, production=True, publish=False
record_id, metadata, token, production=production, publish=False
)
if response:
print("Metadata edited successfully.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll also need to update line 554, 558, 594, and 567

Expand All @@ -518,33 +533,36 @@ def edit_record():
print(f"An error occurred during metadata editing: {e}")
else:
print("No metadata file found.")

choice = get_user_input("Do you want to add files? (y/n): ").lower()
if choice == "y":
API_URL_TEMPLATE = "https://data.caltech.edu/api/records/{record_id}/files"
if production:
API_URL_TEMPLATE = "https://data.caltech.edu/api/records/{record_id}/files"
API_URL_TEMPLATE_DRAFT = "https://data.caltech.edu/api/records/{record_id}/draft/files"
else:
API_URL_TEMPLATE = "https://data.caltechlibrary.dev/api/records/{record_id}/files"
API_URL_TEMPLATE_DRAFT = "https://data.caltechlibrary.dev/api/records/{record_id}/draft/files"

url = API_URL_TEMPLATE.format(record_id=record_id)

API_URL_TEMPLATE2 = (
"https://data.caltech.edu/api/records/{record_id}/draft/files"
)
url2 = API_URL_TEMPLATE2.format(record_id=record_id)
url_draft = API_URL_TEMPLATE_DRAFT.format(record_id=record_id)

response = requests.get(url)
response2 = requests.get(url2)
response_draft = requests.get(url_draft)

filepath, file_link = upload_supporting_file(record_id)
print(file_link)
if response.status_code == 404 and response2.status_code == 404:

if response.status_code == 404 and response_draft.status_code == 404:
keepfile = False
else:
keepfile = input("Do you want to keep existing files? y/n: ")
if keepfile == "y":
keepfile = True
else:
keepfile = False
keepfile = input("Do you want to keep existing files? (y/n): ").lower() == "y"

if filepath != "":
response = caltechdata_edit(
record_id,
token=token,
files=filepath,
production=True,
production=production,
publish=False,
keepfiles=keepfile,
)
Expand All @@ -554,14 +572,16 @@ def edit_record():
metadata,
token=token,
file_links=file_link,
production=True,
production=production,
publish=False,
keepfile=keepfile,
)

rec_id = response
print(
f"You can view and publish this record at https://data.caltech.edu/uploads/{rec_id}\n"
)
print_upload_message(rec_id, production)





def download_file_by_id(record_id, token=None):
Expand Down