Skip to content

modules

Attributes

ONBOARDING_MODEL_NAME_PREFIX = 'onboarding.file.from.' module-attribute

Classes

OnboardFileConfig

Bases: KiaraModuleConfig

Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
16
17
18
19
20
21
22
23
class OnboardFileConfig(KiaraModuleConfig):

    onboard_type: Union[None, str] = Field(
        description="The name of the type of onboarding.", default=None
    )
    attach_metadata: Union[bool, None] = Field(
        description="Whether to attach metadata.", default=None
    )

Attributes

onboard_type: Union[None, str] = Field(description='The name of the type of onboarding.', default=None) instance-attribute class-attribute
attach_metadata: Union[bool, None] = Field(description='Whether to attach metadata.', default=None) instance-attribute class-attribute

OnboardFileModule

Bases: KiaraModule

A generic module that imports a file from one of several possible sources.

Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
class OnboardFileModule(KiaraModule):
    """A generic module that imports a file from one of several possible sources."""

    _module_type_name = "import.file"
    _config_cls = OnboardFileConfig

    def create_inputs_schema(
        self,
    ) -> ValueMapSchema:

        result = {
            "source": {
                "type": "string",
                "doc": "The source uri of the file to be onboarded.",
                "optional": False,
            },
            "file_name": {
                "type": "string",
                "doc": "The file name to use for the onboarded file (defaults to source file name if possible).",
                "optional": True,
            },
        }

        if self.get_config_value("attach_metadata") is None:
            result["attach_metadata"] = {
                "type": "boolean",
                "doc": "Whether to attach onboarding metadata to the result file.",
                "default": True,
            }

        onboard_model_cls = self.get_onboard_model_cls()
        if not onboard_model_cls:

            available = (
                ModelRegistry.instance()
                .get_models_of_type(OnboardDataModel)
                .item_infos.keys()
            )

            if not available:
                raise KiaraException(msg="No onboard models available. This is a bug.")

            idx = len(ONBOARDING_MODEL_NAME_PREFIX)
            allowed = sorted((x[idx:] for x in available))

            result["onboard_type"] = {
                "type": "string",
                "type_config": {"allowed_strings": allowed},
                "doc": "The type of onboarding to use. Allowed: {}".format(
                    ", ".join(allowed)
                ),
                "optional": True,
            }
        elif onboard_model_cls.get_config_fields():
            result = {
                "onboard_config": {
                    "type": "kiara_model",
                    "type_config": {
                        "kiara_model_id": self.get_config_value("onboard_type"),
                    },
                }
            }

        return result

    def create_outputs_schema(
        self,
    ) -> ValueMapSchema:

        result = {"file": {"type": "file", "doc": "The file that was onboarded."}}
        return result

    @lru_cache(maxsize=1)
    def get_onboard_model_cls(self) -> Union[None, Type[OnboardDataModel]]:

        onboard_type: Union[str, None] = self.get_config_value("onboard_type")
        if not onboard_type:
            return None

        model_registry = ModelRegistry.instance()
        model_cls = model_registry.get_model_cls(onboard_type, OnboardDataModel)
        return model_cls  # type: ignore

    def find_matching_onboard_models(
        self, uri: str
    ) -> Mapping[Type[OnboardDataModel], Tuple[bool, str]]:

        model_registry = ModelRegistry.instance()
        onboard_models = model_registry.get_models_of_type(
            OnboardDataModel
        ).item_infos.values()

        result = {}
        onboard_model: Type[OnboardDataModel]
        for onboard_model in onboard_models:  # type: ignore

            python_cls: Type[OnboardDataModel] = onboard_model.python_class.get_class()  # type: ignore
            result[python_cls] = python_cls.accepts_uri(uri)

        return result

    def process(self, inputs: ValueMap, outputs: ValueMap):

        onboard_type = self.get_config_value("onboard_type")

        source: str = inputs.get_value_data("source")
        file_name: Union[str, None] = inputs.get_value_data("file_name")

        if not onboard_type:

            user_input_onboard_type = inputs.get_value_data("onboard_type")

            if not user_input_onboard_type:
                model_clsses = self.find_matching_onboard_models(source)
                matches = [k for k, v in model_clsses.items() if v[0]]
                if not matches:
                    raise KiaraProcessingException(
                        msg=f"Can't onboard file from '{source}': no onboard models found that accept this source type."
                    )
                elif len(matches) > 1:
                    msg = "Valid onboarding types for this uri:\n\n"
                    for k, v in model_clsses.items():
                        if not v[0]:
                            continue
                        msg += f"  - {k._kiara_model_id}: {v[1]}\n"
                    raise KiaraProcessingException(
                        msg=f"Can't onboard file from '{source}': multiple onboard models found that accept this source type.\n\n{msg}"
                    )

                model_cls: Type[OnboardDataModel] = matches[0]
            else:
                full_onboard_type = (
                    f"{ONBOARDING_MODEL_NAME_PREFIX}{user_input_onboard_type}"
                )
                model_registry = ModelRegistry.instance()
                model_cls = model_registry.get_model_cls(full_onboard_type, OnboardDataModel)  # type: ignore
                valid, msg = model_cls.accepts_uri(source)
                if not valid:
                    raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{model_cls._kiara_model_id}': {msg}")  # type: ignore
        else:
            model_cls = self.get_onboard_model_cls()  # type: ignore
            if not model_cls:
                raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{onboard_type}': no onboard model found with this name.")  # type: ignore

            valid, msg = model_cls.accepts_uri(source)
            if not valid:
                raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{model_cls._kiara_model_id}': {msg}")  # type: ignore

        if not model_cls.get_config_fields():
            model = model_cls()
        else:
            raise NotImplementedError()

        attach_metadata = self.get_config_value("attach_metadata")
        if attach_metadata is None:
            attach_metadata = inputs.get_value_data("attach_metadata")

        result = model.retrieve(
            uri=source, file_name=file_name, attach_metadata=attach_metadata
        )
        if not result:
            raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{model_cls._kiara_model_id}': no result data retrieved. This is most likely a bug.")  # type: ignore

        if isinstance(result, str):
            data = KiaraFile.load_file(result, file_name=file_name)
        elif not isinstance(result, KiaraFile):
            raise KiaraProcessingException(
                "Can't onboard file: onboard model returned data that is not a file. This is most likely a bug."
            )
        else:
            data = result

        outputs.set_value("file", data)

