# -*- coding: utf-8 -*- """ harvest_tools.msg """ import json from gluon.storage import Storage from invenio_tools import OAI_URL class Msg(Storage): """Message and action taken for a publication. - The publication is found by an harvester tool, in a store. - The action refers to the database. Fours action are defined: - C{idle} - C{load} - C{modify} - C{reject} The class contains the attributes: - C{action}: action taken - C{collection}: the harvester collection - C{harvester}: the harvester encoded as a JSON string - C{record_id}; the store identifier of the record - C{title}: title of the publication - C{txt}: text of the message - C{url}: url of the record - C{year}: year of the publication """ def __init__(self, collection=None, harvester=None, record_id=None, title=None): """ @type collection: str @param collection: the collection containing the record @type harvester: gluon.dal.Row or gluon.storage.Storage @param harvester: the current harvester used to retrieve the record. @type record_id: int @param record_id: the store identifier of the record @type title: str @param title: the title associated to the record """ 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.title = title self.txt = None self.url = OAI_URL % (harvester.host, record_id) self.year = None def idle(self, txt, year=None): """Set the action as idle and the message as C{txt}. @type txt: unicode @param txt: message @type year: unicode @param year: year of the publication """ self.action = 'idle' self._set(txt, year) def load(self, txt, year=None): """Set the action as C{load} and the message as C{txt}. @type txt: unicode @param txt: message @type year: unicode @param year: year of the publication """ self.action = 'load' self._set(txt, year) def modify(self, txt, year=None): """Set the action as C{modify} and the message as C{txt}. @type txt: unicode @param txt: message @type year: unicode @param year: year of the publication """ self.action = 'modify' self._set(txt, year) def reject(self, txt, year=None): """Set the action as C{reject} set the message as C{txt}. @type txt: unicode @param txt: message @type year: unicode @param year: year of the publication """ self.action = 'reject' self._set(txt, year) def _set(self, txt, year): if isinstance(txt, unicode): txt = txt.encode("utf-8") elif not isinstance(txt, str): txt = str(txt) self.txt = txt if year: if isinstance(year, list): self.year = ', '.join(year) else: self.year = year