Commit 24087376 authored by Christophe Benz's avatar Christophe Benz

Restrict formulas to batch

parent 3798fe47
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -15,7 +15,7 @@ import pprint
import textwrap
from m_language_parser.unloop_helpers import iter_unlooped_nodes
from toolz import concatv, interpose
from toolz import concatv, interpose, mapcat
from . import core
......@@ -204,11 +204,8 @@ def visit_product_expression(node):
def visit_regle(node):
return map(
lambda node1: {
'applications': node['applications'],
'sources': visit_node(node1) if node1['type'] == 'pour_formula' else [visit_node(node1)],
},
return mapcat(
lambda node1: visit_node(node1) if node1['type'] == 'pour_formula' else [visit_node(node1)],
node['formulas'],
)
......
......@@ -11,9 +11,9 @@ import sys
from toolz.curried import map, pipe, unique
from calculateur_impots import core
from calculateur_impots import core, formulas_helpers
from calculateur_impots.generated import formulas, verifs
from calculateur_impots.generated.variables_definitions import definition_by_variable_name
# from calculateur_impots.generated.variables_definitions import definition_by_variable_name
# Globals
......@@ -107,30 +107,31 @@ def main():
saisie_variables['V_ANREV'] = 2014
# src/tgvH.m:14339:V_0DA : saisie famille classe = 0 priorite = 20 categorie_TL = 10 nat_code = 0 restituee alias 0DA : "Annee de naissance du declarant" type DATE_AAAA ; # noqa
# saisie_variables['V_0DA'] = 1980
saisie_variables['V_0DA'] = 1980
# src/tgvH.m:14317:V_0AC : saisie famille classe = 0 priorite = 20 categorie_TL = 10 nat_code = 0 alias 0AC : "Case a cocher : situation de famille Celibataire" type BOOLEEN ; # noqa
# saisie_variables['V_0AC'] = 1
# Avoid division by 0
saisie_variables['CARPENBAV'] = 1
saisie_variables['CARPENBAC'] = 1
# saisie_variables['CARPENBAV'] = 1
# saisie_variables['CARPENBAC'] = 1
if args.saisie_variables is not None:
saisie_variables.update(iter_saisie_variables(args.saisie_variables))
log.debug('saisie_variables: {}'.format(saisie_variables))
base_variables_value_by_name = {} # Override base variables values
base_variables = {
variable_name: base_variables_value_by_name.get(variable_name, 0)
for variable_name, variable_definition in definition_by_variable_name.items()
if core.is_base_variable(variable_name)
}
errors = verifs.get_errors(
base_variables=base_variables,
saisie_variables=saisie_variables,
)
# base_variables_value_by_name = {} # Override base variables values
# base_variables = {
# variable_name: base_variables_value_by_name.get(variable_name, 0)
# for variable_name, variable_definition in definition_by_variable_name.items()
# if core.is_base_variable(variable_name)
# }
# errors = verifs.get_errors(
# base_variables=base_variables,
# saisie_variables=saisie_variables,
# )
errors = None
if errors is not None:
errors_definitions = load_errors_definitions()
definition_by_error_name = pipe(errors_definitions, map(lambda d: (d['name'], d)), dict)
......@@ -145,7 +146,8 @@ def main():
))
formulas_functions = formulas.get_formulas(
base_variables=base_variables,
# base_variables=base_variables,
base_variables=None,
saisie_variables=saisie_variables,
)
......
......@@ -96,17 +96,19 @@ def iter_ast_json_file_names(filenames, excluded_filenames=None):
def load_regles_file(json_file_name):
regles_nodes = read_ast_json_file(json_file_name)
return mapcat(python_source_visitors.visit_node, regles_nodes)
return pipe(
read_ast_json_file(json_file_name),
filter(lambda node: 'batch' in node['applications']),
mapcat(python_source_visitors.visit_node),
)
def load_verifs_file(json_file_name):
verifs_nodes = read_ast_json_file(json_file_name)
verifs_nodes = filter(
lambda node: 'batch' in node['applications'],
verifs_nodes,
return pipe(
read_ast_json_file(json_file_name),
filter(lambda node: 'batch' in node['applications']),
map(python_source_visitors.visit_node),
)
return map(python_source_visitors.visit_node, verifs_nodes)
# Main
......@@ -154,9 +156,8 @@ def main():
# Transpile verification functions
verif_sources = list(
mapcat(load_verifs_file, iter_ast_json_file_names(filenames=['coc*.json', 'coi*.json'])),
mapcat(load_verifs_file, iter_ast_json_file_names(filenames=['coc*.json', 'coi*.json']))
)
verifs_source = """\
from . import formulas
from ..formulas_helpers import *
......@@ -178,32 +179,10 @@ def get_errors(base_variables, saisie_variables):
# Transpile formulas
regles_sources_dicts = list(mapcat(
source_by_formula_name = dict(list(mapcat(
load_regles_file,
iter_ast_json_file_names(filenames=['chap-*.json', 'res-ser*.json']),
))
def iter_formula_name_and_source_pairs(preferred_application='batch'):
visited_applications_by_variable_name = {}
for regle_sources_dicts in regles_sources_dicts:
applications = regle_sources_dicts['applications']
assert applications is not None
for variable_name, source in regle_sources_dicts['sources']:
visited_applications = visited_applications_by_variable_name.get(variable_name)
if visited_applications is not None:
is_double_defined_in_preferred_application = preferred_application in applications and \
preferred_application in visited_applications
assert not is_double_defined_in_preferred_application, (variable_name, visited_applications)
log.debug(
'Variable "{}" already visited and had another application, '
'but this one of "{}" is preferred => keep the source ({}) '
'and the applications({}) of this one.'.format(
variable_name, preferred_application, source, applications))
visited_applications_by_variable_name[variable_name] = applications
if preferred_application in applications or visited_applications is None:
yield variable_name, source
source_by_formula_name = dict(iter_formula_name_and_source_pairs())
)))
def get_formula_source(formula_name):
sanitized_name = core.sanitized_variable_name(formula_name)
......@@ -212,7 +191,7 @@ def get_errors(base_variables, saisie_variables):
'corrective' not in definition_by_variable_name.get(formula_name, {}).get('regle_tags', []) \
else 'def {}(): return 0\n'.format(sanitized_name)
def should_appear_in_formulas_file(variable_name):
def should_be_in_formulas_file(variable_name):
return not core.is_saisie_variable(variable_name) and not core.is_constant(variable_name)
dependencies_by_formula_name = formulas_helpers.load_dependencies_by_formula_name()
......@@ -222,12 +201,12 @@ def get_errors(base_variables, saisie_variables):
concat(dependencies_by_formula_name.values()),
definition_by_variable_name.keys(),
),
filter(should_appear_in_formulas_file),
filter(should_be_in_formulas_file),
set,
)
write_source_file(
file_name='formulas.py',
source=formulas_file_source(formulas_sources=map(get_formula_source, formula_names)),
source=formulas_file_source(map(get_formula_source, formula_names)),
)
return 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment