comparison sat_frontends/jp/arg_tools.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/jp/arg_tools.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # jp: a SàT command line tool
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.core import exceptions
22
23
24 def escape(arg, smart=True):
25 """format arg with quotes
26
27 @param smart(bool): if True, only escape if needed
28 """
29 if smart and not ' ' in arg and not '"' in arg:
30 return arg
31 return u'"' + arg.replace(u'"',u'\\"') + u'"'
32
33
34 def get_cmd_choices(cmd=None, parser=None):
35 try:
36 choices = parser._subparsers._group_actions[0].choices
37 return choices[cmd] if cmd is not None else choices
38 except (KeyError, AttributeError):
39 raise exceptions.NotFound
40
41
42 def get_use_args(host, args, use, verbose=False, parser=None):
43 """format args for argparse parser with values prefilled
44
45 @param host(JP): jp instance
46 @param args(list(str)): arguments to use
47 @param use(dict[str, str]): arguments to fill if found in parser
48 @param verbose(bool): if True a message will be displayed when argument is used or not
49 @param parser(argparse.ArgumentParser): parser to use
50 @return (tuple[list[str],list[str]]): 2 args lists:
51 - parser args, i.e. given args corresponding to parsers
52 - use args, i.e. generated args from use
53 """
54 # FIXME: positional args are not handled correclty
55 # if there is more that one, the position is not corrected
56 if parser is None:
57 parser = host.parser
58
59 # we check not optional args to see if there
60 # is a corresonding parser
61 # else USE args would not work correctly (only for current parser)
62 parser_args = []
63 for arg in args:
64 if arg.startswith('-'):
65 break
66 try:
67 parser = get_cmd_choices(arg, parser)
68 except exceptions.NotFound:
69 break
70 parser_args.append(arg)
71
72 # post_args are remaning given args,
73 # without the ones corresponding to parsers
74 post_args = args[len(parser_args):]
75
76 opt_args = []
77 pos_args = []
78 actions = {a.dest: a for a in parser._actions}
79 for arg, value in use.iteritems():
80 try:
81 if arg == u'item' and not u'item' in actions:
82 # small hack when --item is appended to a --items list
83 arg = u'items'
84 action = actions[arg]
85 except KeyError:
86 if verbose:
87 host.disp(_(u'ignoring {name}={value}, not corresponding to any argument (in USE)').format(
88 name=arg,
89 value=escape(value)))
90 else:
91 if verbose:
92 host.disp(_(u'arg {name}={value} (in USE)').format(name=arg, value=escape(value)))
93 if not action.option_strings:
94 pos_args.append(value)
95 else:
96 opt_args.append(action.option_strings[0])
97 opt_args.append(value)
98 return parser_args, opt_args + pos_args + post_args