Skip to content

Commit

Permalink
Merge pull request #8754 from nateprewitt/six_removal
Browse files Browse the repository at this point in the history
Remove six from awscli codebase
  • Loading branch information
nateprewitt authored Jun 20, 2024
2 parents d3ac781 + 341a42d commit 1094c86
Show file tree
Hide file tree
Showing 69 changed files with 231 additions and 400 deletions.
5 changes: 2 additions & 3 deletions awscli/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# language governing permissions and limitations under the License.
import argparse
import sys
from awscli.compat import six
from difflib import get_close_matches


Expand Down Expand Up @@ -106,12 +105,12 @@ def parse_known_args(self, args, namespace=None):
# default to utf-8.
terminal_encoding = 'utf-8'
for arg, value in vars(parsed).items():
if isinstance(value, six.binary_type):
if isinstance(value, bytes):
setattr(parsed, arg, value.decode(terminal_encoding))
elif isinstance(value, list):
encoded = []
for v in value:
if isinstance(v, six.binary_type):
if isinstance(v, bytes):
encoded.append(v.decode(terminal_encoding))
else:
encoded.append(v)
Expand Down
9 changes: 4 additions & 5 deletions awscli/argprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"""Module for processing CLI args."""
import os
import logging
from awscli.compat import six

from botocore.compat import OrderedDict, json

Expand Down Expand Up @@ -166,7 +165,7 @@ def _unpack_cli_arg(argument_model, value, cli_name):
return _unpack_complex_cli_arg(
argument_model, value, cli_name)
else:
return six.text_type(value)
return str(value)


