Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Merge 3a12677 into 3f0d9c1
Browse files Browse the repository at this point in the history
  • Loading branch information
romuald committed Mar 15, 2020
2 parents 3f0d9c1 + 3a12677 commit b2242de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
23 changes: 20 additions & 3 deletions gandi/cli/core/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
from gandi.cli.core.base import GandiContextHelper


def str_type(what):
"""
python2 / click compatibilty:
assert that "str" type is used (not unicode)
"""
if not isinstance(what, str):
return what.encode('utf-8')
return what

class GandiChoice(click.Choice):

""" Base class for custom Choice parameters. """
Expand Down Expand Up @@ -36,12 +45,14 @@ def choices(self):
"api '%s' and that it's running." % (api.host))
sys.exit(1)

self._choices = [str_type(val) for val in self._choices]

return self._choices

def convert(self, value, param, ctx):
""" Internal method to use correct context. """
self.gandi = ctx.obj
return click.Choice.convert(self, value, param, ctx)
return click.Choice.convert(self, str_type(value), param, ctx)

def convert_deprecated_value(self, value):
""" To override when needed """
Expand All @@ -62,13 +73,14 @@ def _get_choices(self, gandi):
iso_codes.append(item['iso'])
if item.get('dc_code'):
dc_codes.append(item['dc_code'])

return dc_codes + iso_codes

def convert(self, value, param, ctx):
""" Convert value to uppercase. """
self.gandi = ctx.obj
value = value.upper()
return click.Choice.convert(self, value, param, ctx)
return click.Choice.convert(self, str_type(value), param, ctx)

def convert_deprecated_value(self, value):
""" To update the configuration with the new datacenter naming """
Expand Down Expand Up @@ -280,6 +292,7 @@ class CertificateDcvMethod(click.Choice):

name = 'certificate dcv method'
choices = ['email', 'dns', 'file', 'auto']
case_sensitive = True

def __init__(self):
""" Initialize choices list. """
Expand All @@ -295,6 +308,7 @@ class IpType(click.Choice):

name = 'ip type'
choices = ['private', 'public']
case_sensitive = True

def __init__(self):
""" Initialize choices list. """
Expand Down Expand Up @@ -446,6 +460,7 @@ class OperStepParamType(click.Choice):

name = 'oper step'
choices = ['BILL', 'WAIT', 'RUN', 'ERROR']
case_sensitive = True

def __init__(self):
""" Initialize choices list. """
Expand All @@ -466,7 +481,7 @@ def convert(self, value, param, ctx):
""" Convert value to uppercase. """
self.gandi = ctx.obj
value = value.upper()
return click.Choice.convert(self, value, param, ctx)
return click.Choice.convert(self, str_type(value), param, ctx)


class AlgorithmType(click.Choice):
Expand All @@ -475,6 +490,7 @@ class AlgorithmType(click.Choice):
name = 'algorithm'
choices = ['1', '2', '3', '5', '6', '7', '8', '10', '12', '13', '14', '15',
'16', '253', '254']
case_sensitive = True

def __init__(self):
""" Initialize choices list. """
Expand All @@ -486,6 +502,7 @@ class FlagsType(click.Choice):

name = 'flags'
choices = ['256', '257']
case_sensitive = True

def __init__(self):
""" Initialize choices list. """
Expand Down
3 changes: 2 additions & 1 deletion gandi/cli/tests/commands/test_paas.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ def test_console(self):
def test_clone_missing(self):
result = self.invoke_with_exceptions(paas.clone, [])

self.assertEqual(result.output, """\
# output differs between click < 7.1 and >: 7.1 (' -> ")
self.assertEqual(result.output.replace("'", '"'), """\
Usage: clone [OPTIONS] NAME
Try "clone --help" for help.
Expand Down

0 comments on commit b2242de

Please sign in to comment.