author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 05 Feb 2010 08:11:32 +0100 | |
changeset 4460 | 5c22869079b9 |
parent 4252 | 6c4f109c2b03 |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
0 | 1 |
"""a query preprocesser to handle quick search shortcuts for cubicweb |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
||
10 |
__docformat__ = "restructuredtext en" |
|
11 |
||
12 |
import re |
|
13 |
from logging import getLogger |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
14 |
from warnings import warn |
0 | 15 |
|
16 |
from rql import RQLSyntaxError, BadRQLQuery, parse |
|
17 |
from rql.nodes import Relation |
|
18 |
||
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
19 |
from cubicweb import Unauthorized, typed_eid |
984 | 20 |
from cubicweb.view import Component |
0 | 21 |
|
22 |
LOGGER = getLogger('cubicweb.magicsearch') |
|
23 |
||
24 |
def _get_approriate_translation(translations_found, eschema): |
|
25 |
"""return the first (should be the only one) possible translation according |
|
26 |
to the given entity type |
|
27 |
""" |
|
28 |
# get the list of all attributes / relations for this kind of entity |
|
29 |
existing_relations = set(eschema.subject_relations()) |
|
30 |
consistent_translations = translations_found & existing_relations |
|
31 |
if len(consistent_translations) == 0: |
|
32 |
return None |
|
33 |
return consistent_translations.pop() |
|
34 |
||
35 |
||
36 |
def translate_rql_tree(rqlst, translations, schema): |
|
37 |
"""Try to translate each relation in the RQL syntax tree |
|
38 |
||
39 |
:type rqlst: `rql.stmts.Statement` |
|
40 |
:param rqlst: the RQL syntax tree |
|
41 |
||
42 |
:type translations: dict |
|
43 |
:param translations: the reverted l10n dict |
|
44 |
||
45 |
:type schema: `cubicweb.schema.Schema` |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
46 |
:param schema: the instance's schema |
0 | 47 |
""" |
48 |
# var_types is used as a map : var_name / var_type |
|
49 |
vartypes = {} |
|
50 |
# ambiguous_nodes is used as a map : relation_node / (var_name, available_translations) |
|
51 |
ambiguous_nodes = {} |
|
52 |
# For each relation node, check if it's a localized relation name |
|
53 |
# If it's a localized name, then use the original relation name, else |
|
54 |
# keep the existing relation name |
|
55 |
for relation in rqlst.get_nodes(Relation): |
|
56 |
rtype = relation.r_type |
|
57 |
lhs, rhs = relation.get_variable_parts() |
|
58 |
if rtype == 'is': |
|
59 |
try: |
|
60 |
etype = translations[rhs.value] |
|
61 |
rhs.value = etype |
|
62 |
except KeyError: |
|
63 |
# If no translation found, leave the entity type as is |
|
64 |
etype = rhs.value |
|
65 |
# Memorize variable's type |
|
66 |
vartypes[lhs.name] = etype |
|
67 |
else: |
|
68 |
try: |
|
69 |
translation_set = translations[rtype] |
|
70 |
except KeyError: |
|
71 |
pass # If no translation found, leave the relation type as is |
|
72 |
else: |
|
73 |
# Only one possible translation, no ambiguity |
|
74 |
if len(translation_set) == 1: |
|
75 |
relation.r_type = iter(translations[rtype]).next() |
|
76 |
# More than 1 possible translation => resolve it later |
|
77 |
else: |
|
78 |
ambiguous_nodes[relation] = (lhs.name, translation_set) |
|
79 |
if ambiguous_nodes: |
|
80 |
resolve_ambiguities(vartypes, ambiguous_nodes, schema) |
|
81 |
||
82 |
||
83 |
def resolve_ambiguities(var_types, ambiguous_nodes, schema): |
|
84 |
"""Tries to resolve remaining ambiguities for translation |
|
85 |
/!\ An ambiguity is when two different string can be localized with |
|
86 |
the same string |
|
87 |
A simple example: |
|
88 |
- 'name' in a company context will be localized as 'nom' in French |
|
89 |
- but ... 'surname' will also be localized as 'nom' |
|
90 |
||
91 |
:type var_types: dict |
|
92 |
:param var_types: a map : var_name / var_type |
|
93 |
||
94 |
:type ambiguous_nodes: dict |
|
95 |
:param ambiguous_nodes: a map : relation_node / (var_name, available_translations) |
|
96 |
||
97 |
:type schema: `cubicweb.schema.Schema` |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
98 |
:param schema: the instance's schema |
0 | 99 |
""" |
100 |
# Now, try to resolve ambiguous translations |
|
101 |
for relation, (var_name, translations_found) in ambiguous_nodes.items(): |
|
102 |
try: |
|
103 |
vartype = var_types[var_name] |
|
104 |
except KeyError: |
|
105 |
continue |
|
106 |
# Get schema for this entity type |
|
107 |
eschema = schema.eschema(vartype) |
|
108 |
rtype = _get_approriate_translation(translations_found, eschema) |
|
109 |
if rtype is None: |
|
110 |
continue |
|
111 |
relation.r_type = rtype |
|
1433 | 112 |
|
0 | 113 |
|
114 |
||
115 |
QUOTED_SRE = re.compile(r'(.*?)(["\'])(.+?)\2') |
|
116 |
||
117 |
TRANSLATION_MAPS = {} |
|
118 |
def trmap(config, schema, lang): |
|
119 |
try: |
|
120 |
return TRANSLATION_MAPS[lang] |
|
121 |
except KeyError: |
|
122 |
assert lang in config.translations, '%s %s' % (lang, config.translations) |
|
3362
2a2dcfb379a0
[magicsearch] update to match new i18n API: config.translations[lang] now returns a couple of function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2650
diff
changeset
|
123 |
tr, ctxtr = config.translations[lang] |
0 | 124 |
langmap = {} |
125 |
for etype in schema.entities(): |
|
126 |
etype = str(etype) |
|
127 |
langmap[tr(etype).capitalize()] = etype |
|
128 |
langmap[etype.capitalize()] = etype |
|
129 |
for rtype in schema.relations(): |
|
130 |
rtype = str(rtype) |
|
131 |
langmap.setdefault(tr(rtype).lower(), set()).add(rtype) |
|
132 |
langmap.setdefault(rtype, set()).add(rtype) |
|
133 |
TRANSLATION_MAPS[lang] = langmap |
|
134 |
return langmap |
|
135 |
||
136 |
||
137 |
class BaseQueryProcessor(Component): |
|
138 |
__abstract__ = True |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
139 |
__regid__ = 'magicsearch_processor' |
0 | 140 |
# set something if you want explicit component search facility for the |
141 |
# component |
|
142 |
name = None |
|
143 |
||
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
144 |
def process_query(self, uquery): |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
145 |
args = self.preprocess_query(uquery) |
0 | 146 |
try: |
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
147 |
return self._cw.execute(*args) |
0 | 148 |
finally: |
149 |
# rollback necessary to avoid leaving the connection in a bad state |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
150 |
self._cw.cnx.rollback() |
0 | 151 |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
152 |
def preprocess_query(self, uquery): |
0 | 153 |
raise NotImplementedError() |
154 |
||
155 |
||
156 |
||
157 |
||
158 |
class DoNotPreprocess(BaseQueryProcessor): |
|
159 |
"""this one returns the raw query and should be placed in first position |
|
160 |
of the chain |
|
161 |
""" |
|
162 |
name = 'rql' |
|
163 |
priority = 0 |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
164 |
def preprocess_query(self, uquery): |
0 | 165 |
return uquery, |
1433 | 166 |
|
0 | 167 |
|
168 |
class QueryTranslator(BaseQueryProcessor): |
|
1433 | 169 |
""" parses through rql and translates into schema language entity names |
0 | 170 |
and attributes |
171 |
""" |
|
172 |
priority = 2 |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
173 |
def preprocess_query(self, uquery): |
2567
961aa959f07a
avoid execution of queries which are known to be wrong by letting error propagates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
174 |
rqlst = parse(uquery, print_errors=False) |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
175 |
schema = self._cw.vreg.schema |
0 | 176 |
# rql syntax tree will be modified in place if necessary |
4084
69739e6ebd2a
more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4045
diff
changeset
|
177 |
translate_rql_tree(rqlst, trmap(self._cw.vreg.config, schema, self._cw.lang), |
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
178 |
schema) |
0 | 179 |
return rqlst.as_string(), |
180 |
||
181 |
||
182 |
class QSPreProcessor(BaseQueryProcessor): |
|
183 |
"""Quick search preprocessor |
|
184 |
||
185 |
preprocessing query in shortcut form to their RQL form |
|
186 |
""" |
|
187 |
priority = 4 |
|
1433 | 188 |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
189 |
def preprocess_query(self, uquery): |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
984
diff
changeset
|
190 |
"""try to get rql from an unicode query string""" |
0 | 191 |
args = None |
192 |
try: |
|
193 |
# Process as if there was a quoted part |
|
194 |
args = self._quoted_words_query(uquery) |
|
1433 | 195 |
## No quoted part |
0 | 196 |
except BadRQLQuery: |
197 |
words = uquery.split() |
|
198 |
if len(words) == 1: |
|
199 |
args = self._one_word_query(*words) |
|
200 |
elif len(words) == 2: |
|
201 |
args = self._two_words_query(*words) |
|
202 |
elif len(words) == 3: |
|
203 |
args = self._three_words_query(*words) |
|
204 |
else: |
|
2567
961aa959f07a
avoid execution of queries which are known to be wrong by letting error propagates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
205 |
raise |
0 | 206 |
return args |
1433 | 207 |
|
0 | 208 |
def _get_entity_type(self, word): |
209 |
"""check if the given word is matching an entity type, return it if |
|
210 |
it's the case or raise BadRQLQuery if not |
|
211 |
""" |
|
212 |
etype = word.capitalize() |
|
213 |
try: |
|
4084
69739e6ebd2a
more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4045
diff
changeset
|
214 |
return trmap(self._cw.vreg.config, self._cw.vreg.schema, self._cw.lang)[etype] |
0 | 215 |
except KeyError: |
1433 | 216 |
raise BadRQLQuery('%s is not a valid entity name' % etype) |
0 | 217 |
|
218 |
def _get_attribute_name(self, word, eschema): |
|
219 |
"""check if the given word is matching an attribute of the given entity type, |
|
220 |
return it normalized if found or return it untransformed else |
|
221 |
""" |
|
222 |
"""Returns the attributes's name as stored in the DB""" |
|
223 |
# Need to convert from unicode to string (could be whatever) |
|
224 |
rtype = word.lower() |
|
225 |
# Find the entity name as stored in the DB |
|
4084
69739e6ebd2a
more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4045
diff
changeset
|
226 |
translations = trmap(self._cw.vreg.config, self._cw.vreg.schema, self._cw.lang) |
0 | 227 |
try: |
228 |
translations = translations[rtype] |
|
229 |
except KeyError: |
|
230 |
raise BadRQLQuery('%s is not a valid attribute for %s entity type' |
|
231 |
% (word, eschema)) |
|
232 |
rtype = _get_approriate_translation(translations, eschema) |
|
233 |
if rtype is None: |
|
234 |
raise BadRQLQuery('%s is not a valid attribute for %s entity type' |
|
235 |
% (word, eschema)) |
|
236 |
return rtype |
|
237 |
||
238 |
def _one_word_query(self, word): |
|
239 |
"""Specific process for one word query (case (1) of preprocess_rql) |
|
240 |
""" |
|
241 |
# if this is an integer, then directly go to eid |
|
242 |
try: |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
243 |
eid = typed_eid(word) |
0 | 244 |
return 'Any X WHERE X eid %(x)s', {'x': eid}, 'x' |
245 |
except ValueError: |
|
246 |
etype = self._get_entity_type(word) |
|
247 |
return '%s %s' % (etype, etype[0]), |
|
248 |
||
249 |
def _complete_rql(self, searchstr, etype, rtype=None, var=None, searchattr=None): |
|
250 |
searchop = '' |
|
251 |
if '%' in searchstr: |
|
252 |
if rtype: |
|
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
253 |
possible_etypes = self._cw.vreg.schema.rschema(rtype).objects(etype) |
0 | 254 |
else: |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
255 |
possible_etypes = [self._cw.vreg.schema.eschema(etype)] |
0 | 256 |
if searchattr or len(possible_etypes) == 1: |
257 |
searchattr = searchattr or possible_etypes[0].main_attribute() |
|
258 |
searchop = 'LIKE ' |
|
259 |
searchattr = searchattr or 'has_text' |
|
260 |
if var is None: |
|
261 |
var = etype[0] |
|
262 |
return '%s %s %s%%(text)s' % (var, searchattr, searchop) |
|
1433 | 263 |
|
0 | 264 |
def _two_words_query(self, word1, word2): |
265 |
"""Specific process for two words query (case (2) of preprocess_rql) |
|
266 |
""" |
|
267 |
etype = self._get_entity_type(word1) |
|
268 |
# this is a valid RQL query : ("Person X", or "Person TMP1") |
|
269 |
if len(word2) == 1 and word2.isupper(): |
|
270 |
return '%s %s' % (etype, word2), |
|
271 |
# else, suppose it's a shortcut like : Person Smith |
|
272 |
rql = '%s %s WHERE %s' % (etype, etype[0], self._complete_rql(word2, etype)) |
|
273 |
return rql, {'text': word2} |
|
1433 | 274 |
|
0 | 275 |
def _three_words_query(self, word1, word2, word3): |
276 |
"""Specific process for three words query (case (3) of preprocess_rql) |
|
277 |
""" |
|
278 |
etype = self._get_entity_type(word1) |
|
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
279 |
eschema = self._cw.vreg.schema.eschema(etype) |
0 | 280 |
rtype = self._get_attribute_name(word2, eschema) |
281 |
# expand shortcut if rtype is a non final relation |
|
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
282 |
if not self._cw.vreg.schema.rschema(rtype).final: |
0 | 283 |
return self._expand_shortcut(etype, rtype, word3) |
284 |
if '%' in word3: |
|
285 |
searchop = 'LIKE ' |
|
286 |
else: |
|
287 |
searchop = '' |
|
288 |
rql = '%s %s WHERE %s' % (etype, etype[0], |
|
289 |
self._complete_rql(word3, etype, searchattr=rtype)) |
|
290 |
return rql, {'text': word3} |
|
291 |
||
292 |
def _expand_shortcut(self, etype, rtype, searchstr): |
|
293 |
"""Expands shortcut queries on a non final relation to use has_text or |
|
294 |
the main attribute (according to possible entity type) if '%' is used in the |
|
295 |
search word |
|
296 |
||
297 |
Transforms : 'person worksat IBM' into |
|
298 |
'Personne P WHERE P worksAt C, C has_text "IBM"' |
|
299 |
""" |
|
300 |
# check out all possilbe entity types for the relation represented |
|
301 |
# by 'rtype' |
|
302 |
mainvar = etype[0] |
|
303 |
searchvar = mainvar + '1' |
|
304 |
rql = '%s %s WHERE %s %s %s, %s' % (etype, mainvar, # Person P |
|
305 |
mainvar, rtype, searchvar, # P worksAt C |
|
306 |
self._complete_rql(searchstr, etype, |
|
307 |
rtype=rtype, var=searchvar)) |
|
308 |
return rql, {'text': searchstr} |
|
309 |
||
310 |
||
311 |
def _quoted_words_query(self, ori_rql): |
|
312 |
"""Specific process when there's a "quoted" part |
|
313 |
""" |
|
314 |
m = QUOTED_SRE.match(ori_rql) |
|
315 |
# if there's no quoted part, then no special pre-processing to do |
|
316 |
if m is None: |
|
317 |
raise BadRQLQuery("unable to handle request %r" % ori_rql) |
|
318 |
left_words = m.group(1).split() |
|
319 |
quoted_part = m.group(3) |
|
320 |
# Case (1) : Company "My own company" |
|
321 |
if len(left_words) == 1: |
|
322 |
try: |
|
323 |
word1 = left_words[0] |
|
324 |
return self._two_words_query(word1, quoted_part) |
|
325 |
except BadRQLQuery, error: |
|
326 |
raise BadRQLQuery("unable to handle request %r" % ori_rql) |
|
327 |
# Case (2) : Company name "My own company"; |
|
328 |
elif len(left_words) == 2: |
|
329 |
word1, word2 = left_words |
|
330 |
return self._three_words_query(word1, word2, quoted_part) |
|
331 |
# return ori_rql |
|
332 |
raise BadRQLQuery("unable to handle request %r" % ori_rql) |
|
1433 | 333 |
|
0 | 334 |
|
1433 | 335 |
|
0 | 336 |
class FullTextTranslator(BaseQueryProcessor): |
337 |
priority = 10 |
|
338 |
name = 'text' |
|
1433 | 339 |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
340 |
def preprocess_query(self, uquery): |
0 | 341 |
"""suppose it's a plain text query""" |
342 |
return 'Any X WHERE X has_text %(text)s', {'text': uquery} |
|
343 |
||
344 |
||
345 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
346 |
class MagicSearchComponent(Component): |
3408
c92170fca813
[api] use __regid__ instead of deprecated id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
347 |
__regid__ = 'magicsearch' |
0 | 348 |
def __init__(self, req, rset=None): |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
349 |
super(MagicSearchComponent, self).__init__(req, rset=rset) |
0 | 350 |
processors = [] |
351 |
self.by_name = {} |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
352 |
for processorcls in self._cw.vreg['components']['magicsearch_processor']: |
0 | 353 |
# instantiation needed |
3462
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
354 |
processor = processorcls(self._cw) |
0 | 355 |
processors.append(processor) |
356 |
if processor.name is not None: |
|
357 |
assert not processor.name in self.by_name |
|
358 |
self.by_name[processor.name.lower()] = processor |
|
359 |
self.processors = sorted(processors, key=lambda x: x.priority) |
|
360 |
||
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
361 |
def process_query(self, uquery): |
0 | 362 |
assert isinstance(uquery, unicode) |
363 |
try: |
|
364 |
procname, query = uquery.split(':', 1) |
|
365 |
proc = self.by_name[procname.strip().lower()] |
|
366 |
uquery = query.strip() |
|
367 |
except: |
|
368 |
# use processor chain |
|
369 |
unauthorized = None |
|
370 |
for proc in self.processors: |
|
371 |
try: |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
372 |
try: |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
373 |
return proc.process_query(uquery) |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
374 |
except TypeError, exc: # cw 3.5 compat |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
375 |
print "EXC", exc |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
376 |
warn("[3.6] %s.%s.process_query() should now accept uquery " |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
377 |
"as unique argument, use self._cw instead of req" |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
378 |
% (proc.__module__, proc.__class__.__name__), |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
379 |
DeprecationWarning) |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
380 |
return proc.process_query(uquery, self._cw) |
0 | 381 |
# FIXME : we don't want to catch any exception type here ! |
382 |
except (RQLSyntaxError, BadRQLQuery): |
|
383 |
pass |
|
384 |
except Unauthorized, ex: |
|
385 |
unauthorized = ex |
|
386 |
continue |
|
387 |
except Exception, ex: |
|
388 |
LOGGER.debug('%s: %s', ex.__class__.__name__, ex) |
|
389 |
continue |
|
390 |
if unauthorized: |
|
391 |
raise unauthorized |
|
392 |
else: |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
393 |
# explicitly specified processor: don't try to catch the exception |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
394 |
return proc.process_query(uquery) |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
395 |
raise BadRQLQuery(self._cw._('sorry, the server is unable to handle this query')) |