Attributes

_config_cls = OnboardFileConfig instance-attribute class-attribute

Functions

create_inputs_schema() -> ValueMapSchema
Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def create_inputs_schema(
    self,
) -> ValueMapSchema:

    result = {
        "source": {
            "type": "string",
            "doc": "The source uri of the file to be onboarded.",
            "optional": False,
        },
        "file_name": {
            "type": "string",
            "doc": "The file name to use for the onboarded file (defaults to source file name if possible).",
            "optional": True,
        },
    }

    if self.get_config_value("attach_metadata") is None:
        result["attach_metadata"] = {
            "type": "boolean",
            "doc": "Whether to attach onboarding metadata to the result file.",
            "default": True,
        }

    onboard_model_cls = self.get_onboard_model_cls()
    if not onboard_model_cls:

        available = (
            ModelRegistry.instance()
            .get_models_of_type(OnboardDataModel)
            .item_infos.keys()
        )

        if not available:
            raise KiaraException(msg="No onboard models available. This is a bug.")

        idx = len(ONBOARDING_MODEL_NAME_PREFIX)
        allowed = sorted((x[idx:] for x in available))

        result["onboard_type"] = {
            "type": "string",
            "type_config": {"allowed_strings": allowed},
            "doc": "The type of onboarding to use. Allowed: {}".format(
                ", ".join(allowed)
            ),
            "optional": True,
        }
    elif onboard_model_cls.get_config_fields():
        result = {
            "onboard_config": {
                "type": "kiara_model",
                "type_config": {
                    "kiara_model_id": self.get_config_value("onboard_type"),
                },
            }
        }

    return result
create_outputs_schema() -> ValueMapSchema
Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
94
95
96
97
98
99
def create_outputs_schema(
    self,
) -> ValueMapSchema:

    result = {"file": {"type": "file", "doc": "The file that was onboarded."}}
    return result
