
    jZ                         d dl Z d dlmZmZmZ ddlmZ ddlmZm	Z	 ddl
mZmZ dZdZ	 d dlZdgZdgZ G d	 d
e      Zy# e$ r  e j                          ZY *w xY w)    N)AnyUnionBinaryIO   )
StreamInfo)DocumentConverterDocumentConverterResult)MissingDependencyExceptionMISSING_DEPENDENCY_MESSAGEzapplication/vnd.ms-outlook.msgc                   ^    e Zd ZdZdedededefdZdededede	fdZ
ded	edeed
f   fdZy
)OutlookMsgConverterzConverts Outlook .msg files to markdown by extracting email metadata and content.

    Uses the olefile package to parse the .msg file structure and extract:
    - Email headers (From, To, Subject)
    - Email body content
    file_streamstream_infokwargsreturnc                    |j                   xs dj                         }|j                  xs dj                         }|t        v ryt        D ]  }|j                  |      s y |j                         }	 t        r(t        j                  |      s	 |j                  |       y|j                  |       	 t        gt        j                  |      }dj                  |j                         D 	cg c]  }	t        |	       c}	      }
d|
v xr d|
v 	 |j                  |       S 	 |j                  |       y# |j                  |       w xY wc c}	w # t        $ r
}Y d }~;d }~ww xY w# |j                  |       w xY w)N TF
z__properties_version1.0z__recip_version1.0_#00000000)mimetypelower	extensionACCEPTED_FILE_EXTENSIONSACCEPTED_MIME_TYPE_PREFIXES
startswithtellolefile	isOleFileseek	OleFileIOjoinlistdirstr	Exception)selfr   r   r   r   r   prefixcur_posmsgstreamtoces               m/root/aria/tools/markitdown-venv/lib/python3.12/site-packages/markitdown/converters/_outlook_msg_converter.pyacceptszOutlookMsgConverter.accepts    sd     ((.B557 **0b779	 001 	F""6*	
 ""$	&w00=W%KW%	&"''4ii3;;= IV IJ-4 >6#= W% # W%! W% !J
  		 W%sH   5D5 58E -E?E "E& 5EE 	E#E& E##E& &E9c                 Z   t         Rt        t        j                  t	        |       j
                  dd            t         d   j                  t         d         t        J t        j                  |      }d}| j                  |d      | j                  |d      | j                  |d	      d
}|j                         D ]  \  }}|s	|d| d| dz  } |dz  }| j                  |d      }	|	r||	z  }|j                          t        |j                         |j                  d            S )Nr   outlook)	converterr   feature   r   z# Email Message

z__substg1.0_0C1F001Fz__substg1.0_0E04001Fz__substg1.0_0037001F)FromToSubjectz**z:** r   z
## Content

z__substg1.0_1000001Fr5   )markdowntitle)_dependency_exc_infor
   r   formattype__name__with_tracebackr   r    _get_stream_dataitemscloser	   stripget)
r%   r   r   r   r(   
md_contentheaderskeyvaluebodys
             r,   convertzOutlookMsgConverter.convertI   sN     +,*11"4j11$% (n$Q'
 	
, +
 ))#/EF''-CD,,S2HI
 "--/ 	6JC3%tE7"55
	6 	((
 $$S*@A$J		&%%'++i(
 	
    r(   stream_pathNc                    t         J t        |t         j                        sJ 	 |j                  |      r?|j	                  |      j                         }	 |j                  d      j                         S 	 y# t        $ rU 	 |j                  d      j                         cY S # t        $ r& |j                  dd      j                         cY cY S w xY ww xY w# t        $ r Y yw xY w)zBHelper to safely extract and decode stream data from the MSG file.Nz	utf-16-lezutf-8ignore)errors)
r   
isinstancer    exists
openstreamreaddecoder@   UnicodeDecodeErrorr$   )r%   r(   rI   datas       r,   r=   z$OutlookMsgConverter._get_stream_data   s    """""
 	
 
	zz+&~~k2779M;;{399;;	 '  * MM#{{7399;;- M#{{78{DJJLLM	M  		sS   0C A8 8	CB# C!C #*CCC CCC 	C%$C%)r;   
__module____qualname____doc__r   r   r   boolr-   r	   rG   r#   r   r=    rH   r,   r   r      s    ''  ' 	'
 
'R4
4
  4
 	4

 
!4
lC c eCI>N rH   r   )systypingr   r   r   _stream_infor   _base_converterr   r	   _exceptionsr
   r   r8   r   ImportErrorexc_infor   r   r   rX   rH   r,   <module>r`      sq    
 ' ' % H P  
* !  #8 }+ }  *'3<<>*s   A   AA