author | Julien Cristau <julien.cristau@logilab.fr> |
Wed, 29 Jan 2014 16:03:42 +0100 | |
changeset 9501 | 2904e2ba81a0 |
parent 8748 | f5027f8d2478 |
child 9701 | 46c8d8701240 |
permissions | -rw-r--r-- |
8510
e2913c9880a0
[test rql completion] dont limit number of completion, else we've impredicable results. Also should tell rql.parse to not print error on stdout
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8505
diff
changeset
|
1 |
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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
|
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/>. |
8505
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
18 |
"""a query processor to handle quick search shortcuts for cubicweb |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
19 |
""" |
0 | 20 |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
23 |
import re |
|
24 |
from logging import getLogger |
|
8505
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
25 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
26 |
from yams.interfaces import IVocabularyConstraint |
0 | 27 |
|
28 |
from rql import RQLSyntaxError, BadRQLQuery, parse |
|
8505
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
29 |
from rql.utils import rqlvar_maker |
0 | 30 |
from rql.nodes import Relation |
31 |
||
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8695
diff
changeset
|
32 |
from cubicweb import Unauthorized |
984 | 33 |
from cubicweb.view import Component |
8505
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
34 |
from cubicweb.web.views.ajaxcontroller import ajaxfunc |
0 | 35 |
|
36 |
LOGGER = getLogger('cubicweb.magicsearch') |
|
37 |
||
38 |
def _get_approriate_translation(translations_found, eschema): |
|
39 |
"""return the first (should be the only one) possible translation according |
|
40 |
to the given entity type |
|
41 |
""" |
|
42 |
# get the list of all attributes / relations for this kind of entity |
|
43 |
existing_relations = set(eschema.subject_relations()) |
|
44 |
consistent_translations = translations_found & existing_relations |
|
45 |
if len(consistent_translations) == 0: |
|
46 |
return None |
|
47 |
return consistent_translations.pop() |
|
48 |
||
49 |
||
50 |
def translate_rql_tree(rqlst, translations, schema): |
|
51 |
"""Try to translate each relation in the RQL syntax tree |
|
52 |
||
53 |
:type rqlst: `rql.stmts.Statement` |
|
54 |
:param rqlst: the RQL syntax tree |
|
55 |
||
56 |
:type translations: dict |
|
57 |
:param translations: the reverted l10n dict |
|
58 |
||
59 |
:type schema: `cubicweb.schema.Schema` |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
60 |
:param schema: the instance's schema |
0 | 61 |
""" |
62 |
# var_types is used as a map : var_name / var_type |
|
63 |
vartypes = {} |
|
64 |
# ambiguous_nodes is used as a map : relation_node / (var_name, available_translations) |
|
65 |
ambiguous_nodes = {} |
|
66 |
# For each relation node, check if it's a localized relation name |
|
67 |
# If it's a localized name, then use the original relation name, else |
|
68 |
# keep the existing relation name |
|
69 |
for relation in rqlst.get_nodes(Relation): |
|
70 |
rtype = relation.r_type |
|
71 |
lhs, rhs = relation.get_variable_parts() |
|
72 |
if rtype == 'is': |
|
73 |
try: |
|
74 |
etype = translations[rhs.value] |
|
75 |
rhs.value = etype |
|
76 |
except KeyError: |
|
77 |
# If no translation found, leave the entity type as is |
|
78 |
etype = rhs.value |
|
79 |
# Memorize variable's type |
|
80 |
vartypes[lhs.name] = etype |
|
81 |
else: |
|
82 |
try: |
|
83 |
translation_set = translations[rtype] |
|
84 |
except KeyError: |
|
85 |
pass # If no translation found, leave the relation type as is |
|
86 |
else: |
|
87 |
# Only one possible translation, no ambiguity |
|
88 |
if len(translation_set) == 1: |
|
89 |
relation.r_type = iter(translations[rtype]).next() |
|
90 |
# More than 1 possible translation => resolve it later |
|
91 |
else: |
|
92 |
ambiguous_nodes[relation] = (lhs.name, translation_set) |
|
93 |
if ambiguous_nodes: |
|
94 |
resolve_ambiguities(vartypes, ambiguous_nodes, schema) |
|
95 |
||
96 |
||
97 |
def resolve_ambiguities(var_types, ambiguous_nodes, schema): |
|
98 |
"""Tries to resolve remaining ambiguities for translation |
|
99 |
/!\ An ambiguity is when two different string can be localized with |
|
100 |
the same string |
|
101 |
A simple example: |
|
102 |
- 'name' in a company context will be localized as 'nom' in French |
|
103 |
- but ... 'surname' will also be localized as 'nom' |
|
104 |
||
105 |
:type var_types: dict |
|
106 |
:param var_types: a map : var_name / var_type |
|
107 |
||
108 |
:type ambiguous_nodes: dict |
|
109 |
:param ambiguous_nodes: a map : relation_node / (var_name, available_translations) |
|
110 |
||
111 |
:type schema: `cubicweb.schema.Schema` |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
112 |
:param schema: the instance's schema |
0 | 113 |
""" |
114 |
# Now, try to resolve ambiguous translations |
|
115 |
for relation, (var_name, translations_found) in ambiguous_nodes.items(): |
|
116 |
try: |
|
117 |
vartype = var_types[var_name] |
|
118 |
except KeyError: |
|
119 |
continue |
|
120 |
# Get schema for this entity type |
|
121 |
eschema = schema.eschema(vartype) |
|
122 |
rtype = _get_approriate_translation(translations_found, eschema) |
|
123 |
if rtype is None: |
|
124 |
continue |
|
125 |
relation.r_type = rtype |
|
1433 | 126 |
|
0 | 127 |
|
128 |
||
129 |
QUOTED_SRE = re.compile(r'(.*?)(["\'])(.+?)\2') |
|
130 |
||
131 |
TRANSLATION_MAPS = {} |
|
132 |
def trmap(config, schema, lang): |
|
133 |
try: |
|
134 |
return TRANSLATION_MAPS[lang] |
|
135 |
except KeyError: |
|
136 |
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
|
137 |
tr, ctxtr = config.translations[lang] |
0 | 138 |
langmap = {} |
139 |
for etype in schema.entities(): |
|
140 |
etype = str(etype) |
|
141 |
langmap[tr(etype).capitalize()] = etype |
|
142 |
langmap[etype.capitalize()] = etype |
|
143 |
for rtype in schema.relations(): |
|
144 |
rtype = str(rtype) |
|
145 |
langmap.setdefault(tr(rtype).lower(), set()).add(rtype) |
|
146 |
langmap.setdefault(rtype, set()).add(rtype) |
|
147 |
TRANSLATION_MAPS[lang] = langmap |
|
148 |
return langmap |
|
149 |
||
150 |
||
151 |
class BaseQueryProcessor(Component): |
|
152 |
__abstract__ = True |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
153 |
__regid__ = 'magicsearch_processor' |
0 | 154 |
# set something if you want explicit component search facility for the |
155 |
# component |
|
156 |
name = None |
|
157 |
||
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
|
158 |
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
|
159 |
args = self.preprocess_query(uquery) |
0 | 160 |
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
|
161 |
return self._cw.execute(*args) |
0 | 162 |
finally: |
163 |
# 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
|
164 |
self._cw.cnx.rollback() |
0 | 165 |
|
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
|
166 |
def preprocess_query(self, uquery): |
0 | 167 |
raise NotImplementedError() |
168 |
||
169 |
||
170 |
||
171 |
||
172 |
class DoNotPreprocess(BaseQueryProcessor): |
|
173 |
"""this one returns the raw query and should be placed in first position |
|
174 |
of the chain |
|
175 |
""" |
|
176 |
name = 'rql' |
|
177 |
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
|
178 |
def preprocess_query(self, uquery): |
0 | 179 |
return uquery, |
1433 | 180 |
|
0 | 181 |
|
182 |
class QueryTranslator(BaseQueryProcessor): |
|
1433 | 183 |
""" parses through rql and translates into schema language entity names |
0 | 184 |
and attributes |
185 |
""" |
|
186 |
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
|
187 |
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
|
188 |
rqlst = parse(uquery, print_errors=False) |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
189 |
schema = self._cw.vreg.schema |
0 | 190 |
# 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
|
191 |
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
|
192 |
schema) |
0 | 193 |
return rqlst.as_string(), |
194 |
||
195 |
||
196 |
class QSPreProcessor(BaseQueryProcessor): |
|
197 |
"""Quick search preprocessor |
|
198 |
||
199 |
preprocessing query in shortcut form to their RQL form |
|
200 |
""" |
|
201 |
priority = 4 |
|
1433 | 202 |
|
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
|
203 |
def preprocess_query(self, uquery): |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
984
diff
changeset
|
204 |
"""try to get rql from an unicode query string""" |
0 | 205 |
args = None |
206 |
try: |
|
207 |
# Process as if there was a quoted part |
|
208 |
args = self._quoted_words_query(uquery) |
|
1433 | 209 |
## No quoted part |
0 | 210 |
except BadRQLQuery: |
211 |
words = uquery.split() |
|
212 |
if len(words) == 1: |
|
213 |
args = self._one_word_query(*words) |
|
214 |
elif len(words) == 2: |
|
215 |
args = self._two_words_query(*words) |
|
216 |
elif len(words) == 3: |
|
217 |
args = self._three_words_query(*words) |
|
218 |
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
|
219 |
raise |
0 | 220 |
return args |
1433 | 221 |
|
0 | 222 |
def _get_entity_type(self, word): |
223 |
"""check if the given word is matching an entity type, return it if |
|
224 |
it's the case or raise BadRQLQuery if not |
|
225 |
""" |
|
226 |
etype = word.capitalize() |
|
227 |
try: |
|
4084
69739e6ebd2a
more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4045
diff
changeset
|
228 |
return trmap(self._cw.vreg.config, self._cw.vreg.schema, self._cw.lang)[etype] |
0 | 229 |
except KeyError: |
1433 | 230 |
raise BadRQLQuery('%s is not a valid entity name' % etype) |
0 | 231 |
|
232 |
def _get_attribute_name(self, word, eschema): |
|
233 |
"""check if the given word is matching an attribute of the given entity type, |
|
234 |
return it normalized if found or return it untransformed else |
|
235 |
""" |
|
236 |
"""Returns the attributes's name as stored in the DB""" |
|
237 |
# Need to convert from unicode to string (could be whatever) |
|
238 |
rtype = word.lower() |
|
239 |
# 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
|
240 |
translations = trmap(self._cw.vreg.config, self._cw.vreg.schema, self._cw.lang) |
0 | 241 |
try: |
242 |
translations = translations[rtype] |
|
243 |
except KeyError: |
|
244 |
raise BadRQLQuery('%s is not a valid attribute for %s entity type' |
|
245 |
% (word, eschema)) |
|
246 |
rtype = _get_approriate_translation(translations, eschema) |
|
247 |
if rtype is None: |
|
248 |
raise BadRQLQuery('%s is not a valid attribute for %s entity type' |
|
249 |
% (word, eschema)) |
|
250 |
return rtype |
|
251 |
||
252 |
def _one_word_query(self, word): |
|
253 |
"""Specific process for one word query (case (1) of preprocess_rql) |
|
254 |
""" |
|
255 |
# if this is an integer, then directly go to eid |
|
256 |
try: |
|
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8695
diff
changeset
|
257 |
eid = int(word) |
0 | 258 |
return 'Any X WHERE X eid %(x)s', {'x': eid}, 'x' |
259 |
except ValueError: |
|
260 |
etype = self._get_entity_type(word) |
|
261 |
return '%s %s' % (etype, etype[0]), |
|
262 |
||
263 |
def _complete_rql(self, searchstr, etype, rtype=None, var=None, searchattr=None): |
|
264 |
searchop = '' |
|
265 |
if '%' in searchstr: |
|
266 |
if rtype: |
|
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
267 |
possible_etypes = self._cw.vreg.schema.rschema(rtype).objects(etype) |
0 | 268 |
else: |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
269 |
possible_etypes = [self._cw.vreg.schema.eschema(etype)] |
0 | 270 |
if searchattr or len(possible_etypes) == 1: |
271 |
searchattr = searchattr or possible_etypes[0].main_attribute() |
|
272 |
searchop = 'LIKE ' |
|
273 |
searchattr = searchattr or 'has_text' |
|
274 |
if var is None: |
|
275 |
var = etype[0] |
|
276 |
return '%s %s %s%%(text)s' % (var, searchattr, searchop) |
|
1433 | 277 |
|
0 | 278 |
def _two_words_query(self, word1, word2): |
279 |
"""Specific process for two words query (case (2) of preprocess_rql) |
|
280 |
""" |
|
281 |
etype = self._get_entity_type(word1) |
|
282 |
# this is a valid RQL query : ("Person X", or "Person TMP1") |
|
283 |
if len(word2) == 1 and word2.isupper(): |
|
284 |
return '%s %s' % (etype, word2), |
|
285 |
# else, suppose it's a shortcut like : Person Smith |
|
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
286 |
restriction = self._complete_rql(word2, etype) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
287 |
if ' has_text ' in restriction: |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
288 |
rql = '%s %s ORDERBY FTIRANK(%s) DESC WHERE %s' % ( |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
289 |
etype, etype[0], etype[0], restriction) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
290 |
else: |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
291 |
rql = '%s %s WHERE %s' % ( |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
292 |
etype, etype[0], restriction) |
0 | 293 |
return rql, {'text': word2} |
1433 | 294 |
|
0 | 295 |
def _three_words_query(self, word1, word2, word3): |
296 |
"""Specific process for three words query (case (3) of preprocess_rql) |
|
297 |
""" |
|
298 |
etype = self._get_entity_type(word1) |
|
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
299 |
eschema = self._cw.vreg.schema.eschema(etype) |
0 | 300 |
rtype = self._get_attribute_name(word2, eschema) |
301 |
# 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
|
302 |
if not self._cw.vreg.schema.rschema(rtype).final: |
0 | 303 |
return self._expand_shortcut(etype, rtype, word3) |
304 |
if '%' in word3: |
|
305 |
searchop = 'LIKE ' |
|
306 |
else: |
|
307 |
searchop = '' |
|
308 |
rql = '%s %s WHERE %s' % (etype, etype[0], |
|
309 |
self._complete_rql(word3, etype, searchattr=rtype)) |
|
310 |
return rql, {'text': word3} |
|
311 |
||
312 |
def _expand_shortcut(self, etype, rtype, searchstr): |
|
313 |
"""Expands shortcut queries on a non final relation to use has_text or |
|
314 |
the main attribute (according to possible entity type) if '%' is used in the |
|
315 |
search word |
|
316 |
||
317 |
Transforms : 'person worksat IBM' into |
|
318 |
'Personne P WHERE P worksAt C, C has_text "IBM"' |
|
319 |
""" |
|
320 |
# check out all possilbe entity types for the relation represented |
|
321 |
# by 'rtype' |
|
322 |
mainvar = etype[0] |
|
323 |
searchvar = mainvar + '1' |
|
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
324 |
restriction = self._complete_rql(searchstr, etype, rtype=rtype, |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
325 |
var=searchvar) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
326 |
if ' has_text ' in restriction: |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
327 |
rql = ('%s %s ORDERBY FTIRANK(%s) DESC ' |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
328 |
'WHERE %s %s %s, %s' % (etype, mainvar, searchvar, |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
329 |
mainvar, rtype, searchvar, # P worksAt C |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
330 |
restriction)) |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
331 |
else: |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
332 |
rql = ('%s %s WHERE %s %s %s, %s' % (etype, mainvar, |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
333 |
mainvar, rtype, searchvar, # P worksAt C |
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
334 |
restriction)) |
0 | 335 |
return rql, {'text': searchstr} |
336 |
||
337 |
||
338 |
def _quoted_words_query(self, ori_rql): |
|
339 |
"""Specific process when there's a "quoted" part |
|
340 |
""" |
|
341 |
m = QUOTED_SRE.match(ori_rql) |
|
342 |
# if there's no quoted part, then no special pre-processing to do |
|
343 |
if m is None: |
|
344 |
raise BadRQLQuery("unable to handle request %r" % ori_rql) |
|
345 |
left_words = m.group(1).split() |
|
346 |
quoted_part = m.group(3) |
|
347 |
# Case (1) : Company "My own company" |
|
348 |
if len(left_words) == 1: |
|
349 |
try: |
|
350 |
word1 = left_words[0] |
|
351 |
return self._two_words_query(word1, quoted_part) |
|
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8510
diff
changeset
|
352 |
except BadRQLQuery as error: |
0 | 353 |
raise BadRQLQuery("unable to handle request %r" % ori_rql) |
354 |
# Case (2) : Company name "My own company"; |
|
355 |
elif len(left_words) == 2: |
|
356 |
word1, word2 = left_words |
|
357 |
return self._three_words_query(word1, word2, quoted_part) |
|
358 |
# return ori_rql |
|
359 |
raise BadRQLQuery("unable to handle request %r" % ori_rql) |
|
1433 | 360 |
|
0 | 361 |
|
1433 | 362 |
|
0 | 363 |
class FullTextTranslator(BaseQueryProcessor): |
364 |
priority = 10 |
|
365 |
name = 'text' |
|
1433 | 366 |
|
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
|
367 |
def preprocess_query(self, uquery): |
0 | 368 |
"""suppose it's a plain text query""" |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
369 |
return 'Any X ORDERBY FTIRANK(X) DESC WHERE X has_text %(text)s', {'text': uquery} |
0 | 370 |
|
371 |
||
372 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
373 |
class MagicSearchComponent(Component): |
3408
c92170fca813
[api] use __regid__ instead of deprecated id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
374 |
__regid__ = 'magicsearch' |
0 | 375 |
def __init__(self, req, rset=None): |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
376 |
super(MagicSearchComponent, self).__init__(req, rset=rset) |
0 | 377 |
processors = [] |
378 |
self.by_name = {} |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
379 |
for processorcls in self._cw.vreg['components']['magicsearch_processor']: |
0 | 380 |
# 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
|
381 |
processor = processorcls(self._cw) |
0 | 382 |
processors.append(processor) |
383 |
if processor.name is not None: |
|
384 |
assert not processor.name in self.by_name |
|
385 |
self.by_name[processor.name.lower()] = processor |
|
386 |
self.processors = sorted(processors, key=lambda x: x.priority) |
|
387 |
||
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
|
388 |
def process_query(self, uquery): |
0 | 389 |
assert isinstance(uquery, unicode) |
390 |
try: |
|
391 |
procname, query = uquery.split(':', 1) |
|
392 |
proc = self.by_name[procname.strip().lower()] |
|
393 |
uquery = query.strip() |
|
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
394 |
except Exception: |
0 | 395 |
# use processor chain |
396 |
unauthorized = None |
|
397 |
for proc in self.processors: |
|
398 |
try: |
|
7990
a673d1d9a738
[diet]Â drop pre 3.6 API compatibility (but attempt to keep data cmopatibility). Closes #2017916
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7815
diff
changeset
|
399 |
return proc.process_query(uquery) |
0 | 400 |
# FIXME : we don't want to catch any exception type here ! |
401 |
except (RQLSyntaxError, BadRQLQuery): |
|
402 |
pass |
|
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8510
diff
changeset
|
403 |
except Unauthorized as ex: |
0 | 404 |
unauthorized = ex |
405 |
continue |
|
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8510
diff
changeset
|
406 |
except Exception as ex: |
0 | 407 |
LOGGER.debug('%s: %s', ex.__class__.__name__, ex) |
408 |
continue |
|
409 |
if unauthorized: |
|
410 |
raise unauthorized |
|
411 |
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
|
412 |
# 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
|
413 |
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
|
414 |
raise BadRQLQuery(self._cw._('sorry, the server is unable to handle this query')) |
8505
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
415 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
416 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
417 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
418 |
## RQL suggestions builder #################################################### |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
419 |
class RQLSuggestionsBuilder(Component): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
420 |
"""main entry point is `build_suggestions()` which takes |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
421 |
an incomplete RQL query and returns a list of suggestions to complete |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
422 |
the query. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
423 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
424 |
This component is enabled by default and is used to provide autocompletion |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
425 |
in the RQL search bar. If you don't want this feature in your application, |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
426 |
just unregister it or make it unselectable. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
427 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
428 |
.. automethod:: cubicweb.web.views.magicsearch.RQLSuggestionsBuilder.build_suggestions |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
429 |
.. automethod:: cubicweb.web.views.magicsearch.RQLSuggestionsBuilder.etypes_suggestion_set |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
430 |
.. automethod:: cubicweb.web.views.magicsearch.RQLSuggestionsBuilder.possible_etypes |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
431 |
.. automethod:: cubicweb.web.views.magicsearch.RQLSuggestionsBuilder.possible_relations |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
432 |
.. automethod:: cubicweb.web.views.magicsearch.RQLSuggestionsBuilder.vocabulary |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
433 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
434 |
__regid__ = 'rql.suggestions' |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
435 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
436 |
#: maximum number of results to fetch when suggesting attribute values |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
437 |
attr_value_limit = 20 |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
438 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
439 |
def build_suggestions(self, user_rql): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
440 |
"""return a list of suggestions to complete `user_rql` |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
441 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
442 |
:param user_rql: an incomplete RQL query |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
443 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
444 |
req = self._cw |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
445 |
try: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
446 |
if 'WHERE' not in user_rql: # don't try to complete if there's no restriction |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
447 |
return [] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
448 |
variables, restrictions = [part.strip() for part in user_rql.split('WHERE', 1)] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
449 |
if ',' in restrictions: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
450 |
restrictions, incomplete_part = restrictions.rsplit(',', 1) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
451 |
user_rql = '%s WHERE %s' % (variables, restrictions) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
452 |
else: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
453 |
restrictions, incomplete_part = '', restrictions |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
454 |
user_rql = variables |
8510
e2913c9880a0
[test rql completion] dont limit number of completion, else we've impredicable results. Also should tell rql.parse to not print error on stdout
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8505
diff
changeset
|
455 |
select = parse(user_rql, print_errors=False).children[0] |
8505
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
456 |
req.vreg.rqlhelper.annotate(select) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
457 |
req.vreg.solutions(req, select, {}) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
458 |
if restrictions: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
459 |
return ['%s, %s' % (user_rql, suggestion) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
460 |
for suggestion in self.rql_build_suggestions(select, incomplete_part)] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
461 |
else: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
462 |
return ['%s WHERE %s' % (user_rql, suggestion) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
463 |
for suggestion in self.rql_build_suggestions(select, incomplete_part)] |
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8510
diff
changeset
|
464 |
except Exception as exc: # we never want to crash |
8505
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
465 |
self.debug('failed to build suggestions: %s', exc) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
466 |
return [] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
467 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
468 |
## actual completion entry points ######################################### |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
469 |
def rql_build_suggestions(self, select, incomplete_part): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
470 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
471 |
:param select: the annotated select node (rql syntax tree) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
472 |
:param incomplete_part: the part of the rql query that needs |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
473 |
to be completed, (e.g. ``X is Pr``, ``X re``) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
474 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
475 |
chunks = incomplete_part.split(None, 2) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
476 |
if not chunks: # nothing to complete |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
477 |
return [] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
478 |
if len(chunks) == 1: # `incomplete` looks like "MYVAR" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
479 |
return self._complete_rqlvar(select, *chunks) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
480 |
elif len(chunks) == 2: # `incomplete` looks like "MYVAR some_rel" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
481 |
return self._complete_rqlvar_and_rtype(select, *chunks) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
482 |
elif len(chunks) == 3: # `incomplete` looks like "MYVAR some_rel something" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
483 |
return self._complete_relation_object(select, *chunks) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
484 |
else: # would be anything else, hard to decide what to do here |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
485 |
return [] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
486 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
487 |
# _complete_* methods are considered private, at least while the API |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
488 |
# isn't stabilized. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
489 |
def _complete_rqlvar(self, select, rql_var): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
490 |
"""return suggestions for "variable only" incomplete_part |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
491 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
492 |
as in : |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
493 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
494 |
- Any X WHERE X |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
495 |
- Any X WHERE X is Project, Y |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
496 |
- etc. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
497 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
498 |
return ['%s %s %s' % (rql_var, rtype, dest_var) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
499 |
for rtype, dest_var in self.possible_relations(select, rql_var)] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
500 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
501 |
def _complete_rqlvar_and_rtype(self, select, rql_var, user_rtype): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
502 |
"""return suggestions for "variable + rtype" incomplete_part |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
503 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
504 |
as in : |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
505 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
506 |
- Any X WHERE X is |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
507 |
- Any X WHERE X is Person, X firstn |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
508 |
- etc. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
509 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
510 |
# special case `user_type` == 'is', return every possible type. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
511 |
if user_rtype == 'is': |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
512 |
return self._complete_is_relation(select, rql_var) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
513 |
else: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
514 |
return ['%s %s %s' % (rql_var, rtype, dest_var) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
515 |
for rtype, dest_var in self.possible_relations(select, rql_var) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
516 |
if rtype.startswith(user_rtype)] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
517 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
518 |
def _complete_relation_object(self, select, rql_var, user_rtype, user_value): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
519 |
"""return suggestions for "variable + rtype + some_incomplete_value" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
520 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
521 |
as in : |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
522 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
523 |
- Any X WHERE X is Per |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
524 |
- Any X WHERE X is Person, X firstname " |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
525 |
- Any X WHERE X is Person, X firstname "Pa |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
526 |
- etc. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
527 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
528 |
# special case `user_type` == 'is', return every possible type. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
529 |
if user_rtype == 'is': |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
530 |
return self._complete_is_relation(select, rql_var, user_value) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
531 |
elif user_value: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
532 |
if user_value[0] in ('"', "'"): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
533 |
# if finished string, don't suggest anything |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
534 |
if len(user_value) > 1 and user_value[-1] == user_value[0]: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
535 |
return [] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
536 |
user_value = user_value[1:] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
537 |
return ['%s %s "%s"' % (rql_var, user_rtype, value) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
538 |
for value in self.vocabulary(select, rql_var, |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
539 |
user_rtype, user_value)] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
540 |
return [] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
541 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
542 |
def _complete_is_relation(self, select, rql_var, prefix=''): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
543 |
"""return every possible types for rql_var |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
544 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
545 |
:param prefix: if specified, will only return entity types starting |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
546 |
with the specified value. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
547 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
548 |
return ['%s is %s' % (rql_var, etype) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
549 |
for etype in self.possible_etypes(select, rql_var, prefix)] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
550 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
551 |
def etypes_suggestion_set(self): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
552 |
"""returns the list of possible entity types to suggest |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
553 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
554 |
The default is to return any non-final entity type available |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
555 |
in the schema. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
556 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
557 |
Can be overridden for instance if an application decides |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
558 |
to restrict this list to a meaningful set of business etypes. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
559 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
560 |
schema = self._cw.vreg.schema |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
561 |
return set(eschema.type for eschema in schema.entities() if not eschema.final) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
562 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
563 |
def possible_etypes(self, select, rql_var, prefix=''): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
564 |
"""return all possible etypes for `rql_var` |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
565 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
566 |
The returned list will always be a subset of meth:`etypes_suggestion_set` |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
567 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
568 |
:param select: the annotated select node (rql syntax tree) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
569 |
:param rql_var: the variable name for which we want to know possible types |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
570 |
:param prefix: if specified, will only return etypes starting with it |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
571 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
572 |
available_etypes = self.etypes_suggestion_set() |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
573 |
possible_etypes = set() |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
574 |
for sol in select.solutions: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
575 |
if rql_var in sol and sol[rql_var] in available_etypes: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
576 |
possible_etypes.add(sol[rql_var]) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
577 |
if not possible_etypes: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
578 |
# `Any X WHERE X is Person, Y is` |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
579 |
# -> won't have a solution, need to give all etypes |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
580 |
possible_etypes = available_etypes |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
581 |
return sorted(etype for etype in possible_etypes if etype.startswith(prefix)) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
582 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
583 |
def possible_relations(self, select, rql_var, include_meta=False): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
584 |
"""returns a list of couple (rtype, dest_var) for each possible |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
585 |
relations with `rql_var` as subject. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
586 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
587 |
``dest_var`` will be picked among availabel variables if types match, |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
588 |
otherwise a new one will be created. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
589 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
590 |
schema = self._cw.vreg.schema |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
591 |
relations = set() |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
592 |
untyped_dest_var = rqlvar_maker(defined=select.defined_vars).next() |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
593 |
# for each solution |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
594 |
# 1. find each possible relation |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
595 |
# 2. for each relation: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
596 |
# 2.1. if the relation is meta, skip it |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
597 |
# 2.2. for each possible destination type, pick up possible |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
598 |
# variables for this type or use a new one |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
599 |
for sol in select.solutions: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
600 |
etype = sol[rql_var] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
601 |
sol_by_types = {} |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
602 |
for varname, var_etype in sol.items(): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
603 |
# don't push subject var to avoid "X relation X" suggestion |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
604 |
if varname != rql_var: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
605 |
sol_by_types.setdefault(var_etype, []).append(varname) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
606 |
for rschema in schema[etype].subject_relations(): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
607 |
if include_meta or not rschema.meta: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
608 |
for dest in rschema.objects(etype): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
609 |
for varname in sol_by_types.get(dest.type, (untyped_dest_var,)): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
610 |
suggestion = (rschema.type, varname) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
611 |
if suggestion not in relations: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
612 |
relations.add(suggestion) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
613 |
return sorted(relations) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
614 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
615 |
def vocabulary(self, select, rql_var, user_rtype, rtype_incomplete_value): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
616 |
"""return acceptable vocabulary for `rql_var` + `user_rtype` in `select` |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
617 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
618 |
Vocabulary is either found from schema (Yams) definition or |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
619 |
directly from database. |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
620 |
""" |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
621 |
schema = self._cw.vreg.schema |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
622 |
vocab = [] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
623 |
for sol in select.solutions: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
624 |
# for each solution : |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
625 |
# - If a vocabulary constraint exists on `rql_var+user_rtype`, use it |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
626 |
# to define possible values |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
627 |
# - Otherwise, query the database to fetch available values from |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
628 |
# database (limiting results to `self.attr_value_limit`) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
629 |
try: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
630 |
eschema = schema.eschema(sol[rql_var]) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
631 |
rdef = eschema.rdef(user_rtype) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
632 |
except KeyError: # unknown relation |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
633 |
continue |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
634 |
cstr = rdef.constraint_by_interface(IVocabularyConstraint) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
635 |
if cstr is not None: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
636 |
# a vocabulary is found, use it |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
637 |
vocab += [value for value in cstr.vocabulary() |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
638 |
if value.startswith(rtype_incomplete_value)] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
639 |
elif rdef.final: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
640 |
# no vocab, query database to find possible value |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
641 |
vocab_rql = 'DISTINCT Any V LIMIT %s WHERE X is %s, X %s V' % ( |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
642 |
self.attr_value_limit, eschema.type, user_rtype) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
643 |
vocab_kwargs = {} |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
644 |
if rtype_incomplete_value: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
645 |
vocab_rql += ', X %s LIKE %%(value)s' % user_rtype |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
646 |
vocab_kwargs['value'] = '%s%%' % rtype_incomplete_value |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
647 |
vocab += [value for value, in |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
648 |
self._cw.execute(vocab_rql, vocab_kwargs)] |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
649 |
return sorted(set(vocab)) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
650 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
651 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
652 |
|
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
653 |
@ajaxfunc(output_type='json') |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
654 |
def rql_suggest(self): |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
655 |
rql_builder = self._cw.vreg['components'].select_or_none('rql.suggestions', self._cw) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
656 |
if rql_builder: |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
657 |
return rql_builder.build_suggestions(self._cw.form['term']) |
dcd9bc1d1bca
[ui] provide an autocomplete RQL bar (closes #2439846)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7990
diff
changeset
|
658 |
return [] |