get_onboard_model_cls() -> Union[None, Type[OnboardDataModel]] cached
Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
101
102
103
104
105
106
107
108
109
110
@lru_cache(maxsize=1)
def get_onboard_model_cls(self) -> Union[None, Type[OnboardDataModel]]:

    onboard_type: Union[str, None] = self.get_config_value("onboard_type")
    if not onboard_type:
        return None

    model_registry = ModelRegistry.instance()
    model_cls = model_registry.get_model_cls(onboard_type, OnboardDataModel)
    return model_cls  # type: ignore
find_matching_onboard_models(uri: str) -> Mapping[Type[OnboardDataModel], Tuple[bool, str]]
Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def find_matching_onboard_models(
    self, uri: str
) -> Mapping[Type[OnboardDataModel], Tuple[bool, str]]:

    model_registry = ModelRegistry.instance()
    onboard_models = model_registry.get_models_of_type(
        OnboardDataModel
    ).item_infos.values()

    result = {}
    onboard_model: Type[OnboardDataModel]
    for onboard_model in onboard_models:  # type: ignore

        python_cls: Type[OnboardDataModel] = onboard_model.python_class.get_class()  # type: ignore
        result[python_cls] = python_cls.accepts_uri(uri)

    return result
process(inputs: ValueMap, outputs: ValueMap)
Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def process(self, inputs: ValueMap, outputs: ValueMap):

    onboard_type = self.get_config_value("onboard_type")

    source: str = inputs.get_value_data("source")
    file_name: Union[str, None] = inputs.get_value_data("file_name")

    if not onboard_type:

        user_input_onboard_type = inputs.get_value_data("onboard_type")

        if not user_input_onboard_type:
            model_clsses = self.find_matching_onboard_models(source)
            matches = [k for k, v in model_clsses.items() if v[0]]
            if not matches:
                raise KiaraProcessingException(
                    msg=f"Can't onboard file from '{source}': no onboard models found that accept this source type."
                )
            elif len(matches) > 1:
                msg = "Valid onboarding types for this uri:\n\n"
                for k, v in model_clsses.items():
                    if not v[0]:
                        continue
                    msg += f"  - {k._kiara_model_id}: {v[1]}\n"
                raise KiaraProcessingException(
                    msg=f"Can't onboard file from '{source}': multiple onboard models found that accept this source type.\n\n{msg}"
                )

            model_cls: Type[OnboardDataModel] = matches[0]
        else:
            full_onboard_type = (
                f"{ONBOARDING_MODEL_NAME_PREFIX}{user_input_onboard_type}"
            )
            model_registry = ModelRegistry.instance()
            model_cls = model_registry.get_model_cls(full_onboard_type, OnboardDataModel)  # type: ignore
            valid, msg = model_cls.accepts_uri(source)
            if not valid:
                raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{model_cls._kiara_model_id}': {msg}")  # type: ignore
    else:
        model_cls = self.get_onboard_model_cls()  # type: ignore
        if not model_cls:
            raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{onboard_type}': no onboard model found with this name.")  # type: ignore

        valid, msg = model_cls.accepts_uri(source)
        if not valid:
            raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{model_cls._kiara_model_id}': {msg}")  # type: ignore

    if not model_cls.get_config_fields():
        model = model_cls()
    else:
        raise NotImplementedError()

    attach_metadata = self.get_config_value("attach_metadata")
    if attach_metadata is None:
        attach_metadata = inputs.get_value_data("attach_metadata")

    result = model.retrieve(
        uri=source, file_name=file_name, attach_metadata=attach_metadata
    )
    if not result:
        raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{model_cls._kiara_model_id}': no result data retrieved. This is most likely a bug.")  # type: ignore

    if isinstance(result, str):
        data = KiaraFile.load_file(result, file_name=file_name)
    elif not isinstance(result, KiaraFile):
        raise KiaraProcessingException(
            "Can't onboard file: onboard model returned data that is not a file. This is most likely a bug."
        )
    else:
        data = result

    outputs.set_value("file", data)

OnboardFileBundleConfig

