6/7ixpdZddlZddlmZmZddlmZddlmZeje Z GddZ y)zeOutput Service - Business Logic for Output Management. Handles output file operations and tracking. N)ListOptional)Output) file_utilsc eZdZdZdZddedededeefdZ dede efdZ de efd Z d ede fd Zd ede fd Zd ede efdZd edeefdZd edede fdZy) OutputServicezService class for output management business logic. Provides clean interface for managing output files and their database records. c||_y)zhInitialize the output service. Args: db_manager: DatabaseManager instance. N) db_manager)selfr s B/mnt/ssd/data/python-lab/ChefSystem/src/services/output_service.py__init__zOutputService.__init__s % recipe_idsource_file_pathexecution_notesreturnc. tj|j|||}|r'tj d|d|j |Stj d||S#t$r$}tj d|dYd}~yd}~wwxYw)aSave an output file for a recipe. Copies file to recipe output directory and creates database record. Args: recipe_id: ID of the associated recipe. source_file_path: Path to the source file to copy. execution_notes: Notes about this execution. Returns: Output instance if successful, None otherwise. zSaved output for recipe : z!Failed to save output for recipe zError saving output: Texc_infoN)rsave_output_filer loggerinfofilenameerror Exception)r rrroutputes r save_outputzOutputService.save_outputs 00  F 6ykFOOCTUVM @ LMM  LL04tL D sA A' A'' B0BBc tj|j|}tj dt |d||S#t $r)}tjd|d|dgcYd}~Sd}~wwxYw)zGet all outputs for a specific recipe. Args: recipe_id: Recipe ID. Returns: List of outputs for the recipe, sorted by generation date (newest first). Retrieved z outputs for recipe z$Error retrieving outputs for recipe rTrN)r get_by_reciper rdebuglenrr)r routputsrs r get_recipe_outputsz OutputService.get_recipe_outputs>st **4??IFG LL:c'l^3G {S TN  LL? {"QCP[_L `I sAA A:A5/A:5A:c tj|j}tj dt |d|S#t $r&}tjd|dgcYd}~Sd}~wwxYw)z_Get all outputs across all recipes. Returns: List of all outputs. r!z total outputszError retrieving all outputs: TrN)rget_allr rr#r$rr)r r%rs r get_all_outputszOutputService.get_all_outputsPsd  nnT__5G LL:c'l^>B CN  LL9!=L MI sAA A4A/)A4/A4 output_idc* tj|j|}|stj d|dyt j |j}|s"tj d|jtj|j|}|r&tjd|d|jytjd|y#t$r'}tjd |d|d Yd }~yd }~wwxYw) zDelete an output file and its database record. Args: output_id: ID of output to delete. Returns: bool: True if successful, False otherwise. Output z not found for deletionFzFailed to delete file: zDeleted output rTzFailed to delete output record zError deleting output rN) r get_by_idr rwarningrdelete_output_filefilepathdeleterrrr)r r*r file_deleted db_deletedrs r delete_outputzOutputService.delete_output_s %%dooyAF 3JKL&88IL!88IJK t BJ oi[6??:KLM >ykJK  LL1)BqcBTL R s#;C"B C" C"" D+D  Dc tj|j|}|stj d|dyt j |j}|r'tjd|d|j|Stj d|d|j|S#t$r'}tj d|d|d Yd }~yd }~wwxYw) zOpen an output file with the system default application. Args: output_id: ID of output to open. Returns: bool: True if successful, False otherwise. r, not foundFzOpened output rzFailed to open output zError opening output TrN) rr-r rrr open_filer0rrr)r r*rsuccessrs r open_outputzOutputService.open_outputs %%dooyAF wyk<=!**6??;G nYKr&//9JKLN 5i[6??BSTUN  LL0 2aSADL Q s#;B-AB-&B-- C6CCqueryc |r|js|jS|j}|jj}|Dcgc] }||jjvr|"}}tj d|dt |d|Scc}w#t$r&}tjd|dgcYd}~Sd}~wwxYw)zSearch outputs by filename. Args: query: Search query string. Returns: List of matching outputs. zSearch 'z' found z outputszError searching outputs: TrN) stripr)lowerrrr#r$rr)r r: all_outputs query_lowerrmatchingrs r search_outputszOutputService.search_outputss  ++--..0K++---/K&1!&//"7"7"99H LL8E7(3x=/J KO   LL4QC84L HI s4!B)2B)%B$;(B)$B)) C2C CCc2 tj|j|}|r'tj d|d|j |Stj d|d|S#t $r'}tjd|d|dYd}~yd}~wwxYw) zGet an output by ID. Args: output_id: Output ID. Returns: Output instance or None. zRetrieved output rr,r6zError retrieving output TrN)rr-r rr#rrr)r r*rrs r get_output_by_idzOutputService.get_output_by_ids %%dooyAF 0 2foo=NOPM wyk<=M  LL3I;bDtL T sAA& A&& B/BBnotesc tj|j||}|rtj d||Stj d||S#t $r$}tj d|dYd}~yd}~wwxYw) zUpdate execution notes for an output. Args: output_id: ID of output to update. notes: New execution notes. Returns: bool: True if successful, False otherwise. )rz#Updated execution notes for output z,Failed to update execution notes for output z Error updating execution notes: TrNF)rupdater rrrr)r r*rDr8rs r update_execution_notesz$OutputService.update_execution_notess mm %G  A)MNN KI;WXN  LL;A3?$L O s=AA B#BBN))__name__ __module__ __qualname____doc__r intstrrrrrr&r)boolr4r9rArCrGrr rrs %SCRU_ghn_oBCDL$ f $s$t$LST>CDL<#(62B.CDrr) rLloggingtypingrrsrc.database.modelsr src.utilsr getLoggerrIrrrPrr rVs6 !&   8 $ddr