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

Feat: AM format #774

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
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
Empty file.
130 changes: 130 additions & 0 deletions packtools/sps/formats/am/am.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import datetime

from packtools.sps.models.article_ids import ArticleIds


def code(xml_tree, am_dict):
article_id_v2 = ArticleIds(xml_tree).v2
am_dict.update({"code": article_id_v2})
return am_dict


def collection(xml_tree, am_dict):
# TODO: adicionar a lógica de obtenção do código da coleção
return am_dict


def processing_date(xml_tree, am_dict):
date = (
datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[
:-3
]
+ "Z"
)
am_dict.update({"processing_date": date})
return am_dict


def _citation_id(citation_data, citation_dict):
citation_id = citation_data.get("ref_id")
citation_dict.update({"v700": [{"_": citation_id}]})
return citation_dict


def _citation_code(citation_data, citation_dict):
# TODO: adicionar a lógica de obtenção do código da citação, eg.: "code": "S1519-6984202400010016500001"
return citation_dict


def _citation_date(citation_data, citation_dict):
citation_date = citation_data.get("year") + "0000"
citation_dict.update({"v865": [{"_": citation_date}]})
return citation_dict


def _citation_title(citation_data, citation_dict):
# TODO: adicionar a lógica de obtenção do idioma da citação
citation_title = citation_data.get("article_title")
citation_dict.update({"v12": [{"_": citation_title}]})
return citation_dict


def _citation_volume(citation_data, citation_dict):
citation_volume = citation_data.get("volume")
citation_dict.update({"v31": [{"_": citation_volume}]})
return citation_dict


def _translate_publication_type_to_literature_type(publication_type):
"""
Translates a publication type from the SciELO XML standard to the v705 classification.

Args:
publication_type (str): The 'publication-type' value from SciELO XML.

Returns:
str: The corresponding v705 classification. Defaults to 'D' (Document of work) if the type is not recognized.
"""
publication_type_to_v705 = {
"journal": "S", # Serial (journals, periodicals)
"book": "M", # Monograph (books)
"chapter": "C", # Component (book chapters)
"conference": "A", # Analytical (conference proceedings)
"thesis": "M", # Monograph (theses and dissertations)
"report": "M", # Monograph (technical reports)
"web": "G", # Collection (digital material)
"other": "D", # Document of work (undefined or others)
}
return publication_type_to_v705.get(publication_type, "D")


def _citation_literature_type(citation_data, citation_dict):
citation_type = citation_data.get("publication_type")
literature_type = _translate_publication_type_to_literature_type(citation_type)
citation_dict.update({"v705": [{"_": literature_type}]})
return citation_dict


def _citation_page_info(citation_data, citation_dict):
lpage = citation_data.get("lpage") or ""
fpage = citation_data.get("fpage") or ""
try:
range_page = str(int(lpage) - int(fpage))
except ValueError:
range_page = ""
elocation = citation_data.get("elocation_id") or ""
citation_dict.update({"v514": [{"_": "", "l": lpage, "f": fpage, "r": range_page, "e": elocation}]})
return citation_dict


def _citation_author(author_data):
# TODO adicionar a lógica de obtenção de "role" ("r")
return {"n": author_data.get("given-names"), "r": "ND", "s": author_data.get("surname"), "_": ""}


def _citation_authors(citation_data, citation_dict):
authors = []
for author_data in citation_data.get("all_authors"):
author_dict = _citation_author(author_data)
authors.append(author_dict)
citation_dict.update({"v10": authors})
return citation_dict


def _citation_institutional_author(author_data):
return {"_": author_data.get("collab")}

def _citation_institutional_authors(citation_data, citation_dict):
institutional_authors = []
for institutional_author_data in citation_data.get("all_authors"):
institutional_author_dict = _citation_institutional_author(institutional_author_data)
institutional_authors.append(institutional_author_dict)
citation_dict.update({"v17": institutional_authors})
return citation_dict


def _citation_publication_type(citation_data, citation_dict):
citation_type = citation_data.get("publication_type")
citation_dict.update({"v71": [{"_": citation_type}],})
return citation_dict

Empty file.
90 changes: 90 additions & 0 deletions tests/sps/formats/am/test_am.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from unittest import TestCase

from packtools.sps.utils import xml_utils
from packtools.sps.formats.am import am
from packtools.sps.models.article_citations import ArticleCitations


class AM(TestCase):
def setUp(self):
self.xml_tree = xml_utils.get_xml_tree(
"tests/sps/fixtures/standard_scielo_xml/S0080-62342022000100445_JATS.xml"
)
self.citation_data = list(ArticleCitations(self.xml_tree).article_citations)[0]
self.citation_data_collab = list(ArticleCitations(self.xml_tree).article_citations)[26]

def test_code(self):
self.assertDictEqual(
{"code": "S0080-62342022000100445"}, am.code(self.xml_tree, dict())
)

def test_citation_id(self):
self.assertDictEqual(
{"v700": [{"_": "B1"}]}, am._citation_id(self.citation_data, dict())
)

def test_citation_date(self):
self.assertDictEqual(
{"v865": [{"_": "20150000"}]}, am._citation_date(self.citation_data, dict())
)

def test_citation_title(self):
self.assertDictEqual(
{
"v12": [
{
"_": "Smoking and potentially preventable hospitalisation: the benefit of smoking cessation in older ages"
}
]
},
am._citation_title(self.citation_data, dict()),
)

def test_citation_volume(self):
self.assertDictEqual(
{"v31": [{"_": "150"}]}, am._citation_volume(self.citation_data, dict())
)

def test_citation_literature_type(self):
self.assertDictEqual(
{"v705": [{"_": "S"}]}, am._citation_literature_type(self.citation_data, dict())
)

def test_citation_page_info(self):
self.assertDictEqual(
{"v514": [{"_": "", "l": "91", "f": "85", "r": "6", "e": ""}]},
am._citation_page_info(self.citation_data, dict())
)

def test_citation_authors(self):
self.maxDiff = None
self.assertDictEqual(
{
"v10": [
{'_': '', 'n': 'B', 'r': 'ND', 's': 'Tran'},
{'_': '', 'n': 'MO', 'r': 'ND', 's': 'Falster'},
{'_': '', 'n': 'K', 'r': 'ND', 's': 'Douglas'},
{'_': '', 'n': 'F', 'r': 'ND', 's': 'Blyth'},
{'_': '', 'n': 'LR', 'r': 'ND', 's': 'Jorm'}
]
},
am._citation_authors(self.citation_data, dict())
)

def test_institutional_authors(self):
self.assertDictEqual(
{
"v17": [
{'_': ['Instituto Nacional de Câncer José Alencar Gomes da Silva']}
]
},
am._citation_institutional_authors(self.citation_data_collab, dict())
)

def test_citation_type(self):
self.assertDictEqual(
{"v71": [{"_": "journal"}]},
am._citation_publication_type(self.citation_data, dict())
)