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

Add the ConfMixin and update RecordHepConfPaper

parent 8e4dbfc0
"""confmixin.py
"""
from datetime import datetime
from plugin_dbui import CLEAN_SPACES
class ConfMixin(object):
"""Mixin to handle conference data.
The parent class must have the attribute ``conference``.
It is a dictionary with at least the following keys:
* addresses: [{cities: [], country: str, ...}, ...]
* cnum: str
* control_number: int
* closing_date: str
* opening_date: str
* titles: [{value: str}, ...]
* urls: [{value: str}, ...]
"""
def conference_country(self):
"""The country where the conference took place.
Returns:
str:
* the filter *CLEAN_SPACES* is applied.
* empty when the country is not defined.
"""
conference = self.conference
if conference is None:
return ""
val = (conference
.get("addresses", [{}])[0]
.get("country", ""))
return CLEAN_SPACES(val)
def conference_dates(self):
"""The dates of the conference.
Returns:
str:
* usual pattern are ``6-5 March 2012``
or ``30 March - 5 April 2012``
* empty string when not defined
"""
conference = self.conference
if conference is None:
return ""
opening = conference.get("opening_date", None)
closing = conference.get("closing_date", None)
if opening is None or closing is None:
return ""
ds = datetime.strptime(opening, "%Y-%m-%d")
de = datetime.strptime(closing, "%Y-%m-%d")
val = de.strftime("%d %b %Y")
if ds.month == de.month:
val = f"{ds.day}-{val}"
else:
val = f"{ds.strftime('%d %b')} - {val}"
return val
def conference_id(self):
"""The conference identifier used in the store.
Returns:
int or None
"""
conference = self.conference
if conference is None:
return ""
return conference.get("control_number", None)
def conference_key(self):
"""The conference key used in the store.
Returns:
str:
* empty string when it is not defined
"""
for elt in self["publication_info"]:
if "cnum" in elt:
return elt["cnum"]
return ""
def conference_location(self):
"""The conference location.
Returns:
str:
- the pattern is ``town, country``
- empty string when town or country is missing
- empty string when it is not defined
"""
conference = self.conference
if conference is None:
return ""
addresses = conference.get("addresses", [{}])[0]
city = addresses.get("cities", [None])[0]
country = addresses.get("country", None)
if city is None or country is None:
return ""
return f"{city}, {country}"
def conference_title(self):
"""The title of the conference.
Returns:
str:
* empty string when it is not defined
* first title is selected is there is more than one
"""
conference = self.conference
if conference is None:
return ""
val = (conference
.get("titles", [{}])[0]
.get("title", ""))
return val
def conference_town(self):
"""The town where the conference took place.
Returns:
str:
* empty string when it is not defined.
"""
conference = self.conference
if conference is None:
return ""
val = (conference
.get("addresses", [{}])[0]
.get("cities", [""])[0])
return val
def conference_url(self):
"""The URL of the conference home page.
Returns:
str:
* select the first URL when there is more than one
* empty string when it is not defined
"""
conference = self.conference
if conference is None:
return ""
val = (conference
.get("urls", [{}])[0]
.get("value", ""))
return val
def conference_year(self):
"""The year of the conference.
Returns:
str:
* empty string when it is not defined.
"""
conference = self.conference
if conference is None:
return ""
opening = conference.get("opening_date", None)
return ("" if opening is None else opening[:4])
......@@ -4,12 +4,11 @@
import requests
from .base import T4, T6
from datetime import datetime
from plugin_dbui import CLEAN_SPACES
from .confmixin import ConfMixin
from .recordheppubli import RecordHepPubli
class RecordHepConfPaper(RecordHepPubli):
class RecordHepConfPaper(RecordHepPubli, ConfMixin):
"""Conference proceeding from inspirehep.net version 2.
Schema for conference paper is documented here:
......@@ -71,176 +70,5 @@ class RecordHepConfPaper(RecordHepPubli):
logger.debug(f"{T6}failed to retrieve conference data")
return
# append conference data
self.conference = obj.get("metadata", None)
def conference_country(self):
"""The country where the conference took place.
Returns:
str:
* the filter *CLEAN_SPACES* is applied.
* empty when the country is not defined.
"""
conference = self.conference
if conference is None:
return ""
val = (conference
.get("addresses", [{}])[0]
.get("country", ""))
return CLEAN_SPACES(val)
def conference_dates(self):
"""The dates of the conference.
Returns:
str:
* usual pattern are ``6-5 March 2012``
or ``30 March - 5 April 2012``
* empty string when not defined
"""
conference = self.conference
if conference is None:
return ""
opening = conference.get("opening_date", None)
closing = conference.get("closing_date", None)
if opening is None or closing is None:
return ""
ds = datetime.strptime(opening, "%Y-%m-%d")
de = datetime.strptime(closing, "%Y-%m-%d")
val = de.strftime("%d %b %Y")
if ds.month == de.month:
val = f"{ds.day}-{val}"
else:
val = f"{ds.strftime('%d %b')} - {val}"
return val
def conference_id(self):
"""The conference identifier used in the store.
Returns:
int or None
"""
conference = self.conference
if conference is None:
return ""
return conference.get("control_number", None)
def conference_key(self):
"""The conference key used in the store.
Returns:
str:
* empty string when it is not defined
"""
for elt in self["publication_info"]:
if "cnum" in elt:
return elt["cnum"]
return ""
def conference_location(self):
"""The conference location.
Returns:
str:
- the pattern is ``town, country``
- empty string when town or country is missing
- empty string when it is not defined
"""
conference = self.conference
if conference is None:
return ""
addresses = conference.get("addresses", [{}])[0]
city = addresses.get("cities", [None])[0]
country = addresses.get("country", None)
if city is None or country is None:
return ""
return f"{city}, {country}"
def conference_title(self):
"""The title of the conference.
Returns:
str:
* empty string when it is not defined
* first title is selected is there is more than one
"""
conference = self.conference
if conference is None:
return ""
val = (conference
.get("titles", [{}])[0]
.get("title", ""))
return val
def conference_town(self):
"""The town where the conference took place.
Returns:
str:
* empty string when it is not defined.
"""
conference = self.conference
if conference is None:
return ""
val = (conference
.get("addresses", [{}])[0]
.get("cities", [""])[0])
return val
def conference_url(self):
"""The URL of the conference home page.
Returns:
str:
* select the first URL when there is more than one
* empty string when it is not defined
"""
conference = self.conference
if conference is None:
return ""
val = (conference
.get("urls", [{}])[0]
.get("value", ""))
return val
def conference_year(self):
"""The year of the conference.
Returns:
str:
* empty string when it is not defined.
"""
conference = self.conference
if conference is None:
return ""
opening = conference.get("opening_date", None)
return ("" if opening is None else opening[:4])
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