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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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 | 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_files") is None:
result["include_files"] = {
"type": "list",
"doc": "Include files that end with one of those tokens. If not specified, all file extensions are included.",
"optional": True,
}
if self.get_config_value("exclude_files") is None:
result["exclude_files"] = {
"type": "list",
"doc": "Exclude files that end with one of those tokens. If not specified, no file extensions are excluded.",
"optional": True,
}
if self.get_config_value("exclude_dirs") is None:
result["exclude_dirs"] = {
"type": "list",
"doc": "Exclude directories that end with one of those tokens. If not specified, no directories are excluded.",
"optional": True,
}
onboard_type: Union[str, None] = self.get_config_value("onboard_type")
if not onboard_type:
onboard_model_cls = None
else:
onboard_model_cls = get_onboard_model_cls(onboard_type)
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
def process(self, inputs: ValueMap, outputs: ValueMap):
onboard_type = self.get_config_value("onboard_type")
source: str = inputs.get_value_data("source")
if onboard_type:
user_input_onboard_type = inputs.get_value_data("onboard_type")
if not user_input_onboard_type:
onboard_type = (
f"{ONBOARDING_MODEL_NAME_PREFIX}{user_input_onboard_type}"
)
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_files")
if include is None:
_include = inputs.get_value_data("include_files")
if _include:
include = _include.list_data
exclude = self.get_config_value("exclude_files")
if exclude is None:
_exclude = inputs.get_value_data("exclude_files")
if _exclude:
exclude = _exclude.list_data
exclude_dirs = self.get_config_value("exclude_dirs")
if exclude_dirs is None:
_exclude_dirs = inputs.get_value_data("exclude_dirs")
if _exclude_dirs:
exclude_dirs = _exclude_dirs.list_data
import_config_data = {
"sub_path": sub_path,
}
if include:
import_config_data["include_files"] = include
if exclude:
import_config_data["exclude_files"] = exclude
if exclude_dirs:
import_config_data["exclude_dirs"] = exclude_dirs
import_config = FolderImportConfig(**import_config_data)
attach_metadata = self.get_config_value("attach_metadata")
if attach_metadata is None:
attach_metadata = inputs.get_value_data("attach_metadata")
imported_bundle = onboard_file_bundle(
source=source,
import_config=import_config,
onboard_type=onboard_type,
attach_metadata=attach_metadata,
)
outputs.set_value("file_bundle", imported_bundle)
|