A generic rendering fallback that upgrades known mimetypes into rich components. If the mimetype is recognized, this function dynamically calls the mapped Preswald component. Otherwise, it returns a minimal generic component that just displays the raw value.
(content, mimetype: str, component_id: str | None = None, **kwargs)
| 267 | |
| 268 | @with_render_tracking("generic") |
| 269 | def generic(content, mimetype: str, component_id: str | None = None, **kwargs) -> ComponentReturn: |
| 270 | """ |
| 271 | A generic rendering fallback that upgrades known mimetypes into rich components. |
| 272 | |
| 273 | If the mimetype is recognized, this function dynamically calls the mapped Preswald component. |
| 274 | Otherwise, it returns a minimal generic component that just displays the raw value. |
| 275 | """ |
| 276 | from preswald.interfaces.render.registry import get_component_type_for_mimetype |
| 277 | from preswald import interfaces |
| 278 | |
| 279 | logger.debug(f"[generic] Rendering {mimetype=} with id={component_id}") |
| 280 | |
| 281 | component_type = get_component_type_for_mimetype(mimetype) |
| 282 | |
| 283 | if component_type: |
| 284 | component_fn = getattr(interfaces.components, component_type, None) |
| 285 | if callable(component_fn): |
| 286 | return component_fn(content, component_id=component_id, mimetype=mimetype, **kwargs) |
| 287 | else: |
| 288 | logger.warning(f"[generic] No such component function: {component_type=} for {mimetype=}") |
| 289 | |
| 290 | # Fallback to generic if no match or unsupported |
| 291 | fallback_component = { |
| 292 | "type": "generic", |
| 293 | "id": component_id, |
| 294 | "mimetype": mimetype, |
| 295 | "value": content, |
| 296 | **kwargs, |
| 297 | } |
| 298 | |
| 299 | return ComponentReturn(content, fallback_component) |
| 300 | |
| 301 | |
| 302 | @with_render_tracking("image") |
no test coverage detected