TechDebt - remove Py2 references (#5488)

This commit is contained in:
Bert Blommers 2022-09-20 14:08:01 +00:00 committed by GitHub
parent d8a084b662
commit 72db853d08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 13 additions and 41 deletions

View File

@ -44,8 +44,7 @@ RnGfN8j8KLDVmWyTYMk8V+6j0LI4+4zFh2upqGMQHL3VFVFWBek6vCDWhB/b
def datetime_to_epoch(date):
# As only Py3 has datetime.timestamp()
return int((date - datetime.datetime(1970, 1, 1)).total_seconds())
return date.timestamp()
class AWSValidationException(AWSError):
@ -295,14 +294,11 @@ class CertBundle(BaseModel):
self._chain = []
for cert_armored in self.chain.split(b"-\n-"):
# Would leave encoded but Py2 does not have raw binary strings
cert_armored = cert_armored.decode()
# Fix missing -'s on split
cert_armored = re.sub(r"^----B", "-----B", cert_armored)
cert_armored = re.sub(r"E----$", "E-----", cert_armored)
cert_armored = re.sub(b"^----B", b"-----B", cert_armored)
cert_armored = re.sub(b"E----$", b"E-----", cert_armored)
cert = cryptography.x509.load_pem_x509_certificate(
cert_armored.encode(), default_backend()
cert_armored, default_backend()
)
self._chain.append(cert)

View File

@ -159,8 +159,6 @@ class _DockerDataVolumeContext:
def _zipfile_content(zipfile):
# more hackery to handle unicode/bytes/str in python3 and python2 -
# argh!
try:
to_unzip_code = base64.b64decode(bytes(zipfile, "utf-8"))
except Exception:

View File

@ -37,9 +37,6 @@ class Dimension(object):
)
return False
def __ne__(self, item): # Only needed on Py2; Py3 defines it implicitly
return self != item
def __lt__(self, other):
return self.name < other.name and self.value < other.name

View File

@ -51,8 +51,6 @@ class InvalidResourceTypeException(JsonRESTError):
"'configurationRecorder.recordingGroup.resourceTypes' failed to satisfy constraint: "
f"Member must satisfy constraint: [Member must satisfy enum value set: {good_list}]"
)
# For PY2:
message = str(message)
super().__init__("ValidationException", message)

View File

@ -248,11 +248,7 @@ def amz_crc32(f):
if "status" in headers:
headers["status"] = str(headers["status"])
try:
# Doesnt work on python2 for some odd unicode strings
gen_amz_crc32(body, headers)
except Exception:
pass
gen_amz_crc32(body, headers)
return status, headers, body
@ -282,7 +278,7 @@ def amzn_request_id(f):
# Update request ID in XML
try:
body = re.sub(r"(?<=<RequestId>).*(?=<\/RequestId>)", request_id, body)
except Exception: # Will just ignore if it cant work on bytes (which are str's on python2)
except Exception: # Will just ignore if it cant work
pass
return status, headers, body

View File

@ -7,7 +7,6 @@ import uuid
from datetime import datetime
import json
import re
import time
from cryptography import x509
from cryptography.hazmat.backends import default_backend
@ -21,6 +20,7 @@ from moto.core.utils import (
iso_8601_datetime_without_milliseconds,
iso_8601_datetime_with_milliseconds,
BackendDict,
unix_time,
)
from moto.iam.policy_validation import (
IAMPolicyDocumentValidator,
@ -329,9 +329,7 @@ class ManagedPolicy(Policy, CloudFormationModel):
"version": "1.3",
"configurationItemCaptureTime": str(self.create_date),
"configurationItemStatus": "OK",
"configurationStateId": str(
int(time.mktime(self.create_date.timetuple()))
), # PY2 and 3 compatible
"configurationStateId": str(int(unix_time())),
"arn": "arn:aws:iam::{}:policy/{}".format(self.account_id, self.name),
"resourceType": "AWS::IAM::Policy",
"resourceId": self.id,
@ -720,9 +718,7 @@ class Role(CloudFormationModel):
"version": "1.3",
"configurationItemCaptureTime": str(self.create_date),
"configurationItemStatus": "ResourceDiscovered",
"configurationStateId": str(
int(time.mktime(self.create_date.timetuple()))
), # PY2 and 3 compatible
"configurationStateId": str(int(unix_time())),
"arn": f"arn:aws:iam::{self.account_id}:role/{self.name}",
"resourceType": "AWS::IAM::Role",
"resourceId": self.name,

View File

@ -11,7 +11,6 @@ import tempfile
import threading
import pytz
import sys
import time
import uuid
import urllib.parse
@ -23,6 +22,7 @@ from moto.core import CloudWatchMetricProvider
from moto.core.utils import (
iso_8601_datetime_without_milliseconds_s3,
rfc_1123_datetime,
unix_time,
unix_time_millis,
BackendDict,
)
@ -1309,9 +1309,7 @@ class FakeBucket(CloudFormationModel):
"version": "1.3",
"configurationItemCaptureTime": str(self.creation_date),
"configurationItemStatus": "ResourceDiscovered",
"configurationStateId": str(
int(time.mktime(self.creation_date.timetuple()))
), # PY2 and 3 compatible
"configurationStateId": str(int(unix_time())),
"configurationItemMD5Hash": "",
"arn": self.arn,
"resourceType": "AWS::S3::Bucket",

View File

@ -1,11 +1,11 @@
import datetime
import json
import time
from boto3 import Session
from moto.core.exceptions import InvalidNextTokenException
from moto.core.common_models import ConfigQueryModel
from moto.core.utils import unix_time
from moto.s3control import s3control_backends
@ -135,9 +135,7 @@ class S3AccountPublicAccessBlockConfigQuery(ConfigQueryModel):
"accountId": account_id,
"configurationItemCaptureTime": str(creation_time),
"configurationItemStatus": "OK",
"configurationStateId": str(
int(time.mktime(creation_time.timetuple()))
), # PY2 and 3 compatible
"configurationStateId": str(int(unix_time())),
"resourceType": "AWS::S3::AccountPublicAccessBlock",
"resourceId": account_id,
"awsRegion": pab_region,

View File

@ -482,11 +482,6 @@ class Document(BaseModel):
if document_format == "JSON":
try:
content_json = json.loads(content)
except ValueError:
# Python2
raise InvalidDocumentContent(
"The content for the document is not valid."
)
except json.decoder.JSONDecodeError:
raise InvalidDocumentContent(
"The content for the document is not valid."