def _unpack_json_cli_arg(argument_model, value, cli_name):
Expand All @@ -185,7 +184,7 @@ def _unpack_complex_cli_arg(argument_model, value, cli_name):
return _unpack_json_cli_arg(argument_model, value, cli_name)
raise ParamError(cli_name, "Invalid JSON:\n%s" % value)
elif type_name == 'list':
if isinstance(value, six.string_types):
if isinstance(value, str):
if value.lstrip()[0] == '[':
return _unpack_json_cli_arg(argument_model, value, cli_name)
elif isinstance(value, list) and len(value) == 1:
Expand Down Expand Up @@ -226,7 +225,7 @@ def unpack_scalar_cli_arg(argument_model, value, cli_name=''):
raise ParamError(cli_name, msg)
return open(file_path, 'rb')
elif argument_model.type_name == 'boolean':
if isinstance(value, six.string_types) and value.lower() == 'false':
if isinstance(value, str) and value.lower() == 'false':
return False
return bool(value)
else:
Expand Down Expand Up @@ -401,7 +400,7 @@ def _should_parse_as_shorthand(self, cli_argument, value):
check_val = value[0]
else:
check_val = value
if isinstance(check_val, six.string_types) and check_val.strip().startswith(
if isinstance(check_val, str) and check_val.strip().startswith(
('[', '{')):
LOG.debug("Param %s looks like JSON, not considered for "
"param shorthand.", cli_argument.py_name)
Expand Down
14 changes: 7 additions & 7 deletions awscli/bcdoc/docstringparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from botocore.compat import six
from html.parser import HTMLParser


class DocStringParser(six.moves.html_parser.HTMLParser):
class DocStringParser(HTMLParser):
"""
A simple HTML parser. Focused on converting the subset of HTML
that appears in the documentation strings of the JSON models into
Expand All @@ -23,20 +23,20 @@ class DocStringParser(six.moves.html_parser.HTMLParser):
def __init__(self, doc):
self.tree = None
self.doc = doc
six.moves.html_parser.HTMLParser.__init__(self)
HTMLParser.__init__(self)

def reset(self):
six.moves.html_parser.HTMLParser.reset(self)
HTMLParser.reset(self)
self.tree = HTMLTree(self.doc)

def feed(self, data):
# HTMLParser is an old style class, so the super() method will not work.
six.moves.html_parser.HTMLParser.feed(self, data)
HTMLParser.feed(self, data)
self.tree.write()
self.tree = HTMLTree(self.doc)

def close(self):
six.moves.html_parser.HTMLParser.close(self)
HTMLParser.close(self)
# Write if there is anything remaining.
self.tree.write()
self.tree = HTMLTree(self.doc)
Expand Down Expand Up @@ -176,7 +176,7 @@ class DataNode(Node):
"""
def __init__(self, data, parent=None):
super(DataNode, self).__init__(parent)
if not isinstance(data, six.string_types):
if not isinstance(data, str):
raise ValueError("Expecting string type, %s given." % type(data))
self.data = data

Expand Down
1 change: 0 additions & 1 deletion awscli/clidriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from awscli.formatter import get_formatter
from awscli.plugin import load_plugins
from awscli.commands import CLICommand
from awscli.compat import six
from awscli.argparser import MainArgParser
from awscli.argparser import ServiceArgParser
from awscli.argparser import ArgTableArgParser
Expand Down
132 changes: 40 additions & 92 deletions awscli/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,29 @@
import zipfile
import signal
import contextlib
import queue
import io
import collections.abc as collections_abc
import locale
import urllib.parse as urlparse
from urllib.error import URLError
from urllib.request import urlopen
from configparser import RawConfigParser
from functools import partial

from botocore.compat import six
#import botocore.compat
from urllib.error import URLError

from botocore.compat import six
from botocore.compat import OrderedDict

# If you ever want to import from the vendored six. Add it here and then
# import from awscli.compat. Also try to keep it in alphabetical order.
# This may get large.
advance_iterator = six.advance_iterator
PY3 = six.PY3
queue = six.moves.queue
shlex_quote = six.moves.shlex_quote
StringIO = six.StringIO
BytesIO = six.BytesIO
urlopen = six.moves.urllib.request.urlopen
binary_type = six.binary_type
# Backwards compatible definitions from six
PY3 = sys.version_info[0] == 3
advance_iterator = next
shlex_quote = shlex.quote
StringIO = io.StringIO
BytesIO = io.BytesIO
binary_type = bytes
raw_input = input


# Most, but not all, python installations will have zlib. This is required to
Expand Down Expand Up @@ -97,96 +101,40 @@ def __exit__(self, type, value, traceback):


def ensure_text_type(s):
if isinstance(s, six.text_type):
if isinstance(s, str):
return s
if isinstance(s, six.binary_type):
if isinstance(s, bytes):
return s.decode('utf-8')
raise ValueError("Expected str, unicode or bytes, received %s." % type(s))


if six.PY3:
import collections.abc as collections_abc
import locale
import urllib.parse as urlparse

from urllib.error import URLError

raw_input = input

def get_binary_stdin():
if sys.stdin is None:
raise StdinMissingError()
return sys.stdin.buffer

def get_binary_stdout():
return sys.stdout.buffer

def _get_text_writer(stream, errors):
return stream

def bytes_print(statement, stdout=None):
"""
This function is used to write raw bytes to stdout.
"""
if stdout is None:
stdout = sys.stdout

if getattr(stdout, 'buffer', None):
stdout.buffer.write(statement)
else:
# If it is not possible to write to the standard out buffer.
# The next best option is to decode and write to standard out.
stdout.write(statement.decode('utf-8'))

else:
import codecs
import collections as collections_abc
import locale
import io
import urlparse

from urllib2 import URLError
def get_binary_stdin():
if sys.stdin is None:
raise StdinMissingError()
return sys.stdin.buffer

raw_input = raw_input

def get_binary_stdin():
if sys.stdin is None:
raise StdinMissingError()
return sys.stdin
def get_binary_stdout():
return sys.stdout.buffer

def get_binary_stdout():
return sys.stdout

def _get_text_writer(stream, errors):
# In python3, all the sys.stdout/sys.stderr streams are in text
# mode. This means they expect unicode, and will encode the
# unicode automatically before actually writing to stdout/stderr.
# In python2, that's not the case. In order to provide a consistent
# interface, we can create a wrapper around sys.stdout that will take
# unicode, and automatically encode it to the preferred encoding.
# That way consumers can just call get_text_writer(stream) and write
# unicode to the returned stream. Note that get_text_writer
# just returns the stream in the PY3 section above because python3
# handles this.

# We're going to use the preferred encoding, but in cases that there is
# no preferred encoding we're going to fall back to assuming ASCII is
# what we should use. This will currently break the use of
# PYTHONIOENCODING, which would require checking stream.encoding first,
# however, the existing behavior is to only use
# locale.getpreferredencoding() and so in the hope of not breaking what
# is currently working, we will continue to only use that.
encoding = locale.getpreferredencoding()
if encoding is None:
encoding = "ascii"
def _get_text_writer(stream, errors):
return stream

return codecs.getwriter(encoding)(stream, errors)

def bytes_print(statement, stdout=None):
if stdout is None:
stdout = sys.stdout
def bytes_print(statement, stdout=None):
"""
This function is used to write raw bytes to stdout.
"""
if stdout is None:
stdout = sys.stdout

stdout.write(statement)
if getattr(stdout, 'buffer', None):
stdout.buffer.write(statement)
else:
# If it is not possible to write to the standard out buffer.
# The next best option is to decode and write to standard out.
stdout.write(statement.decode('utf-8'))


def compat_open(filename, mode='r', encoding=None, access_permissions=None):
Expand Down Expand Up @@ -252,7 +200,7 @@ def compat_shell_quote(s, platform=None):
if platform == "win32":
return _windows_shell_quote(s)
else:
return shlex_quote(s)
return shlex.quote(s)


def _windows_shell_quote(s):
Expand Down
5 changes: 2 additions & 3 deletions awscli/customizations/awslambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
import copy
from contextlib import closing

from botocore.vendored import six

from awscli.arguments import CustomArgument, CLIArgument
from awscli.compat import BytesIO


ERROR_MSG = (
Expand Down Expand Up @@ -90,7 +89,7 @@ def _should_contain_zip_content(value):
# still try to load the contents as a zip file
# to be absolutely sure.
value = value.encode('utf-8')
fileobj = six.BytesIO(value)
fileobj = BytesIO(value)
try:
with closing(zipfile.ZipFile(fileobj)) as f:
f.infolist()
Expand Down
5 changes: 2 additions & 3 deletions awscli/customizations/cloudformation/artifact_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import contextlib
import uuid
import shutil
from awscli.compat import six
from botocore.utils import set_value_from_jmespath

from awscli.compat import urlparse
Expand All @@ -33,7 +32,7 @@


def is_path_value_valid(path):
return isinstance(path, six.string_types)
return isinstance(path, str)


def make_abs_path(directory, path):
Expand Down Expand Up @@ -70,7 +69,7 @@ def parse_s3_url(url,
object_key_property="Key",
version_property=None):

if isinstance(url, six.string_types) \
if isinstance(url, str) \
and url.startswith("s3://"):

# Python < 2.7.10 don't parse query parameters from URI with custom
Expand Down
4 changes: 1 addition & 3 deletions awscli/customizations/cloudformation/yamlhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import yaml
from yaml.resolver import ScalarNode, SequenceNode

from awscli.compat import six


def intrinsics_multi_constructor(loader, tag_prefix, node):
"""
Expand All @@ -35,7 +33,7 @@ def intrinsics_multi_constructor(loader, tag_prefix, node):

cfntag = prefix + tag

if tag == "GetAtt" and isinstance(node.value, six.string_types):
if tag == "GetAtt" and isinstance(node.value, str):
# ShortHand notation for !GetAtt accepts Resource.Attribute format
# while the standard notation is to use an array
# [Resource, Attribute]. Convert shorthand to standard format
Expand Down
5 changes: 2 additions & 3 deletions awscli/customizations/codedeploy/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

from botocore.exceptions import ClientError

from awscli.compat import six
from awscli.customizations.codedeploy.utils import validate_s3_location
from awscli.customizations.commands import BasicCommand
from awscli.compat import ZIP_COMPRESSION_MODE
from awscli.compat import BytesIO, ZIP_COMPRESSION_MODE


ONE_MB = 1 << 20
Expand Down Expand Up @@ -246,7 +245,7 @@ def _multipart_upload_to_s3(self, params, bundle, size_remaining):
Key=params.key,
UploadId=upload_id,
PartNumber=part_num,
Body=six.BytesIO(data)
Body=BytesIO(data)
)
multipart_list.append({
'PartNumber': part_num,
Expand Down
4 changes: 2 additions & 2 deletions awscli/customizations/configure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import string
from botocore.vendored.six.moves import shlex_quote
from awscli.compat import shlex

NOT_SET = '<not set>'
PREDEFINED_SECTION_NAMES = ('preview', 'plugins')
Expand Down Expand Up @@ -45,5 +45,5 @@ def mask_value(current_value):
def profile_to_section(profile_name):
"""Converts a profile name to a section header to be used in the config."""
if any(c in _WHITESPACE for c in profile_name):
profile_name = shlex_quote(profile_name)
profile_name = shlex.quote(profile_name)
return 'profile %s' % profile_name
Loading

0 comments on commit 1094c86

Please sign in to comment.