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): |
52 self.set_attributes(config=cfgstr) |
54 self.set_attributes(config=cfgstr) |
53 |
55 |
54 |
56 |
55 class CWSource(_CWSourceCfgMixIn, AnyEntity): |
57 class CWSource(_CWSourceCfgMixIn, AnyEntity): |
56 __regid__ = 'CWSource' |
58 __regid__ = 'CWSource' |
57 fetch_attrs, fetch_order = fetch_config(['name', 'type']) |
59 fetch_attrs, cw_fetch_order = fetch_config(['name', 'type']) |
58 |
60 |
59 @property |
61 @property |
60 def host_config(self): |
62 def host_config(self): |
61 dictconfig = self.dictconfig |
63 dictconfig = self.dictconfig |
62 host = gethostname() |
64 host = gethostname() |
105 return self._cw.repo.sources_by_eid[self.eid] |
107 return self._cw.repo.sources_by_eid[self.eid] |
106 |
108 |
107 |
109 |
108 class CWSourceHostConfig(_CWSourceCfgMixIn, AnyEntity): |
110 class CWSourceHostConfig(_CWSourceCfgMixIn, AnyEntity): |
109 __regid__ = 'CWSourceHostConfig' |
111 __regid__ = 'CWSourceHostConfig' |
110 fetch_attrs, fetch_order = fetch_config(['match_host', 'config']) |
112 fetch_attrs, cw_fetch_order = fetch_config(['match_host', 'config']) |
111 |
113 |
112 @property |
114 @property |
113 def cwsource(self): |
115 def cwsource(self): |
114 return self.cw_host_config_of[0] |
116 return self.cw_host_config_of[0] |
115 |
117 |
117 return re.match(self.match_host, hostname) |
119 return re.match(self.match_host, hostname) |
118 |
120 |
119 |
121 |
120 class CWSourceSchemaConfig(AnyEntity): |
122 class CWSourceSchemaConfig(AnyEntity): |
121 __regid__ = 'CWSourceSchemaConfig' |
123 __regid__ = 'CWSourceSchemaConfig' |
122 fetch_attrs, fetch_order = fetch_config(['cw_for_source', 'cw_schema', 'options']) |
124 fetch_attrs, cw_fetch_order = fetch_config(['cw_for_source', 'cw_schema', 'options']) |
123 |
125 |
124 def dc_title(self): |
126 def dc_title(self): |
125 return self._cw._(self.__regid__) + ' #%s' % self.eid |
127 return self._cw._(self.__regid__) + ' #%s' % self.eid |
126 |
128 |
127 @property |
129 @property |
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 repo_source = _logs = None # please pylint |
|
141 |
|
142 def init(self): |
|
143 self._logs = [] |
|
144 self.repo_source = self.cwsource.repo_source |
|
145 |
|
146 def dc_title(self): |
|
147 return '%s [%s]' % (self.printable_value('start_timestamp'), |
|
148 self.printable_value('status')) |
|
149 |
|
150 @property |
|
151 def cwsource(self): |
|
152 return self.cw_import_of[0] |
|
153 |
|
154 def record_debug(self, msg, path=None, line=None): |
|
155 self._log(logging.DEBUG, msg, path, line) |
|
156 self.repo_source.debug(msg) |
|
157 |
|
158 def record_info(self, msg, path=None, line=None): |
|
159 self._log(logging.INFO, msg, path, line) |
|
160 self.repo_source.info(msg) |
|
161 |
|
162 def record_warning(self, msg, path=None, line=None): |
|
163 self._log(logging.WARNING, msg, path, line) |
|
164 self.repo_source.warning(msg) |
|
165 |
|
166 def record_error(self, msg, path=None, line=None): |
|
167 self._status = u'failed' |
|
168 self._log(logging.ERROR, msg, path, line) |
|
169 self.repo_source.error(msg) |
|
170 |
|
171 def record_fatal(self, msg, path=None, line=None): |
|
172 self._status = u'failed' |
|
173 self._log(logging.FATAL, msg, path, line) |
|
174 self.repo_source.fatal(msg) |
|
175 |
|
176 def _log(self, severity, msg, path=None, line=None): |
|
177 encodedmsg = u'%s\t%s\t%s\t%s<br/>' % (severity, path or u'', |
|
178 line or u'', xml_escape(msg)) |
|
179 self._logs.append(encodedmsg) |
|
180 |
|
181 def write_log(self, session, **kwargs): |
|
182 if 'status' not in kwargs: |
|
183 kwargs['status'] = getattr(self, '_status', u'success') |
|
184 self.set_attributes(log=u'<br/>'.join(self._logs), **kwargs) |
|
185 self._logs = [] |