consolidator
/
debugHandlers.py
62 строки · 2.4 Кб
1import os2from typing import Any3import pandas as pd4from MyLogger import MyLogger5from MyConfig import MyConfig6from MainConsolidator import MainConsolidator7import sys8
9cur_path = os.path.dirname(os.path.realpath(__file__))10conf = f"{cur_path}\\consolidation.json"11logger = MyLogger(name="debug",log_file="debug.log",outStream=sys.stdout,mode="w")12cfg = MyConfig(conf)13hlist:list = cfg.get("handlers",None)14# hdict:dict = cfg.get("level2 handlers",None)
15hdict={}16for item in hlist:17hdict[item["key"]]=item["module"]18compiled_handlers={}19modules = ["Рулон"]20
21def handler_cc(rec:pd.Series,consolidator:MainConsolidator)->pd.Series:22try:23result:pd.Series={"f1":0}24module_name=f"__{hdict[rec['Производство2']]}__"25# logger.info(f"module {module_name}")26namespace={"consolidator":consolidator , "record":rec, "result":result,"__name__":module_name}27cc=compiled_handlers[rec["Производство2"]]28exec(cc,namespace)29return namespace["result"]30except Exception as exp:31logger.error("handler2 error",exc_info=True)32
33def get_compiled(key:str)->Any:34cc=None35try:36with open(f"{cur_path}\\handlers2\\{hdict[key]}.py",mode="r",encoding="utf-8") as f:37code=f.read()38f.close()39cc = compile(code,"","exec")40logger.debug(f"handler '{key}' compiled")41return cc42except: raise43
44
45if __name__=="__main__":46try:47# module = "Рулон"48code:str=None49df = pd.read_excel(f"{cur_path}\\output\\input_data.xlsx",sheet_name="Sheet1")50cols=df.columns.to_list()+["Продукт УП","error","Время укрупнения"]51modules_df = df[["Производство2"]].drop_duplicates().to_dict("list")52for key in modules_df["Производство2"]: compiled_handlers[key]=get_compiled(key)53# df = df[df["Производство2"]==module]54logger.info("input data loaded")55consolidator = MainConsolidator(f"{cur_path}\\debug.log",logStream=sys.stdout)56consolidator.load_guids("D:\\work\\Укрупнение\\current\\справочники2")57df = df.apply(lambda row: handler_cc(row,consolidator), axis=1)58logger.info("handlers applied")59df[cols].to_excel(f"{cur_path}\\output\\debug_data1.xlsx",index=False)60logger.info("file saved")61except:62logger.error("error",exc_info=True)63
64
65