Techdebt: Replace string-format with f-strings (for k* dirs) (#5684)
This commit is contained in:
parent
2c8e795991
commit
efeb110d06
@ -85,7 +85,7 @@ class Shard(BaseModel):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def shard_id(self):
|
def shard_id(self):
|
||||||
return "shardId-{0}".format(str(self._shard_id).zfill(12))
|
return f"shardId-{str(self._shard_id).zfill(12)}"
|
||||||
|
|
||||||
def get_records(self, last_sequence_id, limit):
|
def get_records(self, last_sequence_id, limit):
|
||||||
last_sequence_id = int(last_sequence_id)
|
last_sequence_id = int(last_sequence_id)
|
||||||
|
@ -43,17 +43,13 @@ def compose_new_shard_iterator(
|
|||||||
elif shard_iterator_type == "AT_TIMESTAMP":
|
elif shard_iterator_type == "AT_TIMESTAMP":
|
||||||
last_sequence_id = shard.get_sequence_number_at(at_timestamp)
|
last_sequence_id = shard.get_sequence_number_at(at_timestamp)
|
||||||
else:
|
else:
|
||||||
raise InvalidArgumentError(
|
raise InvalidArgumentError(f"Invalid ShardIteratorType: {shard_iterator_type}")
|
||||||
"Invalid ShardIteratorType: {0}".format(shard_iterator_type)
|
|
||||||
)
|
|
||||||
return compose_shard_iterator(stream_name, shard, last_sequence_id)
|
return compose_shard_iterator(stream_name, shard, last_sequence_id)
|
||||||
|
|
||||||
|
|
||||||
def compose_shard_iterator(stream_name, shard, last_sequence_id):
|
def compose_shard_iterator(stream_name, shard, last_sequence_id):
|
||||||
return encode_method(
|
return encode_method(
|
||||||
"{0}:{1}:{2}".format(stream_name, shard.shard_id, last_sequence_id).encode(
|
f"{stream_name}:{shard.shard_id}:{last_sequence_id}".encode("utf-8")
|
||||||
"utf-8"
|
|
||||||
)
|
|
||||||
).decode("utf-8")
|
).decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,9 +32,7 @@ class Stream(BaseModel):
|
|||||||
|
|
||||||
def get_data_endpoint(self, api_name):
|
def get_data_endpoint(self, api_name):
|
||||||
data_endpoint_prefix = "s-" if api_name in ("PUT_MEDIA", "GET_MEDIA") else "b-"
|
data_endpoint_prefix = "s-" if api_name in ("PUT_MEDIA", "GET_MEDIA") else "b-"
|
||||||
return "https://{}{}.kinesisvideo.{}.amazonaws.com".format(
|
return f"https://{data_endpoint_prefix}{self.data_endpoint_number}.kinesisvideo.{self.region_name}.amazonaws.com"
|
||||||
data_endpoint_prefix, self.data_endpoint_number, self.region_name
|
|
||||||
)
|
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {
|
return {
|
||||||
@ -66,9 +64,7 @@ class KinesisVideoBackend(BaseBackend):
|
|||||||
):
|
):
|
||||||
streams = [_ for _ in self.streams.values() if _.stream_name == stream_name]
|
streams = [_ for _ in self.streams.values() if _.stream_name == stream_name]
|
||||||
if len(streams) > 0:
|
if len(streams) > 0:
|
||||||
raise ResourceInUseException(
|
raise ResourceInUseException(f"The stream {stream_name} already exists.")
|
||||||
"The stream {} already exists.".format(stream_name)
|
|
||||||
)
|
|
||||||
stream = Stream(
|
stream = Stream(
|
||||||
self.account_id,
|
self.account_id,
|
||||||
self.region_name,
|
self.region_name,
|
||||||
|
@ -17,8 +17,7 @@ class KinesisVideoArchivedMediaBackend(BaseBackend):
|
|||||||
"GET_DASH_STREAMING_SESSION_URL": "/dash/v1/getDASHManifest.mpd",
|
"GET_DASH_STREAMING_SESSION_URL": "/dash/v1/getDASHManifest.mpd",
|
||||||
}
|
}
|
||||||
relative_path = api_to_relative_path[api_name]
|
relative_path = api_to_relative_path[api_name]
|
||||||
url = "{}{}?SessionToken={}".format(data_endpoint, relative_path, session_token)
|
return f"{data_endpoint}{relative_path}?SessionToken={session_token}"
|
||||||
return url
|
|
||||||
|
|
||||||
def get_hls_streaming_session_url(self, stream_name, stream_arn):
|
def get_hls_streaming_session_url(self, stream_name, stream_arn):
|
||||||
# Ignore option paramters as the format of hls_url does't depends on them
|
# Ignore option paramters as the format of hls_url does't depends on them
|
||||||
|
@ -61,14 +61,12 @@ class KmsResponse(BaseResponse):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if not is_arn and not is_raw_key_id:
|
if not is_arn and not is_raw_key_id:
|
||||||
raise NotFoundException("Invalid keyId {key_id}".format(key_id=key_id))
|
raise NotFoundException(f"Invalid keyId {key_id}")
|
||||||
|
|
||||||
cmk_id = self.kms_backend.get_key_id(key_id)
|
cmk_id = self.kms_backend.get_key_id(key_id)
|
||||||
|
|
||||||
if cmk_id not in self.kms_backend.keys:
|
if cmk_id not in self.kms_backend.keys:
|
||||||
raise NotFoundException(
|
raise NotFoundException(f"Key '{self._display_arn(key_id)}' does not exist")
|
||||||
"Key '{key_id}' does not exist".format(key_id=self._display_arn(key_id))
|
|
||||||
)
|
|
||||||
|
|
||||||
def _validate_alias(self, key_id):
|
def _validate_alias(self, key_id):
|
||||||
"""Determine whether an alias exists.
|
"""Determine whether an alias exists.
|
||||||
@ -76,9 +74,7 @@ class KmsResponse(BaseResponse):
|
|||||||
- alias name
|
- alias name
|
||||||
- alias ARN
|
- alias ARN
|
||||||
"""
|
"""
|
||||||
error = NotFoundException(
|
error = NotFoundException(f"Alias {self._display_arn(key_id)} is not found.")
|
||||||
"Alias {key_id} is not found.".format(key_id=self._display_arn(key_id))
|
|
||||||
)
|
|
||||||
|
|
||||||
is_arn = key_id.startswith("arn:") and ":alias/" in key_id
|
is_arn = key_id.startswith("arn:") and ":alias/" in key_id
|
||||||
is_name = key_id.startswith("alias/")
|
is_name = key_id.startswith("alias/")
|
||||||
@ -211,16 +207,14 @@ class KmsResponse(BaseResponse):
|
|||||||
|
|
||||||
if ":" in alias_name:
|
if ":" in alias_name:
|
||||||
raise ValidationException(
|
raise ValidationException(
|
||||||
"{alias_name} contains invalid characters for an alias".format(
|
f"{alias_name} contains invalid characters for an alias"
|
||||||
alias_name=alias_name
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if not re.match(r"^[a-zA-Z0-9:/_-]+$", alias_name):
|
if not re.match(r"^[a-zA-Z0-9:/_-]+$", alias_name):
|
||||||
raise ValidationException(
|
raise ValidationException(
|
||||||
"1 validation error detected: Value '{alias_name}' at 'aliasName' "
|
f"1 validation error detected: Value '{alias_name}' at 'aliasName' "
|
||||||
"failed to satisfy constraint: Member must satisfy regular "
|
"failed to satisfy constraint: Member must satisfy regular "
|
||||||
"expression pattern: ^[a-zA-Z0-9:/_-]+$".format(alias_name=alias_name)
|
"expression pattern: ^[a-zA-Z0-9:/_-]+$"
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.kms_backend.alias_exists(target_key_id):
|
if self.kms_backend.alias_exists(target_key_id):
|
||||||
@ -232,12 +226,7 @@ class KmsResponse(BaseResponse):
|
|||||||
|
|
||||||
if self.kms_backend.alias_exists(alias_name):
|
if self.kms_backend.alias_exists(alias_name):
|
||||||
raise AlreadyExistsException(
|
raise AlreadyExistsException(
|
||||||
"An alias with the name arn:aws:kms:{region}:{account_id}:{alias_name} "
|
f"An alias with the name arn:aws:kms:{self.region}:{self.current_account}:{alias_name} already exists"
|
||||||
"already exists".format(
|
|
||||||
region=self.region,
|
|
||||||
account_id=self.current_account,
|
|
||||||
alias_name=alias_name,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self._validate_cmk_id(target_key_id)
|
self._validate_cmk_id(target_key_id)
|
||||||
|
@ -22,9 +22,7 @@ IV_LEN = 12
|
|||||||
TAG_LEN = 16
|
TAG_LEN = 16
|
||||||
HEADER_LEN = KEY_ID_LEN + IV_LEN + TAG_LEN
|
HEADER_LEN = KEY_ID_LEN + IV_LEN + TAG_LEN
|
||||||
# NOTE: This is just a simple binary format. It is not what KMS actually does.
|
# NOTE: This is just a simple binary format. It is not what KMS actually does.
|
||||||
CIPHERTEXT_HEADER_FORMAT = ">{key_id_len}s{iv_len}s{tag_len}s".format(
|
CIPHERTEXT_HEADER_FORMAT = f">{KEY_ID_LEN}s{IV_LEN}s{TAG_LEN}s"
|
||||||
key_id_len=KEY_ID_LEN, iv_len=IV_LEN, tag_len=TAG_LEN
|
|
||||||
)
|
|
||||||
Ciphertext = namedtuple("Ciphertext", ("key_id", "iv", "ciphertext", "tag"))
|
Ciphertext = namedtuple("Ciphertext", ("key_id", "iv", "ciphertext", "tag"))
|
||||||
|
|
||||||
RESERVED_ALIASES = [
|
RESERVED_ALIASES = [
|
||||||
@ -134,11 +132,8 @@ def encrypt(master_keys, key_id, plaintext, encryption_context):
|
|||||||
key = master_keys[key_id]
|
key = master_keys[key_id]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
is_alias = key_id.startswith("alias/") or ":alias/" in key_id
|
is_alias = key_id.startswith("alias/") or ":alias/" in key_id
|
||||||
raise NotFoundException(
|
id_type = "Alias" if is_alias else "keyId"
|
||||||
"{id_type} {key_id} is not found.".format(
|
raise NotFoundException(f"{id_type} {key_id} is not found.")
|
||||||
id_type="Alias" if is_alias else "keyId", key_id=key_id
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
if plaintext == b"":
|
if plaintext == b"":
|
||||||
raise ValidationException(
|
raise ValidationException(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user