Commit f531e739 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Add recordhep.py

parent e42aa50d
"""recordhep.py
"""
import pprint
class RecordHep(dict):
"""Base class for JSON record coming from inspirehep.net version v2.
Schema documentation is defined here:
https://inspire-schemas.readthedocs.io/en/latest/schemas/
Args:
recjson (dict):
meta data from the JSON record returns by the store
"""
def __init__(self, recjson):
super().__init__(recjson)
# meta data
# the authors of my institutes signing the record
# string containing a list of name separated by a comma
self.my_authors = ""
@staticmethod
def _oai_url(value):
"""Build the Open Archive Initiative URL.
Args:
value (str):
OAI identifier, *e.g.* ``oai:host:id``
Returns:
str:
the pattern of the string is `https://host/api/literature/id`
The string is empty when it is not defined or when the value
is not well formed.
"""
def debug(self):
"""Print the record structure on the standard output.
"""
pprint.pprint(self)
def host(self):
"""The store housing the record.
Returns:
str:
"""
return "inspirehep.net"
def id(self):
"""The id of the record in the store.
Returns:
int:
"""
return self["control_number"]
def oai(self):
"""The Open Archive Initiative identifier(s).
Returns:
str:
the primary and secondary OAI identifier are separated
by a comma. The pattern of the identifier is ``oai:host:id`` or
an empty string when it is not defined.
"""
lst = [self.primary_oai(), self.secondary_oai()]
return ", ".join(lst).strip(", ")
def oai_url(self):
"""The Open Archive Initiative identifier URL(s).
Returns:
str:
the primary and secondary URLs are separated by a comma.
The pattern of the URL is ``https://host/api/literature/id``
or an empty string when it is not defined or when the OAI is
not well formed.
"""
lst = [self.primary_oai_url(), self.secondary_oai_url()]
return ", ".join(lst).strip(", ")
def primary_oai(self):
"""The primary Open Archive Initiative identifier.
The primary OAI identifier matches the record identifier.
Returns:
str:
the pattern of the string is ``oai:host:id``.
It is an empty string when not defined
"""
return f"oai:inspirehep.net:{self['control_number']}"
def primary_oai_url(self):
"""The Open Archive Initiative URL for the primary OAI.
Returns:
str:
the pattern of the string is ``https://host/api/literature/id``.
The string is empty when it is not defined or when the OAI
is not well formed.
"""
recid = self["control_number"]
return f"https://inspirehep.net/api/literature/{recid}"
def secondary_oai(self):
"""The secondary OAI identifier.
the secondary OAI identifier corresponds to the record in the
store, *cds.cern.ch*.
Returns:
str:
the pattern of the string is ``oai:host:id``.
It is an empty string when not defined
"""
if "external_system_identifiers" not in self:
return ""
for elt in self["external_system_identifiers"]:
if elt["schema"] == "CDS":
return f"oai:cds.cern.ch:{elt['value']}"
return ""
def secondary_oai_url(self):
"""The Open Archive Initiative URL for the secondary OAI.
the secondary OAI URL corresponds to the record in the
store, *cds.cern.ch*.
Returns:
str:
the pattern of the string is ``https://host/record/id``.
The string is empty when it is not defined or when the OAI
is not well formed.
"""
if "external_system_identifiers" not in self:
return ""
for elt in self["external_system_identifiers"]:
if elt["schema"] == "CDS":
return f"https://cds.cern.ch/record/{elt['value']}"
return ""
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