Skip to content

Commit 2bcd4f7

Browse files
kyteinskybigcat88
andauthoredAug 11, 2024··
feat: Add enum and default value support in task processing (#284)
All related PRs: #284 nextcloud/app_api#359 nextcloud/translate2#11 --------- Signed-off-by: Anupam Kumar <kyteinsky@gmail.com> Signed-off-by: Alexander Piskun <bigcat88@icloud.com> Co-authored-by: Alexander Piskun <13381981+bigcat88@users.noreply.github.com>
1 parent 3d26987 commit 2bcd4f7

File tree

4 files changed

+116
-32
lines changed

4 files changed

+116
-32
lines changed
 

‎CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## [0.16.0 - 2024-08-xx]
5+
## [0.16.0 - 2024-08-12]
6+
7+
### Changed
8+
9+
- NextcloudApp: rework of TaskProcessing provider API. #284
610

711
### Fixed
812

‎docs/reference/ExApp.rst

+15
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ UI methods should be accessed with the help of :class:`~nc_py_api.nextcloud.Next
8787
.. autoclass:: nc_py_api.ex_app.providers.translations._TranslationsProviderAPI
8888
:members:
8989

90+
.. autoclass:: nc_py_api.ex_app.providers.task_processing.ShapeType
91+
:members:
92+
93+
.. autoclass:: nc_py_api.ex_app.providers.task_processing.ShapeEnumValue
94+
:members:
95+
96+
.. autoclass:: nc_py_api.ex_app.providers.task_processing.ShapeDescriptor
97+
:members:
98+
99+
.. autoclass:: nc_py_api.ex_app.providers.task_processing.TaskType
100+
:members:
101+
102+
.. autoclass:: nc_py_api.ex_app.providers.task_processing.TaskProcessingProvider
103+
:members:
104+
90105
.. autoclass:: nc_py_api.ex_app.providers.task_processing._TaskProcessingProviderAPI
91106
:members:
92107

‎nc_py_api/ex_app/providers/task_processing.py

+94-30
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,101 @@
33
import contextlib
44
import dataclasses
55
import typing
6+
from enum import IntEnum
7+
8+
from pydantic import RootModel
9+
from pydantic.dataclasses import dataclass
610

711
from ..._exceptions import NextcloudException, NextcloudExceptionNotFound
8-
from ..._misc import clear_from_params_empty, require_capabilities
12+
from ..._misc import require_capabilities
913
from ..._session import AsyncNcSessionApp, NcSessionApp
1014

1115
_EP_SUFFIX: str = "ai_provider/task_processing"
1216

1317

14-
@dataclasses.dataclass
15-
class TaskProcessingProvider:
16-
"""TaskProcessing provider description."""
18+
class ShapeType(IntEnum):
19+
"""Enum for shape types."""
20+
21+
NUMBER = 0
22+
TEXT = 1
23+
IMAGE = 2
24+
AUDIO = 3
25+
VIDEO = 4
26+
FILE = 5
27+
ENUM = 6
28+
LIST_OF_NUMBERS = 10
29+
LIST_OF_TEXTS = 11
30+
LIST_OF_IMAGES = 12
31+
LIST_OF_AUDIOS = 13
32+
LIST_OF_VIDEOS = 14
33+
LIST_OF_FILES = 15
34+
35+
36+
@dataclass
37+
class ShapeEnumValue:
38+
"""Data object for input output shape enum slot value."""
39+
40+
name: str
41+
"""Name of the enum slot value which will be displayed in the UI"""
42+
value: str
43+
"""Value of the enum slot value"""
44+
1745

18-
def __init__(self, raw_data: dict):
19-
self._raw_data = raw_data
46+
@dataclass
47+
class ShapeDescriptor:
48+
"""Data object for input output shape entries."""
2049

21-
@property
22-
def name(self) -> str:
23-
"""Unique ID for the provider."""
24-
return self._raw_data["name"]
50+
name: str
51+
"""Name of the shape entry"""
52+
description: str
53+
"""Description of the shape entry"""
54+
shape_type: ShapeType
55+
"""Type of the shape entry"""
2556

26-
@property
27-
def display_name(self) -> str:
28-
"""Providers display name."""
29-
return self._raw_data["display_name"]
3057

31-
@property
32-
def task_type(self) -> str:
33-
"""The TaskType provided by this provider."""
34-
return self._raw_data["task_type"]
58+
@dataclass
59+
class TaskType:
60+
"""TaskType description for the provider."""
61+
62+
id: str
63+
"""The unique ID for the task type."""
64+
name: str
65+
"""The localized name of the task type."""
66+
description: str
67+
"""The localized description of the task type."""
68+
input_shape: list[ShapeDescriptor]
69+
"""The input shape of the task."""
70+
output_shape: list[ShapeDescriptor]
71+
"""The output shape of the task."""
72+
73+
74+
@dataclass
75+
class TaskProcessingProvider:
76+
77+
id: str
78+
"""Unique ID for the provider."""
79+
name: str
80+
"""The localized name of this provider"""
81+
task_type: str
82+
"""The TaskType provided by this provider."""
83+
expected_runtime: int = dataclasses.field(default=0)
84+
"""Expected runtime of the task in seconds."""
85+
optional_input_shape: list[ShapeDescriptor] = dataclasses.field(default_factory=list)
86+
"""Optional input shape of the task."""
87+
optional_output_shape: list[ShapeDescriptor] = dataclasses.field(default_factory=list)
88+
"""Optional output shape of the task."""
89+
input_shape_enum_values: dict[str, list[ShapeEnumValue]] = dataclasses.field(default_factory=dict)
90+
"""The option dict for each input shape ENUM slot."""
91+
input_shape_defaults: dict[str, str | int | float] = dataclasses.field(default_factory=dict)
92+
"""The default values for input shape slots."""
93+
optional_input_shape_enum_values: dict[str, list[ShapeEnumValue]] = dataclasses.field(default_factory=dict)
94+
"""The option list for each optional input shape ENUM slot."""
95+
optional_input_shape_defaults: dict[str, str | int | float] = dataclasses.field(default_factory=dict)
96+
"""The default values for optional input shape slots."""
97+
output_shape_enum_values: dict[str, list[ShapeEnumValue]] = dataclasses.field(default_factory=dict)
98+
"""The option list for each output shape ENUM slot."""
99+
optional_output_shape_enum_values: dict[str, list[ShapeEnumValue]] = dataclasses.field(default_factory=dict)
100+
"""The option list for each optional output shape ENUM slot."""
35101

36102
def __repr__(self):
37103
return f"<{self.__class__.__name__} name={self.name}, type={self.task_type}>"
@@ -44,17 +110,16 @@ def __init__(self, session: NcSessionApp):
44110
self._session = session
45111

46112
def register(
47-
self, name: str, display_name: str, task_type: str, custom_task_type: dict[str, typing.Any] | None = None
113+
self,
114+
provider: TaskProcessingProvider,
115+
custom_task_type: TaskType | None = None,
48116
) -> None:
49117
"""Registers or edit the TaskProcessing provider."""
50118
require_capabilities("app_api", self._session.capabilities)
51119
params = {
52-
"name": name,
53-
"displayName": display_name,
54-
"taskType": task_type,
55-
"customTaskType": custom_task_type,
120+
"provider": RootModel(provider).model_dump(),
121+
**({"customTaskType": RootModel(custom_task_type).model_dump()} if custom_task_type else {}),
56122
}
57-
clear_from_params_empty(["customTaskType"], params)
58123
self._session.ocs("POST", f"{self._session.ae_url}/{_EP_SUFFIX}", json=params)
59124

60125
def unregister(self, name: str, not_fail=True) -> None:
@@ -123,17 +188,16 @@ def __init__(self, session: AsyncNcSessionApp):
123188
self._session = session
124189

125190
async def register(
126-
self, name: str, display_name: str, task_type: str, custom_task_type: dict[str, typing.Any] | None = None
191+
self,
192+
provider: TaskProcessingProvider,
193+
custom_task_type: TaskType | None = None,
127194
) -> None:
128195
"""Registers or edit the TaskProcessing provider."""
129196
require_capabilities("app_api", await self._session.capabilities)
130197
params = {
131-
"name": name,
132-
"displayName": display_name,
133-
"taskType": task_type,
134-
"customTaskType": custom_task_type,
198+
"provider": RootModel(provider).model_dump(),
199+
**({"customTaskType": RootModel(custom_task_type).model_dump()} if custom_task_type else {}),
135200
}
136-
clear_from_params_empty(["customTaskType"], params)
137201
await self._session.ocs("POST", f"{self._session.ae_url}/{_EP_SUFFIX}", json=params)
138202

139203
async def unregister(self, name: str, not_fail=True) -> None:

‎pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ lint.select = [
129129
"W",
130130
]
131131
lint.extend-ignore = [
132+
"D101",
132133
"D105",
133134
"D107",
134135
"D203",
@@ -173,7 +174,6 @@ master.py-version = "3.10"
173174
master.extension-pkg-allow-list = [
174175
"pydantic",
175176
]
176-
design.max-attributes = 8
177177
design.max-locals = 20
178178
design.max-branches = 16
179179
design.max-returns = 8
@@ -206,6 +206,7 @@ messages_control.disable = [
206206
"line-too-long",
207207
"too-few-public-methods",
208208
"too-many-public-methods",
209+
"too-many-instance-attributes",
209210
]
210211

211212
[tool.pytest.ini_options]

0 commit comments

Comments
 (0)
Please sign in to comment.