Add attribute validation functions and mapping

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-02-22 11:57:03 -03:00
commit cb4fe92e9b
3 changed files with 39 additions and 39 deletions

View file

@ -2,9 +2,10 @@ import operator
import warnings
from typing import Any, ClassVar, Optional
import emoji
from cachetools import TTLCache, cachedmethod
from fastapi import HTTPException
from langflow.interface.custom.attributes import ATTR_FUNC_MAPPING
from langflow.interface.custom.code_parser import CodeParser
from langflow.interface.custom.eval import eval_custom_component_code
from langflow.utils import validate
@ -65,14 +66,6 @@ class Component:
return validate.create_function(self.code, self._function_entrypoint_name)
def getattr_return_str(self, value):
return str(value) if value else ""
def getattr_return_bool(self, value):
if isinstance(value, bool):
return value
def build_template_config(self) -> dict:
if not self.code:
return {}
@ -80,15 +73,8 @@ class Component:
cc_class = eval_custom_component_code(self.code)
component_instance = cc_class()
template_config = {}
attributes_func_mapping = {
"display_name": self.getattr_return_str,
"description": self.getattr_return_str,
"beta": self.getattr_return_str,
"documentation": self.getattr_return_str,
"icon": self.validate_icon,
}
for attribute, func in attributes_func_mapping.items():
for attribute, func in ATTR_FUNC_MAPPING.items():
if hasattr(component_instance, attribute):
value = getattr(component_instance, attribute)
if value is not None:
@ -96,17 +82,5 @@ class Component:
return template_config
def validate_icon(self, value: str, *args, **kwargs):
# we are going to use the emoji library to validate the emoji
# emojis can be defined using the :emoji_name: syntax
if not value.startswith(":") or not value.endswith(":"):
warnings.warn("Invalid emoji. Please use the :emoji_name: syntax.")
return value
emoji_value = emoji.emojize(value, variant="emoji_type")
if value == emoji_value:
warnings.warn(f"Invalid emoji. {value} is not a valid emoji.")
return value
return emoji_value
def build(self, *args: Any, **kwargs: Any) -> Any:
raise NotImplementedError