Techdebt: Replace string-format with f-strings (for o* dirs) (#5689)

This commit is contained in:
Bert Blommers 2022-11-20 11:59:02 -01:00 committed by GitHub
parent 8ebb8a7e17
commit a092763ce5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 31 deletions

View File

@ -81,7 +81,7 @@ class OpsworkInstance(BaseModel):
self.infrastructure_class = "ec2 (fixed)"
self.platform = "linux (fixed)"
self.id = "{0}".format(random.uuid4())
self.id = str(random.uuid4())
self.created_at = datetime.datetime.utcnow()
def start(self):
@ -271,7 +271,7 @@ class Layer(BaseModel):
self.install_updates_on_boot = install_updates_on_boot
self.use_ebs_optimized_instances = use_ebs_optimized_instances
self.id = "{0}".format(random.uuid4())
self.id = str(random.uuid4())
self.created_at = datetime.datetime.utcnow()
def __eq__(self, other):
@ -367,7 +367,7 @@ class Stack(BaseModel):
self.default_root_device_type = default_root_device_type
self.agent_version = agent_version
self.id = "{0}".format(random.uuid4())
self.id = str(random.uuid4())
self.layers = []
self.apps = []
self.account_number = account_id
@ -378,16 +378,11 @@ class Stack(BaseModel):
def generate_hostname(self):
# this doesn't match amazon's implementation
return "{theme}-{rand}-(moto)".format(
theme=self.hostname_theme,
rand=[random.choice("abcdefghijhk") for _ in range(4)],
)
return f"{self.hostname_theme}-{[random.choice('abcdefghijhk') for _ in range(4)]}-(moto)"
@property
def arn(self):
return "arn:aws:opsworks:{region}:{account_number}:stack/{id}".format(
region=self.region, account_number=self.account_number, id=self.id
)
return f"arn:aws:opsworks:{self.region}:{self.account_number}:stack/{self.id}"
def to_dict(self):
response = {
@ -468,7 +463,7 @@ class App(BaseModel):
if environment is None:
self.environment = {}
self.id = "{0}".format(random.uuid4())
self.id = str(random.uuid4())
self.created_at = datetime.datetime.utcnow()
def __eq__(self, other):
@ -516,12 +511,11 @@ class OpsWorksBackend(BaseBackend):
raise ResourceNotFoundException(stackid)
if name in [layer.name for layer in self.stacks[stackid].layers]:
raise ValidationException(
'There is already a layer named "{0}" ' "for this stack".format(name)
f'There is already a layer named "{name}" for this stack'
)
if shortname in [layer.shortname for layer in self.stacks[stackid].layers]:
raise ValidationException(
'There is already a layer with shortname "{0}" '
"for this stack".format(shortname)
f'There is already a layer with shortname "{shortname}" for this stack'
)
layer = Layer(**kwargs)
self.layers[layer.id] = layer
@ -535,7 +529,7 @@ class OpsWorksBackend(BaseBackend):
raise ResourceNotFoundException(stackid)
if name in [a.name for a in self.stacks[stackid].apps]:
raise ValidationException(
'There is already an app named "{0}" ' "for this stack".format(name)
f'There is already an app named "{name}" for this stack'
)
app = App(**kwargs)
self.apps[app.id] = app
@ -547,9 +541,7 @@ class OpsWorksBackend(BaseBackend):
layer_ids = kwargs["layer_ids"]
if stack_id not in self.stacks:
raise ResourceNotFoundException(
"Unable to find stack with ID {0}".format(stack_id)
)
raise ResourceNotFoundException(f"Unable to find stack with ID {stack_id}")
unknown_layers = set(layer_ids) - set(self.layers.keys())
if unknown_layers:
@ -601,7 +593,7 @@ class OpsWorksBackend(BaseBackend):
if stack_id is not None:
if stack_id not in self.stacks:
raise ResourceNotFoundException(
"Unable to find stack with ID {0}".format(stack_id)
f"Unable to find stack with ID {stack_id}"
)
return [layer.to_dict() for layer in self.stacks[stack_id].layers]
@ -618,7 +610,7 @@ class OpsWorksBackend(BaseBackend):
if stack_id is not None:
if stack_id not in self.stacks:
raise ResourceNotFoundException(
"Unable to find stack with ID {0}".format(stack_id)
f"Unable to find stack with ID {stack_id}"
)
return [app.to_dict() for app in self.stacks[stack_id].apps]
@ -643,7 +635,7 @@ class OpsWorksBackend(BaseBackend):
if layer_id:
if layer_id not in self.layers:
raise ResourceNotFoundException(
"Unable to find layer with ID {0}".format(layer_id)
f"Unable to find layer with ID {layer_id}"
)
instances = [
i.to_dict() for i in self.instances.values() if layer_id in i.layer_ids
@ -653,7 +645,7 @@ class OpsWorksBackend(BaseBackend):
if stack_id:
if stack_id not in self.stacks:
raise ResourceNotFoundException(
"Unable to find stack with ID {0}".format(stack_id)
f"Unable to find stack with ID {stack_id}"
)
instances = [
i.to_dict() for i in self.instances.values() if stack_id == i.stack_id
@ -663,7 +655,7 @@ class OpsWorksBackend(BaseBackend):
def start_instance(self, instance_id):
if instance_id not in self.instances:
raise ResourceNotFoundException(
"Unable to find instance with ID {0}".format(instance_id)
f"Unable to find instance with ID {instance_id}"
)
self.instances[instance_id].start()

View File

@ -208,7 +208,7 @@ class FakePolicy(BaseModel):
self._arn_format = utils.SCP_ARN_FORMAT
else:
raise NotImplementedError(
"The {0} policy type has not been implemented".format(self.type)
f"The {self.type} policy type has not been implemented"
)
@property
@ -683,7 +683,7 @@ class OrganizationsBackend(BaseBackend):
if _filter not in ["AISERVICES_OPT_OUT_POLICY", "SERVICE_CONTROL_POLICY"]:
raise NotImplementedError(
"The {0} policy type has not been implemented".format(_filter)
f"The {_filter} policy type has not been implemented"
)
return dict(

View File

@ -24,12 +24,12 @@ CREATE_ACCOUNT_STATUS_ID_SIZE = 8
POLICY_ID_SIZE = 8
EMAIL_REGEX = "^.+@[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}|[0-9]{1,3}$"
ORG_ID_REGEX = r"o-[a-z0-9]{%s}" % ORG_ID_SIZE
ROOT_ID_REGEX = r"r-[a-z0-9]{%s}" % ROOT_ID_SIZE
OU_ID_REGEX = r"ou-[a-z0-9]{%s}-[a-z0-9]{%s}" % (ROOT_ID_SIZE, OU_ID_SUFFIX_SIZE)
ACCOUNT_ID_REGEX = r"[0-9]{%s}" % ACCOUNT_ID_SIZE
CREATE_ACCOUNT_STATUS_ID_REGEX = r"car-[a-z0-9]{%s}" % CREATE_ACCOUNT_STATUS_ID_SIZE
POLICY_ID_REGEX = r"%s|p-[a-z0-9]{%s}" % (DEFAULT_POLICY_ID, POLICY_ID_SIZE)
ORG_ID_REGEX = rf"o-[a-z0-9]{{{ORG_ID_SIZE}}}"
ROOT_ID_REGEX = rf"r-[a-z0-9]{{{ROOT_ID_SIZE}}}"
OU_ID_REGEX = rf"ou-[a-z0-9]{{{ROOT_ID_SIZE}}}-[a-z0-9]{{{OU_ID_SUFFIX_SIZE}}}"
ACCOUNT_ID_REGEX = rf"[0-9]{{{ACCOUNT_ID_SIZE}}}"
CREATE_ACCOUNT_STATUS_ID_REGEX = rf"car-[a-z0-9]{{{CREATE_ACCOUNT_STATUS_ID_SIZE}}}"
POLICY_ID_REGEX = rf"{DEFAULT_POLICY_ID}|p-[a-z0-9]{{{POLICY_ID_SIZE}}}"
PAGINATION_MODEL = {
"list_accounts": {