Skip to content

Commit 5d026dc

Browse files
Add profile, token deletion, and switch to setup.cfg/pyproject.toml
* Add profile and token deletion to cli.py * Create setup.cfg * Create pyproject.toml * Update setup.py * Create update_setupcfg.yaml Co-authored-by: RohanBhattaraiNP <[email protected]>
1 parent 7fd6982 commit 5d026dc

File tree

8 files changed

+214
-218
lines changed

8 files changed

+214
-218
lines changed

.github/workflows/bot.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@ jobs:
3030
with:
3131
python-version: '3.x'
3232

33-
- name: Check for Required Environment Variables
34-
env:
35-
CALTECHDATA_TOKEN: ${{ secrets.CALTECHDATA_TOKEN }}
36-
run: |
37-
if [ -z "$CALTECHDATA_TOKEN" ]; then
38-
echo "Error: CALTECHDATA_TOKEN environment variable is not set"
39-
exit 1
40-
fi
41-
4233
- name: Install dependencies
4334
run: |
4435
python -m pip install --upgrade pip
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Sync Codemeta with Setup
2+
3+
on:
4+
push:
5+
paths:
6+
- codemeta.json
7+
8+
jobs:
9+
sync-codemeta:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v3
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: '3.x'
20+
21+
- name: Install jq for JSON parsing
22+
run: sudo apt-get install -y jq
23+
24+
- name: Parse and update setup.cfg
25+
run: |
26+
# Extract values from codemeta.json
27+
NAME=$(jq -r '.name' codemeta.json)
28+
VERSION=$(jq -r '.version' codemeta.json)
29+
AUTHORS=$(jq -r '[.author[] | .givenName + " " + .familyName] | join(", ")' codemeta.json)
30+
AUTHOR_EMAILS=$(jq -r '[.author[] | .email // empty] | join(", ")' codemeta.json)
31+
DESCRIPTION=$(jq -r '.description' codemeta.json)
32+
URL=$(jq -r '.codeRepository // .url' codemeta.json)
33+
34+
# Update setup.cfg fields
35+
sed -i "s/^name = .*/name = $NAME/" setup.cfg
36+
sed -i "s/^version = .*/version = $VERSION/" setup.cfg
37+
sed -i "s/^author = .*/author = $AUTHORS/" setup.cfg
38+
sed -i "s/^author_email = .*/author_email = $AUTHOR_EMAILS/" setup.cfg
39+
sed -i "s/^description = .*/description = $DESCRIPTION/" setup.cfg
40+
sed -i "s|^url = .*|url = $URL|" setup.cfg
41+
42+
- name: Commit changes
43+
run: |
44+
if ! git diff --quiet; then
45+
git config user.name "github-actions[bot]"
46+
git config user.email "github-actions[bot]@users.noreply.github.com"
47+
git add setup.cfg
48+
git commit -m "Sync setup.cfg with codemeta.json changes"
49+
git push
50+
fi

CITATION.cff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ keywords:
2222
- metadata
2323
- software
2424
- InvenioRDM
25-
25+
date-released: 2025-01-07

caltechdata_api/cli.py

Lines changed: 110 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def get_or_set_token(production=True):
9999
print("Tokens do not match. Please try again.")
100100

101101

102+
# Delete the saved token file.
103+
def delete_saved_token():
104+
token_file = os.path.join(caltechdata_directory, "token.txt")
105+
if os.path.exists(token_file):
106+
os.remove(token_file)
107+
print("Token deleted successfully.")
108+
else:
109+
print("No token found to delete.")
110+
111+
102112
def welcome_message():
103113
print("Welcome to CaltechDATA CLI")
104114

@@ -186,6 +196,61 @@ def get_funding_details():
186196
}
187197

188198

199+
# Add profile handling functions
200+
def save_profile():
201+
profile_file = os.path.join(caltechdata_directory, "profile.json")
202+
203+
# Get ORCID
204+
while True:
205+
orcid = get_user_input("Enter your ORCID identifier: ")
206+
orcid = normalize_orcid(orcid)
207+
family_name, given_name = get_names(orcid)
208+
if family_name is not None and given_name is not None:
209+
break
210+
retry = input("Do you want to try again? (y/n): ")
211+
if retry.lower() != "y":
212+
return None
213+
214+
# Get funding details
215+
funding_references = []
216+
num_funding_entries = get_funding_entries()
217+
for _ in range(num_funding_entries):
218+
funding_references.append(get_funding_details())
219+
220+
profile_data = {
221+
"orcid": orcid,
222+
"family_name": family_name,
223+
"given_name": given_name,
224+
"funding_references": funding_references,
225+
}
226+
227+
with open(profile_file, "w") as f:
228+
json.dump(profile_data, f, indent=2)
229+
230+
print("Profile saved successfully!")
231+
return profile_data
232+
233+
234+
def load_profile():
235+
profile_file = os.path.join(caltechdata_directory, "profile.json")
236+
try:
237+
with open(profile_file, "r") as f:
238+
return json.load(f)
239+
except FileNotFoundError:
240+
return None
241+
242+
243+
def get_or_create_profile():
244+
profile = load_profile()
245+
if profile:
246+
use_saved = input("Use saved profile? (y/n): ").lower()
247+
if use_saved == "y":
248+
return profile
249+
250+
print("Creating new profile...")
251+
return save_profile()
252+
253+
189254
def parse_arguments():
190255
welcome_message()
191256
args = {}
@@ -222,23 +287,17 @@ def parse_arguments():
222287
break
223288
else:
224289
print("Invalid input. Please enter a number between 1 and 8.")
290+
# Load or create profile
291+
profile = get_or_create_profile()
292+
if profile:
293+
args["orcid"] = profile["orcid"]
294+
args["family_name"] = profile["family_name"]
295+
args["given_name"] = profile["given_name"]
296+
args["fundingReferences"] = profile["funding_references"]
297+
else:
298+
print("Failed to load or create profile. Exiting.")
299+
return None
225300

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

