comparison sat_frontends/jp/arg_tools.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
24 def escape(arg, smart=True): 24 def escape(arg, smart=True):
25 """format arg with quotes 25 """format arg with quotes
26 26
27 @param smart(bool): if True, only escape if needed 27 @param smart(bool): if True, only escape if needed
28 """ 28 """
29 if smart and not ' ' in arg and not '"' in arg: 29 if smart and not " " in arg and not '"' in arg:
30 return arg 30 return arg
31 return u'"' + arg.replace(u'"',u'\\"') + u'"' 31 return u'"' + arg.replace(u'"', u'\\"') + u'"'
32 32
33 33
34 def get_cmd_choices(cmd=None, parser=None): 34 def get_cmd_choices(cmd=None, parser=None):
35 try: 35 try:
36 choices = parser._subparsers._group_actions[0].choices 36 choices = parser._subparsers._group_actions[0].choices
59 # we check not optional args to see if there 59 # we check not optional args to see if there
60 # is a corresonding parser 60 # is a corresonding parser
61 # else USE args would not work correctly (only for current parser) 61 # else USE args would not work correctly (only for current parser)
62 parser_args = [] 62 parser_args = []
63 for arg in args: 63 for arg in args:
64 if arg.startswith('-'): 64 if arg.startswith("-"):
65 break 65 break
66 try: 66 try:
67 parser = get_cmd_choices(arg, parser) 67 parser = get_cmd_choices(arg, parser)
68 except exceptions.NotFound: 68 except exceptions.NotFound:
69 break 69 break
70 parser_args.append(arg) 70 parser_args.append(arg)
71 71
72 # post_args are remaning given args, 72 # post_args are remaning given args,
73 # without the ones corresponding to parsers 73 # without the ones corresponding to parsers
74 post_args = args[len(parser_args):] 74 post_args = args[len(parser_args) :]
75 75
76 opt_args = [] 76 opt_args = []
77 pos_args = [] 77 pos_args = []
78 actions = {a.dest: a for a in parser._actions} 78 actions = {a.dest: a for a in parser._actions}
79 for arg, value in use.iteritems(): 79 for arg, value in use.iteritems():
80 try: 80 try:
81 if arg == u'item' and not u'item' in actions: 81 if arg == u"item" and not u"item" in actions:
82 # small hack when --item is appended to a --items list 82 # small hack when --item is appended to a --items list
83 arg = u'items' 83 arg = u"items"
84 action = actions[arg] 84 action = actions[arg]
85 except KeyError: 85 except KeyError:
86 if verbose: 86 if verbose:
87 host.disp(_(u'ignoring {name}={value}, not corresponding to any argument (in USE)').format( 87 host.disp(
88 name=arg, 88 _(
89 value=escape(value))) 89 u"ignoring {name}={value}, not corresponding to any argument (in USE)"
90 ).format(name=arg, value=escape(value))
91 )
90 else: 92 else:
91 if verbose: 93 if verbose:
92 host.disp(_(u'arg {name}={value} (in USE)').format(name=arg, value=escape(value))) 94 host.disp(
95 _(u"arg {name}={value} (in USE)").format(
96 name=arg, value=escape(value)
97 )
98 )
93 if not action.option_strings: 99 if not action.option_strings:
94 pos_args.append(value) 100 pos_args.append(value)
95 else: 101 else:
96 opt_args.append(action.option_strings[0]) 102 opt_args.append(action.option_strings[0])
97 opt_args.append(value) 103 opt_args.append(value)