Skip to content

Commit

Permalink
Merge pull request #174 from elifesciences/develop
Browse files Browse the repository at this point in the history
PR for version 0.72.0 release
  • Loading branch information
gnott authored Oct 25, 2024
2 parents a65b4a3 + df753f3 commit 3dea2b0
Show file tree
Hide file tree
Showing 8 changed files with 734 additions and 11 deletions.
2 changes: 1 addition & 1 deletion elifecleaner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging


__version__ = "0.71.0"
__version__ = "0.72.0"


LOGGER = logging.getLogger(__name__)
Expand Down
222 changes: 222 additions & 0 deletions elifecleaner/prc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import time
from xml.etree.ElementTree import Element, SubElement
from docmaptools import parse as docmap_parse
from elifearticle.article import Affiliation, Contributor, Role
from elifetools import xmlio
from jatsgenerator import build as jats_build
from elifecleaner import LOGGER

Expand Down Expand Up @@ -418,3 +420,223 @@ def add_history_date(root, date_type, date_struct, identifier=None):
date_tag.set("iso-8601-date", time.strftime("%Y-%m-%d", date_struct))
jats_build.set_dmy(date_tag, date_struct)
return root


def set_article_id(xml_root, article_id, doi, version_doi):
"add article-id tags"
# add manuscript ID
if article_id:
article_meta_tag = xml_root.find(".//front/article-meta")
article_id_tag = Element("article-id")
article_id_tag.set("pub-id-type", "publisher-id")
article_id_tag.text = str(article_id)
article_meta_tag.insert(0, article_id_tag)
# add regular DOI tag
add_doi(xml_root, doi, specific_use=None, identifier=version_doi)
# add version DOI tag
add_version_doi(xml_root, version_doi, version_doi)


def set_volume(root, volume):
"set volume tag text, add volume tag if not present"
# find volume tag
tag = root.find(".//front/article-meta/volume")
if tag is None:
article_meta_tag = root.find(".//front/article-meta")
if article_meta_tag is not None:
# add volume tag
tag_index = xmlio.get_first_element_index(article_meta_tag, "elocation-id")
if tag_index:
# insert volume tag in the right tag order
tag = Element("volume")
article_meta_tag.insert(tag_index - 1, tag)
else:
# append to the end of article-meta
tag = SubElement(article_meta_tag, "volume")
if tag is not None:
tag.text = str(volume)


def set_elocation_id(root, elocation_id):
"set elocation-id tag text, add elocation-id tag if not present"
# find elocation-id tag
tag = root.find(".//front/article-meta/elocation-id")
if tag is None:
article_meta_tag = root.find(".//front/article-meta")
if article_meta_tag is not None:
# add elocation-id tag
tag_index = xmlio.get_first_element_index(article_meta_tag, "volume")
if tag_index:
# insert elocation-id tag in the right tag order
tag = Element("elocation-id")
article_meta_tag.insert(tag_index, tag)
else:
# append to the end of article-meta
tag = SubElement(article_meta_tag, "elocation-id")
if tag is not None:
tag.text = str(elocation_id)


def set_article_categories(xml_root, display_channel=None, article_categories=None):
"add tags to article-categories tag"
article_meta_tag = xml_root.find(".//front/article-meta")
article_categories_tag = article_meta_tag.find(".//article-categories")
if article_categories_tag is None:
# insert the new tag into the XML after the article-id tags and before title-group
insert_index = None
for tag_index, tag in enumerate(article_meta_tag.findall("*")):
if tag.tag == "article-id":
insert_index = tag_index + 1
if tag.tag == "title-group":
break
article_categories_tag = Element("article-categories")
article_meta_tag.insert(insert_index, article_categories_tag)

if article_categories_tag is not None and display_channel:
jats_build.set_display_channel(article_categories_tag, display_channel)

if article_categories_tag is not None and article_categories:
for article_category in article_categories:
jats_build.set_major_subject_area(article_categories_tag, article_category)


def set_license_tag(parent, license_data_dict):
"add license tag to parent tag"
if not license_data_dict.get("href"):
return
license_tag = SubElement(parent, "license")
license_tag.set(
"{http://www.w3.org/1999/xlink}href",
str(license_data_dict.get("href")),
)
ali_license_href_tag = SubElement(
license_tag, "{http://www.niso.org/schemas/ali/1.0/}license_ref"
)
ali_license_href_tag.text = license_data_dict.get("href")
# license-p tag
license_p_tag = SubElement(license_tag, "license-p")
license_p_tag.text = license_data_dict.get("paragraph1")
license_ext_link_tag = SubElement(license_p_tag, "ext-link")
license_ext_link_tag.set("ext-link-type", "uri")
license_ext_link_tag.set(
"{http://www.w3.org/1999/xlink}href",
license_data_dict.get("href"),
)
license_ext_link_tag.text = license_data_dict.get("name")
license_ext_link_tag.tail = license_data_dict.get("paragraph2")


