Skip to content

Commit 63a373d

Browse files
committed
added SDK new function get_editor_context
tod
1 parent ce3c84f commit 63a373d

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/superannotate/lib/app/interface/sdk_interface.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import os
77
import sys
8+
import typing
89
import warnings
910
from pathlib import Path
1011
from typing import Any
@@ -277,6 +278,55 @@ def get_team_metadata(self):
277278
response = self.controller.get_team()
278279
return TeamSerializer(response.data).serialize()
279280

281+
def get_editor_context(self, project: Union[NotEmptyStr, int], component_id: str):
282+
"""
283+
Retrieves the editor context for a given project and component ID.
284+
285+
:param project: The identifier of the project, which can be a string or an integer representing the project ID.
286+
:type project: Union[str, int]
287+
288+
:param component_id: The ID of the component for which the context is to be retrieved.
289+
:type component_id: str
290+
291+
:return: The context associated with the `webComponent`.
292+
:rtype: Any
293+
294+
:raises AppException: If the project type is not `MULTIMODAL` or no `webComponent` context is found.
295+
"""
296+
297+
def retrieve_context(
298+
component_data: List[dict], component_pk: str
299+
) -> Tuple[bool, typing.Any]:
300+
for component in component_data:
301+
if (
302+
component["type"] == "webComponent"
303+
and component["id"] == component_pk
304+
):
305+
return True, component.get("context")
306+
if component["type"] == "group" and "children" in component:
307+
found, val = retrieve_context(component["children"], component_pk)
308+
if found:
309+
return found, val
310+
return False, None
311+
312+
project = (
313+
self.controller.get_project(project)
314+
if isinstance(project, int)
315+
else self.controller.get_project_by_id(project)
316+
)
317+
if project.type != ProjectType.MULTIMODAL:
318+
raise AppException(
319+
"This function is only supported for Multimodal projects."
320+
)
321+
322+
editor_template = self.controller.projects.get_editor_template(project)
323+
components = editor_template.get("components", [])
324+
325+
_found, _context = retrieve_context(components, component_id)
326+
if not _found:
327+
raise AppException("No component context found for project.")
328+
return _context
329+
280330
def search_team_contributors(
281331
self,
282332
email: EmailStr = None,

src/superannotate/lib/infrastructure/controller.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def __init__(self, service_provider: ServiceProvider):
8888

8989

9090
class ProjectManager(BaseManager):
91+
def __init__(self, service_provider: ServiceProvider, team: TeamEntity):
92+
super().__init__(service_provider)
93+
self._team = team
94+
9195
def get_by_id(self, project_id):
9296
use_case = usecases.GetProjectByIDUseCase(
9397
project_id=project_id, service_provider=self.service_provider
@@ -239,6 +243,13 @@ def upload_priority_scores(
239243
)
240244
return use_case.execute()
241245

246+
def get_editor_template(self, project: ProjectEntity) -> dict:
247+
response = self.service_provider.projects.get_editor_template(
248+
team=self._team, project=project
249+
)
250+
response.raise_for_status()
251+
return response.data
252+
242253

243254
class AnnotationClassManager(BaseManager):
244255
@timed_lru_cache(seconds=3600)
@@ -974,7 +985,7 @@ def __init__(self, config: ConfigEntity):
974985
self._user = self.get_current_user()
975986
self._team = self.get_team().data
976987
self.annotation_classes = AnnotationClassManager(self.service_provider)
977-
self.projects = ProjectManager(self.service_provider)
988+
self.projects = ProjectManager(self.service_provider, team=self._team)
978989
self.folders = FolderManager(self.service_provider)
979990
self.items = ItemManager(self.service_provider)
980991
self.annotations = AnnotationManager(self.service_provider, config)

0 commit comments

Comments
 (0)