a
    ¡˜ëjÓ,  ã                
   @  s  d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZmZ ddddddœddddddddœdd„Z G dd„ dƒZ!G dd„ dƒZ"G dd„ dƒZ#dS )é    )ÚannotationsN)ÚIOÚAnyÚIteratorÚOptional)ÚMessage)Ú
Attachment)ÚElementÚElementMetadata)ÚFileType)Úlogger)ÚUnsupportedFileFormatError)Úget_last_modified_date)Úpartition_html)Úpartition_text)Úis_temp_file_pathÚlazypropertyT)ÚfileÚmetadata_filenameÚmetadata_last_modifiedÚprocess_attachmentszOptional[str]zOptional[IO[bytes]]Úboolr   zlist[Element])Úfilenamer   r   r   r   ÚkwargsÚreturnc                K  s"   t || ||||d�}tt |¡ƒS )a  Partitions a MSFT Outlook .msg file

    Parameters
    ----------
    filename
        A string defining the target filename path.
    file
        A file-like object using "rb" mode --> open(filename, "rb").
    metadata_filename
        The filename to use for the metadata.
    metadata_last_modified
        The last modified date for the document.
    process_attachments
        If True, partition_email will process email attachments in addition to
        processing the content of the email itself.
    ©r   Ú	file_pathÚmetadata_file_pathr   Úpartition_attachmentsr   )ÚMsgPartitionerOptionsÚlistÚ_MsgPartitionerÚiter_message_elements)r   r   r   r   r   r   Úopts© r$   úY/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/msg.pyÚpartition_msg   s    ú	r&   c                   @  sÊ   e Zd ZdZdddddddœdd„Zed	d
œdd„ƒZedd
œdd„ƒZedd
œdd„ƒZedd
œdd„ƒZ	edd
œdd„ƒZ
edd
œdd„ƒZedd
œdd„ƒZedd
œdd„ƒZedd
œdd„ƒZdS ) r   zVEncapsulates partitioning option validation, computation, and application of defaults.zIO[bytes] | Noneú
str | Noner   zdict[str, Any]r   c                C  s(   || _ || _|| _|| _|| _|| _d S ©N)Ú_fileÚ
_file_pathÚ_metadata_file_pathÚ_metadata_last_modifiedÚ_partition_attachmentsÚ_kwargs)Úselfr   r   r   r   r   r   r$   r$   r%   Ú__init__=   s    
zMsgPartitionerOptions.__init__r
   ©r   c                 C  sÎ   | j }|j }r$dd„ | d¡D ƒnd}dd„ |jD ƒp:d}|j d¡ }r`dd„ | d¡D ƒnd}|j d¡ }rˆd	d„ | d¡D ƒnd}|j d
¡ }	rªt dd|	¡}	t|||	|||j	p¾dd�}
d|
_
|
S )a]  ElementMetadata suitable for use on an element formed from message content.

        These are only the metadata fields specific to email messages. The remaining metadata
        fields produced by the delegate partitioner are used as produced.

        None of these metadata fields change based on the element, so we just compute it once.
        c                 S  s   g | ]}|  ¡ ‘qS r$   ©Ústrip)Ú.0Úsr$   r$   r%   Ú
<listcomp>Y   ó    z<MsgPartitionerOptions.extra_msg_metadata.<locals>.<listcomp>ú,Nc                 S  s   g | ]
}|j ‘qS r$   )Zemail_address)r4   Úrr$   r$   r%   r6   Z   r7   ZBccc                 S  s   g | ]}|  ¡ ‘qS r$   r2   ©r4   Úcr$   r$   r%   r6   \   r7   ÚCcc                 S  s   g | ]}|  ¡ ‘qS r$   r2   r:   r$   r$   r%   r6   _   r7   z
Message-Idz^<|>$Ú )Úbcc_recipientÚcc_recipientÚemail_message_idÚ	sent_fromÚsent_toÚsubjectÚmsg)rD   ÚsenderÚsplitZ
recipientsÚmessage_headersÚgetÚreÚsubr
   rC   Zdetection_origin)r/   rD   rE   rA   rB   Zbccr>   Úccr?   r@   Zelement_metadatar$   r$   r%   Úextra_msg_metadataN   s&    	"&ÿ&ÿúz(MsgPartitionerOptions.extra_msg_metadatac                 C  s   d| j j dd¡v S )zTrue when message is encrypted.Z	encryptedzContent-Typer=   )rD   rG   rH   ©r/   r$   r$   r%   Úis_encryptedp   s    
z"MsgPartitionerOptions.is_encryptedc                 C  s   | j p
| jS )zÉBest available path for MSG file.

        The value is the caller supplied `metadata_filename` if present, falling back to the
        source file-path if that was provided, otherwise `None`.
        )r+   r*   rM   r$   r$   r%   r   |   s    z(MsgPartitionerOptions.metadata_file_pathc                 C  s(   | j j }r| ¡ nd}| jp&|p&| jS )zLCaller override for `.metadata.last_modified` to be applied to all elements.N)rD   Ú	sent_dateÚ	isoformatr,   Ú_last_modified)r/   rO   Z
email_dater$   r$   r%   r   …   s    z,MsgPartitionerOptions.metadata_last_modifiedr   c                 C  s   t  | j¡S )z8The `oxmsg.Message` object loaded from file or filename.)r   ÚloadÚ	_msg_filerM   r$   r$   r%   rD   ‹   s    zMsgPartitionerOptions.msgc                 C  s   | j S )z9True when message attachments should also be partitioned.)r-   rM   r$   r$   r%   r   �   s    z+MsgPartitionerOptions.partition_attachmentsc                 C  s   | j S )z÷The "extra" keyword arguments received by `partition_msg()`.

        These are passed along to delegate partitioners which extract keyword args like
        `chunking_strategy` etc. in their decorators to control metadata behaviors, etc.
        )r.   rM   r$   r$   r%   Úpartitioning_kwargs•   s    z)MsgPartitionerOptions.partitioning_kwargsc                 C  s   | j rt| j ƒrdS t| j ƒS )zNThe best last-modified date available from source-file, None if not available.N)r*   r   r   rM   r$   r$   r%   rQ   ž   s    z$MsgPartitionerOptions._last_modifiedzstr | IO[bytes]c                 C  s(   | j  }r|S | j }r|S tdƒ‚dS )zRThe source for the bytes of the message, either a file-path or a file-like object.z6one of `file` or `filename` arguments must be providedN)r*   r)   Ú
ValueError)r/   r   r   r$   r$   r%   rS   ¦   s
    

zMsgPartitionerOptions._msg_fileN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r   rL   rN   r   r   rD   r   rT   rQ   rS   r$   r$   r$   r%   r   :   s(   !r   c                   @  s`   e Zd ZdZddœdd„Zedddœdd	„ƒZdd
œdd„Zedd
œdd„ƒZ	dd
œdd„Z
dS )r!   z-Partitions Outlook email message (MSG) files.r   )r#   c                 C  s
   || _ d S r(   )Ú_opts)r/   r#   r$   r$   r%   r0   µ   s    z_MsgPartitioner.__init__úIterator[Element])r#   r   c                 c  s*   |j rt d¡ dS | |ƒ ¡ E dH  dS )ú?Partition MS Outlook email messages (.msg files) into elements.z@Encrypted email detected. Partitioner will return an empty list.N)rN   r   ÚwarningÚ_iter_message_elements)Úclsr#   r$   r$   r%   r"   ¸   s    
z%_MsgPartitioner.iter_message_elementsr1   c                 c  s>   |   ¡ E dH  | jjsdS | jD ]}t || j¡E dH  q dS )r\   N)Ú_iter_message_body_elementsrZ   r   Ú_attachmentsÚ_AttachmentPartitionerÚiter_elements)r/   Ú
attachmentr$   r$   r%   r^   Á   s
    