Bases: KiaraModuleConfig

Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
class OnboardFileBundleConfig(KiaraModuleConfig):

    onboard_type: Union[None, str] = Field(
        description="The name of the type of onboarding.", default=None
    )
    attach_metadata: Union[bool, None] = Field(
        description="Whether to attach onboarding metadata.", default=None
    )
    sub_path: Union[None, str] = Field(description="The sub path to use.", default=None)
    include_file_types: Union[None, List[str]] = Field(
        description="File types to include.", default=None
    )
    exclude_file_types: Union[None, List[str]] = Field(
        description="File types to include.", default=None
    )

Attributes

onboard_type: Union[None, str] = Field(description='The name of the type of onboarding.', default=None) instance-attribute class-attribute
attach_metadata: Union[bool, None] = Field(description='Whether to attach onboarding metadata.', default=None) instance-attribute class-attribute
sub_path: Union[None, str] = Field(description='The sub path to use.', default=None) instance-attribute class-attribute
include_file_types: Union[None, List[str]] = Field(description='File types to include.', default=None) instance-attribute class-attribute
exclude_file_types: Union[None, List[str]] = Field(description='File types to include.', default=None) instance-attribute class-attribute

OnboardFileBundleModule

Bases: KiaraModule

A generic module that imports a file from one of several possible sources.

Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
class OnboardFileBundleModule(KiaraModule):
    """A generic module that imports a file from one of several possible sources."""

    _module_type_name = "import.file_bundle"
    _config_cls = OnboardFileBundleConfig

    def create_inputs_schema(
        self,
    ) -> ValueMapSchema:

        result = {
            "source": {
                "type": "string",
                "doc": "The source uri of the file to be onboarded.",
                "optional": False,
            }
        }

        if self.get_config_value("attach_metadata") is None:
            result["attach_metadata"] = {
                "type": "boolean",
                "doc": "Whether to attach onboarding metadata.",
                "default": True,
            }
        if self.get_config_value("sub_path") is None:
            result["sub_path"] = {
                "type": "string",
                "doc": "The sub path to use. If not specified, the root of the source folder will be used.",
                "optional": True,
            }
        if self.get_config_value("include_file_types") is None:
            result["include_file_types"] = {
                "type": "list",
                "doc": "A list of file extensions to include. If not specified, all file extensions are included.",
                "optional": True,
            }

        if self.get_config_value("exclude_file_types") is None:
            result["exclude_file_types"] = {
                "type": "list",
                "doc": "A list of file extensions to exclude. If not specified, no file extensions are excluded.",
                "optional": True,
            }

        onboard_model_cls = self.get_onboard_model_cls()
        if not onboard_model_cls:

            available = (
                ModelRegistry.instance()
                .get_models_of_type(OnboardDataModel)
                .item_infos.keys()
            )

            if not available:
                raise KiaraException(msg="No onboard models available. This is a bug.")

            idx = len(ONBOARDING_MODEL_NAME_PREFIX)
            allowed = sorted((x[idx:] for x in available))

            result["onboard_type"] = {
                "type": "string",
                "type_config": {"allowed_strings": allowed},
                "doc": "The type of onboarding to use. Allowed: {}".format(
                    ", ".join(allowed)
                ),
                "optional": True,
            }
        elif onboard_model_cls.get_config_fields():
            result = {
                "onboard_config": {
                    "type": "kiara_model",
                    "type_config": {
                        "kiara_model_id": self.get_config_value("onboard_type"),
                    },
                }
            }

        return result

    def create_outputs_schema(
        self,
    ) -> ValueMapSchema:

        result = {
            "file_bundle": {
                "type": "file_bundle",
                "doc": "The file_bundle that was onboarded.",
            }
        }
        return result

    @lru_cache(maxsize=1)
    def get_onboard_model_cls(self) -> Union[None, Type[OnboardDataModel]]:

        onboard_type: Union[str, None] = self.get_config_value("onboard_type")
        if not onboard_type:
            return None

        model_registry = ModelRegistry.instance()
        model_cls = model_registry.get_model_cls(onboard_type, OnboardDataModel)
        return model_cls  # type: ignore

    def find_matching_onboard_models(
        self, uri: str
    ) -> Mapping[Type[OnboardDataModel], Tuple[bool, str]]:

        model_registry = ModelRegistry.instance()
        onboard_models = model_registry.get_models_of_type(
            OnboardDataModel
        ).item_infos.values()

        result = {}
        onboard_model: Type[OnboardDataModel]
        for onboard_model in onboard_models:  # type: ignore

            python_cls: Type[OnboardDataModel] = onboard_model.python_class.get_class()  # type: ignore
            result[python_cls] = python_cls.accepts_bundle_uri(uri)

        return result

    def process(self, inputs: ValueMap, outputs: ValueMap):

        onboard_type = self.get_config_value("onboard_type")

        source: str = inputs.get_value_data("source")

        if not onboard_type:

            user_input_onboard_type = inputs.get_value_data("onboard_type")
            if not user_input_onboard_type:
                model_clsses = self.find_matching_onboard_models(source)
                matches = [k for k, v in model_clsses.items() if v[0]]
                if not matches:
                    raise KiaraProcessingException(
                        msg=f"Can't onboard file from '{source}': no onboard models found that accept this source type."
                    )
                elif len(matches) > 1:
                    msg = "Valid onboarding types for this uri:\n\n"
                    for k, v in model_clsses.items():
                        if not v[0]:
                            continue
                        msg += f"  - {k._kiara_model_id}: {v[1]}\n"
                    raise KiaraProcessingException(
                        msg=f"Can't onboard file from '{source}': multiple onboard models found that accept this source type.\n\n{msg}"
                    )

                model_cls: Type[OnboardDataModel] = matches[0]
            else:
                full_onboard_type = (
                    f"{ONBOARDING_MODEL_NAME_PREFIX}{user_input_onboard_type}"
                )
                model_registry = ModelRegistry.instance()
                model_cls = model_registry.get_model_cls(full_onboard_type, OnboardDataModel)  # type: ignore
                valid, msg = model_cls.accepts_bundle_uri(source)
                if not valid:
                    raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{model_cls._kiara_model_id}': {msg}")  # type: ignore
        else:
            model_cls = self.get_onboard_model_cls()  # type: ignore
            if not model_cls:
                raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{onboard_type}': no onboard model found with this name.")  # type: ignore
            valid, msg = model_cls.accepts_bundle_uri(source)
            if not valid:
                raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{model_cls._kiara_model_id}': {msg}")  # type: ignore

        if not model_cls.get_config_fields():
            model = model_cls()
        else:
            raise NotImplementedError()

        sub_path = self.get_config_value("sub_path")
        if sub_path is None:
            sub_path = inputs.get_value_data("sub_path")

        include = self.get_config_value("include_file_types")
        if include is None:
            include = inputs.get_value_data("include_file_types")
        exclude = self.get_config_value("exclude_file_types")
        if exclude is None:
            exclude = inputs.get_value_data("exclude_file_types")

        import_config = FolderImportConfig(
            sub_path=sub_path, include_files=include, exclude_files=exclude
        )
        attach_metadata = self.get_config_value("attach_metadata")
        if attach_metadata is None:
            attach_metadata = inputs.get_value_data("attach_metadata")

        try:
            result: Union[None, KiaraFileBundle] = model.retrieve_bundle(
                uri=source, import_config=import_config, attach_metadata=attach_metadata
            )

            if not result:
                raise KiaraProcessingException(msg=f"Can't onboard file bundle from '{source}' using onboard type '{model_cls._kiara_model_id}': no result data retrieved. This is most likely a bug.")  # type: ignore

            if isinstance(result, str):
                result = KiaraFileBundle.import_folder(source=result)

        except NotImplementedError:
            result = None

        if not result:
            result_file = model.retrieve(
                uri=source, file_name=None, attach_metadata=attach_metadata
            )
            if not result_file:
                raise KiaraProcessingException(msg=f"Can't onboard file bundle from '{source}' using onboard type '{model_cls._kiara_model_id}': no result data retrieved. This is most likely a bug.")  # type: ignore

            if isinstance(result, str):
                imported_bundle_file = KiaraFile.load_file(result_file)  # type: ignore
            elif not isinstance(result_file, KiaraFile):
                raise KiaraProcessingException(
                    "Can't onboard file: onboard model returned data that is not a file. This is most likely a bug."
                )
            else:
                imported_bundle_file = result_file

            imported_bundle = KiaraFileBundle.from_archive_file(
                imported_bundle_file, import_config=import_config
            )
        else:
            imported_bundle = result

        outputs.set_value("file_bundle", imported_bundle)

Attributes

_config_cls = OnboardFileBundleConfig instance-attribute class-attribute

Functions

create_inputs_schema() -> ValueMapSchema
Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
def create_inputs_schema(
    self,
) -> ValueMapSchema:

    result = {
        "source": {
            "type": "string",
            "doc": "The source uri of the file to be onboarded.",
            "optional": False,
        }
    }

    if self.get_config_value("attach_metadata") is None:
        result["attach_metadata"] = {
            "type": "boolean",
            "doc": "Whether to attach onboarding metadata.",
            "default": True,
        }
    if self.get_config_value("sub_path") is None:
        result["sub_path"] = {
            "type": "string",
            "doc": "The sub path to use. If not specified, the root of the source folder will be used.",
            "optional": True,
        }
    if self.get_config_value("include_file_types") is None:
        result["include_file_types"] = {
            "type": "list",
            "doc": "A list of file extensions to include. If not specified, all file extensions are included.",
            "optional": True,
        }

    if self.get_config_value("exclude_file_types") is None:
        result["exclude_file_types"] = {
            "type": "list",
            "doc": "A list of file extensions to exclude. If not specified, no file extensions are excluded.",
            "optional": True,
        }

    onboard_model_cls = self.get_onboard_model_cls()
    if not onboard_model_cls:

        available = (
            ModelRegistry.instance()
            .get_models_of_type(OnboardDataModel)
            .item_infos.keys()
        )

        if not available:
            raise KiaraException(msg="No onboard models available. This is a bug.")

        idx = len(ONBOARDING_MODEL_NAME_PREFIX)
        allowed = sorted((x[idx:] for x in available))

        result["onboard_type"] = {
            "type": "string",
            "type_config": {"allowed_strings": allowed},
            "doc": "The type of onboarding to use. Allowed: {}".format(
                ", ".join(allowed)
            ),
            "optional": True,
        }
    elif onboard_model_cls.get_config_fields():
        result = {
            "onboard_config": {
                "type": "kiara_model",
                "type_config": {
                    "kiara_model_id": self.get_config_value("onboard_type"),
                },
            }
        }

    return result
