add --pop-leader to remove a value in a leader option

This commit is contained in:
Emmanuel Garette 2019-07-27 18:51:16 +02:00 committed by Emmanuel Garette
parent 452e1fb0e0
commit dcc7510676
6 changed files with 109 additions and 57 deletions

View file

@ -7,7 +7,7 @@ from argparse import RawDescriptionHelpFormatter
from tiramisu_cmdline_parser import TiramisuCmdlineParser from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \ from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Config SymLinkOption, OptionDescription, Config
from tiramisu_json_api import Config as JsonConfig from tiramisu_api import Config as JsonConfig

View file

@ -6,7 +6,7 @@ import pytest
from tiramisu_cmdline_parser import TiramisuCmdlineParser from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \ from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Leadership, Config, submulti SymLinkOption, OptionDescription, Leadership, Config, submulti
from tiramisu_json_api import Config as JsonConfig from tiramisu_api import Config as JsonConfig
def get_config(json, with_mandatory=False): def get_config(json, with_mandatory=False):
@ -23,7 +23,7 @@ def get_config(json, with_mandatory=False):
opt_list = [leader, follower, follower_submulti, follower_integer, follower_boolean, follower_choice] opt_list = [leader, follower, follower_submulti, follower_integer, follower_boolean, follower_choice]
if with_mandatory: if with_mandatory:
opt_list.append(StrOption('follower_mandatory', "Follower mandatory", multi=True, properties=('mandatory',))) opt_list.append(StrOption('follower_mandatory', "Follower mandatory", multi=True, properties=('mandatory',)))
leadership = Leadership('leader', '', opt_list) leadership = Leadership('leader', 'leader', opt_list)
config = Config(OptionDescription('root', 'root', [leadership])) config = Config(OptionDescription('root', 'root', [leadership]))
if json == 'tiramisu': if json == 'tiramisu':
return config return config
@ -38,6 +38,7 @@ def json(request):
def test_leadership_help(json): def test_leadership_help(json):
output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]] output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]]
[--leader.pop-leader INDEX]
[--leader.follower INDEX [FOLLOWER]] --leader.follower_submulti [--leader.follower INDEX [FOLLOWER]] --leader.follower_submulti
INDEX [FOLLOWER_SUBMULTI ...] INDEX [FOLLOWER_SUBMULTI ...]
[--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]]
@ -54,6 +55,7 @@ leader:
--leader.leader [LEADER [LEADER ...]] --leader.leader [LEADER [LEADER ...]]
Leader var Leader var
--leader.pop-leader INDEX
--leader.follower INDEX [FOLLOWER] --leader.follower INDEX [FOLLOWER]
Follower Follower
--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...] --leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]
@ -105,6 +107,7 @@ def test_leadership_modif_follower(json):
def test_leadership_modif_follower_not_submulti(json): def test_leadership_modif_follower_not_submulti(json):
output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]] output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]]
[--leader.pop-leader INDEX]
[--leader.follower INDEX [FOLLOWER]] [--leader.follower INDEX [FOLLOWER]]
[--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]] [--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]]
[--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]]
@ -199,6 +202,7 @@ def test_leadership_modif_follower_choice(json):
def test_leadership_modif_follower_choice_unknown(json): def test_leadership_modif_follower_choice_unknown(json):
output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]] output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]]
[--leader.pop-leader INDEX]
[--leader.follower INDEX [FOLLOWER]] [--leader.follower INDEX [FOLLOWER]]
[--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]] [--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]]
[--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]]
@ -222,6 +226,7 @@ prog.py: error: invalid choice: 'opt_unknown' (choose from 'opt1', 'opt2')
def test_leadership_modif_follower_not_number(json): def test_leadership_modif_follower_not_number(json):
output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]] output = """usage: prog.py [-h] [--leader.leader [LEADER [LEADER ...]]]
[--leader.pop-leader INDEX]
[--leader.follower INDEX [FOLLOWER]] [--leader.follower INDEX [FOLLOWER]]
[--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]] [--leader.follower_submulti INDEX [FOLLOWER_SUBMULTI ...]]
[--leader.follower_integer INDEX [FOLLOWER_INTEGER]] [--leader.follower_integer INDEX [FOLLOWER_INTEGER]]
@ -260,6 +265,23 @@ def test_leadership_modif_multi(json):
assert config.value.dict() == output assert config.value.dict() == output
def test_leadership_modif_multi_reduce(json):
output = {'leader.leader': ['192.168.1.1', '192.168.253.1'],
'leader.follower': ['255.255.255.128', '255.255.255.0'],
'leader.follower_boolean': [None, None],
'leader.follower_choice': [None, None],
'leader.follower_integer': [None, None],
'leader.follower_submulti': [[], []]}
config = get_config(json)
parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['--leader.leader', '192.168.1.1', '10.253.10.1', '192.168.253.1',
'--leader.follower', '0', '255.255.255.128',
'--leader.follower', '2', '255.255.255.0',
'--leader.pop-leader', '1'])
assert config.value.dict() == output
def test_leadership_modif_mandatory(json): def test_leadership_modif_mandatory(json):
output = {'leader.leader': ['192.168.1.1'], output = {'leader.leader': ['192.168.1.1'],
'leader.follower': [None], 'leader.follower': [None],
@ -269,6 +291,7 @@ def test_leadership_modif_mandatory(json):
'leader.follower_integer': [None], 'leader.follower_integer': [None],
'leader.follower_submulti': [['255.255.255.128']]} 'leader.follower_submulti': [['255.255.255.128']]}
output2 = """usage: prog.py --leader.leader ['192.168.1.1'] [-h] output2 = """usage: prog.py --leader.leader ['192.168.1.1'] [-h]
[--leader.pop-leader INDEX]
[--leader.follower INDEX [FOLLOWER]] [--leader.follower INDEX [FOLLOWER]]
--leader.follower_submulti --leader.follower_submulti
INDEX [FOLLOWER_SUBMULTI ...] INDEX [FOLLOWER_SUBMULTI ...]

View file

@ -6,7 +6,7 @@ import pytest
from tiramisu_cmdline_parser import TiramisuCmdlineParser from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \ from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Config SymLinkOption, OptionDescription, Config
from tiramisu_json_api import Config as JsonConfig from tiramisu_api import Config as JsonConfig
def get_config(json, has_tree=False, default_verbosity=False, add_long=False, add_store_false=False): def get_config(json, has_tree=False, default_verbosity=False, add_long=False, add_store_false=False):
@ -80,8 +80,6 @@ od1.od0:
-nv, --od1.od0.no-verbosity -nv, --od1.od0.no-verbosity
od2: od2:
od2
--od2.before BEFORE Before --od2.before BEFORE Before
--od2.after AFTER After --od2.after AFTER After
@ -115,8 +113,6 @@ od1.od0:
-nv, --od1.od0.no-verbosity -nv, --od1.od0.no-verbosity
od2: od2:
od2
--od2.before BEFORE Before --od2.before BEFORE Before
--od2.after AFTER After --od2.after AFTER After

View file

@ -6,7 +6,7 @@ import pytest
from tiramisu_cmdline_parser import TiramisuCmdlineParser from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \ from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Config SymLinkOption, OptionDescription, Config
from tiramisu_json_api import Config as JsonConfig from tiramisu_api import Config as JsonConfig
def get_config(json, has_tree=False, default_verbosity=False, add_long=False, add_store_false=False): def get_config(json, has_tree=False, default_verbosity=False, add_long=False, add_store_false=False):

View file

@ -6,7 +6,7 @@ from contextlib import redirect_stderr
from tiramisu_cmdline_parser import TiramisuCmdlineParser from tiramisu_cmdline_parser import TiramisuCmdlineParser
from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \ from tiramisu import IntOption, StrOption, BoolOption, ChoiceOption, \
SymLinkOption, OptionDescription, Config SymLinkOption, OptionDescription, Config
from tiramisu_json_api import Config as JsonConfig from tiramisu_api import Config as JsonConfig
@pytest.fixture(params=['tiramisu', 'tiramisu-json']) @pytest.fixture(params=['tiramisu', 'tiramisu-json'])

View file

@ -39,6 +39,7 @@ class TiramisuNamespace(Namespace):
super().__setattr__('_config', config) super().__setattr__('_config', config)
super().__setattr__('_root', root) super().__setattr__('_root', root)
super().__setattr__('list_force_no', {}) super().__setattr__('list_force_no', {})
super().__setattr__('list_force_del', {})
self._populate() self._populate()
super().__init__() super().__init__()
@ -61,6 +62,8 @@ class TiramisuNamespace(Namespace):
value: Any) -> None: value: Any) -> None:
if key in self.list_force_no: if key in self.list_force_no:
true_key = self.list_force_no[key] true_key = self.list_force_no[key]
elif key in self.list_force_del:
true_key = self.list_force_del[key]
else: else:
true_key = key true_key = key
option = self._config.option(true_key) option = self._config.option(true_key)
@ -71,7 +74,10 @@ class TiramisuNamespace(Namespace):
_setattr = self._setattr _setattr = self._setattr
true_value = value true_value = value
try: try:
_setattr(option, true_key, key, value) if key in self.list_force_del:
option.value.pop(value)
else:
_setattr(option, true_key, key, value)
except ValueError as err: except ValueError as err:
if option.option.type() == 'choice': if option.option.type() == 'choice':
raise ValueError("invalid choice: '{}' (choose from {})".format(true_value, ', '.join([f"'{val}'" for val in option.value.list()]))) raise ValueError("invalid choice: '{}' (choose from {})".format(true_value, ', '.join([f"'{val}'" for val in option.value.list()])))
@ -122,17 +128,12 @@ class TiramisuHelpFormatter:
self.items[0][0].__name__ == '_format_text': self.items[0][0].__name__ == '_format_text':
return '' return ''
# Remove OD if name == description # Remove OD if name == description
if self.items and \ if self.formatter.remove_empty_description_od and \
self.formatter.remove_empty_description_od and \ self.items is not None and \
self.items[0][0].__name__ == '_format_text': self.heading is not None and \
name = self.items[0][1][0] len(self.items) > 1 and \
path = self.heading self.items[0][0].__name__ != '_format_text':
if '.' in path: return ''
compare = path.rsplit('.', 1)[1]
else:
compare = path
if name == compare:
return ''
return super().format_help() return super().format_help()
@ -151,18 +152,23 @@ class _BuildKwargs:
option: 'Option', option: 'Option',
cmdlineparser: 'TiramisuCmdlineParser', cmdlineparser: 'TiramisuCmdlineParser',
properties: List[str], properties: List[str],
force_no: bool) -> None: force_no: bool,
force_del: bool) -> None:
self.kwargs = {} self.kwargs = {}
self.cmdlineparser = cmdlineparser self.cmdlineparser = cmdlineparser
self.properties = properties self.properties = properties
self.force_no = force_no self.force_no = force_no
if not self.force_no: self.force_del = force_del
if not self.force_no and not self.force_del:
self.kwargs['help'] = option.doc().replace('%', '%%') self.kwargs['help'] = option.doc().replace('%', '%%')
if 'positional' not in self.properties: if 'positional' not in self.properties:
is_short_name = self.cmdlineparser._is_short_name(name, 'longargument' in self.properties) is_short_name = self.cmdlineparser._is_short_name(name, 'longargument' in self.properties)
if self.force_no: if self.force_no:
ga_name = self.gen_argument_name(name, is_short_name) ga_name = self.gen_argument_name(name, is_short_name)
self.cmdlineparser.namespace.list_force_no[ga_name] = option.path() self.cmdlineparser.namespace.list_force_no[ga_name] = option.path()
elif self.force_del:
ga_name = self.gen_argument_name(name, is_short_name)
self.cmdlineparser.namespace.list_force_del[ga_name] = option.path()
else: else:
ga_name = name ga_name = name
self.kwargs['dest'] = self.gen_argument_name(option.path(), False) self.kwargs['dest'] = self.gen_argument_name(option.path(), False)
@ -180,6 +186,8 @@ class _BuildKwargs:
is_short_name = self.cmdlineparser._is_short_name(option.name(), 'longargument' in self.properties) is_short_name = self.cmdlineparser._is_short_name(option.name(), 'longargument' in self.properties)
if self.force_no: if self.force_no:
name = self.gen_argument_name(option.name(), is_short_name) name = self.gen_argument_name(option.name(), is_short_name)
elif self.force_del:
name = self.gen_argument_name(option.name(), is_short_name)
else: else:
name = option.name() name = option.name()
self.args.insert(0, self.cmdlineparser._gen_argument(name, is_short_name)) self.args.insert(0, self.cmdlineparser._gen_argument(name, is_short_name))
@ -195,6 +203,16 @@ class _BuildKwargs:
name = sname[0] + '.' + prefix + sname[1] name = sname[0] + '.' + prefix + sname[1]
else: else:
name = prefix + name name = prefix + name
if self.force_del:
if is_short_name:
prefix = 'p'
else:
prefix = 'pop-'
if '.' in name:
sname = name.rsplit('.', 1)
name = sname[0] + '.' + prefix + sname[1]
else:
name = prefix + name
return name return name
def get(self) -> Tuple[Dict]: def get(self) -> Tuple[Dict]:
@ -315,7 +333,7 @@ class TiramisuCmdlineParser(ArgumentParser):
config: Config, config: Config,
prefix: Optional[str], prefix: Optional[str],
_forhelp: bool, _forhelp: bool,
group): group, level):
for obj in config.list(type='all'): for obj in config.list(type='all'):
# do not display frozen option # do not display frozen option
if 'frozen' in obj.option.properties(): if 'frozen' in obj.option.properties():
@ -329,23 +347,32 @@ class TiramisuCmdlineParser(ArgumentParser):
prefix_ = prefix + '.' + obj.option.name() prefix_ = prefix + '.' + obj.option.name()
else: else:
prefix_ = obj.option.path() prefix_ = obj.option.path()
self._config_to_argparser(_forhelp, obj, prefix_, newgroup) self._config_to_argparser(_forhelp, obj, prefix_, newgroup, level + 1)
elif obj.option.type() == 'boolean' and not obj.option.issymlinkoption(): elif obj.option.type() == 'boolean' and not obj.option.issymlinkoption():
yield obj, False if not obj.option.isleader():
yield obj, True yield obj, False, None
yield obj, True, None
else:
yield obj, False, False
yield obj, False, True
yield obj, True, None
elif obj.option.isleader():
yield obj, None, False
yield obj, None, True
else: else:
yield obj, None yield obj, None, None
def _config_to_argparser(self, def _config_to_argparser(self,
_forhelp: bool, _forhelp: bool,
config, config,
prefix: Optional[str], prefix: Optional[str],
group=None) -> None: group=None,
level=0) -> None:
if group is None: if group is None:
group = super() group = super()
actions = {} actions = {}
leadership_len = None leadership_len = None
for obj, force_no in self._config_list(config, prefix, _forhelp, group): for obj, force_no, force_del in self._config_list(config, prefix, _forhelp, group, level):
option = obj.option option = obj.option
name = option.name() name = option.name()
if name.startswith(self.prefix_chars): if name.startswith(self.prefix_chars):
@ -356,7 +383,9 @@ class TiramisuCmdlineParser(ArgumentParser):
for action in actions[symlink_name]: for action in actions[symlink_name]:
action.add_argument(option) action.add_argument(option)
continue continue
if option.isleader(): if force_del:
value = None
elif option.isleader():
value = obj.value.get() value = obj.value.get()
leadership_len = len(value) leadership_len = len(value)
elif option.isfollower(): elif option.isfollower():
@ -371,7 +400,7 @@ class TiramisuCmdlineParser(ArgumentParser):
properties = obj.option.properties() properties = obj.option.properties()
else: else:
properties = obj.property.get() properties = obj.property.get()
kwargs = _BuildKwargs(name, option, self, properties, force_no) kwargs = _BuildKwargs(name, option, self, properties, force_no, force_del)
if not option.isfollower() and _forhelp and not obj.owner.isdefault() and value is not None: if not option.isfollower() and _forhelp and not obj.owner.isdefault() and value is not None:
if not force_no: if not force_no:
self._option_is_not_default(properties, self._option_is_not_default(properties,
@ -393,33 +422,34 @@ class TiramisuCmdlineParser(ArgumentParser):
kwargs['default'] = SUPPRESS kwargs['default'] = SUPPRESS
if _forhelp and 'mandatory' in properties: if _forhelp and 'mandatory' in properties:
kwargs['required'] = True kwargs['required'] = True
if option.type() == 'boolean' and not option.isfollower(): if not force_del and option.type() == 'boolean':
if 'storefalse' in properties: if not option.isfollower():
if force_no: if 'storefalse' in properties:
action = 'store_true' if force_no:
else: action = 'store_true'
else:
action = 'store_false'
elif force_no:
action = 'store_false' action = 'store_false'
elif force_no: else:
action = 'store_false' action = 'store_true'
kwargs['action'] = action
else: else:
action = 'store_true'
kwargs['action'] = action
else:
if option.type() == 'boolean':
kwargs['metavar'] = 'INDEX' kwargs['metavar'] = 'INDEX'
if option.type() != 'boolean': if option.type() != 'boolean' or force_del:
if _forhelp: if not force_del:
value = obj.value.default() if _forhelp:
if value not in [None, []]: value = obj.value.default()
#kwargs['default'] = kwargs['const'] = option.default() if value not in [None, []]:
#kwargs['action'] = 'store_const' #kwargs['default'] = kwargs['const'] = option.default()
kwargs['nargs'] = '?' #kwargs['action'] = 'store_const'
kwargs['nargs'] = '?'
if not option.isfollower() and option.ismulti(): if not option.isfollower() and option.ismulti():
if _forhelp and 'mandatory' in properties: if _forhelp and 'mandatory' in properties:
kwargs['nargs'] = '+' kwargs['nargs'] = '+'
else: else:
kwargs['nargs'] = '*' kwargs['nargs'] = '*'
if option.isfollower() and not option.type() == 'boolean': if option.isfollower() and not option.type() == 'boolean':
metavar = option.name().upper() metavar = option.name().upper()
if option.issubmulti(): if option.issubmulti():
@ -438,7 +468,10 @@ class TiramisuCmdlineParser(ArgumentParser):
kwargs['metavar'] = ('INDEX', choices) kwargs['metavar'] = ('INDEX', choices)
else: else:
kwargs['metavar'] = ('INDEX', metavar) kwargs['metavar'] = ('INDEX', metavar)
if option.type() == 'string': if force_del:
kwargs['metavar'] = 'INDEX'
kwargs['type'] = int
elif option.type() == 'string':
pass pass
elif option.type() == 'integer' or option.type() == 'boolean': elif option.type() == 'integer' or option.type() == 'boolean':
# when boolean we are here only if follower # when boolean we are here only if follower