cubicweb/toolsutils.py
author Simon Chabot <simon.chabot@logilab.fr>
Thu, 05 Mar 2020 17:49:34 +0100
branch3.26
changeset 12908 5c6d242069b6
parent 12422 ca3fe2f275eb
child 12567 26744ad37953
permissions -rw-r--r--
[pkg] version 3.26.17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11693
diff changeset
    21
2397
cdedc2a32b06 [shell] move toolsutils.confirm() to logilab.common.shellutils
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2395
diff changeset
    22
# 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
    23
10966
8909593f46d4 [toolsutils] Small fixes to fill_templated_file()
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10688
diff changeset
    24
import io
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    25
import os
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    26
import 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
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    28
from os import listdir, makedirs, chmod, walk, remove
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    29
from os.path import exists, join, 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
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    34
except ImportError:  # readline not available, no completion
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
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    47
from cubicweb import warning  # pylint: disable=E0611
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
from cubicweb import ConfigurationError, ExecutionError
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    50
2790
968108e16066 move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2615
diff changeset
    51
def underline_title(title, car='-'):
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    52
    return title + '\n' + (car * len(title))
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    53
2790
968108e16066 move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2615
diff changeset
    54
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
def iter_dir(directory, condition_file=None, ignore=()):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
    """iterate on a directory"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
    for sub in listdir(directory):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        if sub in ('CVS', '.svn', '.hg'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
            continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
        if condition_file is not None and \
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    61
                not exists(join(directory, sub, condition_file)):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
            continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        if sub in ignore:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
            continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        yield sub
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    67
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
def create_dir(directory):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
    """create a directory if it doesn't exist yet"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
        makedirs(directory)
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
    72
        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
    73
    except OSError as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        import errno
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
        if ex.errno != errno.EEXIST:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
            raise
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
    77
        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
    78
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    79
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
def create_symlink(source, target):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
    """create a symbolic link"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    if exists(target):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
        remove(target)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
    symlink(source, target)
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
    85
    print('[symlink] %s <-- %s' % (target, source))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    87
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
def create_copy(source, target):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
    import shutil
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
    90
    print('[copy] %s <-- %s' % (target, source))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
    shutil.copy2(source, target)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
    92
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    93
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
def rm(whatever):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
    import shutil
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
    shutil.rmtree(whatever)
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
    97
    print('-> removed %s' % whatever)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
    99
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
def show_diffs(appl_file, ref_file, askconfirm=True):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
    """interactivly replace the old file with the new file according to
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
    user decision
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
    import shutil
4554
2279ba039494 use subprocess instead of os.popen to run diff
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 4212
diff changeset
   105
    pipe = subprocess.Popen(['diff', '-u', appl_file, ref_file], stdout=subprocess.PIPE)
12321
e116275bf1ac [py3] Decode subprocess stdout in toolsutils.show_diffs()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11767
diff changeset
   106
    diffs = pipe.stdout.read().decode('utf-8')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
    if diffs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
        if askconfirm:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
   109
            print()
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
   110
            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
   111
            action = ASK.ask('Replace ?', ('Y', 'n', 'q'), 'Y').lower()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
            action = 'y'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
        if action == 'y':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
                shutil.copyfile(ref_file, appl_file)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
            except IOError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
                os.system('chmod a+w %s' % appl_file)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
                shutil.copyfile(ref_file, appl_file)
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
   120
            print('replaced')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        elif action == 'q':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
            sys.exit(0)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
            copy_file = appl_file + '.default'
10614
57dfde80df11 [py3k] file → open
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10591
diff changeset
   125
            copy = open(copy_file, 'w')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
            copy.write(open(ref_file).read())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
            copy.close()
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
   128
            print('keep current version, the new file has been written to', copy_file)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
    else:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
   130
        print('no diff between %s and %s' % (appl_file, ref_file))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   132
5184
955ee1b24756 [c-c newcube] #1192: simpler cubicweb-ctl newcube, and more
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5021
diff changeset
   133
SKEL_EXCLUDE = ('*.py[co]', '*.orig', '*~', '*_flymake.py')
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   134
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   135
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
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
   137
                  exclude=SKEL_EXCLUDE, askconfirm=False):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
    import shutil
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
    from fnmatch import fnmatch
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
    skeldir = normpath(skeldir)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
    targetdir = normpath(targetdir)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
    for dirpath, dirnames, filenames in walk(skeldir):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
        tdirpath = dirpath.replace(skeldir, targetdir)
11454
7770559e3945 [devtools] Update skeleton and newcube command to cube as package layout
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   144
        if 'cubicweb_CUBENAME' in tdirpath:
