Skip to content

Commit e8fad4e

Browse files
committed
Switch to production and formatting
1 parent 40a403f commit e8fad4e

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

caltechdata_api/caltechdata_write.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ def write_files_rdm(files, file_link, headers, f_headers, s3=None, keepfiles=Fal
5959
raise Exception(result.text)
6060
else:
6161
# Delete any files not included in this write command
62-
if (keepfiles == False):
62+
if keepfiles == False:
6363
result = requests.delete(self, headers=f_headers)
6464
if result.status_code != 204:
6565
raise Exception(result.text)
6666

67+
6768
def add_file_links(
6869
metadata, file_links, file_descriptions=[], additional_descriptions="", s3_link=None
6970
):

caltechdata_api/cli.py

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
if not os.path.exists(caltechdata_directory):
3030
os.makedirs(caltechdata_directory)
3131

32+
3233
def generate_key():
3334
return Fernet.generate_key()
3435

36+
3537
# Load the key from a file or generate a new one if not present
3638
def load_or_generate_key():
3739
key_file = os.path.join(caltechdata_directory, "key.key")
@@ -44,19 +46,22 @@ def load_or_generate_key():
4446
f.write(key)
4547
return key
4648

49+
4750
# Encrypt the token
4851
def encrypt_token(token, key):
4952
f = Fernet(key)
5053
return f.encrypt(token.encode())
5154

55+
5256
# Decrypt the token
5357
def decrypt_token(encrypted_token, key):
5458
f = Fernet(key)
5559
return f.decrypt(encrypted_token).decode()
5660

61+
5762
# Function to get or set token
5863
def get_or_set_token():
59-
64+
6065
key = load_or_generate_key()
6166
token_file = os.path.join(caltechdata_directory, "token.txt")
6267
try:
@@ -75,7 +80,8 @@ def get_or_set_token():
7580
return token
7681
else:
7782
print("Tokens do not match. Please try again.")
78-
83+
84+
7985
def welcome_message():
8086
print("Welcome to CaltechDATA CLI")
8187

@@ -245,6 +251,7 @@ def get_names(orcid):
245251
return None, None
246252
return family_name, given_name
247253

254+
248255
def write_s3cmd_config(access_key, secret_key, endpoint):
249256
configf = os.path.join(home_directory, ".s3cfg")
250257
if not os.path.exists(key_file):
@@ -257,6 +264,8 @@ def write_s3cmd_config(access_key, secret_key, endpoint):
257264
secret_key = {secret_key}
258265
"""
259266
)
267+
268+
260269
def upload_supporting_file(record_id=None):
261270
filepath = ""
262271
filepaths = []
@@ -328,7 +337,9 @@ def upload_supporting_file(record_id=None):
328337
print(
329338
f"Error: File '{filename}' not found. Please enter a valid filename."
330339
)
331-
add_more = get_user_input("Do you want to add more files? (y/n): ").lower()
340+
add_more = get_user_input(
341+
"Do you want to add more files? (y/n): "
342+
).lower()
332343
if add_more != "y":
333344
break
334345
break
@@ -337,6 +348,8 @@ def upload_supporting_file(record_id=None):
337348
else:
338349
print("Invalid input. Please enter 'link' or 'upload' or 'n'.")
339350
return filepaths, file_links
351+
352+
340353
def upload_data_from_file():
341354
while True:
342355
print("Current JSON files in the directory:")
@@ -388,24 +401,25 @@ def create_record():
388401
if existing_data:
389402
if filepath != "":
390403
response = caltechdata_write(
391-
existing_data, token, filepath, production=False, publish=False
404+
existing_data, token, filepath, production=True, publish=False
392405
)
393406
elif file_link != "":
394407
response = caltechdata_write(
395408
existing_data,
396409
token,
397410
file_links=[file_link],
398411
s3_link=file_link,
399-
production=False,
412+
production=True,
400413
publish=False,
401414
)
402415
else:
403416
response = caltechdata_write(
404-
existing_data, token, production=False, publish=False
417+
existing_data, token, production=True, publish=False
405418
)
406419
rec_id = response
407420
print(
408-
f"""You can view and publish this record at https://data.caltechlibrary.dev/uploads/{rec_id}
421+
f"""You can view and publish this record at
422+
https://data.caltech.edu/uploads/{rec_id}
409423
If you need to upload large files to S3, you can type
410424
`s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/"""
411425
)
@@ -454,23 +468,24 @@ def create_record():
454468
if confirm_upload():
455469
if filepath != "":
456470
response = caltechdata_write(
457-
metadata, token, filepath, production=False, publish=False
471+
metadata, token, filepath, production=True, publish=False
458472
)
459473
elif file_link != "":
460474
response = caltechdata_write(
461475
metadata,
462476
token,
463477
file_links=[file_link],
464-
production=False,
478+
production=True,
465479
publish=False,
466480
)
467481
else:
468482
response = caltechdata_write(
469-
metadata, token, production=False, publish=False
483+
metadata, token, production=True, publish=False
470484
)
471485
rec_id = response
472486
print(
473-
f"""You can view and publish this record at https://data.caltechlibrary.dev/uploads/{rec_id}
487+
f"""You can view and publish this record at
488+
https://data.caltech.edu/uploads/{rec_id}
474489
If you need to upload large files to S3, you can type
475490
`s3cmd put DATA_FILE s3://ini230004-bucket01/{rec_id}/"""
476491
)
@@ -493,7 +508,7 @@ def edit_record():
493508
with open(file_name, "r") as file:
494509
metadata = json.load(file)
495510
response = caltechdata_edit(
496-
record_id, metadata, token, production=False, publish=False
511+
record_id, metadata, token, production=True, publish=False
497512
)
498513
if response:
499514
print("Metadata edited successfully.")
@@ -505,46 +520,52 @@ def edit_record():
505520
print("No metadata file found.")
506521
choice = get_user_input("Do you want to add files? (y/n): ").lower()
507522
if choice == "y":
508-
API_URL_TEMPLATE = "https://data.caltechlibrary.dev/api/records/{record_id}/files"
523+
API_URL_TEMPLATE = "https://data.caltech.edu/api/records/{record_id}/files"
509524
url = API_URL_TEMPLATE.format(record_id=record_id)
510525

511-
API_URL_TEMPLATE2 = "https://data.caltechlibrary.dev/api/records/{record_id}/draft/files"
526+
API_URL_TEMPLATE2 = (
527+
"https://data.caltech.edu/api/records/{record_id}/draft/files"
528+
)
512529
url2 = API_URL_TEMPLATE2.format(record_id=record_id)
513530
response = requests.get(url)
514531
response2 = requests.get(url2)
515532
filepath, file_link = upload_supporting_file(record_id)
516533
print(file_link)
517534
if response.status_code == 404 and response2.status_code == 404:
518535
keepfile = False
519-
else:
536+
else:
520537
keepfile = input("Do you want to keep existing files? y/n: ")
521538
if keepfile == "y":
522539
keepfile = True
523540
else:
524541
keepfile = False
525542
if filepath != "":
526543
response = caltechdata_edit(
527-
record_id, token=token, files=filepath, production=False, publish=False, keepfiles=keepfile,
544+
record_id,
545+
token=token,
546+
files=filepath,
547+
production=True,
548+
publish=False,
549+
keepfiles=keepfile,
528550
)
529551
elif file_link != "":
530552
response = caltechdata_edit(
531553
record_id,
532554
metadata,
533555
token=token,
534556
file_links=file_link,
535-
production=False,
557+
production=True,
536558
publish=False,
537-
keepfile=keepfile
559+
keepfile=keepfile,
538560
)
539561
rec_id = response
540562
print(
541-
f"You can view and publish this record at https://data.caltechlibrary.dev/uploads/{rec_id}\n"
563+
f"You can view and publish this record at https://data.caltech.edu/uploads/{rec_id}\n"
542564
)
543565

544566

545567
def download_file_by_id(record_id, token=None):
546-
url = f"https://data.caltechlibrary.dev/api/records/{record_id}"
547-
568+
url = f"https://data.caltech.edu/api/records/{record_id}"
548569

549570
headers = {
550571
"accept": "application/vnd.datacite.datacite+json",
@@ -555,7 +576,7 @@ def download_file_by_id(record_id, token=None):
555576

556577
try:
557578
response = requests.get(url, headers=headers)
558-
579+
559580
if response.status_code != 200:
560581
# Might have a draft
561582
response = requests.get(

caltechdata_api/md_to_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def parse_readme_to_json(readme_path):
157157
link_match = link_pattern.search(value)
158158
if link_match:
159159
value = link_match.group(1)
160-
160+
161161
current_object[key] = value
162162

163163
elif line.strip() and not section_match:

0 commit comments

Comments
 (0)