generateur_v3/backend/api_old/generateur/generateur_main.py
2022-09-16 21:50:55 +02:00

50 lines
1.8 KiB
Python

import re
import importlib.util
def getObjectKey(obj, key):
if obj[key] == None:
return None
return key if obj[key] != False else 'calcul' if obj['calcul'] != False else None
def getCorrectionKey(obj, key):
return key if (obj[key] != False and obj['correction'] == False) else 'calcul' if(obj['calcul'] != False and obj['correction'] == False) else 'correction' if obj['correction'] != False else None
def parseCorrection(calc, replacer='...'):
exp_list = re.findall(r"\[([A-Za-z0-9_]+)\]", calc)
for exp in exp_list:
calc = calc.replace(f'[{exp}]', replacer)
return calc
def Generateur(path, quantity, key, forcedCorrection=False):
spec = importlib.util.spec_from_file_location(
"tmp", path)
tmp = importlib.util.module_from_spec(spec)
spec.loader.exec_module(tmp)
try:
main_func = tmp.main
except:
return None
main_result = main_func()
default_object = {"calcul": False, 'pdf': False, 'csv': False,
'web': False, 'correction': False} # les valeurs par défaut
# Si l'utilisateur n'a pas entré une valeur, elle est définie à False
print(main_result)
result_object = {**default_object, **main_result}
object_key = getObjectKey(result_object, key)
correction_key = getCorrectionKey(result_object, key)
op_list = []
try:
replacer = tmp.CORRECTION_REPLACER
except:
replacer = '...'
for i in range(quantity):
main_result = main_func()
main = {**default_object, **main_result}
op_list.append({'calcul': parseCorrection(main[
object_key], replacer) if (forcedCorrection or (key != 'web' and main['correction'] == False)) else main[object_key], 'correction': main[correction_key]})
return op_list