27 from logilab.common import configuration |
27 from logilab.common import configuration |
28 from logilab.common.textutils import unormalize |
28 from logilab.common.textutils import unormalize |
29 |
29 |
30 from yams.schema import role_name |
30 from yams.schema import role_name |
31 |
31 |
32 from cubicweb import ValidationError, set_log_methods, server |
32 from cubicweb import ValidationError, set_log_methods, server, _ |
33 from cubicweb.server import SOURCE_TYPES |
33 from cubicweb.server import SOURCE_TYPES |
34 |
34 |
35 |
35 |
36 def dbg_st_search(uri, union, args, cachekey=None, prefix='rql for'): |
36 def dbg_st_search(uri, union, args, cachekey=None, prefix='rql for'): |
37 if server.DEBUG & server.DBG_RQL: |
37 if server.DEBUG & server.DBG_RQL: |
131 def restore(self, backupfile, confirm, drop, format='native'): |
131 def restore(self, backupfile, confirm, drop, format='native'): |
132 """method called to restore a backup of source's data""" |
132 """method called to restore a backup of source's data""" |
133 pass |
133 pass |
134 |
134 |
135 @classmethod |
135 @classmethod |
136 def check_conf_dict(cls, eid, confdict, _=text_type, fail_if_unknown=True): |
136 def check_conf_dict(cls, eid, confdict, fail_if_unknown=True): |
137 """check configuration of source entity. Return config dict properly |
137 """check configuration of source entity. Return config dict properly |
138 typed with defaults set. |
138 typed with defaults set. |
139 """ |
139 """ |
140 processed = {} |
140 processed = {} |
141 for optname, optdict in cls.options: |
141 for optname, optdict in cls.options: |
142 value = confdict.pop(optname, optdict.get('default')) |
142 value = confdict.pop(optname, optdict.get('default')) |
143 if value is configuration.REQUIRED: |
143 if value is configuration.REQUIRED: |
144 if not fail_if_unknown: |
144 if not fail_if_unknown: |
145 continue |
145 continue |
146 msg = _('specifying %s is mandatory' % optname) |
146 msg = _('specifying %s is mandatory') |
147 raise ValidationError(eid, {role_name('config', 'subject'): msg}) |
147 msgargs = optname |
|
148 raise ValidationError(eid, {role_name('config', 'subject'): msg}, msgargs) |
148 elif value is not None: |
149 elif value is not None: |
149 # type check |
150 # type check |
150 try: |
151 try: |
151 value = configuration._validate(value, optdict, optname) |
152 value = configuration._validate(value, optdict, optname) |
152 except Exception as ex: |
153 except Exception as ex: |
153 msg = text_type(ex) # XXX internationalization |
154 msg = text_type(ex) |
154 raise ValidationError(eid, {role_name('config', 'subject'): msg}) |
155 raise ValidationError(eid, {role_name('config', 'subject'): msg}) |
155 processed[optname] = value |
156 processed[optname] = value |
156 # cw < 3.10 bw compat |
157 # cw < 3.10 bw compat |
157 try: |
158 try: |
158 processed['adapter'] = confdict['adapter'] |
159 processed['adapter'] = confdict['adapter'] |
159 except KeyError: |
160 except KeyError: |
160 pass |
161 pass |
161 # check for unknown options |
162 # check for unknown options |
162 if confdict and tuple(confdict) != ('adapter',): |
163 if confdict and tuple(confdict) != ('adapter',): |
163 if fail_if_unknown: |
164 if fail_if_unknown: |
164 msg = _('unknown options %s') % ', '.join(confdict) |
165 msg = _('unknown options %s') |
165 raise ValidationError(eid, {role_name('config', 'subject'): msg}) |
166 msgargs = ', '.join(confdict) |
|
167 raise ValidationError(eid, {role_name('config', 'subject'): msg}, msgargs) |
166 else: |
168 else: |
167 logger = getLogger('cubicweb.sources') |
169 logger = getLogger('cubicweb.sources') |
168 logger.warning('unknown options %s', ', '.join(confdict)) |
170 logger.warning('unknown options %s', ', '.join(confdict)) |
169 # add options to processed, they may be necessary during migration |
171 # add options to processed, they may be necessary during migration |
170 processed.update(confdict) |
172 processed.update(confdict) |
171 return processed |
173 return processed |
172 |
174 |
173 @classmethod |
175 @classmethod |
174 def check_config(cls, source_entity): |
176 def check_config(cls, source_entity): |
175 """check configuration of source entity""" |
177 """check configuration of source entity""" |
176 return cls.check_conf_dict(source_entity.eid, source_entity.host_config, |
178 return cls.check_conf_dict(source_entity.eid, source_entity.dictconfig) |
177 _=source_entity._cw._) |
|
178 |
179 |
179 # source initialization / finalization ##################################### |
180 # source initialization / finalization ##################################### |
180 |
181 |
181 def set_schema(self, schema): |
182 def set_schema(self, schema): |
182 """set the instance'schema""" |
183 """set the instance'schema""" |