244303

@@ -410,24 +469,48 @@ def parse_args():
410469
parser.add_argument(
411470
"-test", action="store_true", help="Use test mode, sets production to False"
412471
)
472+
parser.add_argument(
473+
"--delete-token", action="store_true", help="Delete the saved token."
474+
)
413475
args = parser.parse_args()
414476
return args
415477

416478

479+
def normalize_orcid(val):
480+
orcid_urls = ["https://orcid.org/", "http://orcid.org/", "orcid.org/"]
481+
for orcid_url in orcid_urls:
482+
if val.startswith(orcid_url):
483+
val = val[len(orcid_url) :]
484+
break
485+
486+
val = val.replace("-", "").replace(" ", "")
487+
if len(val) != 16 or not val.isdigit():
488+
raise ValueError(f"Invalid ORCID identifier: {val}")
489+
return "-".join([val[0:4], val[4:8], val[8:12], val[12:16]])
490+
491+
417492
def main():
418493
args = parse_args()
494+
production = not args.test
495+
if args.delete_token:
496+
delete_saved_token()
497+
while True:
498+
choice = get_user_input(
499+
"What would you like to do? (create/edit/profile/exit): "
500+
).lower()
419501

420-
production = not args.test # Set production to False if -test flag is provided
421-
422-
choice = get_user_input(
423-
"Do you want to create or edit a CaltechDATA record? (create/edit): "
424-
).lower()
425-
if choice == "create":
426-
create_record(production)
427-
elif choice == "edit":
428-
edit_record(production)
429-
else:
430-
print("Invalid choice. Please enter 'create' or 'edit'.")
502+
if choice == "create":
503+
create_record(production)
504+
elif choice == "edit":
505+
edit_record(production)
506+
elif choice == "profile":
507+
save_profile()
508+
elif choice == "exit":
509+
break
510+
else:
511+
print(
512+
"Invalid choice. Please enter 'create', 'edit', 'profile', or 'exit'."
513+
)
431514

432515

433516
def create_record(production):

codemeta.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
},
2222
{
2323
"@type": "Person",
24-
"givenName": "Bhattarai",
25-
"familyName": "Rohan ",
24+
"givenName": "Rohan",
25+
"familyName": "Bhattarai",
2626
"affiliation": {
2727
"@type": "Organization",
2828
"name": "Caltech"

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["setuptools>=61.0", "wheel"]
3+
build-backend = "setuptools.build_meta"

setup.cfg

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[build-system]
2+
requires = ["setuptools>=64.0","wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[metadata]
6+
name = caltechdata_api
7+
version = 1.9.1
8+
author = Thomas E Morrell, Rohan Bhattarai, Won Elizabeth, Alexander A Abakah
9+
10+
description = Python wrapper for CaltechDATA API.
11+
long_description = file: README.md
12+
long_description_content_type = text/markdown
13+
url = https://github.com/caltechlibrary/caltechdata_api
14+
license = MIT
15+
classifiers =
16+
License :: OSI Approved :: MIT License
17+
Programming Language :: Python :: 3
18+
Programming Language :: Python :: 3.7
19+
Programming Language :: Python :: 3.8
20+
Programming Language :: Python :: 3.9
21+
Programming Language :: Python :: 3.10
22+
Programming Language :: Python :: Implementation :: CPython
23+
Operating System :: OS Independent
24+
25+
[options]
26+
packages = find:
27+
python_requires = >=3.6.0
28+
install_requires =
29+
requests
30+
datacite>1.1.0
31+
tqdm>=4.62.3
32+
pyyaml
33+
s3fs
34+
cryptography
35+
s3cmd
36+
include_package_data = True
37+
38+
[options.packages.find]
39+
exclude = tests
40+
41+
[options.entry_points]
42+
console_scripts =
43+
caltechdata_api=caltechdata_api.cli:main
44+
45+
[tool:pytest]
46+
addopts = --verbose

0 commit comments

Comments
 (0)