entities/sources.py
changeset 7995 9a9f35ef418c
parent 7827 9bbf83f68bcc
child 8068 72210779ff6d
--- a/entities/sources.py	Fri Oct 21 14:32:37 2011 +0200
+++ b/entities/sources.py	Fri Oct 21 14:32:37 2011 +0200
@@ -21,9 +21,11 @@
 
 import re
 from socket import gethostname
+import logging
 
 from logilab.common.textutils import text_to_dict
 from logilab.common.configuration import OptionError
+from logilab.mtconverter import xml_escape
 
 from cubicweb import ValidationError
 from cubicweb.entities import AnyEntity, fetch_config
@@ -131,3 +133,52 @@
     @property
     def cwsource(self):
         return self.cw_for_source[0]
+
+
+class CWDataImport(AnyEntity):
+    __regid__ = 'CWDataImport'
+
+    def __init__(self, *args, **kwargs):
+        super(CWDataImport, self).__init__(*args, **kwargs)
+        self._logs = []
+
+    def dc_title(self):
+        return '%s [%s]' % (self.printable_value('start_timestamp'),
+                            self.printable_value('status'))
+
+    @property
+    def cwsource(self):
+        return self.cw_import_of[0]
+
+    def record_debug(self, msg, path=None, line=None):
+        self._log(logging.DEBUG, msg, path, line)
+        self.debug(msg)
+
+    def record_info(self, msg, path=None, line=None):
+        self._log(logging.INFO, msg, path, line)
+        self.info(msg)
+
+    def record_warning(self, msg, path=None, line=None):
+        self._log(logging.WARNING, msg, path, line)
+        self.warning(msg)
+
+    def record_error(self, msg, path=None, line=None):
+        self._status = u'failed'
+        self._log(logging.ERROR, msg, path, line)
+        self.error(msg)
+
+    def record_fatal(self, msg, path=None, line=None):
+        self._status = u'failed'
+        self._log(logging.FATAL, msg, path, line)
+        self.fatal(msg)
+
+    def _log(self, severity, msg, path=None, line=None):
+        encodedmsg =  u'%s\t%s\t%s\t%s<br/>' % (severity, path or u'',
+                                                line or u'', xml_escape(msg))
+        self._logs.append(encodedmsg)
+
+    def write_log(self, session, **kwargs):
+        if 'status' not in kwargs:
+            kwargs['status'] = getattr(self, '_status', u'success')
+        self.set_attributes(log=u'<br/>'.join(self._logs), **kwargs)
+        self._logs = []