def set_permissions(xml_root, license_data_dict, copyright_year, copyright_holder):
"add license data to permissions tag"
article_meta_tag = xml_root.find(".//front/article-meta")
permissions_tag = article_meta_tag.find("./permissions")
if permissions_tag is None:
# insert the new tag into the XML after the pub-history or history tag
insert_index = None
for tag_index, tag in enumerate(article_meta_tag.findall("*")):
if tag.tag == "pub-history":
insert_index = tag_index + 1
break
if tag.tag == "history":
insert_index = tag_index + 1
permissions_tag = Element("permissions")
article_meta_tag.insert(insert_index, permissions_tag)
if license_data_dict:
if license_data_dict.get("copyright"):
jats_build.set_copyright_tags(
permissions_tag, copyright_year, copyright_holder
)
ali_free_to_read_tag = SubElement(
permissions_tag, "{http://www.niso.org/schemas/ali/1.0/}free_to_read"
)
set_license_tag(permissions_tag, license_data_dict)


CONTRIB_TYPE_ROLE_MAP = {"editor": "Reviewing Editor", "senior_editor": "Senior Editor"}


def editor_contributors(docmap_string, version_doi):
"populate Contributor objects with editor data from a docmap"
editors = []

data = docmap_parse.docmap_editor_data(docmap_string, version_doi)

for data_item in data:
contrib_type = data_item.get("role", "").replace("-", "_")
roles = []
aff = None
if contrib_type:
editor_role = Role()
editor_role.text = CONTRIB_TYPE_ROLE_MAP.get(contrib_type)
roles.append(editor_role)
if data_item.get("actor"):
surname = data_item.get("actor").get("surname")
given_name = " ".join(
[
name_part
for name_part in [
data_item.get("actor").get("firstName"),
data_item.get("actor").get("_middleName"),
]
if name_part
]
)
# format affiliation
affiliation_data_dict = data_item.get("actor").get("affiliation")
if affiliation_data_dict:
aff = Affiliation()
if (
affiliation_data_dict.get("name")
and affiliation_data_dict.get("type") == "organization"
):
aff.institution = (
data_item.get("actor").get("affiliation").get("name")
)
if affiliation_data_dict.get("location"):
# parse and set city and country values
location_parts = affiliation_data_dict.get("location").split(",")
if len(location_parts) == 2:
aff.city = location_parts[0]
aff.country = location_parts[1].lstrip()

editor = Contributor(contrib_type, surname, given_name)
editor.roles = roles
if aff:
editor.set_affiliation(aff)
editors.append(editor)

return editors


def set_editors(parent, editors):
"set editor contrib tags"
if not editors:
return
# find where to insert a contrib-group tag
insert_index = None
for tag_index, tag in enumerate(parent.findall("*")):
if tag.tag == "contrib-group":
insert_index = tag_index + 1
# insert contrib-group tag
contrib_group_tag = Element("contrib-group")
if insert_index:
parent.insert(insert_index, contrib_group_tag)
else:
parent.append(contrib_group_tag)
contrib_group_tag.set("content-type", "section")
for editor in editors:
contrib_tag = SubElement(contrib_group_tag, "contrib")
contrib_tag.set("contrib-type", editor.contrib_type)
jats_build.set_contrib_name(contrib_tag, editor)
jats_build.set_contrib_orcid(contrib_tag, editor)
# role tag
jats_build.set_contrib_role(contrib_tag, editor.contrib_type, editor)
# add inline aff tags
for affiliation in editor.affiliations:
jats_build.set_aff(
contrib_tag,
affiliation,
editor.contrib_type,
tail="",
institution_wrap=True,
)
14 changes: 14 additions & 0 deletions elifecleaner/pub_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
from elifecleaner.prc import date_struct_from_string


def prune_history_data(history_data, doi, version):
"return history data related to doi for versions less than version provided"
return [
data
for data in history_data
if not data.get("doi").startswith(doi)
or (
data.get("doi").startswith(doi)
and data.get("versionIdentifier")
and int(data.get("versionIdentifier")) < int(version)
)
]


def find_pub_history_tag(root, identifier=None):
"find pub-history tag in the article-meta tag"
article_meta_tag = root.find(".//front/article-meta")
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
docmaptools==0.25.0
elifetools==0.32.0
elifearticle==0.14.0
jatsgenerator==0.7.0
docmaptools==0.26.0
elifetools==0.40.0
elifearticle==0.20.0
jatsgenerator==0.16.0
mock==4.0.3
pytest==6.2.2
PyYAML==5.4.1
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
packages=["elifecleaner"],
license="MIT",
install_requires=[
"docmaptools>=0.25.0",
"elifetools",
"elifearticle>=0.15.0",
"jatsgenerator>=0.7.0",
"docmaptools>=0.26.0",
"elifetools>=0.40.0",
"elifearticle>=0.20.0",
"jatsgenerator>=0.16.0",
"PyYAML>=5.4.1",
"wand >= 0.5.2",
],
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/sub_articles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<given-names>Given</given-names>
<suffix>X</suffix>
</name>
<contrib-id authenticated="true" contrib-id-type="orcid">http://orcid.org/https://orcid.org/0000-0000-0000-0000</contrib-id>
<contrib-id authenticated="true" contrib-id-type="orcid">http://orcid.org/0000-0000-0000-0000</contrib-id>
<aff>
<institution-wrap>
<institution-id institution-id="ror">ror</institution-id>
Expand Down
Loading

0 comments on commit 3dea2b0

Please sign in to comment.