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
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 | class KiaraAPIWrap(object):
def __init__(
self,
config: Union[str, None],
context: Union[str, None],
pipelines: Union[None, Iterable[str]] = None,
ensure_plugins: Union[str, Iterable[str], None] = None,
exit_process: bool = True,
):
if not context:
context = os.environ.get("KIARA_CONTEXT", None)
self._config: Union[str, None] = config
self._context: Union[str, None] = context
self._pipelines: Union[None, Iterable[str]] = pipelines
self._ensure_plugins: Union[str, Iterable[str], None] = ensure_plugins
self._kiara_config: Union["KiaraConfig", None] = None
self._api: Union[KiaraAPI, None] = None
self._reload_process_if_plugins_installed = True
self._items: Dict[str, Any] = {}
self._exit_process: bool = exit_process
@property
def kiara_context_name(self) -> str:
if not self._context:
self._context = self.kiara_config.default_context
return self._context
@property
def exit_process(self) -> bool:
return self._exit_process
@exit_process.setter
def exit_process(self, exit_process: bool):
self._exit_process = exit_process
def exit(self, msg: Union[None, Any] = None, exit_code: int = 1):
if self._exit_process:
if msg:
terminal_print(msg)
sys.exit(exit_code)
else:
if not msg:
msg = "An error occured."
raise KiaraException(str(msg))
@property
def current_kiara_context_id(self) -> uuid.UUID:
return self.kiara_api.context.id
@property
def kiara(self) -> "Kiara":
return self.kiara_api.context
@property
def kiara_config(self) -> "KiaraConfig":
if self._kiara_config is not None:
return self._kiara_config
from kiara.context.config import KiaraConfig
# kiara_config: Optional[KiaraConfig] = None
exists = False
create = False
if self._config:
config_path = Path(self._config)
if config_path.exists():
if config_path.is_file():
config_file_path = config_path
exists = True
else:
config_file_path = config_path / KIARA_CONFIG_FILE_NAME
if config_file_path.exists():
exists = True
else:
config_path.parent.mkdir(parents=True, exist_ok=True)
config_file_path = config_path
else:
config_file_path = Path(KIARA_MAIN_CONFIG_FILE)
if not config_file_path.exists():
create = True
exists = False
else:
exists = True
if not exists:
if not create:
from kiara.utils.cli import terminal_print
terminal_print()
terminal_print(
f"Can't create kiara context, specified config file does not exist: {self._config}."
)
sys.exit(1)
kiara_config = KiaraConfig()
kiara_config.save(config_file_path)
else:
kiara_config = KiaraConfig.load_from_file(config_file_path)
self._kiara_config = kiara_config
return self._kiara_config
def lock_file(self, context: str) -> str:
"""The path to the lock file for this context."""
return "asdf"
@property
def kiara_api(self) -> "KiaraAPI":
if self._api is not None:
return self._api
from kiara.utils import log_message
context = self._context
if not context:
context = self.kiara_config.default_context
from kiara.interfaces.python_api import KiaraAPI
api = KiaraAPI(kiara_config=self.kiara_config)
if self._ensure_plugins:
installed = api.ensure_plugin_packages(self._ensure_plugins, update=False)
if installed and self._reload_process_if_plugins_installed:
log_message(
"replacing.process",
reason="reloading this process, in order to pick up new plugin packages",
)
os.execvp(sys.executable, (sys.executable,) + tuple(sys.argv)) # noqa
import fasteners
fasteners.InterProcessReaderWriterLock(self.lock_file(context))
api.set_active_context(context, create=True)
if self._pipelines:
for pipeline in self._pipelines:
ops = api.context.operation_registry.register_pipelines(pipeline)
for op_id in ops.keys():
log_message("register.pipeline", operation_id=op_id)
self._api = api
return self._api
def add_item(self, key: str, item: Any):
self._items[key] = item
def get_item(self, key: str) -> Any:
if key not in self._items.keys():
raise ValueError(f"No item with key '{key}'")
return self._items[key]
|