annotate frontends/src/jp/arg_tools.py @ 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
author Goffi <goffi@goffi.org>
date Sat, 08 Jul 2017 21:45:14 +0200
parents
children f4e05600577b
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
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
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
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
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
50 """
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
51 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
52 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
53
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
54 # 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
55 # 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
56 # else USE args would not work correctly (only for current 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
57 cmd_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
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 cmd_args.append(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
66
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 # we remove command 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
68 # they'll be in returned list (use_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
69 del args[:len(cmd_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
70
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 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
72 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
73 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
74 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
75 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
76 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
77 # 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
78 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
79 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
80 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
81 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
82 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
83 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
84 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
85 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
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'arg {name}={value} set (in USE)').format(name=arg, 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
88 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
89 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
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 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
92 opt_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
93 return cmd_args + opt_args + pos_args