Commit 9b4a0fdb authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Redesing the base function is_conference, is_institute, is_thesis.

parent 5a7bf40d
......@@ -31,64 +31,81 @@ REG_YEAR = re.compile(r"(\d{4})")
THESIS_DIR = "dir."
def is_conference(record):
def is_conference(recjson):
"""True when the record describes a publication related to a conference.
Args:
record (Record): MARC12 record associated to a publication
or to and institute.
recjson (dict):
record associated to a publication or to and institute.
Return:
bool: ``True`` when the MARC record describes a publication related
bool:
``True`` when the record describes a publication related
to a conference.
"""
if "111" in record:
# ConferencePaper in collection
# find proceeding in both stores
if "collection" in recjson:
data = recjson["collection"]
data = (data if isinstance(data, list) else [data])
value = "ConferencePaper"
li = [di for di in data if "primary" in di and di["primary"] == value]
if len(li) > 0:
return True
# try with the conference key
# the location of this values depends on the store
# cds.cern.ch (962, n) and inspirehep.net (773,w).
if record.host().startswith("cds"):
field, subfield = "962", "n"
else:
field, subfield = "773", "w"
# try to identify talk in cds
# look for a conference key or for a subject equal to Talk
found = \
("aleph_linking_page" in recjson) or \
("subject" in recjson
and "term" in recjson["subject"]
and recjson["subject"]["term"] == u"Talk")
return len(record._get(field, subfield)) > 0
if found:
return True
# try to identify talk in inspirehep
# look for a conference key
found = \
("publication_info" in recjson
and "cnum" in recjson["publication_info"])
if found:
return True
return False
def is_institute(record):
def is_institute(recjson):
"""True when the record describes an institute.
Args:
record (Record): MARC12 record associated to a publication
or to and institute.
recjson (dict):
record associated to a publication or to and institute.
Return:
bool: ``True`` when the MARC record describes an institute.
bool:
``True`` when the record describes an institute.
"""
# "980": [
# {"b": ["CK90", "HEP200", "PDGLIST", "PPF", "TOP500", "WEB"]},
# {"a": "INSTITUTION"},
# {"a": "CORE"}
# ]
if "980" in record:
if isinstance(record["980"], list):
for di in record["980"]:
for k, v in di.items():
if k == "a" and v == "INSTITUTION":
return True
# INSTITUTION in collection
if "collection" in recjson:
data = recjson["collection"]
data = (data if isinstance(data, list) else [data])
elif isinstance(record["980"], dict) and "a" in record["980"] and \
record["980"]["a"] == "INSTITUTION":
value = "INSTITUTION"
li = [di for di in data if "primary" in di and di["primary"] == value]
if len(li) > 0:
return True
return False
def is_thesis(record):
def is_thesis(recjson):
"""True when the record describes a thesis.
Args:
......@@ -99,6 +116,13 @@ def is_thesis(record):
bool: ``True`` when the MARC record describes a thesis.
"""
li = record._get("980", "a", force_list=True)
val = ", ".join(li)
return "THESIS" in val
# THESIS in collection
if "collection" in recjson:
data = recjson["collection"]
data = (data if isinstance(data, list) else [data])
value = "THESIS"
li = [di for di in data if "primary" in di and di["primary"] == value]
if len(li) > 0:
return True
# -*- coding: utf-8 -*-
"""test_instantiate_Record
* Test functions to introspect the type of record.
* Test functions to upcast the Record from the JSON object.
"""
from invenio_tools.base import is_conference, is_institute, is_thesis
from invenio_tools.factory import build_record
from invenio_tools.inveniostore import InvenioStore
from invenio_tools.recordconf import RecordConf
from invenio_tools.recordinst import RecordInst
from invenio_tools.recordpubli import RecordPubli
from invenio_tools.recordthesis import RecordThesis
def test_conference_cds():
store = InvenioStore("cds.cern.ch")
recjson = store.get_record(1411352)
assert is_conference(recjson)
assert not is_institute(recjson)
assert not is_thesis(recjson)
record = build_record(recjson)
assert isinstance(record, RecordConf)
def test_conference_inspirehep():
store = InvenioStore("inspirehep.net")
recjson = store.get_record(1276938)
assert is_conference(recjson)
assert not is_institute(recjson)
assert not is_thesis(recjson)
record = build_record(recjson)
assert isinstance(record, RecordConf)
def test_institute():
"""CPPM"""
store = InvenioStore("inspirehep.net")
recjson = store.get_record(902989)
assert not is_conference(recjson)
assert is_institute(recjson)
assert not is_thesis(recjson)
record = build_record(recjson)
assert isinstance(record, RecordInst)
def test_publi_cds():
"""Precision luminosity measurements at LHCb"""
store = InvenioStore("cds.cern.ch")
recjson = store.get_record(1951625)
assert not is_conference(recjson)
assert not is_institute(recjson)
assert not is_thesis(recjson)
record = build_record(recjson)
assert isinstance(record, RecordPubli)
def test_publi_inspirehep():
"""Precision luminosity measurements at LHCb"""
store = InvenioStore("inspirehep.net")
recjson = store.get_record(1319638)
assert not is_conference(recjson)
assert not is_institute(recjson)
assert not is_thesis(recjson)
record = build_record(recjson)
assert isinstance(record, RecordPubli)
def test_talk_cds():
store = InvenioStore("cds.cern.ch")
recjson = store.get_record(2239092)
assert is_conference(recjson)
assert not is_institute(recjson)
assert not is_thesis(recjson)
record = build_record(recjson)
assert isinstance(record, RecordConf)
def test_thesis_cds():
store = InvenioStore("cds.cern.ch")
recjson = store.get_record(1632177)
assert not is_conference(recjson)
assert not is_institute(recjson)
assert is_thesis(recjson)
record = build_record(recjson)
assert isinstance(record, RecordThesis)
"""test_marc
"""
from invenio_tools import (is_conference,
is_institute,
is_thesis,
load_record,
Record,
RecordConf,
RecordInst,
RecordPubli,
RecordThesis)
def test_conference_cds():
record = load_record("cds.cern.ch", 1411352)
assert is_conference(record) == True
assert is_institute(record) == False
assert is_thesis(record) == False
assert isinstance(record, RecordConf)
def test_conference_inspirehep():
record = load_record("inspirehep.net", 1276938)
assert is_conference(record) == True
assert is_institute(record) == False
assert is_thesis(record) == False
assert isinstance(record, RecordConf)
def test_institute():
"""CPPM"""
record = load_record("inspirehep.net", 902989)
assert is_conference(record) == False
assert is_institute(record) == True
assert is_thesis(record) == False
assert isinstance(record, RecordInst)
def test_publi_cds():
"""Precision luminosity measurements at LHCb"""
record = load_record("cds.cern.ch", 1951625)
assert is_conference(record) == False
assert is_institute(record) == False
assert is_thesis(record) == False
assert isinstance(record, RecordPubli)
def test_publi_inspirehep():
"""Precision luminosity measurements at LHCb"""
record = load_record("inspirehep.net", 1319638)
assert is_conference(record) == False
assert is_institute(record) == False
assert is_thesis(record) == False
assert isinstance(record, RecordPubli)
def test_thesis_cds():
record = load_record("cds.cern.ch", 1632177)
assert is_conference(record) == False
assert is_institute(record) == False
assert is_thesis(record) == True
assert isinstance(record, RecordThesis)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment