digitalhub 0.8.0b0__py3-none-any.whl → 0.8.0b2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of digitalhub might be problematic. Click here for more details.

Files changed (159) hide show
  1. digitalhub/__init__.py +62 -94
  2. digitalhub/client/__init__.py +0 -0
  3. digitalhub/client/builder.py +105 -0
  4. digitalhub/client/objects/__init__.py +0 -0
  5. digitalhub/client/objects/base.py +56 -0
  6. digitalhub/client/objects/dhcore.py +681 -0
  7. digitalhub/client/objects/local.py +533 -0
  8. digitalhub/context/__init__.py +0 -0
  9. digitalhub/context/builder.py +178 -0
  10. digitalhub/context/context.py +136 -0
  11. digitalhub/datastores/__init__.py +0 -0
  12. digitalhub/datastores/builder.py +134 -0
  13. digitalhub/datastores/objects/__init__.py +0 -0
  14. digitalhub/datastores/objects/base.py +85 -0
  15. digitalhub/datastores/objects/local.py +42 -0
  16. digitalhub/datastores/objects/remote.py +23 -0
  17. digitalhub/datastores/objects/s3.py +38 -0
  18. digitalhub/datastores/objects/sql.py +60 -0
  19. digitalhub/entities/__init__.py +0 -0
  20. digitalhub/entities/_base/__init__.py +0 -0
  21. digitalhub/entities/_base/api.py +346 -0
  22. digitalhub/entities/_base/base.py +82 -0
  23. digitalhub/entities/_base/crud.py +610 -0
  24. digitalhub/entities/_base/entity/__init__.py +0 -0
  25. digitalhub/entities/_base/entity/base.py +132 -0
  26. digitalhub/entities/_base/entity/context.py +118 -0
  27. digitalhub/entities/_base/entity/executable.py +380 -0
  28. digitalhub/entities/_base/entity/material.py +214 -0
  29. digitalhub/entities/_base/entity/unversioned.py +87 -0
  30. digitalhub/entities/_base/entity/versioned.py +94 -0
  31. digitalhub/entities/_base/metadata.py +59 -0
  32. digitalhub/entities/_base/spec/__init__.py +0 -0
  33. digitalhub/entities/_base/spec/base.py +58 -0
  34. digitalhub/entities/_base/spec/material.py +22 -0
  35. digitalhub/entities/_base/state.py +31 -0
  36. digitalhub/entities/_base/status/__init__.py +0 -0
  37. digitalhub/entities/_base/status/base.py +32 -0
  38. digitalhub/entities/_base/status/material.py +49 -0
  39. digitalhub/entities/_builders/__init__.py +0 -0
  40. digitalhub/entities/_builders/metadata.py +60 -0
  41. digitalhub/entities/_builders/name.py +31 -0
  42. digitalhub/entities/_builders/spec.py +43 -0
  43. digitalhub/entities/_builders/status.py +62 -0
  44. digitalhub/entities/_builders/uuid.py +33 -0
  45. digitalhub/entities/artifact/__init__.py +0 -0
  46. digitalhub/entities/artifact/builder.py +133 -0
  47. digitalhub/entities/artifact/crud.py +358 -0
  48. digitalhub/entities/artifact/entity/__init__.py +0 -0
  49. digitalhub/entities/artifact/entity/_base.py +39 -0
  50. digitalhub/entities/artifact/entity/artifact.py +9 -0
  51. digitalhub/entities/artifact/spec.py +39 -0
  52. digitalhub/entities/artifact/status.py +15 -0
  53. digitalhub/entities/dataitem/__init__.py +0 -0
  54. digitalhub/entities/dataitem/builder.py +144 -0
  55. digitalhub/entities/dataitem/crud.py +395 -0
  56. digitalhub/entities/dataitem/entity/__init__.py +0 -0
  57. digitalhub/entities/dataitem/entity/_base.py +75 -0
  58. digitalhub/entities/dataitem/entity/dataitem.py +9 -0
  59. digitalhub/entities/dataitem/entity/iceberg.py +7 -0
  60. digitalhub/entities/dataitem/entity/table.py +125 -0
  61. digitalhub/entities/dataitem/models.py +62 -0
  62. digitalhub/entities/dataitem/spec.py +61 -0
  63. digitalhub/entities/dataitem/status.py +38 -0
  64. digitalhub/entities/entity_types.py +19 -0
  65. digitalhub/entities/function/__init__.py +0 -0
  66. digitalhub/entities/function/builder.py +86 -0
  67. digitalhub/entities/function/crud.py +305 -0
  68. digitalhub/entities/function/entity.py +101 -0
  69. digitalhub/entities/function/models.py +118 -0
  70. digitalhub/entities/function/spec.py +81 -0
  71. digitalhub/entities/function/status.py +9 -0
  72. digitalhub/entities/model/__init__.py +0 -0
  73. digitalhub/entities/model/builder.py +152 -0
  74. digitalhub/entities/model/crud.py +358 -0
  75. digitalhub/entities/model/entity/__init__.py +0 -0
  76. digitalhub/entities/model/entity/_base.py +34 -0
  77. digitalhub/entities/model/entity/huggingface.py +9 -0
  78. digitalhub/entities/model/entity/mlflow.py +90 -0
  79. digitalhub/entities/model/entity/model.py +9 -0
  80. digitalhub/entities/model/entity/sklearn.py +9 -0
  81. digitalhub/entities/model/models.py +26 -0
  82. digitalhub/entities/model/spec.py +146 -0
  83. digitalhub/entities/model/status.py +33 -0
  84. digitalhub/entities/project/__init__.py +0 -0
  85. digitalhub/entities/project/builder.py +82 -0
  86. digitalhub/entities/project/crud.py +350 -0
  87. digitalhub/entities/project/entity.py +2060 -0
  88. digitalhub/entities/project/spec.py +50 -0
  89. digitalhub/entities/project/status.py +9 -0
  90. digitalhub/entities/registries.py +48 -0
  91. digitalhub/entities/run/__init__.py +0 -0
  92. digitalhub/entities/run/builder.py +77 -0
  93. digitalhub/entities/run/crud.py +232 -0
  94. digitalhub/entities/run/entity.py +461 -0
  95. digitalhub/entities/run/spec.py +153 -0
  96. digitalhub/entities/run/status.py +114 -0
  97. digitalhub/entities/secret/__init__.py +0 -0
  98. digitalhub/entities/secret/builder.py +93 -0
  99. digitalhub/entities/secret/crud.py +294 -0
  100. digitalhub/entities/secret/entity.py +73 -0
  101. digitalhub/entities/secret/spec.py +35 -0
  102. digitalhub/entities/secret/status.py +9 -0
  103. digitalhub/entities/task/__init__.py +0 -0
  104. digitalhub/entities/task/builder.py +74 -0
  105. digitalhub/entities/task/crud.py +241 -0
  106. digitalhub/entities/task/entity.py +135 -0
  107. digitalhub/entities/task/models.py +199 -0
  108. digitalhub/entities/task/spec.py +51 -0
  109. digitalhub/entities/task/status.py +9 -0
  110. digitalhub/entities/utils.py +184 -0
  111. digitalhub/entities/workflow/__init__.py +0 -0
  112. digitalhub/entities/workflow/builder.py +91 -0
  113. digitalhub/entities/workflow/crud.py +304 -0
  114. digitalhub/entities/workflow/entity.py +77 -0
  115. digitalhub/entities/workflow/spec.py +15 -0
  116. digitalhub/entities/workflow/status.py +9 -0
  117. digitalhub/readers/__init__.py +0 -0
  118. digitalhub/readers/builder.py +54 -0
  119. digitalhub/readers/objects/__init__.py +0 -0
  120. digitalhub/readers/objects/base.py +70 -0
  121. digitalhub/readers/objects/pandas.py +207 -0
  122. digitalhub/readers/registry.py +15 -0
  123. digitalhub/registry/__init__.py +0 -0
  124. digitalhub/registry/models.py +87 -0
  125. digitalhub/registry/registry.py +74 -0
  126. digitalhub/registry/utils.py +150 -0
  127. digitalhub/runtimes/__init__.py +0 -0
  128. digitalhub/runtimes/base.py +164 -0
  129. digitalhub/runtimes/builder.py +53 -0
  130. digitalhub/runtimes/kind_registry.py +170 -0
  131. digitalhub/stores/__init__.py +0 -0
  132. digitalhub/stores/builder.py +257 -0
  133. digitalhub/stores/objects/__init__.py +0 -0
  134. digitalhub/stores/objects/base.py +189 -0
  135. digitalhub/stores/objects/local.py +230 -0
  136. digitalhub/stores/objects/remote.py +143 -0
  137. digitalhub/stores/objects/s3.py +563 -0
  138. digitalhub/stores/objects/sql.py +328 -0
  139. digitalhub/utils/__init__.py +0 -0
  140. digitalhub/utils/data_utils.py +127 -0
  141. digitalhub/utils/env_utils.py +123 -0
  142. digitalhub/utils/exceptions.py +55 -0
  143. digitalhub/utils/file_utils.py +204 -0
  144. digitalhub/utils/generic_utils.py +207 -0
  145. digitalhub/utils/git_utils.py +148 -0
  146. digitalhub/utils/io_utils.py +79 -0
  147. digitalhub/utils/logger.py +17 -0
  148. digitalhub/utils/uri_utils.py +56 -0
  149. {digitalhub-0.8.0b0.dist-info → digitalhub-0.8.0b2.dist-info}/METADATA +27 -12
  150. digitalhub-0.8.0b2.dist-info/RECORD +161 -0
  151. test/test_crud_artifacts.py +1 -1
  152. test/test_crud_dataitems.py +1 -1
  153. test/test_crud_functions.py +1 -1
  154. test/test_crud_runs.py +1 -1
  155. test/test_crud_tasks.py +1 -1
  156. digitalhub-0.8.0b0.dist-info/RECORD +0 -14
  157. {digitalhub-0.8.0b0.dist-info → digitalhub-0.8.0b2.dist-info}/LICENSE.txt +0 -0
  158. {digitalhub-0.8.0b0.dist-info → digitalhub-0.8.0b2.dist-info}/WHEEL +0 -0
  159. {digitalhub-0.8.0b0.dist-info → digitalhub-0.8.0b2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,61 @@
1
+ from __future__ import annotations
2
+
3
+ from pydantic import Field
4
+
5
+ from digitalhub.entities._base.spec.material import MaterialParams, MaterialSpec
6
+ from digitalhub.entities.dataitem.models import TableSchema
7
+
8
+
9
+ class DataitemSpec(MaterialSpec):
10
+ """
11
+ Dataitem specifications.
12
+ """
13
+
14
+
15
+ class DataitemParams(MaterialParams):
16
+ """
17
+ Dataitem parameters.
18
+ """
19
+
20
+
21
+ class DataitemSpecDataitem(DataitemSpec):
22
+ """
23
+ Dataitem dataitem specifications.
24
+ """
25
+
26
+
27
+ class DataitemParamsDataitem(DataitemParams):
28
+ """
29
+ Dataitem dataitem parameters.
30
+ """
31
+
32
+
33
+ class DataitemSpecTable(DataitemSpec):
34
+ """
35
+ Dataitem table specifications.
36
+ """
37
+
38
+ def __init__(self, path: str, schema: dict | None = None) -> None:
39
+ super().__init__(path)
40
+ self.schema = schema
41
+
42
+
43
+ class DataitemParamsTable(DataitemParams):
44
+ """
45
+ Dataitem table parameters.
46
+ """
47
+
48
+ schema_: TableSchema = Field(default=None, alias="schema")
49
+ """The schema of the dataitem in table schema format."""
50
+
51
+
52
+ class DataitemSpecIceberg(DataitemSpec):
53
+ """
54
+ Dataitem iceberg specifications.
55
+ """
56
+
57
+
58
+ class DataitemParamsIceberg(DataitemParams):
59
+ """
60
+ Dataitem iceberg parameters.
61
+ """
@@ -0,0 +1,38 @@
1
+ from __future__ import annotations
2
+
3
+ from digitalhub.entities._base.status.material import MaterialStatus
4
+
5
+
6
+ class DataitemStatus(MaterialStatus):
7
+ """
8
+ Status class for dataitem entities.
9
+ """
10
+
11
+ def __init__(
12
+ self,
13
+ state: str,
14
+ message: str | None = None,
15
+ files: list[dict] | None = None,
16
+ preview: dict | None = None,
17
+ **kwargs,
18
+ ) -> None:
19
+ super().__init__(state, message, files)
20
+ self.preview = preview
21
+
22
+
23
+ class DataitemStatusDataitem(DataitemStatus):
24
+ """
25
+ Status class for dataitem dataitem entities.
26
+ """
27
+
28
+
29
+ class DataitemStatusTable(DataitemStatus):
30
+ """
31
+ Status class for dataitem table entities.
32
+ """
33
+
34
+
35
+ class DataitemStatusIceberg(DataitemStatus):
36
+ """
37
+ Status class for dataitem iceberg entities.
38
+ """
@@ -0,0 +1,19 @@
1
+ from __future__ import annotations
2
+
3
+ from enum import Enum
4
+
5
+
6
+ class EntityTypes(Enum):
7
+ """
8
+ Entity types.
9
+ """
10
+
11
+ PROJECT = "project"
12
+ ARTIFACT = "artifact"
13
+ DATAITEM = "dataitem"
14
+ MODEL = "model"
15
+ SECRET = "secret"
16
+ FUNCTION = "function"
17
+ WORKFLOW = "workflow"
18
+ TASK = "task"
19
+ RUN = "run"
File without changes
@@ -0,0 +1,86 @@
1
+ from __future__ import annotations
2
+
3
+ from digitalhub.entities._builders.metadata import build_metadata
4
+ from digitalhub.entities._builders.name import build_name
5
+ from digitalhub.entities._builders.spec import build_spec
6
+ from digitalhub.entities._builders.status import build_status
7
+ from digitalhub.entities._builders.uuid import build_uuid
8
+ from digitalhub.entities.function.entity import Function
9
+
10
+
11
+ def function_from_parameters(
12
+ project: str,
13
+ name: str,
14
+ kind: str,
15
+ uuid: str | None = None,
16
+ description: str | None = None,
17
+ labels: list[str] | None = None,
18
+ embedded: bool = True,
19
+ **kwargs,
20
+ ) -> Function:
21
+ """
22
+ Create a new Function instance and persist it to the backend.
23
+
24
+ Parameters
25
+ ----------
26
+ project : str
27
+ Project name.
28
+ name : str
29
+ Object name.
30
+ kind : str
31
+ Kind the object.
32
+ uuid : str
33
+ ID of the object (UUID4, e.g. 40f25c4b-d26b-4221-b048-9527aff291e2).
34
+ description : str
35
+ Description of the object (human readable).
36
+ labels : list[str]
37
+ List of labels.
38
+ embedded : bool
39
+ Flag to determine if object spec must be embedded in project spec.
40
+ **kwargs : dict
41
+ Spec keyword arguments.
42
+
43
+ Returns
44
+ -------
45
+ Function
46
+ Object instance.
47
+ """
48
+ name = build_name(name)
49
+ uuid = build_uuid(uuid)
50
+ spec = build_spec(kind, **kwargs)
51
+ metadata = build_metadata(
52
+ kind,
53
+ project=project,
54
+ name=name,
55
+ version=uuid,
56
+ description=description,
57
+ labels=labels,
58
+ embedded=embedded,
59
+ )
60
+ status = build_status(kind)
61
+ return Function(
62
+ project=project,
63
+ name=name,
64
+ uuid=uuid,
65
+ kind=kind,
66
+ metadata=metadata,
67
+ spec=spec,
68
+ status=status,
69
+ )
70
+
71
+
72
+ def function_from_dict(obj: dict) -> Function:
73
+ """
74
+ Create a new object from dictionary.
75
+
76
+ Parameters
77
+ ----------
78
+ obj : dict
79
+ Dictionary to create object from.
80
+
81
+ Returns
82
+ -------
83
+ Function
84
+ Function object.
85
+ """
86
+ return Function.from_dict(obj)
@@ -0,0 +1,305 @@
1
+ from __future__ import annotations
2
+
3
+ import typing
4
+
5
+ from digitalhub.context.builder import check_context
6
+ from digitalhub.entities._base.crud import (
7
+ delete_entity_api_ctx,
8
+ list_entity_api_ctx,
9
+ read_entity_api_ctx,
10
+ read_entity_api_ctx_versions,
11
+ )
12
+ from digitalhub.entities.entity_types import EntityTypes
13
+ from digitalhub.entities.function.builder import function_from_dict, function_from_parameters
14
+ from digitalhub.utils.exceptions import EntityAlreadyExistsError
15
+ from digitalhub.utils.io_utils import read_yaml
16
+
17
+ if typing.TYPE_CHECKING:
18
+ from digitalhub.entities.function.entity import Function
19
+
20
+ ENTITY_TYPE = EntityTypes.FUNCTION.value
21
+
22
+
23
+ def new_function(
24
+ project: str,
25
+ name: str,
26
+ kind: str,
27
+ uuid: str | None = None,
28
+ description: str | None = None,
29
+ labels: list[str] | None = None,
30
+ embedded: bool = True,
31
+ **kwargs,
32
+ ) -> Function:
33
+ """
34
+ Create a Function instance with the given parameters.
35
+
36
+ Parameters
37
+ ----------
38
+ project : str
39
+ Project name.
40
+ name : str
41
+ Object name.
42
+ kind : str
43
+ Kind the object.
44
+ uuid : str
45
+ ID of the object (UUID4, e.g. 40f25c4b-d26b-4221-b048-9527aff291e2).
46
+ description : str
47
+ Description of the object (human readable).
48
+ labels : list[str]
49
+ List of labels.
50
+ embedded : bool
51
+ Flag to determine if object spec must be embedded in project spec.
52
+ **kwargs : dict
53
+ Spec keyword arguments.
54
+
55
+ Returns
56
+ -------
57
+ Function
58
+ Object instance.
59
+
60
+ Examples
61
+ --------
62
+ >>> obj = new_function(project="my-project",
63
+ >>> name="my-function",
64
+ >>> kind="python",
65
+ >>> code_src="function.py",
66
+ >>> handler="function-handler")
67
+ """
68
+ check_context(project)
69
+ obj = function_from_parameters(
70
+ project=project,
71
+ name=name,
72
+ kind=kind,
73
+ uuid=uuid,
74
+ description=description,
75
+ labels=labels,
76
+ embedded=embedded,
77
+ **kwargs,
78
+ )
79
+ obj.save()
80
+ return obj
81
+
82
+
83
+ def get_function(
84
+ identifier: str,
85
+ project: str | None = None,
86
+ entity_id: str | None = None,
87
+ **kwargs,
88
+ ) -> Function:
89
+ """
90
+ Get object from backend.
91
+
92
+ Parameters
93
+ ----------
94
+ identifier : str
95
+ Entity key (store://...) or entity name.
96
+ project : str
97
+ Project name.
98
+ entity_id : str
99
+ Entity ID.
100
+ **kwargs : dict
101
+ Parameters to pass to the API call.
102
+
103
+ Returns
104
+ -------
105
+ Function
106
+ Object instance.
107
+
108
+ Examples
109
+ --------
110
+ Using entity key:
111
+ >>> obj = get_function("store://my-function-key")
112
+
113
+ Using entity name:
114
+ >>> obj = get_function("my-function-name"
115
+ >>> project="my-project",
116
+ >>> entity_id="my-function-id")
117
+ """
118
+ obj = read_entity_api_ctx(
119
+ identifier,
120
+ ENTITY_TYPE,
121
+ project=project,
122
+ entity_id=entity_id,
123
+ **kwargs,
124
+ )
125
+ return function_from_dict(obj)
126
+
127
+
128
+ def get_function_versions(
129
+ identifier: str,
130
+ project: str | None = None,
131
+ **kwargs,
132
+ ) -> list[Function]:
133
+ """
134
+ Get object versions from backend.
135
+
136
+ Parameters
137
+ ----------
138
+ identifier : str
139
+ Entity key (store://...) or entity name.
140
+ project : str
141
+ Project name.
142
+ **kwargs : dict
143
+ Parameters to pass to the API call.
144
+
145
+ Returns
146
+ -------
147
+ list[Function]
148
+ List of object instances.
149
+
150
+ Examples
151
+ --------
152
+ Using entity key:
153
+ >>> obj = get_function_versions("store://my-function-key")
154
+
155
+ Using entity name:
156
+ >>> obj = get_function_versions("my-function-name"
157
+ >>> project="my-project")
158
+ """
159
+ obj = read_entity_api_ctx_versions(
160
+ identifier,
161
+ entity_type=ENTITY_TYPE,
162
+ project=project,
163
+ **kwargs,
164
+ )
165
+ return [function_from_dict(o) for o in obj]
166
+
167
+
168
+ def list_functions(project: str, **kwargs) -> list[Function]:
169
+ """
170
+ List all latest version objects from backend.
171
+
172
+ Parameters
173
+ ----------
174
+ project : str
175
+ Project name.
176
+ **kwargs : dict
177
+ Parameters to pass to the API call.
178
+
179
+ Returns
180
+ -------
181
+ list[Function]
182
+ List of object instances.
183
+
184
+ Examples
185
+ --------
186
+ >>> objs = list_functions(project="my-project")
187
+ """
188
+ objs = list_entity_api_ctx(
189
+ project=project,
190
+ entity_type=ENTITY_TYPE,
191
+ **kwargs,
192
+ )
193
+ return [function_from_dict(obj) for obj in objs]
194
+
195
+
196
+ def import_function(file: str) -> Function:
197
+ """
198
+ Get object from file.
199
+
200
+ Parameters
201
+ ----------
202
+ file : str
203
+ Path to YAML file.
204
+
205
+ Returns
206
+ -------
207
+ Function
208
+ Object instance.
209
+
210
+ Examples
211
+ --------
212
+ >>> obj = import_function("my-function.yaml")
213
+ """
214
+ dict_obj: dict | list[dict] = read_yaml(file)
215
+ if isinstance(dict_obj, list):
216
+ fnc_dict = dict_obj[0]
217
+ tsk_dicts = dict_obj[1:]
218
+ else:
219
+ fnc_dict = dict_obj
220
+ tsk_dicts = []
221
+
222
+ check_context(fnc_dict.get("project"))
223
+ obj = function_from_dict(fnc_dict)
224
+
225
+ obj.import_tasks(tsk_dicts)
226
+
227
+ try:
228
+ obj.save()
229
+ except EntityAlreadyExistsError:
230
+ pass
231
+ finally:
232
+ return obj
233
+
234
+
235
+ def update_function(entity: Function) -> Function:
236
+ """
237
+ Update object. Note that object spec are immutable.
238
+
239
+ Parameters
240
+ ----------
241
+ entity : Function
242
+ Object to update.
243
+
244
+ Returns
245
+ -------
246
+ Function
247
+ Entity updated.
248
+
249
+ Examples
250
+ --------
251
+ >>> obj = update_function(obj)
252
+ """
253
+ return entity.save(update=True)
254
+
255
+
256
+ def delete_function(
257
+ identifier: str,
258
+ project: str | None = None,
259
+ entity_id: str | None = None,
260
+ delete_all_versions: bool = False,
261
+ cascade: bool = True,
262
+ **kwargs,
263
+ ) -> dict:
264
+ """
265
+ Delete object from backend.
266
+
267
+ Parameters
268
+ ----------
269
+ identifier : str
270
+ Entity key (store://...) or entity name.
271
+ project : str
272
+ Project name.
273
+ entity_id : str
274
+ Entity ID.
275
+ delete_all_versions : bool
276
+ Delete all versions of the named entity. If True, use entity name instead of entity key as identifier.
277
+ cascade : bool
278
+ Cascade delete.
279
+ **kwargs : dict
280
+ Parameters to pass to the API call.
281
+
282
+ Returns
283
+ -------
284
+ dict
285
+ Response from backend.
286
+
287
+ Examples
288
+ --------
289
+ If delete_all_versions is False:
290
+ >>> obj = delete_function("store://my-function-key")
291
+
292
+ Otherwise:
293
+ >>> obj = delete_function("function-name",
294
+ >>> project="my-project",
295
+ >>> delete_all_versions=True)
296
+ """
297
+ return delete_entity_api_ctx(
298
+ identifier=identifier,
299
+ entity_type=ENTITY_TYPE,
300
+ project=project,
301
+ entity_id=entity_id,
302
+ delete_all_versions=delete_all_versions,
303
+ cascade=cascade,
304
+ **kwargs,
305
+ )
@@ -0,0 +1,101 @@
1
+ from __future__ import annotations
2
+
3
+ import typing
4
+ from concurrent.futures import ThreadPoolExecutor
5
+
6
+ from digitalhub.entities._base.entity.executable import ExecutableEntity
7
+ from digitalhub.entities.entity_types import EntityTypes
8
+ from digitalhub.runtimes.builder import get_kind_registry
9
+ from digitalhub.utils.exceptions import BackendError
10
+
11
+ if typing.TYPE_CHECKING:
12
+ from digitalhub.entities._base.metadata import Metadata
13
+ from digitalhub.entities.function.spec import FunctionSpec
14
+ from digitalhub.entities.function.status import FunctionStatus
15
+ from digitalhub.entities.run.entity import Run
16
+
17
+
18
+ class Function(ExecutableEntity):
19
+ """
20
+ A class representing a function.
21
+ """
22
+
23
+ ENTITY_TYPE = EntityTypes.FUNCTION.value
24
+
25
+ def __init__(
26
+ self,
27
+ project: str,
28
+ name: str,
29
+ uuid: str,
30
+ kind: str,
31
+ metadata: Metadata,
32
+ spec: FunctionSpec,
33
+ status: FunctionStatus,
34
+ user: str | None = None,
35
+ ) -> None:
36
+ super().__init__(project, name, uuid, kind, metadata, spec, status, user)
37
+
38
+ self.spec: FunctionSpec
39
+ self.status: FunctionStatus
40
+
41
+ ##############################
42
+ # Function Methods
43
+ ##############################
44
+
45
+ def run(
46
+ self,
47
+ action: str,
48
+ local_execution: bool = False,
49
+ wait: bool = False,
50
+ log_info: bool = True,
51
+ **kwargs,
52
+ ) -> Run:
53
+ """
54
+ Run function. This method creates a new run and executes it.
55
+
56
+ Parameters
57
+ ----------
58
+ action : str
59
+ Action to execute.
60
+ local_execution : bool
61
+ Flag to determine if object has local execution.
62
+ wait : bool
63
+ Flag to wait for execution.
64
+ log_info : bool
65
+ Flag to log information while waiting.
66
+ **kwargs : dict
67
+ Keyword arguments passed to Run builder.
68
+
69
+ Returns
70
+ -------
71
+ Run
72
+ Run instance.
73
+ """
74
+ # Get kind registry
75
+ kind_reg = get_kind_registry(self.kind)
76
+
77
+ # Get task and run kind
78
+ task_kind = kind_reg.get_task_kind_from_action(action=action)
79
+ run_kind = kind_reg.get_run_kind()
80
+
81
+ # Create or update new task
82
+ task = self._get_or_create_task(task_kind)
83
+
84
+ # Run function from task
85
+ run = task.run(run_kind, local_execution, **kwargs)
86
+
87
+ # If execution is done by DHCore backend, return the object
88
+ if not local_execution:
89
+ if self._context().local:
90
+ raise BackendError("Cannot run remote function with local backend.")
91
+ if wait:
92
+ return run.wait(log_info=log_info)
93
+ return run
94
+
95
+ # If local execution, build and launch run.
96
+ # Detach the run from the main thread
97
+ run.build()
98
+ with ThreadPoolExecutor(max_workers=1) as executor:
99
+ result = executor.submit(run.run)
100
+ r = result.result()
101
+ return r