Skip to content

Commit 14357dc

Browse files
Update cli.py
1 parent 00b39bd commit 14357dc

File tree

1 file changed

+84
-29
lines changed

1 file changed

+84
-29
lines changed

caltechdata_api/cli.py

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,14 @@ def decrypt_token(encrypted_token, key):
5959
return f.decrypt(encrypted_token).decode()
6060

6161

62-
# Function to get or set token with support for test system
62+
# Function to get or set token with support for test systems
6363
def get_or_set_token(production=True):
64+
# First check for environment variable
65+
env_token = os.environ.get("CALTECHDATA_TOKEN")
66+
if env_token:
67+
print("Using token from environment variable")
68+
return env_token
69+
6470
key = load_or_generate_key()
6571

6672
# Use different token files for production and test environments
@@ -180,6 +186,56 @@ def get_funding_details():
180186
"funderIdentifierType": "ROR",
181187
}
182188

189+
# Add profile handling functions
190+
def save_profile():
191+
profile_file = os.path.join(caltechdata_directory, "profile.json")
192+
193+
# Get ORCID
194+
while True:
195+
orcid = get_user_input("Enter your ORCID identifier: ")
196+
family_name, given_name = get_names(orcid)
197+
if family_name is not None and given_name is not None:
198+
break
199+
retry = input("Do you want to try again? (y/n): ")
200+
if retry.lower() != "y":
201+
return None
202+
203+
# Get funding details
204+
funding_references = []
205+
num_funding_entries = get_funding_entries()
206+
for _ in range(num_funding_entries):
207+
funding_references.append(get_funding_details())
208+
209+
profile_data = {
210+
"orcid": orcid,
211+
"family_name": family_name,
212+
"given_name": given_name,
213+
"funding_references": funding_references
214+
}
215+
216+
with open(profile_file, "w") as f:
217+
json.dump(profile_data, f, indent=2)
218+
219+
print("Profile saved successfully!")
220+
return profile_data
221+
222+
def load_profile():
223+
profile_file = os.path.join(caltechdata_directory, "profile.json")
224+
try:
225+
with open(profile_file, "r") as f:
226+
return json.load(f)
227+
except FileNotFoundError:
228+
return None
229+
230+
def get_or_create_profile():
231+
profile = load_profile()
232+
if profile:
233+
use_saved = input("Use saved profile? (y/n): ").lower()
234+
if use_saved == 'y':
235+
return profile
236+
237+
print("Creating new profile...")
238+
return save_profile()
183239

184240
def parse_arguments():
185241
welcome_message()
@@ -217,23 +273,17 @@ def parse_arguments():
217273
break
218274
else:
219275
print("Invalid input. Please enter a number between 1 and 8.")
276+
# Load or create profile
277+
profile = get_or_create_profile()
278+
if profile:
279+
args["orcid"] = profile["orcid"]
280+
args["family_name"] = profile["family_name"]
281+
args["given_name"] = profile["given_name"]
282+
args["fundingReferences"] = profile["funding_references"]
283+
else:
284+
print("Failed to load or create profile. Exiting.")
285+
return None
220286

221-
while True:
222-
orcid = get_user_input("Enter your ORCID identifier: ")
223-
family_name, given_name = get_names(orcid)
224-
if family_name is not None and given_name is not None:
225-
args["orcid"] = orcid
226-
break # Break out of the loop if names are successfully retrieved
227-
retry = input("Do you want to try again? (y/n): ")
228-
if retry.lower() != "y":
229-
print("Exiting program.")
230-
return
231-
# Optional arguments
232-
num_funding_entries = get_funding_entries()
233-
funding_references = []
234-
for _ in range(num_funding_entries):
235-
funding_references.append(get_funding_details())
236-
args["fundingReferences"] = funding_references
237287
return args
238288

239289

@@ -411,19 +461,24 @@ def parse_args():
411461

412462
def main():
413463
args = parse_args()
464+
production = not args.test
414465

415-
production = not args.test # Set production to False if -test flag is provided
416-
417-
choice = get_user_input(
418-
"Do you want to create or edit a CaltechDATA record? (create/edit): "
419-
).lower()
420-
if choice == "create":
421-
create_record(production)
422-
elif choice == "edit":
423-
edit_record(production)
424-
else:
425-
print("Invalid choice. Please enter 'create' or 'edit'.")
426-
466+
while True:
467+
choice = get_user_input(
468+
"What would you like to do? (create/edit/profile/exit): "
469+
).lower()
470+
471+
if choice == "create":
472+
create_record(production)
473+
elif choice == "edit":
474+
edit_record(production)
475+
elif choice == "profile":
476+
save_profile()
477+
elif choice == "exit":
478+
break
479+
else:
480+
print("Invalid choice. Please enter 'create', 'edit', 'profile', or 'exit'.")
481+
427482

428483
def create_record(production):
429484
token = get_or_set_token(production)

0 commit comments

Comments
 (0)