""" store_tools.recordcdsconfpaper """ import re from .base import (MSG_WELL_FORMED_DATE, REG_DATE_YYYYMM, T4, T6) from .cdsstore import CdsStore from .confmixin import ConfMixin from .exception import CheckException from .recordcdspubli import RecordCdsPubli REX_DATE8 = re.compile(r"(\d{4})(\d{2})(\d{2})") class RecordCdsConfPaper(RecordCdsPubli, ConfMixin): """The record describing a conference talk or a proceeding. Attributes: conference (dict or None): the conference metadata: +----------------+----------------------------------------+ | key | value | +----------------+----------------------------------------+ | addresses | [{cities: [], country: str, ...}, ...] | | cnum | str | | control_number | int | | closing_date | str | | opening_date | str | | titles | [{title: str}, ...] | | urls | [{value: str}, ...] | | year | str | +----------------+----------------------------------------+ """ def __init__(self, recjson): super().__init__(recjson) self.conference = None self._process_conference_data() def _process_conference_data(self): """Append the conference data to the record. """ logger = self.logger logger.debug(f"{T4}process conference data") if "aleph_linking_page" not in self: logger.debug(f"{T6}no field 'aleph_linking_page'") return dct = self.get("aleph_linking_page", {}) conf_id = dct.get("sysno", None) conf_key = dct.get("up_link", None) if conf_id is None and conf_key is None: logger.debug(f"{T6}no conference id and key") return # ........................................................................ # # Get conference data first id and then by key # store = CdsStore("cds.cern.ch") if conf_id is not None: logger.debug(f"{T6}search conference by id {conf_id}") recjson = store.get_record(conf_id) if recjson["recid"] != int(conf_id): logger.debug(f"{T6}failed to retrieve conference by id") if recjson.get("meeting_name", None) is None: logger.debug(f"{T6}no field 'meeting_name'") return elif conf_key is not None: logger.debug(f"{T6}search conference by key {conf_key}") ids = store.get_ids(p=conf_key) mtch = False for cid in ids: recjson = store.get_record(cid) for elt in recjson.get("meeting_name", []): ck = elt.get("coference_code", "") if ck == conf_key: mtch = True break if mtch is True: break if mtch is False: logger.debug(f"{T6}failed to retrieve conference by key") return # ........................................................................ # # Decode conference data # Convert structure to the one provides by the new inspirehep.net # data = None for elt in recjson["meeting_name"]: if "year" in elt: data = elt break if data is None: logger.debug(f"{T6}conference data not found") return city, country = data.get("location", ",").split(",") # extract conference URL obj = recjson.get("url", {}) if isinstance(obj, dict): url = recjson.get("url", {}).get("url", None) elif isinstance(obj, list): for dct in obj: if dct.get("description", "") == "Conference home page": url = dct["url"] break dct = { "addresses": [{ "cities": [city.strip()], "country": country.strip()}], "cnum": data.get("coference_code"), "closing_date": data.get("closing_date", None), "control_number": recjson["recid"], "opening_date": data.get("opening_date", None), "titles": [{"title": data.get("meeting", None)}], "urls": (None if url is None else [{"value": url}]), "year": data.get("year", None)} # date format issue YYYYMMDD to YYYY-MM-DD for k in ("closing_date", "opening_date"): mtch = REX_DATE8.match(dct[k]) if mtch: dct[k] = "-".join(mtch.groups()) # ........................................................................ # # Append conference data self.conference = dct def check_submitted_date(self): """Check that submitted date is either ``YYYY-MM`` or ``YYYY-MM-DD``. Raises: CheckException:: * the date is not well formed """ self.logger.debug(f"{T6}check submitted date") xdate = self.submitted() if REG_DATE_YYYYMM.match(xdate): return # recover by using the opening date of the conference val = self.conference.get("opening_date", None) if val is not None: if "prepublication" in self: prepublication = self["prepublication"] if isinstance(prepublication, list): prepublication[0]["date"] = val else: prepublication["date"] = val else: self["prepublication"] = {"date": val} else: raise CheckException(MSG_WELL_FORMED_DATE)