create_outputs_schema() -> ValueMapSchema
Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
300
301
302
303
304
305
306
307
308
309
310
def create_outputs_schema(
    self,
) -> ValueMapSchema:

    result = {
        "file_bundle": {
            "type": "file_bundle",
            "doc": "The file_bundle that was onboarded.",
        }
    }
    return result
get_onboard_model_cls() -> Union[None, Type[OnboardDataModel]] cached
Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
312
313
314
315
316
317
318
319
320
321
@lru_cache(maxsize=1)
def get_onboard_model_cls(self) -> Union[None, Type[OnboardDataModel]]:

    onboard_type: Union[str, None] = self.get_config_value("onboard_type")
    if not onboard_type:
        return None

    model_registry = ModelRegistry.instance()
    model_cls = model_registry.get_model_cls(onboard_type, OnboardDataModel)
    return model_cls  # type: ignore
find_matching_onboard_models(uri: str) -> Mapping[Type[OnboardDataModel], Tuple[bool, str]]
Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
def find_matching_onboard_models(
    self, uri: str
) -> Mapping[Type[OnboardDataModel], Tuple[bool, str]]:

    model_registry = ModelRegistry.instance()
    onboard_models = model_registry.get_models_of_type(
        OnboardDataModel
    ).item_infos.values()

    result = {}
    onboard_model: Type[OnboardDataModel]
    for onboard_model in onboard_models:  # type: ignore

        python_cls: Type[OnboardDataModel] = onboard_model.python_class.get_class()  # type: ignore
        result[python_cls] = python_cls.accepts_bundle_uri(uri)

    return result
