Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Aug 28, 2024
1 parent bb54654 commit 90f27b7
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions zyte_spider_templates/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,21 @@ class MaxRequestsParam(BaseModel):
)


INPUT_FIELDS = ("url", "urls", "urls_file")
INPUT_GROUP_FIELDS = ("url", "urls", "urls_file")
INPUT_GROUP = {
"id": "inputs",
"title": "Inputs",
"description": ("Input data that determines the start URLs of the crawl."),
"description": "Input data that determines the start URLs of the crawl.",
"widget": "exclusive",
}


def single_input_validator(model):
input_fields = set(field for field in INPUT_FIELDS if getattr(model, field, None))
def validate_input_group(model):
input_fields = set(
field for field in INPUT_GROUP_FIELDS if getattr(model, field, None)
)
if not input_fields:
input_field_list = ", ".join(INPUT_FIELDS)
input_field_list = ", ".join(INPUT_GROUP_FIELDS)
raise ValueError(
f"No input parameter defined. Please, define one of: "
f"{input_field_list}."
Expand Down Expand Up @@ -128,8 +130,8 @@ class UrlsFileParam(BaseModel):
)

@model_validator(mode="after")
def single_input(self):
return single_input_validator(self)
def input_group(self):
return validate_input_group(self)


class UrlParam(BaseModel):
Expand All @@ -146,8 +148,8 @@ class UrlParam(BaseModel):
)

@model_validator(mode="after")
def single_input(self):
return single_input_validator(self)
def input_group(self):
return validate_input_group(self)


class UrlsParam(BaseModel):
Expand All @@ -167,8 +169,8 @@ class UrlsParam(BaseModel):
)

@model_validator(mode="after")
def single_input(self):
return single_input_validator(self)
def input_group(self):
return validate_input_group(self)

@field_validator("urls", mode="before")
@classmethod
Expand Down

0 comments on commit 90f27b7

Please sign in to comment.