Skip to content

Commit

Permalink
Update author processing to include CITATION.cff generation
Browse files Browse the repository at this point in the history
  • Loading branch information
cofinoa committed Dec 18, 2024
1 parent bda6390 commit 1542e40
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 66 deletions.
10 changes: 5 additions & 5 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ authors:
- affiliation: SMHI
family-names: Raspaud
given-names: Martin
- affiliation: Excalibur Laboratories Inc
family-names: Horne
given-names: Randy
- affiliation: University of Reading
family-names: Blower
given-names: Jon
- affiliation: Excalibur Laboratories Inc
family-names: Horne
given-names: Randy
- affiliation: University of Texas
family-names: Whiteaker
given-names: Timothy
- affiliation: USGS
family-names: Blodgett
given-names: David
- affiliation: UC Irvine
- affiliation: University of California, Irvine
family-names: Zender
given-names: Charlie
- affiliation: EUMETSAT
Expand Down Expand Up @@ -112,7 +112,7 @@ authors:
- affiliation: Puertos del Estado, Madrid
family-names: Manzano
given-names: Fernando
- affiliation: SMHI
- affiliation: SMHI Rossby Centre, Swedish Meteorological and Hydrological Institute
family-names: "B\xE4rring"
given-names: Lars
orcid: 0000-0001-7280-2502
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ about-authors.adoc: authors.adoc scripts/update_authors.py

zenodo.json: authors.adoc scripts/update_authors.py
python3 scripts/update_authors.py --authors-adoc=authors.adoc --update-zenodo=zenodo.json

CITATION.cff: authors.adoc scripts/update_authors.py
python3 scripts/update_authors.py --authors-adoc=authors.adoc --update-citation=CITATION.cff

$(BUILD_DIR):
mkdir -vp $(BUILD_DIR)
Expand Down
82 changes: 59 additions & 23 deletions scripts/update_authors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

