Docker-in-Docker (DinD) capabilities of public runners deactivated. More info

recordcdsconfpaper.py 5.71 KB
Newer Older
1
""" store_tools.recordcdsconfpaper
2 3

"""
4 5
import re

6 7 8 9
from .base import (MSG_WELL_FORMED_DATE,
                   REG_DATE_YYYYMM,
                   T4,
                   T6)
10
from .cdsstore import CdsStore
11
from .confmixin import ConfMixin
12
from .exception import CheckException
13 14 15
from .recordcdspubli import RecordCdsPubli

REX_DATE8 = re.compile(r"(\d{4})(\d{2})(\d{2})")
16 17


18
class RecordCdsConfPaper(RecordCdsPubli, ConfMixin):
LE GAC Renaud's avatar
LE GAC Renaud committed
19 20
    """The record describing a conference talk or a proceeding.

21 22 23
    Attributes:
        conference (dict or None):
            the conference metadata:
24 25 26 27 28 29 30 31 32 33 34 35 36

            +----------------+----------------------------------------+
            | 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                                    |
            +----------------+----------------------------------------+
37

38
    """
LE GAC Renaud's avatar
LE GAC Renaud committed
39

40
    def __init__(self, recjson):
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        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:
73
            logger.debug(f"{T6}search conference by id {conf_id}")
74 75
            recjson = store.get_record(conf_id)

76
            if recjson["recid"] != int(conf_id):
77 78 79 80 81 82 83
                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:
84
            logger.debug(f"{T6}search conference by key {conf_key}")
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
            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(",")
120
        url = recjson.get("url", {}).get("url", None)
121 122 123 124 125 126 127

        dct = {
            "addresses": [{
                "cities": [city.strip()],
                "country": country.strip()}],
            "cnum": data.get("coference_code"),
            "closing_date": data.get("closing_date", None),
128
            "control_number": recjson["recid"],
129
            "opening_date": data.get("opening_date", None),
130 131
            "titles": [{"title": data.get("meeting", None)}],
            "urls": (None if url is None else [{"value": url}]),
132 133 134 135 136 137 138 139 140 141 142 143
            "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
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

    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)