z&_MsgPartitioner._iter_message_elementsztuple[Attachment, ...]c                 C  s   t | jjjƒS )z;The `oxmsg.attachment.Attachment` objects for this message.)ÚtuplerZ   rD   ÚattachmentsrM   r$   r$   r%   ra   Ë   s    z_MsgPartitioner._attachmentsc                 c  s–   | j j}|j }r:tf || j jtj| j jdœ| j j¤Ž}n4|j	rjt
f |j	| j jtj| j jdœ| j j¤Ž}ng }| j j}|D ]}|j |¡ |V  qzdS )z5Partition the message body (but not the attachments).)Útextr   Zmetadata_file_typer   N)rZ   rD   Ú	html_bodyr   r   r   ZMSGr   rT   Úbodyr   rL   ÚmetadataÚupdate)r/   rD   rh   ÚelementsZemail_specific_metadataÚer$   r$   r%   r`   Ð   s0    
üûüûz+_MsgPartitioner._iter_message_body_elementsN)rV   rW   rX   rY   r0   Úclassmethodr"   r^   r   ra   r`   r$   r$   r$   r%   r!   ²   s   
r!   c                   @  sz   e Zd ZdZdddœdd„Zeddddœd	d
„ƒZddœdd„Zeddœdd„ƒZ	eddœdd„ƒZ
eddœdd„ƒZdS )rb   z'Partitions an attachment to a MSG file.r   r   )rd   r#   c                 C  s   || _ || _d S r(   )Ú_attachmentrZ   )r/   rd   r#   r$   r$   r%   r0   ñ   s    z_AttachmentPartitioner.__init__r[   )rd   r#   r   c                 C  s   | ||ƒ  ¡ S )zUPartition an `oxmsg.attachment.Attachment` from an Outlook email message (.msg file).)Ú_iter_elements)r_   rd   r#   r$   r$   r%   rc   õ   s    z$_AttachmentPartitioner.iter_elementsr1   c              	   c  sÞ   ddl m} t ¡ �¶}tj || j¡}t|dƒ�}| 	| j
¡ W d  ƒ n1 sR0    Y  z"||f| j| jdœ| jj¤Ž}W n  tyž   Y W d  ƒ dS 0 |D ]}| jj|j_|V  q¤W d  ƒ n1 sÐ0    Y  dS )zEPartition the file in an `oxmsg.attachment.Attachment` into elements.r   )Ú	partitionÚwbN)r   r   )Zunstructured.partition.autorq   ÚtempfileÚTemporaryDirectoryÚosÚpathÚjoinÚ_attachment_file_nameÚopenÚwriteÚ_file_bytesÚ_attachment_last_modifiedrZ   rT   r   r   rj   Zattached_to_filename)r/   rq   Ztmp_dir_pathZdetached_file_pathÚfrl   rm   r$   r$   r%   rp   ü   s&    
*ÿýü
z%_AttachmentPartitioner._iter_elementsÚstrc                 C  s   | j jp
dS )z�The original name of the attached file, no path.

        This value is 'unknown' if it is not present in the MSG file (not expected).
        Úunknown)ro   Ú	file_namerM   r$   r$   r%   rx     s    z,_AttachmentPartitioner._attachment_file_namer'   c                 C  s   | j j }r| ¡ S | jjS )a  ISO8601 string timestamp of attachment last-modified date.

        This value generally available on the attachment and will be the most reliable last-modifed
        time. There are fallbacks for when it is not present, ultimately `None` if we have no way
        of telling.
        )ro   Úlast_modifiedrP   rZ   r   )r/   r�   r$   r$   r%   r|     s    z0_AttachmentPartitioner._attachment_last_modifiedÚbytesc                 C  s   | j jp
dS )zThe bytes of the attached file.r7   )ro   Z
file_bytesrM   r$   r$   r%   r{   )  s    z"_AttachmentPartitioner._file_bytesN)rV   rW   rX   rY   r0   rn   rc   rp   r   rx   r|   r{   r$   r$   r$   r%   rb   î   s   rb   )N)$Ú
__future__r   ru   rI   rs   Útypingr   r   r   r   Zoxmsgr   Zoxmsg.attachmentr   Zunstructured.documents.elementsr	   r
   Zunstructured.file_utils.modelr   Zunstructured.loggerr   Zunstructured.partition.commonr   Z&unstructured.partition.common.metadatar   Zunstructured.partition.htmlr   Zunstructured.partition.textr   Zunstructured.utilsr   r   r&   r   r!   rb   r$   r$   r$   r%   Ú<module>   s0    ÿú%x<