Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardized author name formatting and eliminated code duplication in Zenodo metadata processing. #616

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
21 changes: 20 additions & 1 deletion scripts/auto-add-zenodo-entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def complete_zenodo_data(zenodo_url):
entry['description'] = remove_html_tags(metadata['description'])
if 'creators' in metadata.keys():
creators = metadata['creators']
entry['authors'] = [c['name'] for c in creators]
entry['authors'] = [reformat_author(c['name']) for c in creators]
if 'license' in metadata.keys():
entry['license'] = metadata['license']['id']

Expand All @@ -104,6 +104,25 @@ def complete_zenodo_data(zenodo_url):

return entry

def reformat_author(author_name):
"""
Reformat author names from 'Lastname, Firstname' format to 'Firstname Lastname'.

Parameters
----------
author_name : str
The author name in 'Lastname, Firstname' or other format.

Returns
-------
str
The reformatted author name in 'Firstname Lastname' format.
"""
if ',' in author_name:
parts = author_name.split(',', 1)
return parts[1].strip() + ' ' + parts[0].strip()
return author_name


if __name__ == "__main__":
main()
74 changes: 20 additions & 54 deletions scripts/generate_link_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def update_yaml_file(yaml_filename, use_github=True, use_zenodo=True):
entry['description'] = remove_html_tags(metadata['description'])
if 'creators' in metadata.keys():
creators = metadata['creators']
entry['authors'] = ", ".join([c['name'] for c in creators])
entry['authors'] = [format_author_name(c['name']) for c in creators]
if 'license' in metadata.keys():
entry['license'] = metadata['license']['id']

Expand All @@ -241,6 +241,25 @@ def update_yaml_file(yaml_filename, use_github=True, use_zenodo=True):
# Write the modified content back to the YAML file
write_yaml_file(yaml_filename, content)

def format_author_name(author_name):
"""
Reformat author names from "Lastname, Firstname" to "Firstname Lastname".

Parameters
----------
author_name : str
Author name, potentially in the format "Lastname, Firstname".

Returns
-------
str
Author name in "Firstname Lastname" format.
"""
if "," in author_name:
parts = author_name.split(",", 1)
return f"{parts[1].strip()} {parts[0].strip()}"
return author_name

def clean_license(license):
"""
Standardize license names.
Expand Down Expand Up @@ -478,56 +497,3 @@ def add_domains_to_dicts(dict_list):
"""Process list of dictionaries and add domains"""
for item in dict_list:
if 'url' in item:
item['domain'] = get_domains(item['url'])
return dict_list


def complete_zenodo_data(zenodo_url):
"""
Completes Zenodo data retrieval and structuring for inclusion in a YAML file.

Parameters
----------
zenodo_url : str
The URL of the Zenodo record.

Returns
-------
entry : dict
A dictionary containing structured metadata and statistics
fetched from the Zenodo record.
"""
zenodo_data = read_zenodo(zenodo_url)
entry = {}
urls = [zenodo_url]

if 'doi_url' in zenodo_data.keys():
doi_url = zenodo_data['doi_url']

# Add DOI URL to the URLs list if it's not already there
if doi_url not in urls:
urls.append(doi_url)
entry['url'] = urls

if 'metadata' in zenodo_data.keys():
metadata = zenodo_data['metadata']
# Update entry with Zenodo metadata and statistics
entry['name'] = metadata['title']
if 'publication_date' in metadata.keys():
entry['publication_date'] = metadata['publication_date']
if 'description' in metadata.keys():
entry['description'] = remove_html_tags(metadata['description'])
if 'creators' in metadata.keys():
creators = metadata['creators']
entry['authors'] = [c['name'] for c in creators]
if 'license' in metadata.keys():
entry['license'] = metadata['license']['id']

if 'stats' in zenodo_data.keys():
entry['num_downloads'] = zenodo_data['stats']['downloads']

return entry


if __name__ == "__main__":
main()