with open('app/utils/tasks.py', 'r', encoding='utf-8') as f: text = f.read() import re old_block = re.search(r'tmp_def_path = None.*?db\.session\.remove\(\)', text, re.DOTALL) new_block = """from app.utils.slice_engines import get_slicer_engine try: # Optionally fetch the preferred engine from db conf or just default to cura # For now default to cura or whichever is passed via kwargs if implemented later conf_engine = SystemConfig.query.filter_by(key='slicer_engine').first() engine_name = conf_engine.value if conf_engine and conf_engine.value else "cura" db.session.remove() slicer = get_slicer_engine(engine_name) success, err_msg = slicer.slice( app=app, stl_filepath=stl_filepath, gcode_filepath=gcode_filepath, quality_preset=quality_preset, infill_density=infill_density, support_enable=support_enable, support_pattern=support_pattern ) # Re-fetch print_file and update status print_file = PrintFile.query.get(file_id) if not print_file: return if success: print_file.status = 'sliced' else: print_file.status = 'failed' app.logger.error(f"Slicing Task Failed: {err_msg}") except Exception as e: print_file = PrintFile.query.get(file_id) if print_file: print_file.status = 'failed' app.logger.error(f"Subprocess Exception: {e}") finally: if delete_stl and os.path.exists(stl_filepath): try: os.remove(stl_filepath) except Exception as e: app.logger.error(f"Failed to delete temp STL {stl_filepath}: {e}") db.session.commit() db.session.remove()""" if old_block: res = text.replace(old_block.group(0), new_block) with open('app/utils/tasks.py', 'w', encoding='utf-8') as f: f.write(res) print("Patched successfully") else: print("Pattern not found!")