"""
This script parses the authors.adoc file to extract author metadata, update the
"creators" section in the zenodo.json file, and generate the about-authors.adoc file.
"creators" section in the zenodo.json file, update CITATION.cff and generate the about-authors.adoc file.
Purpose:
1. Extracts authors' names, affiliations, and ORCID from authors.adoc.
2. Updates the "creators" section in the zenodo.json file with formatted author names.
2. Updates the CITATION.cff file.
3. Generates the about-authors.adoc file to list original and additional authors.
Authors.adoc Format:
Expand Down Expand Up @@ -36,13 +37,15 @@
import sys
import os
import argparse
import yaml

def parse_authors_adoc(authors_adoc_path):
"""Parses the authors.adoc file to extract author metadata."""
print(f"Parsing authors from: {authors_adoc_path}")
authors = {"original": [], "additional": []}
creators = []

citation_authors = []

if not os.path.exists(authors_adoc_path):
print(f"Error: {authors_adoc_path} does not exist.")
sys.exit(1)
Expand All @@ -60,16 +63,20 @@ def parse_authors_adoc(authors_adoc_path):
name_parts = name_match.group(1).split('{nbsp}')
if len(name_parts) == 2: # FirstName LastName
first_name, last_name = name_parts
given_names = first_name
full_name = f"{first_name} {last_name}"
zenodo_name = f"{last_name}, {first_name}"
elif len(name_parts) == 3: # FirstName MiddleName LastName
first_name, middle_name, last_name = name_parts
given_names = f"{first_name} {middle_name}"
full_name = f"{first_name} {middle_name} {last_name}"
zenodo_name = f"{last_name}, {first_name} {middle_name}"
else:
full_name = name_match.group(1).replace('{nbsp}', ' ')
zenodo_name = name_match.group(1).replace('{nbsp}', ' ')

author_data['family-names'] = last_name
author_data['given-names'] = given_names
author_data['name'] = full_name
author_data['zenodo_name'] = zenodo_name

Expand All @@ -93,11 +100,18 @@ def parse_authors_adoc(authors_adoc_path):

author_entry = f"{author_data['name']}, {author_data['affiliation']}"
authors['original' if re.search(rf':author_{author_id}_type: original', content) else 'additional'].append(author_entry)

citation_authors.append({
"affiliation": author_data['affiliation'],
"family-names": author_data['family-names'],
"given-names": author_data['given-names'],
**({"orcid": author_data['orcid']} if 'orcid' in author_data else {})
})
else:
print(f"Warning: Missing name or affiliation for author_{author_id}")

print(f"Total authors processed: {len(creators)} (Original: {len(authors['original'])}, Additional: {len(authors['additional'])})")
return authors, creators
#print(f"Total authors processed: {len(creators)} (Original: {len(authors['original'])}, Additional: {len(authors['additional'])})")
return authors, creators, citation_authors


def generate_about_authors(adoc_output_path, authors):
Expand All @@ -110,10 +124,10 @@ def generate_about_authors(adoc_output_path, authors):
for author in authors['additional']:
content += f"* {author}\n"

print(f"Writing about-authors.adoc to: {adoc_output_path}")
#print(f"Writing about-authors.adoc to: {adoc_output_path}")
with open(adoc_output_path, 'w') as file:
file.write(content)
print(f"about-authors.adoc file written successfully with {len(authors['original'])} original authors and {len(authors['additional'])} additional authors.")
print(f"{adoc_output_path} file written successfully with {len(authors['original'])} original authors and {len(authors['additional'])} additional authors.")


def update_zenodo_json(zenodo_json_path, creators):
Expand All @@ -122,16 +136,32 @@ def update_zenodo_json(zenodo_json_path, creators):
print(f"Error: {zenodo_json_path} does not exist.")
sys.exit(1)

print(f"Updating zenodo.json file: {zenodo_json_path}")
#print(f"Updating zenodo.json file: {zenodo_json_path}")
with open(zenodo_json_path, 'r') as file:
zenodo_data = json.load(file)

zenodo_data['metadata']['creators'] = creators

with open(zenodo_json_path, 'w') as file:
json.dump(zenodo_data, file, indent=2)
print(f"zenodo.json file updated successfully with {len(creators)} authors.")
print(f"{zenodo_json_path} file updated successfully with {len(creators)} authors.")

def update_citation_cff(citation_cff_path, citation_authors):
"""Updates the authors section of the CITATION.cff file."""
if not os.path.exists(citation_cff_path):
print(f"Error: {citation_cff_path} does not exist.")
sys.exit(1)

#print(f"Updating CITATION.cff file: {citation_cff_path}")
with open(citation_cff_path, 'r') as file:
citation_data = yaml.safe_load(file)

citation_data['authors'] = citation_authors

with open(citation_cff_path, 'w') as file:
yaml.dump(citation_data, file, default_flow_style=False, sort_keys=False, indent=2)

print(f"{citation_cff_path} file updated successfully with {len(citation_authors)} authors.")

def main():
parser = argparse.ArgumentParser(
Expand All @@ -141,32 +171,38 @@ def main():
parser.add_argument('--authors-adoc', default='authors.adoc', metavar='AUTHORS_ADOC_FILE', help='Path to the authors.adoc file (default: authors.adoc)')
parser.add_argument('--update-zenodo', nargs='?', const='zenodo.json', metavar='ZENODO_FILE', help='Path to the zenodo.json file (default: zenodo.json)')
parser.add_argument('--write-about-authors', nargs='?', const='about-authors.adoc', metavar='ABOUT_AUTHORS_FILE', help='Path to the about-authors.adoc file (default: about-authors.adoc)')
parser.add_argument('--update-citation', nargs='?', const='CITATION.cff', metavar='CITATION_FILE', help='Path to the CITATION.cff file (default: CITATION.cff)')

args = parser.parse_args()

print("Starting author update process...")
print(f"Authors file: {args.authors_adoc}")
print(f"Zenodo JSON file: {args.update_zenodo}")
print(f"About Authors file: {args.write_about_authors}")
# print("Starting author update process...")
# print(f"Authors file: {args.authors_adoc}")
# print(f"Zenodo JSON file: {args.update_zenodo}")
# print(f"About-Authors file: {args.write_about_authors}")
# print(f"Citation CFF file: {args.write_about_authors}")

authors, creators = parse_authors_adoc(args.authors_adoc)
authors, creators, citation_authors = parse_authors_adoc(args.authors_adoc)

if args.write_about_authors:
generate_about_authors(args.write_about_authors, authors)

if args.update_zenodo:
update_zenodo_json(args.update_zenodo, creators)

print("\n--- Summary of Changes ---")
print(f"Total authors processed: {len(creators)}")
print(f"Original Authors: {len(authors['original'])}")
print(f"Additional Authors: {len(authors['additional'])}")
if args.write_about_authors:
print(f"about-authors.adoc updated at: {args.write_about_authors}")
if args.update_zenodo:
print(f"zenodo.json updated at: {args.update_zenodo}")
print("\n--- End of Summary ---")

if args.update_citation:
update_citation_cff(args.update_citation, citation_authors)

# print("\n--- Summary of Changes ---")
# print(f"Total authors processed: {len(creators)}")
# print(f"Original Authors: {len(authors['original'])}")
# print(f"Additional Authors: {len(authors['additional'])}")
# if args.write_about_authors:
# print(f"About-Authors updated at: {args.write_about_authors}")
# if args.update_zenodo:
# print(f"Zenodo JSON updated at: {args.update_zenodo}")
# if args.update_citation:
# print(f"Citation CFF updated at: {args.update_zenodo}")
# print("\n--- End of Summary ---")

if __name__ == "__main__":
main()
Loading

0 comments on commit 1542e40

Please sign in to comment.