python 3.5 support

This commit is contained in:
Emmanuel Garette 2019-08-22 15:58:34 +02:00 committed by Emmanuel Garette
parent 11b2bcc591
commit 2184332e62
3 changed files with 40 additions and 11 deletions

View file

@ -487,13 +487,13 @@ prog.py: error: unrecognized arguments: --int
def test_readme_cross_tree(json): def test_readme_cross_tree(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none} output = """usage: prog.py "none" [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --int prog.py: error: unrecognized arguments: --root.int
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py') parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py')
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
parser.parse_args(['none', '--int']) parser.parse_args(['none', '--root.int'])
except SystemExit as err: except SystemExit as err:
assert str(err) == "2" assert str(err) == "2"
else: else:
@ -503,13 +503,13 @@ prog.py: error: unrecognized arguments: --int
def test_readme_cross_tree_remove(json): def test_readme_cross_tree_remove(json):
output = """usage: prog.py "none" [-h] [-v] [-nv] output = """usage: prog.py "none" [-h] [-v] [-nv]
prog.py: error: unrecognized arguments: --int prog.py: error: unrecognized arguments: --root.int
""" """
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False) parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', display_modified_value=False)
f = StringIO() f = StringIO()
with redirect_stderr(f): with redirect_stderr(f):
try: try:
parser.parse_args(['none', '--int']) parser.parse_args(['none', '--root.int'])
except SystemExit as err: except SystemExit as err:
assert str(err) == "2" assert str(err) == "2"
else: else:
@ -859,3 +859,32 @@ def test_readme_longargument(json):
parser = TiramisuCmdlineParser(config, 'prog.py') parser = TiramisuCmdlineParser(config, 'prog.py')
parser.parse_args(['list', '--list', 'a', '--v']) parser.parse_args(['list', '--list', 'a', '--v'])
assert config.value.dict() == output assert config.value.dict() == output
def test_readme_unknown_key(json):
output1 = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --unknown
"""
output2 = """usage: prog.py [-h] [-v] [-nv] {str,list,int,none}
prog.py: error: unrecognized arguments: --root.unknown
"""
parser = TiramisuCmdlineParser(get_config(json, True), 'prog.py', fullpath=False)
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--unknown'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output1
f = StringIO()
with redirect_stderr(f):
try:
parser.parse_args(['--root.unknown'])
except SystemExit as err:
assert str(err) == "2"
else:
raise Exception('must raises')
assert f.getvalue() == output2

View file

@ -1,4 +1,4 @@
from .api import TiramisuCmdlineParser from .api import TiramisuCmdlineParser
__version__ = "0.2" __version__ = "0.3"
__all__ = ('TiramisuCmdlineParser',) __all__ = ('TiramisuCmdlineParser',)

View file

@ -20,7 +20,7 @@ from gettext import gettext as _
try: try:
from tiramisu import Config from tiramisu import Config
from tiramisu.error import PropertiesOptionError, RequirementError, LeadershipError from tiramisu.error import PropertiesOptionError, RequirementError, LeadershipError
except (ModuleNotFoundError, ImportError): except ImportError:
Config = None Config = None
from tiramisu_api.error import PropertiesOptionError from tiramisu_api.error import PropertiesOptionError
RequirementError = PropertiesOptionError RequirementError = PropertiesOptionError
@ -29,7 +29,7 @@ try:
from tiramisu__api import Config as ConfigJson from tiramisu__api import Config as ConfigJson
if Config is None: if Config is None:
Config = ConfigJson Config = ConfigJson
except ModuleNotFoundError: except ImportError:
ConfigJson = Config ConfigJson = Config
@ -44,7 +44,7 @@ def get_choice_list(obj, properties, display):
if display: if display:
choices = '{{{}}}'.format(','.join(choices)) choices = '{{{}}}'.format(','.join(choices))
if 'mandatory' not in properties: if 'mandatory' not in properties:
choices = f'[{choices}]' choices = '[{}]'.format(choices)
return choices return choices
@ -376,9 +376,9 @@ class TiramisuCmdlineParser(ArgumentParser):
if type != 'boolean': if type != 'boolean':
if isinstance(value, list): if isinstance(value, list):
for val in value: for val in value:
self.prog += f' "{val}"' self.prog += ' "{}"'.format(val)
else: else:
self.prog += f' "{value}"' self.prog += ' "{}"'.format(value)
def _config_list(self, def _config_list(self,
config: Config, config: Config,
@ -512,7 +512,7 @@ class TiramisuCmdlineParser(ArgumentParser):
else: else:
kwargs['nargs'] = 2 kwargs['nargs'] = 2
if _forhelp and 'mandatory' not in properties: if _forhelp and 'mandatory' not in properties:
metavar = f'[{metavar}]' metavar = '[{}]'.format(metavar)
if option.type() == 'choice': if option.type() == 'choice':
# do not manage choice with argparse there is problem with integer problem # do not manage choice with argparse there is problem with integer problem
kwargs['metavar'] = ('INDEX', get_choice_list(obj, properties, True)) kwargs['metavar'] = ('INDEX', get_choice_list(obj, properties, True))