def action_process(d: DataDir, config: Configuration, logger: Logger, userid: int): if not os.path.exists(d.user_config_path(userid)): raise ValueError(f'config file for user {userid} not found') user_data = DataDir(config) user_data.add_config(d.user_config_path(userid)) acc: UserAccounts = d.UserAccounts(userid) # try to convert xls to xlsx for datafile in d.UserXLSDataFiles(userid): try: import pyexcel as p xlsx_file = datafile + 'x' logger.info(f'Trying to convert {datafile} to {xlsx_file}') if os.path.exists(xlsx_file): logger.info(f'IGNORED (Already exists) {xlsx_file}') else: p.save_book_as(file_name=datafile, dest_file_name=xlsx_file) logger.info(f'Converted') except ImportError as e: logger.error('Error during xls to xlsx conversion', 'Error', exc_info=e) datafiles: List[str] = d.UserDataFiles(userid) if len(datafiles) == 0: logger.error('No xlsx files') import_tag: str = f'IMP-{datetime.date.today().strftime("%Y-%m-%d")}' for datafile in d.UserDataFiles(userid): #datafile_parts:str = os.path.splitext(os.path.basename(datafile)) outputfn = os.path.join(user_data.user_folder_output(userid), os.path.basename(datafile)) stmt_data = XlsxDriver(filepath=datafile, output_file_path=outputfn, first_row=get_config_int( user_data.config, 'datafile.first_row')) stmt_data.read() from execution_context import ExecutionContext ec = ExecutionContext(stmt_data, acc, list_files('rules', r'.*\.yaml'), d.UserTaxNumber(userid), d.user_folder_output(userid)) (total_rows, rows_processed) = ec.execute(import_tag) stmt_data.close() logger.info( f'DONE. Processed {total_rows} rows, prepared {rows_processed} statements with tag {import_tag}' )
outputfn = os.path.join(user_data.user_folder_output(userid), os.path.basename(datafile)) stmt_data = XlsxDriver(filepath=datafile, output_file_path=outputfn, first_row=get_config_int( user_data.config, 'datafile.first_row')) stmt_data.read() from execution_context import ExecutionContext ec = ExecutionContext(stmt_data, d.UserAccounts(userid), list_files('rules', r'.*\.yaml'), d.UserTaxNumber(userid), d.user_folder_output(userid)) (total_rows, rows_processed) = ec.execute(import_tag) stmt_data.close() logger.info( f'DONE. Processed {total_rows} rows, prepared {rows_processed} statements with tag {import_tag}' ) if config['action'].as_str() == 'push': for userid in d.Users(): if not os.path.exists(d.user_config_path(userid)): raise ValueError(f'config file for user {userid} not found') user_data = DataDir(config) user_data.add_config(d.user_config_path(userid)) output_folder = d.user_folder_output(userid) if not os.path.exists(output_folder): continue #raise ValueError(f'folder does not exist {output_folder}')