remove python2 support from awxkit

This commit is contained in:
Ryan Petrello
2020-03-18 09:06:20 -04:00
parent db7f0f9421
commit 06b3e54fb1
18 changed files with 27 additions and 116 deletions

View File

@@ -6,7 +6,6 @@ import pkg_resources
import sys
from requests.exceptions import RequestException
import six
from .custom import handle_custom_actions
from .format import (add_authentication_arguments,
@@ -203,15 +202,7 @@ class CLI(object):
if hasattr(response, 'rc'):
raise SystemExit(response.rc)
else:
if six.PY3:
self.parser.print_help()
elif six.PY2 and not self.help:
# Unfortunately, argparse behavior between py2 and py3
# changed in a notable way when required subparsers
# have invalid (or missing) arguments specified
# see: https://github.com/python/cpython/commit/f97c59aaba2d93e48cbc6d25f7ff9f9c87f8d0b2
print('\nargument resource: invalid choice')
raise SystemExit(2)
self.parser.print_help()
def parse_action(self, page, from_sphinx=False):
"""Perform an HTTP OPTIONS request

View File

@@ -1,8 +1,6 @@
import functools
import json
from six import with_metaclass, PY3
from .stdout import monitor, monitor_workflow
from .utils import CustomRegistryMeta, color_enabled
from awxkit import api
@@ -24,7 +22,7 @@ class CustomActionRegistryMeta(CustomRegistryMeta):
return ' '.join([self.resource, self.action])
class CustomAction(with_metaclass(CustomActionRegistryMeta)):
class CustomAction(metaclass=CustomActionRegistryMeta):
"""Base class for defining a custom action for a resource."""
def __init__(self, page):
@@ -549,11 +547,8 @@ class SettingsModify(CustomAction):
return resp.from_json({'key': key, 'value': resp[key]})
def is_json(self, data):
err = ValueError
if PY3:
err = json.decoder.JSONDecodeError
try:
json.loads(data)
except err:
except json.decoder.JSONDecodeError:
return False
return True

View File

@@ -2,7 +2,6 @@ import locale
import json
from distutils.util import strtobool
import six
import yaml
from awxkit.cli.utils import colored
@@ -81,7 +80,7 @@ def add_output_formatting_arguments(parser, env):
def format_response(response, fmt='json', filter='.', changed=False):
if response is None:
return # HTTP 204
if isinstance(response, six.text_type):
if isinstance(response, str):
return response
if 'results' in response.__dict__:
@@ -115,7 +114,7 @@ def format_jq(output, fmt):
results = []
for x in jq.jq(fmt).transform(output, multiple_output=True):
if x not in (None, ''):
if isinstance(x, six.text_type):
if isinstance(x, str):
results.append(x)
else:
results.append(json.dumps(x))

View File

@@ -7,7 +7,6 @@ import sys
import yaml
from distutils.util import strtobool
import six
from .custom import CustomAction
from .format import add_output_formatting_arguments
@@ -182,7 +181,7 @@ class ResourceOptionsParser(object):
for k, v in parsed.items():
# add support for file reading at top-level JSON keys
# (to make things like SSH key data easier to work with)
if isinstance(v, six.text_type) and v.startswith('@'):
if isinstance(v, str) and v.startswith('@'):
path = os.path.expanduser(v[1:])
parsed[k] = open(path).read()

View File

@@ -1,7 +1,5 @@
import os
from six import PY3, with_metaclass
from awxkit import api, config
from awxkit.utils import to_str
from awxkit.api.pages import Page
@@ -45,7 +43,7 @@ DEPRECATED_RESOURCES_REVERSE = dict(
)
class CustomCommand(with_metaclass(CustomRegistryMeta)):
class CustomCommand(metaclass=CustomRegistryMeta):
"""Base class for implementing custom commands.
Custom commands represent static code which should run - they are
@@ -153,18 +151,7 @@ def parse_resource(client, skip_deprecated=False):
k, help='', **kwargs
)
try:
resource = client.parser.parse_known_args()[0].resource
except SystemExit:
if PY3:
raise
else:
# Unfortunately, argparse behavior between py2 and py3
# changed in a notable way when required subparsers
# have invalid (or missing) arguments specified
# see: https://github.com/python/cpython/commit/f97c59aaba2d93e48cbc6d25f7ff9f9c87f8d0b2
# In py2, this raises a SystemExit; which we want to _ignore_
resource = None
resource = client.parser.parse_known_args()[0].resource
if resource in DEPRECATED_RESOURCES.values():
client.argv[
client.argv.index(resource)

View File

@@ -5,8 +5,6 @@ import os
import sys
import threading
import six
_color = threading.local()
_color.enabled = True
@@ -38,29 +36,6 @@ class CustomRegistryMeta(type):
class HelpfulArgumentParser(ArgumentParser):
def __init__(self, *args, **kwargs):
super(HelpfulArgumentParser, self).__init__(*args, **kwargs)
if six.PY2:
# backport parser aliases support to py2
# see: https://github.com/python/cpython/commit/fd311a712d5876c3a3efff265978452eea759f85
SubParsersAction = self._registries['action']['parsers']
class _SubParsersAction(SubParsersAction):
def add_parser(self, name, **kwargs):
aliases = kwargs.pop('aliases', [])
parser = super(_SubParsersAction, self).add_parser(name, **kwargs)
if aliases:
self._choices_actions[-1].metavar = ' '.join([
name,
'({})'.format(', '.join(aliases))
])
for alias in aliases:
self._name_parser_map[alias] = parser
return parser
self._registries['action']['parsers'] = _SubParsersAction
def error(self, message): # pragma: nocover
"""Prints a usage message incorporating the message to stderr and
exits.