annotate frontends/src/jp/arg_tools.py @ 2493:984792a451bc

jp (common/table): a column can be hidden + fix for empty tables - avoid crash when table is empty - a column can now be hidden on display, this can be useful if one data is needed (e.g. to change color of an other column with a filter), but we don't want to display it - filters have a new argument with all data of a row (for the same reason as above, one column can be used to change display of other column)
author Goffi <goffi@goffi.org>
date Wed, 28 Feb 2018 18:28:39 +0100
parents 0046283a285d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
3
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # jp: a SàT command line tool
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
6
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
11
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
16
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
19
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core import exceptions
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
22
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
23
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
24 def escape(arg, smart=True):
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
25 """format arg with quotes
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
26
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
27 @param smart(bool): if True, only escape if needed
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
28 """
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
29 if smart and not ' ' in arg and not '"' in arg:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
30 return arg
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
31 return u'"' + arg.replace(u'"',u'\\"') + u'"'
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
32
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
33
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
34 def get_cmd_choices(cmd=None, parser=None):
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
35 try:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
36 choices = parser._subparsers._group_actions[0].choices
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
37 return choices[cmd] if cmd is not None else choices
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
38 except (KeyError, AttributeError):
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
39 raise exceptions.NotFound
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
40
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
41
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
42 def get_use_args(host, args, use, verbose=False, parser=None):
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
43 """format args for argparse parser with values prefilled
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
44
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
45 @param host(JP): jp instance
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
46 @param args(list(str)): arguments to use
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
47 @param use(dict[str, str]): arguments to fill if found in parser
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
48 @param verbose(bool): if True a message will be displayed when argument is used or not
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
49 @param parser(argparse.ArgumentParser): parser to use
2317
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
50 @return (tuple[list[str],list[str]]): 2 args lists:
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
51 - parser args, i.e. given args corresponding to parsers
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
52 - use args, i.e. generated args from use
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
53 """
2317
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
54 # FIXME: positional args are not handled correclty
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
55 # if there is more that one, the position is not corrected
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
56 if parser is None:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
57 parser = host.parser
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
58
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
59 # we check not optional args to see if there
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
60 # is a corresonding parser
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
61 # else USE args would not work correctly (only for current parser)
2317
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
62 parser_args = []
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
63 for arg in args:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
64 if arg.startswith('-'):
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
65 break
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
66 try:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
67 parser = get_cmd_choices(arg, parser)
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
68 except exceptions.NotFound:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
69 break
2317
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
70 parser_args.append(arg)
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
71
2317
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
72 # post_args are remaning given args,
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
73 # without the ones corresponding to parsers
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
74 post_args = args[len(parser_args):]
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
75
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
76 opt_args = []
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
77 pos_args = []
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
78 actions = {a.dest: a for a in parser._actions}
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
79 for arg, value in use.iteritems():
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
80 try:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
81 if arg == u'item' and not u'item' in actions:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
82 # small hack when --item is appended to a --items list
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
83 arg = u'items'
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
84 action = actions[arg]
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
85 except KeyError:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
86 if verbose:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
87 host.disp(_(u'ignoring {name}={value}, not corresponding to any argument (in USE)').format(
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
88 name=arg,
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
89 value=escape(value)))
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
90 else:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
91 if verbose:
2354
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
92 host.disp(_(u'arg {name}={value} (in USE)').format(name=arg, value=escape(value)))
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
93 if not action.option_strings:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
94 pos_args.append(value)
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
95 else:
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
96 opt_args.append(action.option_strings[0])
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents:
diff changeset
97 opt_args.append(value)
2317
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
98 return parser_args, opt_args + pos_args + post_args