process(inputs: ValueMap, outputs: ValueMap)
Source code in /opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/site-packages/kiara_plugin/onboarding/modules/__init__.py
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
def process(self, inputs: ValueMap, outputs: ValueMap):

    onboard_type = self.get_config_value("onboard_type")

    source: str = inputs.get_value_data("source")

    if not onboard_type:

        user_input_onboard_type = inputs.get_value_data("onboard_type")
        if not user_input_onboard_type:
            model_clsses = self.find_matching_onboard_models(source)
            matches = [k for k, v in model_clsses.items() if v[0]]
            if not matches:
                raise KiaraProcessingException(
                    msg=f"Can't onboard file from '{source}': no onboard models found that accept this source type."
                )
            elif len(matches) > 1:
                msg = "Valid onboarding types for this uri:\n\n"
                for k, v in model_clsses.items():
                    if not v[0]:
                        continue
                    msg += f"  - {k._kiara_model_id}: {v[1]}\n"
                raise KiaraProcessingException(
                    msg=f"Can't onboard file from '{source}': multiple onboard models found that accept this source type.\n\n{msg}"
                )

            model_cls: Type[OnboardDataModel] = matches[0]
        else:
            full_onboard_type = (
                f"{ONBOARDING_MODEL_NAME_PREFIX}{user_input_onboard_type}"
            )
            model_registry = ModelRegistry.instance()
            model_cls = model_registry.get_model_cls(full_onboard_type, OnboardDataModel)  # type: ignore
            valid, msg = model_cls.accepts_bundle_uri(source)
            if not valid:
                raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{model_cls._kiara_model_id}': {msg}")  # type: ignore
    else:
        model_cls = self.get_onboard_model_cls()  # type: ignore
        if not model_cls:
            raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{onboard_type}': no onboard model found with this name.")  # type: ignore
        valid, msg = model_cls.accepts_bundle_uri(source)
        if not valid:
            raise KiaraProcessingException(msg=f"Can't onboard file from '{source}' using onboard type '{model_cls._kiara_model_id}': {msg}")  # type: ignore

    if not model_cls.get_config_fields():
        model = model_cls()
    else:
        raise NotImplementedError()

    sub_path = self.get_config_value("sub_path")
    if sub_path is None:
        sub_path = inputs.get_value_data("sub_path")

    include = self.get_config_value("include_file_types")
    if include is None:
        include = inputs.get_value_data("include_file_types")
    exclude = self.get_config_value("exclude_file_types")
    if exclude is None:
        exclude = inputs.get_value_data("exclude_file_types")

    import_config = FolderImportConfig(
        sub_path=sub_path, include_files=include, exclude_files=exclude
    )
    attach_metadata = self.get_config_value("attach_metadata")
    if attach_metadata is None:
        attach_metadata = inputs.get_value_data("attach_metadata")

    try:
        result: Union[None, KiaraFileBundle] = model.retrieve_bundle(
            uri=source, import_config=import_config, attach_metadata=attach_metadata
        )

        if not result:
            raise KiaraProcessingException(msg=f"Can't onboard file bundle from '{source}' using onboard type '{model_cls._kiara_model_id}': no result data retrieved. This is most likely a bug.")  # type: ignore

        if isinstance(result, str):
            result = KiaraFileBundle.import_folder(source=result)

    except NotImplementedError:
        result = None

    if not result:
        result_file = model.retrieve(
            uri=source, file_name=None, attach_metadata=attach_metadata
        )
        if not result_file:
            raise KiaraProcessingException(msg=f"Can't onboard file bundle from '{source}' using onboard type '{model_cls._kiara_model_id}': no result data retrieved. This is most likely a bug.")  # type: ignore

        if isinstance(result, str):
            imported_bundle_file = KiaraFile.load_file(result_file)  # type: ignore
        elif not isinstance(result_file, KiaraFile):
            raise KiaraProcessingException(
                "Can't onboard file: onboard model returned data that is not a file. This is most likely a bug."
            )
        else:
            imported_bundle_file = result_file

        imported_bundle = KiaraFileBundle.from_archive_file(
            imported_bundle_file, import_config=import_config
        )
    else:
        imported_bundle = result

    outputs.set_value("file_bundle", imported_bundle)