@@ -237,10 +237,6 @@ def is_situation_handicap(individu: Population, period: Period) -> np.array:
237
237
return individu ('handicap' , period )
238
238
239
239
240
- def not_implemented_condition (_ : Population , __ : Period , condition : ParameterNodeAtInstant ) -> np .array :
241
- raise NotImplementedError (f'Condition in `{ condition .name } ` is not implemented' )
242
-
243
-
244
240
condition_table = {
245
241
'age' : is_age_eligible ,
246
242
'regions' : is_region_eligible ,
@@ -255,7 +251,6 @@ def not_implemented_condition(_: Population, __: Period, condition: ParameterNod
255
251
'communes' : is_commune_eligible ,
256
252
'epcis' : is_epci_eligible ,
257
253
'taux_incapacite' : is_taux_incapacite_eligible ,
258
- 'attached_to_institution' : not_implemented_condition ,
259
254
}
260
255
261
256
@@ -295,7 +290,7 @@ def build_condition_evaluator_list(conditions: 'list[dict]') -> 'list[ConditionE
295
290
for condition in conditions
296
291
]
297
292
except KeyError as e :
298
- raise KeyError (f"Condition `{ (e .args [0 ])} ` is unknown." )
293
+ raise NotImplementedError (f"Condition `{ (e .args [0 ])} ` is unknown." )
299
294
300
295
return evaluators
301
296
@@ -304,7 +299,7 @@ def build_profil_evaluator(profil: dict) -> ProfileEvaluator:
304
299
try :
305
300
predicate = profil_table [profil ['type' ]]
306
301
except KeyError :
307
- raise KeyError (f"Profil `{ profil ['type' ]} ` is unknown." )
302
+ raise NotImplementedError (f"Profil `{ profil ['type' ]} ` is unknown." )
308
303
309
304
conditions = profil .get ('conditions' , [])
310
305
@@ -379,22 +374,23 @@ def formula(individu: Population, period: Period, parameters):
379
374
})
380
375
381
376
382
- root = '.'
383
- benefit_path = 'test_data/benefits'
384
- current_path = path .join (root , benefit_path )
377
+ default_benefit_path = 'test_data/benefits'
378
+ default_institutions_path = 'test_data/institutions'
385
379
386
380
387
381
class aides_jeunes_reform_dynamic (reforms .Reform ):
388
- def __init__ (self , baseline , benefits_folder_path = current_path ):
382
+ def __init__ (self , baseline , benefits_folder_path = default_benefit_path , institutions_folder_path = default_institutions_path ):
389
383
self .benefits_folder_path = getenv ('DYNAMIC_BENEFIT_FOLDER' , benefits_folder_path )
384
+ self .institutions_folder_path = getenv ('DYNAMIC_INSTITUTION_FOLDER' , institutions_folder_path )
390
385
super ().__init__ (baseline )
391
386
392
387
def apply (self ):
393
388
try :
394
- benefit_files_paths : 'list[str]' = self ._extract_benefits_paths (self .benefits_folder_path )
389
+ benefit_files_paths : 'list[str]' = self ._extract_paths (self .benefits_folder_path )
395
390
396
391
for benefit_path in benefit_files_paths :
397
392
benefit : dict = self ._extract_benefit_file_content (benefit_path )
393
+
398
394
benefit_parameters : ParameterNode = convert_benefit_conditions_to_parameters (benefit )
399
395
self ._add_parameters_into_current_tax_and_benefits_system (benefit_parameters )
400
396
@@ -406,21 +402,48 @@ def apply(self):
406
402
raise
407
403
408
404
def _extract_benefit_file_content (self , benefit_path : str ):
405
+ def _convert_institution_to_condition (benefit : dict ) -> dict :
406
+ if {'type' : 'attached_to_institution' } in benefit ['conditions_generales' ]:
407
+ conditions_generales_without_attached_institution = [
408
+ condition for condition
409
+ in benefit ['conditions_generales' ]
410
+ if condition != {'type' : 'attached_to_institution' }]
411
+
412
+ with open (f'{ self .institutions_folder_path } /{ benefit ["institution" ]} .yml' ) as file :
413
+ institution : dict = yaml .safe_load (file )
414
+
415
+ institution_type = institution ['type' ]
416
+ if institution_type in ['msa' , 'caf' ]:
417
+ condition_type = 'departements'
418
+ condition_value = institution ['departments' ]
419
+ elif institution_type == 'epci' :
420
+ condition_type = 'epcis'
421
+ condition_value = [institution ['code_siren' ]]
422
+ else :
423
+ condition_type = f"{ institution_type } s"
424
+ condition_value = [institution ['code_insee' ]]
425
+
426
+ conditions_generales_without_attached_institution .append ({'type' : condition_type , 'values' : condition_value })
427
+
428
+ benefit = {** benefit , 'conditions_generales' : conditions_generales_without_attached_institution }
429
+ return benefit
430
+
409
431
def _slug_from_path (path : str ):
410
432
return path .split ('/' )[- 1 ].replace ('-' , '_' ).split ('.' )[0 ]
411
433
412
- benefit : dict = yaml .safe_load (open (benefit_path ))
413
- benefit ['slug' ] = _slug_from_path (benefit_path )
414
-
434
+ with open (benefit_path ) as file :
435
+ benefit : dict = yaml .safe_load (file )
436
+ benefit ['slug' ] = _slug_from_path (benefit_path )
437
+ benefit = _convert_institution_to_condition (benefit )
415
438
return benefit
416
439
417
- def _extract_benefits_paths (self , benefits_folder : str ) -> 'list[str]' :
440
+ def _extract_paths (self , folder : str ) -> 'list[str]' :
418
441
def _isYAMLfile (path : str ):
419
442
return str (path ).endswith ('.yml' ) or str (path ).endswith ('.yaml' )
420
443
421
444
files : 'list[str]' = [
422
445
str (benefit )
423
- for benefit in Path (benefits_folder ).iterdir ()
446
+ for benefit in Path (folder ).iterdir ()
424
447
if _isYAMLfile (benefit )
425
448
]
426
449
0 commit comments