8
8
import os
9
9
import uuid
10
10
from pathlib import Path
11
- from typing import TYPE_CHECKING , Any , Dict , Iterable , List , Mapping , Union
11
+ from typing import TYPE_CHECKING , Any , Dict , Iterable , List , Mapping , Type , Union
12
12
13
13
import structlog
14
14
from pydantic import BaseModel , ConfigDict , field_validator , model_validator
32
32
from kiara .exceptions import KiaraException
33
33
from kiara .registries .environment import EnvironmentRegistry
34
34
from kiara .registries .ids import ID_REGISTRY
35
+ from kiara .utils import log_message
35
36
from kiara .utils .files import get_data_from_file
36
37
37
38
if TYPE_CHECKING :
@@ -118,6 +119,30 @@ class KiaraSettings(BaseSettings):
118
119
KIARA_SETTINGS = KiaraSettings ()
119
120
120
121
122
+ def create_default_store (
123
+ store_id : str , store_type : str , stores_base_path : str
124
+ ) -> KiaraArchiveConfig :
125
+
126
+ env_registry = EnvironmentRegistry .instance ()
127
+ kiara_types : KiaraTypesRuntimeEnvironment = env_registry .environments ["kiara_types" ] # type: ignore
128
+ available_archives = kiara_types .archive_types
129
+
130
+ assert store_type in available_archives .item_infos .keys ()
131
+
132
+ from kiara .models .archives import ArchiveTypeInfo
133
+
134
+ archive_info : ArchiveTypeInfo = available_archives .item_infos [store_type ]
135
+ cls : Type [BaseArchive ] = archive_info .python_class .get_class () # type: ignore
136
+ config = cls .create_new_config (store_id = store_id , stores_base_path = stores_base_path )
137
+
138
+ data_store = KiaraArchiveConfig (
139
+ archive_id = store_id ,
140
+ archive_type = store_type ,
141
+ config = config .model_dump (),
142
+ )
143
+ return data_store
144
+
145
+
121
146
class KiaraConfig (BaseSettings ):
122
147
model_config = SettingsConfigDict (
123
148
env_prefix = "kiara_" , extra = "forbid" , use_enum_values = True
@@ -309,121 +334,71 @@ def get_context_config(
309
334
310
335
def _validate_context (self , context_config : KiaraContextConfig ) -> bool :
311
336
312
- env_registry = EnvironmentRegistry .instance ()
313
- from kiara .models .runtime_environment .kiara import KiaraTypesRuntimeEnvironment
337
+ changed = False
314
338
315
- kiara_types : KiaraTypesRuntimeEnvironment = env_registry .environments ["kiara_types" ] # type: ignore
316
- available_archives = kiara_types .archive_types
339
+ default_store_id = context_config .context_id
317
340
318
- changed = False
319
341
if DEFAULT_DATA_STORE_MARKER not in context_config .archives .keys ():
320
342
# data_store_type = "filesystem_data_store"
321
343
# TODO: change that back
322
344
data_store_type = "sqlite_data_store"
323
- assert data_store_type in available_archives .item_infos .keys ()
324
-
325
- data_store_id = ID_REGISTRY .generate (comment = "default data store id" )
326
- data_archive_config = {
327
- "archive_path" : os .path .abspath (
328
- os .path .join (
329
- self .stores_base_path , data_store_type , str (data_store_id )
330
- )
331
- )
332
- }
333
- data_store = KiaraArchiveConfig (
334
- archive_id = str (data_store_id ),
335
- archive_type = data_store_type ,
336
- config = data_archive_config ,
345
+
346
+ data_store = create_default_store (
347
+ store_id = default_store_id ,
348
+ store_type = data_store_type ,
349
+ stores_base_path = self .stores_base_path ,
337
350
)
338
351
context_config .archives [DEFAULT_DATA_STORE_MARKER ] = data_store
339
-
340
352
changed = True
341
353
342
354
if DEFAULT_JOB_STORE_MARKER not in context_config .archives .keys ():
343
- job_store_type = "filesystem_job_store"
344
- assert job_store_type in available_archives .item_infos .keys ()
345
-
346
- job_store_id = ID_REGISTRY .generate (comment = "default job store id" )
347
- job_archive_config = {
348
- "archive_path" : os .path .abspath (
349
- os .path .join (
350
- self .stores_base_path , job_store_type , str (job_store_id )
351
- )
352
- )
353
- }
354
- job_store = KiaraArchiveConfig (
355
- archive_id = str (job_store_id ),
356
- archive_type = job_store_type ,
357
- config = job_archive_config ,
355
+
356
+ # job_store_type = "filesystem_job_store"
357
+ job_store_type = "sqlite_job_store"
358
+
359
+ job_store = create_default_store (
360
+ store_id = default_store_id ,
361
+ store_type = job_store_type ,
362
+ stores_base_path = self .stores_base_path ,
358
363
)
359
- context_config .archives [DEFAULT_JOB_STORE_MARKER ] = job_store
360
364
365
+ context_config .archives [DEFAULT_JOB_STORE_MARKER ] = job_store
361
366
changed = True
362
367
363
368
if DEFAULT_ALIAS_STORE_MARKER not in context_config .archives .keys ():
364
369
365
370
alias_store_type = "filesystem_alias_store"
366
- assert alias_store_type in available_archives .item_infos .keys ()
367
- alias_store_id = ID_REGISTRY .generate (comment = "default alias store id" )
368
- alias_store_config = {
369
- "archive_path" : os .path .abspath (
370
- os .path .join (
371
- self .stores_base_path , alias_store_type , str (alias_store_id )
372
- )
373
- )
374
- }
375
- alias_store = KiaraArchiveConfig (
376
- archive_id = str (alias_store_id ),
377
- archive_type = alias_store_type ,
378
- config = alias_store_config ,
371
+ alias_store_type = "sqlite_alias_store"
372
+
373
+ alias_store = create_default_store (
374
+ store_id = default_store_id ,
375
+ store_type = alias_store_type ,
376
+ stores_base_path = self .stores_base_path ,
379
377
)
380
378
context_config .archives [DEFAULT_ALIAS_STORE_MARKER ] = alias_store
381
-
382
379
changed = True
383
380
384
381
if DEFAULT_WORKFLOW_STORE_MARKER not in context_config .archives .keys ():
385
382
386
383
workflow_store_type = "filesystem_workflow_store"
387
- assert workflow_store_type in available_archives .item_infos .keys ()
388
- workflow_store_id = ID_REGISTRY .generate (
389
- comment = "default workflow store id"
390
- )
391
- workflow_store_config = {
392
- "archive_path" : os .path .abspath (
393
- os .path .join (
394
- self .stores_base_path ,
395
- workflow_store_type ,
396
- str (workflow_store_id ),
397
- )
398
- )
399
- }
400
- workflow_store = KiaraArchiveConfig (
401
- archive_id = str (workflow_store_id ),
402
- archive_type = workflow_store_type ,
403
- config = workflow_store_config ,
384
+ workflow_store = create_default_store (
385
+ store_id = default_store_id ,
386
+ store_type = workflow_store_type ,
387
+ stores_base_path = self .stores_base_path ,
404
388
)
405
389
context_config .archives [DEFAULT_WORKFLOW_STORE_MARKER ] = workflow_store
406
-
407
390
changed = True
408
391
409
392
if METADATA_DESTINY_STORE_MARKER not in context_config .archives .keys ():
393
+
410
394
destiny_store_type = "filesystem_destiny_store"
411
- assert destiny_store_type in available_archives .item_infos .keys ()
412
- destiny_store_id = ID_REGISTRY .generate (comment = "default destiny store id" )
413
- destiny_store_config = {
414
- "archive_path" : os .path .abspath (
415
- os .path .join (
416
- self .stores_base_path , destiny_store_type , str (destiny_store_id )
417
- )
418
- )
419
- }
420
- destiny_store = KiaraArchiveConfig (
421
- archive_id = str (destiny_store_id ),
422
- archive_type = destiny_store_type ,
423
- config = destiny_store_config ,
395
+
396
+ destiny_store = create_default_store (
397
+ store_id = default_store_id ,
398
+ store_type = destiny_store_type ,
399
+ stores_base_path = self .stores_base_path ,
424
400
)
425
401
context_config .archives [METADATA_DESTINY_STORE_MARKER ] = destiny_store
426
-
427
402
changed = True
428
403
429
404
return changed
@@ -547,7 +522,7 @@ def save(self, path: Union[Path, None] = None):
547
522
548
523
def delete (
549
524
self , context_name : Union [str , None ] = None , dry_run : bool = True
550
- ) -> "ContextInfo" :
525
+ ) -> Union [ "ContextInfo" , None ] :
551
526
552
527
if context_name is None :
553
528
context_name = self .default_context
@@ -558,20 +533,27 @@ def delete(
558
533
context_config = self .get_context_config (
559
534
context_name = context_name , auto_generate = False
560
535
)
561
- kiara = Kiara (config = context_config , runtime_config = self .runtime_config )
562
536
563
- context_summary = ContextInfo .create_from_context (
564
- kiara = kiara , context_name = context_name
565
- )
537
+ context_summary = None
538
+
539
+ try :
540
+ kiara = Kiara (config = context_config , runtime_config = self .runtime_config )
541
+
542
+ context_summary = ContextInfo .create_from_context (
543
+ kiara = kiara , context_name = context_name
544
+ )
566
545
567
- if dry_run :
568
- return context_summary
546
+ if dry_run :
547
+ return context_summary
569
548
570
- for archive in kiara .get_all_archives ().keys ():
571
- archive .delete_archive (archive_id = archive .archive_id )
549
+ for archive in kiara .get_all_archives ().keys ():
550
+ archive .delete_archive (archive_id = archive .archive_id )
551
+ except Exception as e :
552
+ log_message ("delete.context.error" , context_name = context_name , error = e )
572
553
573
- if context_config ._context_config_path is not None :
574
- os .unlink (context_config ._context_config_path )
554
+ if not dry_run :
555
+ if context_config ._context_config_path is not None :
556
+ os .unlink (context_config ._context_config_path )
575
557
576
558
return context_summary
577
559
0 commit comments