19 |
19 |
20 __docformat__ = "restructuredtext en" |
20 __docformat__ = "restructuredtext en" |
21 |
21 |
22 import re |
22 import re |
23 from socket import gethostname |
23 from socket import gethostname |
|
24 import logging |
24 |
25 |
25 from logilab.common.textutils import text_to_dict |
26 from logilab.common.textutils import text_to_dict |
26 from logilab.common.configuration import OptionError |
27 from logilab.common.configuration import OptionError |
|
28 from logilab.mtconverter import xml_escape |
27 |
29 |
28 from cubicweb import ValidationError |
30 from cubicweb import ValidationError |
29 from cubicweb.entities import AnyEntity, fetch_config |
31 from cubicweb.entities import AnyEntity, fetch_config |
30 |
32 |
31 class _CWSourceCfgMixIn(object): |
33 class _CWSourceCfgMixIn(object): |
129 return self.cw_schema[0] |
131 return self.cw_schema[0] |
130 |
132 |
131 @property |
133 @property |
132 def cwsource(self): |
134 def cwsource(self): |
133 return self.cw_for_source[0] |
135 return self.cw_for_source[0] |
|
136 |
|
137 |
|
138 class CWDataImport(AnyEntity): |
|
139 __regid__ = 'CWDataImport' |
|
140 |
|
141 def __init__(self, *args, **kwargs): |
|
142 super(CWDataImport, self).__init__(*args, **kwargs) |
|
143 self._logs = [] |
|
144 |
|
145 def dc_title(self): |
|
146 return '%s [%s]' % (self.printable_value('start_timestamp'), |
|
147 self.printable_value('status')) |
|
148 |
|
149 @property |
|
150 def cwsource(self): |
|
151 return self.cw_import_of[0] |
|
152 |
|
153 def record_debug(self, msg, path=None, line=None): |
|
154 self._log(logging.DEBUG, msg, path, line) |
|
155 self.debug(msg) |
|
156 |
|
157 def record_info(self, msg, path=None, line=None): |
|
158 self._log(logging.INFO, msg, path, line) |
|
159 self.info(msg) |
|
160 |
|
161 def record_warning(self, msg, path=None, line=None): |
|
162 self._log(logging.WARNING, msg, path, line) |
|
163 self.warning(msg) |
|
164 |
|
165 def record_error(self, msg, path=None, line=None): |
|
166 self._status = u'failed' |
|
167 self._log(logging.ERROR, msg, path, line) |
|
168 self.error(msg) |
|
169 |
|
170 def record_fatal(self, msg, path=None, line=None): |
|
171 self._status = u'failed' |
|
172 self._log(logging.FATAL, msg, path, line) |
|
173 self.fatal(msg) |
|
174 |
|
175 def _log(self, severity, msg, path=None, line=None): |
|
176 encodedmsg = u'%s\t%s\t%s\t%s<br/>' % (severity, path or u'', |
|
177 line or u'', xml_escape(msg)) |
|
178 self._logs.append(encodedmsg) |
|
179 |
|
180 def write_log(self, session, **kwargs): |
|
181 if 'status' not in kwargs: |
|
182 kwargs['status'] = getattr(self, '_status', u'success') |
|
183 self.set_attributes(log=u'<br/>'.join(self._logs), **kwargs) |
|
184 self._logs = [] |