Skip to content

Commit

Permalink
Black Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
xDaile committed Jan 18, 2024
1 parent 7123e63 commit 9675d0a
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 90 deletions.
61 changes: 40 additions & 21 deletions iib/common/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@


class PydanticModel(BaseModel):

@classmethod
def _get_all_keys_to_check_in_db(cls):
"""Class that returns request specific keys to check."""
raise NotImplementedError("Not implemented")

def get_keys_to_check_in_db(self):
"""Filter keys, which need to be checked in db. Return only a keys that are set to values."""
return [
k for k in self._get_all_keys_to_check_in_db() if getattr(self, k, None)
]
return [k for k in self._get_all_keys_to_check_in_db() if getattr(self, k, None)]


class AddPydanticModel(PydanticModel):
Expand All @@ -66,14 +64,16 @@ class AddPydanticModel(PydanticModel):
AfterValidator(images_format_check),
]
cnr_token: Optional[SecretStr] = None # deprecated
check_related_images: Optional[bool] = None # old request without this parameter will not have False but None
# TODO remove this comment -> old request without this parameter will not have False but None
check_related_images: Optional[bool] = None
deprecation_list: Annotated[
Optional[List[str]],
AfterValidator(get_unique_deprecation_list_items),
AfterValidator(images_format_check),
] = [] # deprecated
distribution_scope: Annotated[
Optional[DISTRIBUTION_SCOPE_LITERAL], BeforeValidator(distribution_scope_lower),
Optional[DISTRIBUTION_SCOPE_LITERAL],
BeforeValidator(distribution_scope_lower),
] = None
force_backport: Optional[bool] = False # deprecated
from_index: Annotated[str, AfterValidator(image_format_check)]
Expand Down Expand Up @@ -102,7 +102,7 @@ def verify_graph_update_mode_with_index_image(self) -> 'AddPydanticModel':
@model_validator(mode='after')
def from_index_needed_if_no_bundles(self) -> 'AddPydanticModel':
"""
Check if no bundles and `from_index is specified
Check if no bundles and `from_index is specified.
if no bundles and no from index then an empty index will be created which is a no-op
"""
Expand All @@ -113,7 +113,7 @@ def from_index_needed_if_no_bundles(self) -> 'AddPydanticModel':
# TODO remove this comment -> Validator from RequestADD class
@model_validator(mode='after')
def bundles_needed_with_check_related_images(self) -> 'AddPydanticModel':
"""Verify that `check_related_images` is specified when bundles are specified"""
"""Verify that `check_related_images` is specified when bundles are specified."""
if self.check_related_images and not self.bundles:
raise ValidationError(
'"check_related_images" must be specified only when bundles are specified'
Expand All @@ -123,7 +123,6 @@ def bundles_needed_with_check_related_images(self) -> 'AddPydanticModel':
def get_json_for_request(self):
"""Return json with the parameters we store in the db."""
return self.model_dump(
# include=["deprecation_list"],
exclude=[
"add_arches",
"build_tags",
Expand All @@ -135,7 +134,6 @@ def get_json_for_request(self):
exclude_none=True,
)


def _get_all_keys_to_check_in_db(self):
return ["binary_image", "bundles", "deprecation_list", "from_index"]

Expand All @@ -150,7 +148,8 @@ class RmPydanticModel(PydanticModel):
] = None
build_tags: Optional[List[str]] = []
distribution_scope: Annotated[
Optional[DISTRIBUTION_SCOPE_LITERAL], BeforeValidator(distribution_scope_lower),
Optional[DISTRIBUTION_SCOPE_LITERAL],
BeforeValidator(distribution_scope_lower),
] = None
from_index: Annotated[str, AfterValidator(image_format_check)]
operators: Annotated[List[str], AfterValidator(length_validator)]
Expand All @@ -167,7 +166,12 @@ def verify_overwrite_from_index_token(self) -> 'RmPydanticModel':
def get_json_for_request(self):
"""Return json with the parameters we store in the db."""
return self.model_dump(
exclude=["add_arches", "build_tags", "overwrite_from_index", "overwrite_from_index_token"],
exclude=[
"add_arches",
"build_tags",
"overwrite_from_index",
"overwrite_from_index_token",
],
exclude_none=True,
)

Expand Down Expand Up @@ -228,10 +232,11 @@ class MergeIndexImagePydanticModel(PydanticModel):
AfterValidator(images_format_check),
] = []
distribution_scope: Annotated[
Optional[DISTRIBUTION_SCOPE_LITERAL], BeforeValidator(distribution_scope_lower),
Optional[DISTRIBUTION_SCOPE_LITERAL],
BeforeValidator(distribution_scope_lower),
] = None
graph_update_mode: Optional[GRAPH_MODE_LITERAL] = None
overwrite_target_index: Optional[bool] = False # Why do we need this bool? Isn't the token enough?
overwrite_target_index: Optional[bool] = False
overwrite_target_index_token: Optional[SecretStr] = None
source_from_index: Annotated[str, AfterValidator(image_format_check)]
target_index: Annotated[Optional[str], AfterValidator(image_format_check)] = None
Expand Down Expand Up @@ -260,7 +265,13 @@ def get_json_for_request(self):
)

def _get_all_keys_to_check_in_db(self):
return ["binary_image", "deprecation_list", "source_from_index", "target_index", "target_index"]
return [
"binary_image",
"deprecation_list",
"source_from_index",
"target_index",
"target_index",
]


