author | Julien Cristau <julien.cristau@logilab.fr> |
Thu, 10 Dec 2015 16:58:45 +0100 | |
changeset 10997 | da712d3f0601 |
parent 10966 | 8909593f46d4 |
permissions | -rw-r--r-- |
7879
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7301
diff
changeset
|
1 |
# copyright 2003-2011 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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
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:
5324
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
18 |
"""some utilities for cubicweb command line tools""" |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
19 |
from __future__ import print_function |
0 | 20 |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
2397
cdedc2a32b06
[shell] move toolsutils.confirm() to logilab.common.shellutils
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
23 |
# XXX move most of this in logilab.common (shellutils ?) |
cdedc2a32b06
[shell] move toolsutils.confirm() to logilab.common.shellutils
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
24 |
|
10966
8909593f46d4
[toolsutils] Small fixes to fill_templated_file()
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10688
diff
changeset
|
25 |
import io |
0 | 26 |
import os, sys |
4554
2279ba039494
use subprocess instead of os.popen to run diff
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4212
diff
changeset
|
27 |
import subprocess |
3115
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
28 |
from os import listdir, makedirs, environ, chmod, walk, remove |
0 | 29 |
from os.path import exists, join, abspath, normpath |
9740
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
30 |
import re |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
31 |
from rlcompleter import Completer |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
32 |
try: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
33 |
import readline |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
34 |
except ImportError: # readline not available, no completion |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
35 |
pass |
3115
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
36 |
try: |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
37 |
from os import symlink |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
38 |
except ImportError: |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
39 |
def symlink(*args): |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
40 |
raise NotImplementedError |
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2615
diff
changeset
|
41 |
|
10688
fa29f3628a1b
[py3k] use six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10614
diff
changeset
|
42 |
from six import add_metaclass |
fa29f3628a1b
[py3k] use six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10614
diff
changeset
|
43 |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
44 |
from logilab.common.clcommands import Command as BaseCommand |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
45 |
from logilab.common.shellutils import ASK |
0 | 46 |
|
7879
9aae456abab5
[pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7301
diff
changeset
|
47 |
from cubicweb import warning # pylint: disable=E0611 |
0 | 48 |
from cubicweb import ConfigurationError, ExecutionError |
49 |
||
2790
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
50 |
def underline_title(title, car='-'): |
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
51 |
return title+'\n'+(car*len(title)) |
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
52 |
|
0 | 53 |
def iter_dir(directory, condition_file=None, ignore=()): |
54 |
"""iterate on a directory""" |
|
55 |
for sub in listdir(directory): |
|
56 |
if sub in ('CVS', '.svn', '.hg'): |
|
57 |
continue |
|
58 |
if condition_file is not None and \ |
|
59 |
not exists(join(directory, sub, condition_file)): |
|
60 |
continue |
|
61 |
if sub in ignore: |
|
62 |
continue |
|
63 |
yield sub |
|
64 |
||
65 |
def create_dir(directory): |
|
66 |
"""create a directory if it doesn't exist yet""" |
|
67 |
try: |
|
68 |
makedirs(directory) |
|
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
69 |
print('-> created directory %s' % directory) |
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
70 |
except OSError as ex: |
0 | 71 |
import errno |
72 |
if ex.errno != errno.EEXIST: |
|
73 |
raise |
|
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
74 |
print('-> no need to create existing directory %s' % directory) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
75 |
|
0 | 76 |
def create_symlink(source, target): |
77 |
"""create a symbolic link""" |
|
78 |
if exists(target): |
|
79 |
remove(target) |
|
80 |
symlink(source, target) |
|
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
81 |
print('[symlink] %s <-- %s' % (target, source)) |
0 | 82 |
|
83 |
def create_copy(source, target): |
|
84 |
import shutil |
|
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
85 |
print('[copy] %s <-- %s' % (target, source)) |
0 | 86 |
shutil.copy2(source, target) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
87 |
|
0 | 88 |
def rm(whatever): |
89 |
import shutil |
|
90 |
shutil.rmtree(whatever) |
|
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
91 |
print('-> removed %s' % whatever) |
0 | 92 |
|
93 |
def show_diffs(appl_file, ref_file, askconfirm=True): |
|
94 |
"""interactivly replace the old file with the new file according to |
|
95 |
user decision |
|
96 |
""" |
|
97 |
import shutil |
|
4554
2279ba039494
use subprocess instead of os.popen to run diff
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4212
diff
changeset
|
98 |
pipe = subprocess.Popen(['diff', '-u', appl_file, ref_file], stdout=subprocess.PIPE) |
2279ba039494
use subprocess instead of os.popen to run diff
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
4212
diff
changeset
|
99 |
diffs = pipe.stdout.read() |
0 | 100 |
if diffs: |
101 |
if askconfirm: |
|
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
102 |
print() |
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
103 |
print(diffs) |
5324
449cc4fa9c42
[migration] makes Yes the default answer to replace configuration file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
104 |
action = ASK.ask('Replace ?', ('Y', 'n', 'q'), 'Y').lower() |
0 | 105 |
else: |
106 |
action = 'y' |
|
107 |
if action == 'y': |
|
108 |
try: |
|
109 |
shutil.copyfile(ref_file, appl_file) |
|
110 |
except IOError: |
|
111 |
os.system('chmod a+w %s' % appl_file) |
|
112 |
shutil.copyfile(ref_file, appl_file) |
|
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
113 |
print('replaced') |
0 | 114 |
elif action == 'q': |
115 |
sys.exit(0) |
|
116 |
else: |
|
117 |
copy_file = appl_file + '.default' |
|
10614
57dfde80df11
[py3k] file → open
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10591
diff
changeset
|
118 |
copy = open(copy_file, 'w') |
0 | 119 |
copy.write(open(ref_file).read()) |
120 |
copy.close() |
|
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
121 |
print('keep current version, the new file has been written to', copy_file) |
0 | 122 |
else: |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
123 |
print('no diff between %s and %s' % (appl_file, ref_file)) |
0 | 124 |
|
5184
955ee1b24756
[c-c newcube] #1192: simpler cubicweb-ctl newcube, and more
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5021
diff
changeset
|
125 |
SKEL_EXCLUDE = ('*.py[co]', '*.orig', '*~', '*_flymake.py') |
0 | 126 |
def copy_skeleton(skeldir, targetdir, context, |
5184
955ee1b24756
[c-c newcube] #1192: simpler cubicweb-ctl newcube, and more
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5021
diff
changeset
|
127 |
exclude=SKEL_EXCLUDE, askconfirm=False): |
0 | 128 |
import shutil |
129 |
from fnmatch import fnmatch |
|
130 |
skeldir = normpath(skeldir) |
|
131 |
targetdir = normpath(targetdir) |
|
132 |
for dirpath, dirnames, filenames in walk(skeldir): |
|
133 |
tdirpath = dirpath.replace(skeldir, targetdir) |
|
134 |
create_dir(tdirpath) |
|
135 |
for fname in filenames: |
|
136 |
if any(fnmatch(fname, pat) for pat in exclude): |
|
137 |
continue |
|
138 |
fpath = join(dirpath, fname) |
|
139 |
if 'CUBENAME' in fname: |
|
140 |
tfpath = join(tdirpath, fname.replace('CUBENAME', context['cubename'])) |
|
141 |
elif 'DISTNAME' in fname: |
|
142 |
tfpath = join(tdirpath, fname.replace('DISTNAME', context['distname'])) |
|
143 |
else: |
|
144 |
tfpath = join(tdirpath, fname) |
|
145 |
if fname.endswith('.tmpl'): |
|
146 |
tfpath = tfpath[:-5] |
|
147 |
if not askconfirm or not exists(tfpath) or \ |
|
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2476
diff
changeset
|
148 |
ASK.confirm('%s exists, overwrite?' % tfpath): |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
149 |
fill_templated_file(fpath, tfpath, context) |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
150 |
print('[generate] %s <-- %s' % (tfpath, fpath)) |
0 | 151 |
elif exists(tfpath): |
152 |
show_diffs(tfpath, fpath, askconfirm) |
|
153 |
else: |
|
154 |
shutil.copyfile(fpath, tfpath) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
155 |
|
0 | 156 |
def fill_templated_file(fpath, tfpath, context): |
10966
8909593f46d4
[toolsutils] Small fixes to fill_templated_file()
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10688
diff
changeset
|
157 |
with io.open(fpath, encoding='ascii') as fobj: |
8909593f46d4
[toolsutils] Small fixes to fill_templated_file()
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10688
diff
changeset
|
158 |
template = fobj.read() |
8909593f46d4
[toolsutils] Small fixes to fill_templated_file()
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10688
diff
changeset
|
159 |
with io.open(tfpath, 'w', encoding='ascii') as fobj: |
8909593f46d4
[toolsutils] Small fixes to fill_templated_file()
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10688
diff
changeset
|
160 |
fobj.write(template % context) |
0 | 161 |
|
162 |
def restrict_perms_to_user(filepath, log=None): |
|
163 |
"""set -rw------- permission on the given file""" |
|
164 |
if log: |
|
7155
4bab50b02927
[cwctl] refactor ui messages aiming for consistency and simplicity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6495
diff
changeset
|
165 |
log('set permissions to 0600 for %s', filepath) |
0 | 166 |
else: |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
167 |
print('-> set permissions to 0600 for %s' % filepath) |
10591
8e46ed1a0b8a
[py3k] octals and long
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10589
diff
changeset
|
168 |
chmod(filepath, 0o600) |
0 | 169 |
|
7301
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
170 |
def read_config(config_file, raise_if_unreadable=False): |
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
171 |
"""read some simple configuration from `config_file` and return it as a |
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
172 |
dictionary. If `raise_if_unreadable` is false (the default), an empty |
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
173 |
dictionary will be returned if the file is inexistant or unreadable, else |
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
174 |
:exc:`ExecutionError` will be raised. |
0 | 175 |
""" |
176 |
from logilab.common.fileutils import lines |
|
177 |
config = current = {} |
|
178 |
try: |
|
179 |
for line in lines(config_file, comments='#'): |
|
180 |
try: |
|
181 |
option, value = line.split('=', 1) |
|
182 |
except ValueError: |
|
183 |
option = line.strip().lower() |
|
184 |
if option[0] == '[': |
|
185 |
# start a section |
|
186 |
section = option[1:-1] |
|
8697
574bb05e40a4
[toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8695
diff
changeset
|
187 |
assert section not in config, \ |
0 | 188 |
'Section %s is defined more than once' % section |
189 |
config[section] = current = {} |
|
190 |
continue |
|
7896
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7879
diff
changeset
|
191 |
sys.stderr.write('ignoring malformed line\n%r\n' % line) |
0 | 192 |
continue |
193 |
option = option.strip().replace(' ', '_') |
|
194 |
value = value.strip() |
|
195 |
current[option] = value or None |
|
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
196 |
except IOError as ex: |
7301
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
197 |
if raise_if_unreadable: |
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
198 |
raise ExecutionError('%s. Are you logged with the correct user ' |
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
199 |
'to use this instance?' % ex) |
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
200 |
else: |
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
201 |
warning('missing or non readable configuration file %s (%s)', |
93e96700e0c0
[configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
202 |
config_file, ex) |
0 | 203 |
return config |
204 |
||
205 |
||
206 |
_HDLRS = {} |
|
207 |
||
208 |
class metacmdhandler(type): |
|
209 |
def __new__(mcs, name, bases, classdict): |
|
210 |
cls = super(metacmdhandler, mcs).__new__(mcs, name, bases, classdict) |
|
211 |
if getattr(cls, 'cfgname', None) and getattr(cls, 'cmdname', None): |
|
212 |
_HDLRS.setdefault(cls.cmdname, []).append(cls) |
|
213 |
return cls |
|
214 |
||
215 |
||
10688
fa29f3628a1b
[py3k] use six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10614
diff
changeset
|
216 |
@add_metaclass(metacmdhandler) |
0 | 217 |
class CommandHandler(object): |
218 |
"""configuration specific helper for cubicweb-ctl commands""" |
|
219 |
def __init__(self, config): |
|
220 |
self.config = config |
|
221 |
||
10688
fa29f3628a1b
[py3k] use six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10614
diff
changeset
|
222 |
|
0 | 223 |
class Command(BaseCommand): |
224 |
"""base class for cubicweb-ctl commands""" |
|
225 |
||
226 |
def config_helper(self, config, required=True, cmdname=None): |
|
227 |
if cmdname is None: |
|
228 |
cmdname = self.name |
|
229 |
for helpercls in _HDLRS.get(cmdname, ()): |
|
230 |
if helpercls.cfgname == config.name: |
|
231 |
return helpercls(config) |
|
232 |
if config.name == 'all-in-one': |
|
233 |
for helpercls in _HDLRS.get(cmdname, ()): |
|
234 |
if helpercls.cfgname == 'repository': |
|
235 |
return helpercls(config) |
|
236 |
if required: |
|
237 |
msg = 'No helper for command %s using %s configuration' % ( |
|
238 |
cmdname, config.name) |
|
239 |
raise ConfigurationError(msg) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
240 |
|
0 | 241 |
def fail(self, reason): |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10331
diff
changeset
|
242 |
print("command failed:", reason) |
0 | 243 |
sys.exit(1) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
244 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
245 |
|
0 | 246 |
CONNECT_OPTIONS = ( |
247 |
("user", |
|
248 |
{'short': 'u', 'type' : 'string', 'metavar': '<user>', |
|
249 |
'help': 'connect as <user> instead of being prompted to give it.', |
|
250 |
} |
|
251 |
), |
|
252 |
("password", |
|
253 |
{'short': 'p', 'type' : 'password', 'metavar': '<password>', |
|
254 |
'help': 'automatically give <password> for authentication instead of \ |
|
255 |
being prompted to give it.', |
|
256 |
}), |
|
257 |
("host", |
|
258 |
{'short': 'H', 'type' : 'string', 'metavar': '<hostname>', |
|
541
0d75cfe50f83
fix default value of pyro ns host
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
259 |
'default': None, |
0 | 260 |
'help': 'specify the name server\'s host name. Will be detected by \ |
261 |
broadcast if not provided.', |
|
262 |
}), |
|
263 |
) |
|
264 |
||
9740
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
265 |
## cwshell helpers ############################################################# |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
266 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
267 |
class AbstractMatcher(object): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
268 |
"""Abstract class for CWShellCompleter's matchers. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
269 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
270 |
A matcher should implement a ``possible_matches`` method. This |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
271 |
method has to return the list of possible completions for user's input. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
272 |
Because of the python / readline interaction, each completion should |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
273 |
be a superset of the user's input. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
274 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
275 |
NOTE: readline tokenizes user's input and only passes last token to |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
276 |
completers. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
277 |
""" |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
278 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
279 |
def possible_matches(self, text): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
280 |
"""return possible completions for user's input. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
281 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
282 |
Parameters: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
283 |
text: the user's input |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
284 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
285 |
Return: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
286 |
a list of completions. Each completion includes the original input. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
287 |
""" |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
288 |
raise NotImplementedError() |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
289 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
290 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
291 |
class RQLExecuteMatcher(AbstractMatcher): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
292 |
"""Custom matcher for rql queries. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
293 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
294 |
If user's input starts with ``rql(`` or ``session.execute(`` and |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
295 |
the corresponding rql query is incomplete, suggest some valid completions. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
296 |
""" |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
297 |
query_match_rgx = re.compile( |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
298 |
r'(?P<func_prefix>\s*(?:rql)' # match rql, possibly indented |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
299 |
r'|' # or |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
300 |
r'\s*(?:\w+\.execute))' # match .execute, possibly indented |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
301 |
# end of <func_prefix> |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
302 |
r'\(' # followed by a parenthesis |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
303 |
r'(?P<quote_delim>["\'])' # a quote or double quote |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
304 |
r'(?P<parameters>.*)') # and some content |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
305 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
306 |
def __init__(self, local_ctx, req): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
307 |
self.local_ctx = local_ctx |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
308 |
self.req = req |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
309 |
self.schema = req.vreg.schema |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
310 |
self.rsb = req.vreg['components'].select('rql.suggestions', req) |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
311 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
312 |
@staticmethod |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
313 |
def match(text): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
314 |
"""check if ``text`` looks like a call to ``rql`` or ``session.execute`` |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
315 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
316 |
Parameters: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
317 |
text: the user's input |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
318 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
319 |
Returns: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
320 |
None if it doesn't match, the query structure otherwise. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
321 |
""" |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
322 |
query_match = RQLExecuteMatcher.query_match_rgx.match(text) |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
323 |
if query_match is None: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
324 |
return None |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
325 |
parameters_text = query_match.group('parameters') |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
326 |
quote_delim = query_match.group('quote_delim') |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
327 |
# first parameter is fully specified, no completion needed |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
328 |
if re.match(r"(.*?)%s" % quote_delim, parameters_text) is not None: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
329 |
return None |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
330 |
func_prefix = query_match.group('func_prefix') |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
331 |
return { |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
332 |
# user's input |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
333 |
'text': text, |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
334 |
# rql( or session.execute( |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
335 |
'func_prefix': func_prefix, |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
336 |
# offset of rql query |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
337 |
'rql_offset': len(func_prefix) + 2, |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
338 |
# incomplete rql query |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
339 |
'rql_query': parameters_text, |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
340 |
} |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
341 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
342 |
def possible_matches(self, text): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
343 |
"""call ``rql.suggestions`` component to complete user's input. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
344 |
""" |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
345 |
# readline will only send last token, but we need the entire user's input |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
346 |
user_input = readline.get_line_buffer() |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
347 |
query_struct = self.match(user_input) |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
348 |
if query_struct is None: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
349 |
return [] |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
350 |
else: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
351 |
# we must only send completions of the last token => compute where it |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
352 |
# starts relatively to the rql query itself. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
353 |
completion_offset = readline.get_begidx() - query_struct['rql_offset'] |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
354 |
rql_query = query_struct['rql_query'] |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
355 |
return [suggestion[completion_offset:] |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
356 |
for suggestion in self.rsb.build_suggestions(rql_query)] |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
357 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
358 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
359 |
class DefaultMatcher(AbstractMatcher): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
360 |
"""Default matcher: delegate to standard's `rlcompleter.Completer`` class |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
361 |
""" |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
362 |
def __init__(self, local_ctx): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
363 |
self.completer = Completer(local_ctx) |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
364 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
365 |
def possible_matches(self, text): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
366 |
if "." in text: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
367 |
return self.completer.attr_matches(text) |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
368 |
else: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
369 |
return self.completer.global_matches(text) |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
370 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
371 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
372 |
class CWShellCompleter(object): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
373 |
"""Custom auto-completion helper for cubicweb-ctl shell. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
374 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
375 |
``CWShellCompleter`` provides a ``complete`` method suitable for |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
376 |
``readline.set_completer``. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
377 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
378 |
Attributes: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
379 |
matchers: the list of ``AbstractMatcher`` instances that will suggest |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
380 |
possible completions |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
381 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
382 |
The completion process is the following: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
383 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
384 |
- readline calls the ``complete`` method with user's input, |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
385 |
- the ``complete`` method asks for each known matchers if |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
386 |
it can suggest completions for user's input. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
387 |
""" |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
388 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
389 |
def __init__(self, local_ctx): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
390 |
# list of matchers to ask for possible matches on completion |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
391 |
self.matchers = [DefaultMatcher(local_ctx)] |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
392 |
self.matchers.insert(0, RQLExecuteMatcher(local_ctx, local_ctx['session'])) |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
393 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
394 |
def complete(self, text, state): |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
395 |
"""readline's completer method |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
396 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
397 |
cf http://docs.python.org/2/library/readline.html#readline.set_completer |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
398 |
for more details. |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
399 |
|
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
400 |
Implementation inspired by `rlcompleter.Completer` |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
401 |
""" |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
402 |
if state == 0: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
403 |
# reset self.matches |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
404 |
self.matches = [] |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
405 |
for matcher in self.matchers: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
406 |
matches = matcher.possible_matches(text) |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
407 |
if matches: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
408 |
self.matches = matches |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
409 |
break |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
410 |
else: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
411 |
return None # no matcher able to handle `text` |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
412 |
try: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
413 |
return self.matches[state] |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
414 |
except IndexError: |
c0239d8ae742
[shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
8697
diff
changeset
|
415 |
return None |