7770559e3945 [devtools] Update skeleton and newcube command to cube as package layout
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   145
            tdirpath = tdirpath.replace('cubicweb_CUBENAME',
7770559e3945 [devtools] Update skeleton and newcube command to cube as package layout
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   146
                                        'cubicweb_' + context['cubename'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
        create_dir(tdirpath)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
        for fname in filenames:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
            if any(fnmatch(fname, pat) for pat in exclude):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
            fpath = join(dirpath, fname)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
            if 'CUBENAME' in fname:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
                tfpath = join(tdirpath, fname.replace('CUBENAME', context['cubename']))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
            elif 'DISTNAME' in fname:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
                tfpath = join(tdirpath, fname.replace('DISTNAME', context['distname']))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
                tfpath = join(tdirpath, fname)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
            if fname.endswith('.tmpl'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
                tfpath = tfpath[:-5]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
                if not askconfirm or not exists(tfpath) or \
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   161
                        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
   162
                    fill_templated_file(fpath, tfpath, context)
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
   163
                    print('[generate] %s <-- %s' % (tfpath, fpath))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
            elif exists(tfpath):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
                show_diffs(tfpath, fpath, askconfirm)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
                shutil.copyfile(fpath, tfpath)
11693
b44c0d2462c9 [skeleton] Copy files mode bits (debian/rules in particular)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11476
diff changeset
   168
                shutil.copymode(fpath, tfpath)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   169
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   170
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
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
   172
    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
   173
        template = fobj.read()
8909593f46d4 [toolsutils] Small fixes to fill_templated_file()
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10688
diff changeset
   174
    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
   175
        fobj.write(template % context)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   177
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
def restrict_perms_to_user(filepath, log=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
    """set -rw------- permission on the given file"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
    if log:
7155
4bab50b02927 [cwctl] refactor ui messages aiming for consistency and simplicity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6495
diff changeset
   181
        log('set permissions to 0600 for %s', filepath)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
    else:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
   183
        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
   184
    chmod(filepath, 0o600)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
11476
a9f26de5ea6c Let configuration option be overridden by environment variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11454
diff changeset
   186
a9f26de5ea6c Let configuration option be overridden by environment variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11454
diff changeset
   187
def option_value_from_env(option, default=None):
a9f26de5ea6c Let configuration option be overridden by environment variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11454
diff changeset
   188
    """Return the value of configuration `option` from cannonical environment
a9f26de5ea6c Let configuration option be overridden by environment variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11454
diff changeset
   189
    variable.
a9f26de5ea6c Let configuration option be overridden by environment variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11454
diff changeset
   190
    """
a9f26de5ea6c Let configuration option be overridden by environment variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11454
diff changeset
   191
    envvar = ('CW_' + '_'.join(option.split('-'))).upper()
a9f26de5ea6c Let configuration option be overridden by environment variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11454
diff changeset
   192
    return os.environ.get(envvar, default)
a9f26de5ea6c Let configuration option be overridden by environment variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11454
diff changeset
   193
a9f26de5ea6c Let configuration option be overridden by environment variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11454
diff changeset
   194
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
   195
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
   196
    """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
   197
    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
   198
    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
   199
    :exc:`ExecutionError` will be raised.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
    from logilab.common.fileutils import lines
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
    config = current = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
        for line in lines(config_file, comments='#'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
                option, value = line.split('=', 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
            except ValueError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
                option = line.strip().lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
                if option[0] == '[':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
                    # start a section
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
                    section = option[1:-1]
8697
574bb05e40a4 [toward py3k] rewrite has_key() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8695
diff changeset
   212
                    assert section not in config, \
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   213
                        'Section %s is defined more than once' % section
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
                    config[section] = current = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
                    continue
7896
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7879
diff changeset
   216
                sys.stderr.write('ignoring malformed line\n%r\n' % line)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
            option = option.strip().replace(' ', '_')
11476
a9f26de5ea6c Let configuration option be overridden by environment variables
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11454
diff changeset
   219
            value = option_value_from_env(option, value.strip())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
            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
   221
    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
   222
        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
   223
            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
   224
                                 '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
   225
        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
   226
            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
   227
                    config_file, ex)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
    return config
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
_HDLRS = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   233
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
class metacmdhandler(type):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
    def __new__(mcs, name, bases, classdict):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
        cls = super(metacmdhandler, mcs).__new__(mcs, name, bases, classdict)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
        if getattr(cls, 'cfgname', None) and getattr(cls, 'cmdname', None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
            _HDLRS.setdefault(cls.cmdname, []).append(cls)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
        return cls
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
10688
fa29f3628a1b [py3k] use six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10614
diff changeset
   242
@add_metaclass(metacmdhandler)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
class CommandHandler(object):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
    """configuration specific helper for cubicweb-ctl commands"""
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   245
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
    def __init__(self, config):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   247
        self.config = config
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
10688
fa29f3628a1b [py3k] use six.add_metaclass
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10614
diff changeset
   249
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
class Command(BaseCommand):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   251
    """base class for cubicweb-ctl commands"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   253
    def config_helper(self, config, required=True, cmdname=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
        if cmdname is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   255
            cmdname = self.name
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
        for helpercls in _HDLRS.get(cmdname, ()):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
            if helpercls.cfgname == config.name:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   258
                return helpercls(config)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   259
        if config.name == 'all-in-one':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
            for helpercls in _HDLRS.get(cmdname, ()):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
                if helpercls.cfgname == 'repository':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
                    return helpercls(config)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
        if required:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   264
            msg = 'No helper for command %s using %s configuration' % (
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   265
                cmdname, config.name)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   266
            raise ConfigurationError(msg)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   267
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
    def fail(self, reason):
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10331
diff changeset
   269
        print("command failed:", reason)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
        sys.exit(1)
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   271
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1138
diff changeset
   272
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   273
CONNECT_OPTIONS = (
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   274
    ("user",
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   275
     {'short': 'u', 'type': 'string', 'metavar': '<user>',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   276
      'help': 'connect as <user> instead of being prompted to give it.',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   277
      }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   278
     ),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
    ("password",
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   280
     {'short': 'p', 'type': 'password', 'metavar': '<password>',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
      'help': 'automatically give <password> for authentication instead of \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
being prompted to give it.',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   283
      }),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
    ("host",
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   285
     {'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
   286
      'default': None,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   287
      'help': 'specify the name server\'s host name. Will be detected by \
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   288
broadcast if not provided.',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
      }),
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   290
)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   291
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   292
# cwshell helpers #############################################################
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   293
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
   294
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
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
   296
    """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
   297
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
    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
   299
    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
   300
    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
   301
    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
   302
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
    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
   304
    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
   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
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
    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
   308
        """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
   309
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
        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
   311
            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
   312
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
        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
   314
            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
   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
        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
   317
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
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
   320
    """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
   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
    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
   323
    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
   324
    """
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
    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
   326
        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
   327
        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
   328
        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
   329
        # 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
   330
        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
   331
        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
   332
        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
   333
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
    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
   335
        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
   336
        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
   337
        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
   338
        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
   339
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
    @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
   341
    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
   342
        """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
   343
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
        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
   345
            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
   346
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
        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
   348
            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
   349
        """
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
        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
   351
        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
   352
            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
   353
        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
   354
        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
   355
        # 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
   356
        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
   357
            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
   358
        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
   359
        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
   360
            # 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
   361
            '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
   362
            # 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
   363
            '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
   364
            # 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
   365
            '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
   366
            # 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
   367
            'rql_query': parameters_text,
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   368
        }
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
   369
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
    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
   371
        """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
   372
        """
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
        # 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
   374
        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
   375
        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
   376
        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
   377
            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
   378
        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
   379
            # 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
   380
            # 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
   381
            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
   382
            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
   383
            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
   384
                    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
   385
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
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
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
   388
    """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
   389
    """
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   390
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
   391
    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
   392
        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
   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 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
   395
        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
   396
            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
   397
        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
   398
            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
   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
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
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
   402
    """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
   403
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
    ``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
   405
    ``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
   406
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
    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
   408
        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
   409
                  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
   410
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
    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
   412
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
    - 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
   414
    - 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
   415
      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
   416
    """
c0239d8ae742 [shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8697
diff changeset
   417
c0239d8ae742 [shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8697
diff changeset
   418
    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
   419
        # 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
   420
        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
   421
        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
   422
c0239d8ae742 [shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8697
diff changeset
   423
    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
   424
        """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
   425
c0239d8ae742 [shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8697
diff changeset
   426
        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
   427
        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
   428
c0239d8ae742 [shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8697
diff changeset
   429
        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
   430
        """
c0239d8ae742 [shell] provide autocompletion for rql queries in cubicweb-ctl shell (closes #3560541)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8697
diff changeset
   431
        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
   432
            # 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
   433
            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
   434
            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
   435
                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
   436
                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
   437
                    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
   438
                    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
   439
            else:
12422
ca3fe2f275eb [style] Make toolsutils.py flake8-clean
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12321
diff changeset
   440
                return None  # no matcher able to handle `text`
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
   441
        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
   442
            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
   443
        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
   444
            return None