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