Skip to content

Commit aca9a0d

Browse files
Reform dynamique : ajoute la gestion de la condition attached_to_institution (#191)
1 parent cca352c commit aca9a0d

14 files changed

+151
-22
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# Changelog
2+
## [6.8.0] - 2023-11-22
3+
4+
_Pour les changements détaillés et les discussions associées, référencez la pull request [#191](https://github.com/openfisca/openfisca-france-local/pull/191)
5+
6+
### Added
7+
8+
- Ajoute la gestion de la condition attaches_to_institution dans la `aides_jeunes_reform_dynamic`
29

310
## [6.7.2] - 2023-11-23
411

openfisca_france_local/aides_jeunes_reform.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,6 @@ def is_situation_handicap(individu: Population, period: Period) -> np.array:
237237
return individu('handicap', period)
238238

239239

240-
def not_implemented_condition(_: Population, __: Period, condition: ParameterNodeAtInstant) -> np.array:
241-
raise NotImplementedError(f'Condition in `{condition.name}` is not implemented')
242-
243-
244240
condition_table = {
245241
'age': is_age_eligible,
246242
'regions': is_region_eligible,
@@ -255,7 +251,6 @@ def not_implemented_condition(_: Population, __: Period, condition: ParameterNod
255251
'communes': is_commune_eligible,
256252
'epcis': is_epci_eligible,
257253
'taux_incapacite': is_taux_incapacite_eligible,
258-
'attached_to_institution': not_implemented_condition,
259254
}
260255

261256

@@ -295,7 +290,7 @@ def build_condition_evaluator_list(conditions: 'list[dict]') -> 'list[ConditionE
295290
for condition in conditions
296291
]
297292
except KeyError as e:
298-
raise KeyError(f"Condition `{(e.args[0])}` is unknown.")
293+
raise NotImplementedError(f"Condition `{(e.args[0])}` is unknown.")
299294

300295
return evaluators
301296

@@ -304,7 +299,7 @@ def build_profil_evaluator(profil: dict) -> ProfileEvaluator:
304299
try:
305300
predicate = profil_table[profil['type']]
306301
except KeyError:
307-
raise KeyError(f"Profil `{profil['type']}` is unknown.")
302+
raise NotImplementedError(f"Profil `{profil['type']}` is unknown.")
308303

309304
conditions = profil.get('conditions', [])
310305

@@ -379,22 +374,23 @@ def formula(individu: Population, period: Period, parameters):
379374
})
380375

381376

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'
385379

386380

387381
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):
389383
self.benefits_folder_path = getenv('DYNAMIC_BENEFIT_FOLDER', benefits_folder_path)
384+
self.institutions_folder_path = getenv('DYNAMIC_INSTITUTION_FOLDER', institutions_folder_path)
390385
super().__init__(baseline)
391386

392387
def apply(self):
393388
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)
395390

396391
for benefit_path in benefit_files_paths:
397392
benefit: dict = self._extract_benefit_file_content(benefit_path)
393+
398394
benefit_parameters: ParameterNode = convert_benefit_conditions_to_parameters(benefit)
399395
self._add_parameters_into_current_tax_and_benefits_system(benefit_parameters)
400396

@@ -406,21 +402,48 @@ def apply(self):
406402
raise
407403

408404
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+
409431
def _slug_from_path(path: str):
410432
return path.split('/')[-1].replace('-', '_').split('.')[0]
411433

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)
415438
return benefit
416439

417-
def _extract_benefits_paths(self, benefits_folder: str) -> 'list[str]':
440+
def _extract_paths(self, folder: str) -> 'list[str]':
418441
def _isYAMLfile(path: str):
419442
return str(path).endswith('.yml') or str(path).endswith('.yaml')
420443

421444
files: 'list[str]' = [
422445
str(benefit)
423-
for benefit in Path(benefits_folder).iterdir()
446+
for benefit in Path(folder).iterdir()
424447
if _isYAMLfile(benefit)
425448
]
426449

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='OpenFisca-France-Local',
6-
version='6.7.2',
6+
version='6.8.0',
77
author='OpenFisca Team',
88
author_email='[email protected]',
99
classifiers=[
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
label: Test condition attached_to_institutions caf
2+
institution: institution_caf
3+
prefix: l’
4+
conditions_generales:
5+
- type: attached_to_institution
6+
type: float
7+
unit:
8+
periodicite: mensuelle
9+
montant: 19
10+
floorAt: 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
label: Test condition attached_to_institutions communes
2+
institution: institution_commune
3+
prefix: l’
4+
conditions_generales:
5+
- type: attached_to_institution
6+
type: float
7+
unit:
8+
periodicite: mensuelle
9+
montant: 16.8
10+
floorAt: 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
label: Test condition attached_to_institutions départements
2+
institution: institution_departement
3+
prefix: l’
4+
conditions_generales:
5+
- type: attached_to_institution
6+
type: float
7+
unit:
8+
periodicite: mensuelle
9+
montant: 17.8
10+
floorAt: 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
label: Test condition attached_to_institutions epci
2+
institution: institution_epci
3+
prefix: l’
4+
conditions_generales:
5+
- type: attached_to_institution
6+
type: float
7+
unit:
8+
periodicite: mensuelle
9+
montant: 18
10+
floorAt: 1

test_data/benefits/test_profil_beneficiaire_rsa.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
label: Aide au permis de conduire
2-
institution: intercommunalite_toulon_provence_mediterranee
32
prefix: l’
43
conditions_generales:
54
- type: regions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: CAF de l'Ain
2+
imgSrc: img/institutions/logo_caf_ain.png
3+
type: caf
4+
code_siren: "779311224"
5+
departments:
6+
- "01"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: Ville de Bar-le-Duc
2+
imgSrc: img/institutions/logo_ville_bar_le_duc.png
3+
prefix: de la
4+
type: commune
5+
code_insee: "55029"
6+
code_siren: "215500299"

0 commit comments

Comments
 (0)