class CreateEmptyIndexPydanticModel(PydanticModel):
Expand All @@ -276,8 +287,10 @@ class CreateEmptyIndexPydanticModel(PydanticModel):
AfterValidator(image_format_check),
AfterValidator(length_validator),
]
labels: Optional[Dict[str, str]] = None # old request without this parameter will not have empty labels
output_fbc: Optional[bool] = None # old request without this parameter will not have empty output_fbc
# TODO (remove comment) old request without this parameter will not have empty labels
labels: Optional[Dict[str, str]] = None
# TODO (remove comment) old request without this parameter will not have empty output_fbc
output_fbc: Optional[bool] = None

def get_json_for_request(self):
"""Return json with the parameters we store in the db."""
Expand Down Expand Up @@ -305,7 +318,6 @@ def get_json_for_request(self):
exclude_none=True,
)


def _get_all_keys_to_check_in_db(self):
return ["parent_bundle_image"]

Expand All @@ -317,15 +329,17 @@ class FbcOperationsPydanticModel(PydanticModel):
AfterValidator(image_format_check),
AfterValidator(binary_image_check),
] = None
# TODO (remove comment) old request without this parameter will not have empty list but None
bundles: Annotated[
Optional[List[str]],
AfterValidator(length_validator),
AfterValidator(get_unique_bundles),
AfterValidator(images_format_check),
] = None # old request without this parameter will not have empty list but None
] = None
build_tags: Optional[List[str]] = []
distribution_scope: Annotated[
Optional[DISTRIBUTION_SCOPE_LITERAL], BeforeValidator(distribution_scope_lower),
Optional[DISTRIBUTION_SCOPE_LITERAL],
BeforeValidator(distribution_scope_lower),
] = None
fbc_fragment: Annotated[
str,
Expand All @@ -349,7 +363,12 @@ def verify_overwrite_from_index_token(self) -> 'FbcOperationsPydanticModel':
def get_json_for_request(self):
"""Return json with the parameters we store in the db."""
return self.model_dump(
exclude=["add_arches", "build_tags", "overwrite_from_index", "overwrite_from_index_token"],
exclude=[
"add_arches",
"build_tags",
"overwrite_from_index",
"overwrite_from_index_token",
],
exclude_none=True,
)

Expand Down
27 changes: 13 additions & 14 deletions iib/common/pydantic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
# TODO add regex in future to not allow following values ":s", "s:", ":"?
def image_format_check(image_name: str) -> str:
if '@' not in image_name and ':' not in image_name:
raise ValidationError(
f'Image {image_name} should have a tag or a digest specified.'
)
raise ValidationError(f'Image {image_name} should have a tag or a digest specified.')
return image_name


Expand Down Expand Up @@ -48,7 +46,10 @@ def get_unique_deprecation_list_items(deprecation_list: Optional[List[str]]) ->
return list(set(deprecation_list))


def validate_graph_mode_index_image(graph_update_mode: str, index_image: str) -> 'MergeIndexImageRequestPayload':
def validate_graph_mode_index_image(
graph_update_mode: str,
index_image: str,
) -> 'MergeIndexImageRequestPayload':
"""
Validate graph mode and check if index image is allowed to use different graph mode.
Expand All @@ -57,9 +58,9 @@ def validate_graph_mode_index_image(graph_update_mode: str, index_image: str) ->
:raises: ValidationError when incorrect graph_update_mode is set
:raises: Forbidden when graph_mode can't be used for given index image
"""

if graph_update_mode:
allowed_from_indexes: List[str] = ["REMOVE_#:r"] # current_app.config['IIB_GRAPH_MODE_INDEX_ALLOW_LIST']
# TODO remove this comment, replace value with current_app.config['IIB_GRAPH_MODE_INDEX_ALLOW_LIST']
allowed_from_indexes: List[str] = ["REMOVE_#:r"]
if index_image not in allowed_from_indexes:
raise Forbidden(
'"graph_update_mode" can only be used on the'
Expand All @@ -70,9 +71,7 @@ def validate_graph_mode_index_image(graph_update_mode: str, index_image: str) ->

# RequestIndexImageMixin
def from_index_add_arches(model: 'AddRequestPydanticModel') -> 'AddRequestPydanticModel':
"""
Check if both `from_index` and `add_arches` are not specified
"""
"""Check if both `from_index` and `add_arches` are not specified."""
if not model.from_index and not model.add_arches:
raise ValidationError('One of "from_index" or "add_arches" must be specified')
return model
Expand All @@ -81,7 +80,7 @@ def from_index_add_arches(model: 'AddRequestPydanticModel') -> 'AddRequestPydant
# RequestIndexImageMixin
def binary_image_check(binary_image: str) -> str:
"""
# Validate binary_image is correctly provided
# Validate binary_image is correctly provided.
"""
if not binary_image and not current_app.config['IIB_BINARY_IMAGE_CONFIG']:
raise ValidationError('The "binary_image" value must be a non-empty string')
Expand All @@ -95,9 +94,7 @@ def validate_overwrite_params(
disable_auth_check: Optional[bool] = False,
) -> None:
"""
Check if both `overwrite_index_image` and `overwrite_index_image_token` are specified
Check if both `overwrite_index_image` and `overwrite_index_image_token` are specified.
"""
if overwrite_index_image_token and not overwrite_index_image:
raise ValidationError(
Expand All @@ -122,5 +119,7 @@ def distribution_scope_lower(distribution_scope: str) -> str:

def length_validator(model_property: Any) -> Any:
if len(model_property) == 0:
raise ValidationError(f"The {type(model_property)} {model_property} should have at least 1 item.")
raise ValidationError(
f"The {type(model_property)} {model_property} should have at least 1 item."
)
return model_property
Loading

0 comments on commit 9675d0a

Please sign in to comment.