Commit 726e2fff authored by Christophe Benz's avatar Christophe Benz

Update transpiler

parent c6404867
......@@ -21,6 +21,10 @@ arr = round
inf = math.floor
def interval(first, last):
return range(first, last + 1)
def null(value):
return int(value == 0)
......
This diff is collapsed.
This diff is collapsed.
......@@ -247,7 +247,10 @@ def visit_variable_const(node):
def visit_verif(node):
return '# verif {name}\n{assertions}\n'.format(
assertions='\n'.join(
'assert not ({expression}), get_error({error_name!r})'.format(
"""\
if {expression}:
errors.append({error_name!r})
""".format(
error_name=condition_node['error_name'],
expression=visit_node(condition_node['expression']),
)
......
......@@ -3,16 +3,17 @@
import argparse
import json
import logging
import os
import pkg_resources
import sys
from toolz.curried import map
from toolz.curried import map, pipe, unique
from calculateur_impots import core
from calculateur_impots.generated import formulas
from calculateur_impots.generated import formulas, verifs
from calculateur_impots.generated.variables_definitions import definition_by_variable_name
from calculateur_impots.generated.verif_regles import verif_regles
# Globals
......@@ -42,6 +43,15 @@ def iter_saisie_variables(values):
yield variable_name, variable_value
def load_errors_definitions():
m_language_parser_dir_path = pkg_resources.get_distribution('m_language_parser').location
errors_definitions_file_path = os.path.join(m_language_parser_dir_path, 'json', 'ast', 'errH.json')
with open(errors_definitions_file_path) as errors_definitions_file:
errors_definitions_str = errors_definitions_file.read()
errors_definitions = json.loads(errors_definitions_str)
return errors_definitions
def main():
global args, parser
parser = argparse.ArgumentParser(description=__doc__)
......@@ -91,13 +101,13 @@ def main():
saisie_variables['V_IND_TRAIT'] = 0
# src/tgvH.m:14211:VALREGCO : saisie revenu classe = 0 priorite = 20 categorie_TL = 10 cotsoc = 5 ind_abat = 0 acompte = 1 avfisc = 0 rapcat = 13 sanction = 0 nat_code = 1 alias 8ZA : "Regime d'imposition des non residents - Valeur 1, 2 ou 4" ; # noqa
saisie_variables['VALREGCO'] = 1
# saisie_variables['VALREGCO'] = 1
# src/tgvH.m:14404:V_ANREV : saisie contexte classe = 0 priorite = 10 categorie_TL = 20 alias ANREV : "Annee des revenus" type REEL ; # noqa
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
......@@ -117,10 +127,22 @@ def main():
if core.is_base_variable(variable_name)
}
# verif_regles(
# base_variables=base_variables,
# saisie_variables=saisie_variables,
# )
errors = verifs.get_errors(
base_variables=base_variables,
saisie_variables=saisie_variables,
)
if errors is not None:
errors_definitions = load_errors_definitions()
definition_by_error_name = pipe(errors_definitions, map(lambda d: (d['name'], d)), dict)
print('errors: {}'.format(
json.dumps(
[
(error, definition_by_error_name.get(error, {}).get('description', 'No description found!'))
for error in unique(errors) # Keep order
],
indent=4,
),
))
formulas_functions = formulas.get_formulas(
base_variables=base_variables,
......@@ -129,22 +151,10 @@ def main():
for calculee_variable_name in args.calculee_variables:
sanitized_calculee_variable = core.sanitized_variable_name(calculee_variable_name)
requested_formula_result = formulas_functions[sanitized_calculee_variable]()
# from calculateur_impots import formulas_helpers
# from pprint import pformat
# try:
# requested_formula_result = formulas_functions[sanitized_calculee_variable]()
# except:
# import traceback
# exc_type, exc_value, exc_traceback = sys.exc_info()
# frames = traceback.extract_tb(exc_traceback)
# raise Exception(pformat({
# 'stack_functions': pipe(frames, map(attrgetter('name')), take_nth(2), drop(1), list),
# 'eq_2641': formulas_helpers.inspect_cache(predicate=lambda x: x == 2461),
# }, width=200))
# print(pformat({
# 'eq_2641': formulas_helpers.inspect_cache(predicate=lambda x: x == 2461),
# }, width=200))
try:
requested_formula_result = formulas_functions[sanitized_calculee_variable]()
except:
log.exception('Error while calculating {}'.format(sanitized_calculee_variable))
print('{} = {} ({})'.format(calculee_variable_name, requested_formula_result,
core.get_variable_description(calculee_variable_name)))
......
......@@ -153,20 +153,23 @@ def main():
verif_functions_sources = list(
mapcat(load_verifs_file, iter_ast_json_file_names(filenames=['coc*.json', 'coi*.json']))
)
verif_regles_source = """\
verifs_source = """\
from . import formulas
from ..formulas_helpers import *
from .constants import *
def get_error(code):
return code
def verif_regles(base_variables, saisie_variables):
import ipdb; ipdb.set_trace()
globals().update(results)
def get_errors(base_variables, saisie_variables):
formulas_functions = formulas.get_formulas(base_variables, saisie_variables)
globals().update(formulas_functions)
# Special variables not defined in tgvH
V_9VV = 1
errors = []
{}
return errors or None
""".format(textwrap.indent(''.join(verif_functions_sources), prefix=4 * ' '))
write_source_file(
file_name='verif_regles.py',
source=verif_regles_source,
file_name='verifs.py',
source=verifs_source,
)
# Transpile formulas
......
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