# -*- coding: utf-8 -*- """ harvest_tools.msg """ import json from base import MSG_NO_ENTRY, MSG_TOOMANY_SYNONYM from gluon import current from gluon.storage import Storage from invenio_tools import OAI_URL MSGS = (MSG_NO_ENTRY, MSG_TOOMANY_SYNONYM) TABLES = ("collaborations", "countries", "publishers") class Msg(Storage): """Action taken for a publication and its associated explanation. Note: Actions are defined with respect to the database: * *idle* do nothing. * *load* the record is insert in the database. * *modify* an existing record is modified. * *reject* the record is rejected. Args: collection (unicode): the harvester collection used to search the record. harvester (gluon.dal.Row): the database harvester used to scan the store. record_id (int): the record identifier in the store. title (unicode): the title of the publication. """ def __init__(self, collection=None, harvester=None, record_id=None, title=None): self.action = None self.collection = collection if isinstance(harvester, Storage): self.harvester = harvester else: self.harvester = json.dumps(harvester.as_dict()) self.record_id = record_id self.synonym = None self.title = title self.txt = None self.url = OAI_URL % (harvester.host, record_id) self.year = None def idle(self, txt, year=None, translate=True): """Set the action as *idle* and the explanation as ``txt``. Args: txt (unicode): message associated to the action. year (unicode): year of the publication translate (bool): translate the message according to the current language. """ self.action = "idle" self._set(txt, year, translate) def load(self, txt, year=None, translate=True): """Set the action as *load* and the explanation as ``txt``. Args: txt (unicode): message associated to the action. year (unicode): year of the publication translate (bool): translate the message according to the current language. """ self.action = "load" self._set(txt, year, translate) def modify(self, txt, year=None, translate=True): """Set the action as *modify* and the explanation as ``txt``. Args: txt (unicode): message associated to the action. year (unicode): year of the publication translate (bool): translate the message according to the current language. """ self.action = "modify" self._set(txt, year, translate) def reject(self, txt, year=None, record=None, translate=True): """Set the action as *reject* and the explanation as ``txt``. Args: txt (unicode): message associated to the action. year (unicode): year of the publication record (RecordPubli): the record on which the action is applied. It is used to determine the synonym value when the *collaboration*, *country* or *publisher* data is not understood. Note: The *year* argument is not needed when the *record* is specified. translate (bool): translate the message according to the current language. """ self.action = "reject" if record is not None: for msg in MSGS: for tablename in TABLES: if str(txt) == msg % tablename: if tablename == "collaborations": self.synonym = record.collaboration() elif tablename == "countries": self.synonym = record.conference_country() elif tablename == "publishers": self.synonym = record.paper_editor() if year is None and record is not None: year = record.year() self._set(txt, year, translate) def _set(self, txt, year, translate): if isinstance(txt, unicode): txt = txt.encode("utf-8") elif not isinstance(txt, str): txt = str(txt) if translate: txt = current.T(txt) self.txt = txt if year: if isinstance(year, list): self.year = ", ".join(year) else: self.year = year