From c6a12875270e8229981f282c15d369b7bd8a8edc Mon Sep 17 00:00:00 2001 From: KonstantAnxiety <58992437+KonstantAnxiety@users.noreply.github.com> Date: Fri, 14 Jun 2024 18:05:50 +0300 Subject: [PATCH] BI-5485 Fix icon config loading (#489) * BI-5485 Fix icon config loading * mypy * mypy * fix icon type --- app/app_configs/api.yaml | 3 ++ .../dl_api_lib/connector_availability/base.py | 35 +++++++++++++++---- .../dl_configs/connector_availability.py | 8 +---- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/app/app_configs/api.yaml b/app/app_configs/api.yaml index e94421933..d04156afd 100644 --- a/app/app_configs/api.yaml +++ b/app/app_configs/api.yaml @@ -7,6 +7,9 @@ CONNECTORS: YDB: HAS_AUTH: false CONNECTOR_AVAILABILITY: + icon_src: + icon_type: data + data: "blank" sections: [] uncategorized: - availability: free diff --git a/lib/dl_api_lib/dl_api_lib/connector_availability/base.py b/lib/dl_api_lib/dl_api_lib/connector_availability/base.py index aef242bce..0004192bd 100644 --- a/lib/dl_api_lib/dl_api_lib/connector_availability/base.py +++ b/lib/dl_api_lib/dl_api_lib/connector_availability/base.py @@ -148,6 +148,11 @@ def as_dict(self, conn_type: str) -> dict[str, Any]: conn_type=conn_type, ) + @classmethod + @abc.abstractmethod + def from_settings(cls, settings: ObjectLikeConfig | ConnectorIconSrc) -> ConnectorIconSrcConfig: + raise NotImplementedError + @attr.s(kw_only=True) class ConnectorIconSrcConfigData(ConnectorIconSrcConfig): @@ -160,6 +165,13 @@ def as_dict(self, conn_type: str) -> dict[str, Any]: data=data, ) + @classmethod + def from_settings(cls, settings: ObjectLikeConfig | ConnectorIconSrc) -> ConnectorIconSrcConfigData: + return cls( + icon_type=ConnectorIconSrcType.data, + data=settings.data, + ) + @attr.s(kw_only=True) class ConnectorIconSrcConfigUrl(ConnectorIconSrcConfig): @@ -175,16 +187,27 @@ def as_dict(self, conn_type: str) -> dict[str, Any]: url=url, ) + @classmethod + def from_settings(cls, settings: ObjectLikeConfig | ConnectorIconSrc) -> ConnectorIconSrcConfigUrl: + assert isinstance( + settings.url_prefix, str + ), f'Expected a string value in URL config, got "{type(settings.url_prefix)}"' + return cls( + icon_type=ConnectorIconSrcType.url, + url_prefix=settings.url_prefix, + ) + -def connector_icon_src_config_factory(icon_data: ConnectorIconSrc) -> ConnectorIconSrcConfig: - icon_data_dict = icon_data.as_dict() +def connector_icon_src_config_factory(icon_data: ConnectorIconSrc | ObjectLikeConfig) -> ConnectorIconSrcConfig: + icon_type = icon_data.icon_type + icon_type_str = icon_type.value if isinstance(icon_type, ConnectorIconSrcType) else icon_type - cfg_class: dict[ConnectorIconSrcType, Type[ConnectorIconSrcConfig]] = { - ConnectorIconSrcType.data: ConnectorIconSrcConfigData, - ConnectorIconSrcType.url: ConnectorIconSrcConfigUrl, + cfg_class: dict[str, Type[ConnectorIconSrcConfig]] = { + ConnectorIconSrcType.data.value: ConnectorIconSrcConfigData, + ConnectorIconSrcType.url.value: ConnectorIconSrcConfigUrl, } - return cfg_class[icon_data.icon_type](**icon_data_dict) + return cfg_class[icon_type_str].from_settings(icon_data) @attr.s(kw_only=True) diff --git a/lib/dl_configs/dl_configs/connector_availability.py b/lib/dl_configs/dl_configs/connector_availability.py index dfadbb541..d449c9b4a 100644 --- a/lib/dl_configs/dl_configs/connector_availability.py +++ b/lib/dl_configs/dl_configs/connector_availability.py @@ -33,13 +33,7 @@ class ConnectorIconSrcType(DynamicEnum): class ConnectorIconSrc: icon_type: ConnectorIconSrcType = attr.ib(default=ConnectorIconSrcType.data) url_prefix: Optional[str] = attr.ib(default=None) - - def as_dict(self) -> dict[str, Any]: - conn_icon_settings_dict: dict[str, Any] = dict() - if self.icon_type == ConnectorIconSrcType.url: - conn_icon_settings_dict["url_prefix"] = self.url_prefix - conn_icon_settings_dict["icon_type"] = self.icon_type - return conn_icon_settings_dict + data: Optional[str] = attr.ib(default=None) @attr.s(kw_only=True)