Commit 838de76a authored by Christophe Benz's avatar Christophe Benz

Add JSON output format

parent 997b5ccc
......@@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
from collections import defaultdict
import argparse
import json
import logging
......@@ -9,7 +10,7 @@ import os
import pkg_resources
import sys
from toolz.curried import map, pipe, unique
from toolz.curried import map, merge, pipe, unique, valfilter
from calculette_impots import core
from calculette_impots.generated import formulas, verifs
......@@ -60,23 +61,30 @@ def main():
help='Variables calculées')
parser.add_argument('--saisies', dest='saisie_variables', metavar='nom=valeur', nargs='+', help='Variables saisies')
parser.add_argument('--no-verifs', action='store_true', default=False, help='Skip verifs step')
parser.add_argument('--output-format', choices=['text', 'json'], default='text', help='Output format')
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG if args.debug else (logging.INFO if args.verbose else logging.WARNING),
stream=sys.stdout,
)
for calculee_variable in args.calculee_variables:
if not core.is_restituee_variable(calculee_variable):
not_restituee_message = 'Variable "{}" is not a variable of type "calculee restituee"'.format(
calculee_variable)
log.warning(not_restituee_message)
# parser.error(not_restituee_message)
warning_messages_by_section = defaultdict(list)
saisie_variables = dict(iter_saisie_variables(args.saisie_variables))
if args.calculee_variables is None:
calculee_variable_names = core.find_restituee_variables()
else:
calculee_variable_names = args.calculee_variables
for calculee_variable_name in calculee_variable_names:
if not core.is_restituee_variable(calculee_variable_name):
warning_messages_by_section['saisies'].append(
'Variable "{}" is not a variable of type "calculee restituee"'.format(calculee_variable_name)
)
saisie_variables = {} if args.saisie_variables is None else dict(iter_saisie_variables(args.saisie_variables))
if 'V_ANREV' not in saisie_variables:
log.warning('V_ANREV should be given as a "saisie" variable. Hint: --saisie V_ANREV=2014')
# log.debug('saisie_variables: {}'.format(saisie_variables))
warning_messages_by_section['saisies'].append(
'V_ANREV should be given as a "saisie" variable. Hint: --saisie V_ANREV=2014'
)
result_by_formula_name_cache = {}
formulas_functions = formulas.get_formulas(
......@@ -92,25 +100,36 @@ def main():
if errors is not None:
errors_definitions = load_errors_definitions()
definition_by_error_name = pipe(errors_definitions, map(lambda d: (d['name'], d)), dict)
log.error('verif errors: {}'.format(json.dumps(
[
(
error,
definition_by_error_name.get(error, {}).get('description'),
)
for error in unique(errors) # Keep order
],
indent=4,
)))
for calculee_variable_name in args.calculee_variables:
warning_messages_by_section['verif_errors'] = [
(error, definition_by_error_name.get(error, {}).get('description'))
for error in unique(errors) # Keep order
]
results = {}
for calculee_variable_name in calculee_variable_names:
try:
requested_formula_result = formulas_functions[calculee_variable_name]()
result = formulas_functions[calculee_variable_name]()
except:
log.exception('Error while calculating {}'.format(calculee_variable_name))
else:
print('{} = {} ({})'.format(calculee_variable_name, requested_formula_result,
results[calculee_variable_name] = result
if args.calculee_variables is None:
results = valfilter(lambda val: val > 0, results)
if args.output_format == 'text':
for section, warning_messages in warning_messages_by_section.items():
for warning_message in warning_messages:
log.warning('{}:{}'.format(section, warning_message))
for calculee_variable_name, result in results.items():
print('{} = {} ({})'.format(calculee_variable_name, result,
core.get_variable_description(calculee_variable_name)))
elif args.output_format == 'json':
print(json.dumps(
merge({'results': results}, warning_messages_by_section),
sort_keys=True,
))
else:
raise NotImplementedError()
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