
    Mi0C                    j   % S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSK	r	SSK
JrJr  SSKJr  SSKJrJrJrJrJr  SSKJr  SSKJrJr  SS	KJr  SS
KJr  SSKJr  SSK J!r!  SSK"J#r#  SSK$J%r%  Sr&S\'S'    SSSSSSS.               SS jjjr( " S S5      r) " S S5      r* " S S5      r+g)z{Provides `partition_email()` function.

Suitable for use with `.eml` files, which can be exported from many email clients.
    )annotationsN)EmailMessageMIMEPart)cached_property)IOAnyFinalIteratorcast)parser)ElementElementMetadata)FileType)logger)EXPECTED_ATTACHMENT_ERRORS)get_last_modified_date)partition_html)partition_text)	text/html
text/plainzFinal[tuple[str, ...]]VALID_CONTENT_SOURCESr   T)filecontent_sourcemetadata_filenamemetadata_last_modifiedprocess_attachmentsc          
     l    [         R                  U UUUUUUS9n[        [        R	                  US95      $ )a  Partitions an .eml file into document elements.

Args:
    filename: str path of the target file.
    file: A file-like object open for reading bytes (not str) e.g. --> open(filename, "rb").
    content_source: The preferred message body. Many emails contain both a plain-text and an
        HTML version of the message body. By default, the HTML version will be used when
        available. Specifying "text/plain" will cause the plain-text version to be preferred.
        When the preferred version is not available, the other version will be used.
    metadata_filename: The file-path to use for metadata purposes. Useful when the target file
        is specified as a file-like object or when `filename` is a temporary file and the
        original file-path is known or a more meaningful file-path is desired.
    metadata_last_modified: The last-modified timestamp to be applied in metadata. Useful when
        a file-like object (which can have no last-modified date) target is used. The
        last-modified metadata is otherwise drawn from the filesystem when a path is provided.
    process_attachments: When True, also partition any attachments in the message after
        partitioning the message body. All document elements appear in the single returned
        element list. The filename of the attachment, when available, is used as the
        `filename` metadata value for elements arising from the attachment.

Note that all global keyword arguments such as `unique_element_ids`, `language` and
`chunking_strategy` can be used and will be passed along to the decorators that implement
those functions. Further, any keyword arguments applicable to HTML will be passed along to the
HTML partitioner when processing an HTML message body.
	file_pathr   r   metadata_file_pathr   r   kwargsctx)EmailPartitioningContextloadlist_EmailPartitioneriter_elements)filenamer   r   r   r   r   r!   r#   s           v/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/unstructured/partition/email.pypartition_emailr+      sK    F #
'
'%,5/ ( C !//C/899    c                     \ rS rSrSrSSSSSS0 4             SS jjr\                SS j5       r\SS j5       r	\SS	 j5       r
\SS
 j5       r\SS j5       r\SS j5       r\S S j5       r\S S j5       r\S S j5       r\S S j5       r\S!S j5       r\S"S j5       r\S#S j5       r\S S j5       r\SS j5       r\S S j5       r\S S j5       rS$S jrSrg)%r$   O   zVEncapsulates partitioning option validation, computation, and application of defaults.Nr   Fc                X    Xl         X l        X0l        X@l        XPl        X`l        Xpl        g N)
_file_path_file_content_source_metadata_file_path_metadata_last_modified_process_attachments_kwargs)selfr   r   r   r    r   r   r!   s           r*   __init__!EmailPartitioningContext.__init__R   s+     $
-#5 '=$$7!r,   c           
     6    U " UUUUUUUS9R                  5       $ )z#Construct and validate an instance.r   )	_validate)clsr   r   r   r    r   r   r!   s           r*   r%   EmailPartitioningContext.loadd   s/     )1#9 3
 )+	r,   c                    U R                   R                  S5      nU(       d  g[        R                  R	                  U5      nU Vs/ s H"  n[        R                  R                  U5      PM$     sn$ s  snf )z6The "blind carbon-copy" Bcc: addresses of the message.BccNmsgget_allemailutilsgetaddresses
formataddr)r8   bccsaddrsaddrs       r*   bcc_addresses&EmailPartitioningContext.bcc_addressesz   sY     xx&((.9>?&&t,???   )A3c                H    U R                   R                  U R                  S9$ )zThe message part containing the actual textual email message.

This is as opposed to attachments or "related" parts like an image that appears in the
message etc.
)preferencelist)rB   get_bodycontent_type_preferencer8   s    r*   	body_part"EmailPartitioningContext.body_part   s"     xx  0L0L MMr,   c                    U R                   R                  S5      nU(       d  g[        R                  R	                  U5      nU Vs/ s H"  n[        R                  R                  U5      PM$     sn$ s  snf )z/The "carbon-copy" Cc: addresses of the message.CcNrA   )r8   ccsrI   rJ   s       r*   cc_addresses%EmailPartitioningContext.cc_addresses   Y     hht$((-9>?&&t,???rM   c                *    U R                   S:X  a  S$ S$ )zWhether to prefer HTML or plain-text body when message-body has both.

The default order of preference is `("html", "plain")`. The order can be switched by
specifying `"text/plain"` as the `content_source` arg value.
r   )plainhtml)r]   r\   )r3   rR   s    r*   rQ   0EmailPartitioningContext.content_type_preference   s     %)$8$8L$H _N__r,   c           	         [        U R                  U R                  U R                  U R                  (       a  U R                  /OSU R
                  U R                  S9$ )zThe email-specific metadata fields for this message.

Suitable for use with `.metadata.update()` on the base metadata applied to message body
elements by delegate partitioners for text and HTML.
N)bcc_recipientcc_recipientemail_message_id	sent_fromsent_tosubject)r   rK   rX   
message_idfrom_addressto_addressesre   rR   s    r*   email_metadata'EmailPartitioningContext.email_metadata   sP     ,,**!__-1->->t(()D%%LL
 	
r,   c                    U R                   R                  S5      nU(       d  g[        R                  R	                  U5      nU Vs/ s H"  n[        R                  R                  U5      PM$     nnUS   $ s  snf )z"The address of the message sender.FromNr   rA   )r8   fromsrI   rJ   formatted_addrss        r*   rg   %EmailPartitioningContext.from_address   sf       (((/DIJED5;;11$7EJq!! Ks   )A7c                    U R                   R                  S5      nU(       d  gUR                  5       R                  S5      $ )z2The value of the Message-ID: header, when present.z
Message-IDNz<>)rB   getstrip)r8   raw_ids     r*   rf   #EmailPartitioningContext.message_id   s3     l+||~##D))r,   c                R    U R                   =(       d    U R                  =(       d    S$ )a  The best available file-path information for this email message.

It's value is computed according to these rules, applied in order:

  - The `metadata_filename` arg value when one was provided to `partition_email()`.
  - The `file_path` value when one was provided.
  - None otherwise.

This value is used as the `filename` metadata value for elements produced by partitioning
the email message (but not those from its attachments).
N)r4   r1   rR   s    r*   r    +EmailPartitioningContext.metadata_file_path   s     ''B4??BdBr,   c                f    U R                   =(       d    U R                  =(       d    U R                  $ )a5  The best available last-modified date for this message, as an ISO8601 string.

It's value is computed according to these rules, applied in order:

  - The `metadata_last_modified` arg value when one was provided to `partition_email()`.
  - The date-time in the `Sent:` header of the message, when present.
  - The last-modified date recorded on the filesystem for `file_path` when it was provided.
  - None otherwise.

This value is used as the `last_modified` metadata value for all elements produced by
partitioning this email message, including any attachments.
)r5   
_sent_date_filesystem_last_modifiedrR   s    r*   r   /EmailPartitioningContext.metadata_last_modified   s$     ++`t`$B`B``r,   c           	        U R                   b[  [        U R                   S5       n[        [        [        R
                  " U[        R                  R                  S95      sSSS5        $ U R                  c   eU R                  R                  5       n[        [        [        R                  " U[        R                  R                  S95      $ ! , (       d  f       Nr= f)zOThe Python stdlib `email.message.EmailMessage` object parsed from the EML file.Nrb)policy)r1   openr   r   rD   message_from_binary_filer}   defaultr2   readmessage_from_bytes)r8   f
file_bytess      r*   rB   EmailPartitioningContext.msg   s     ??&doot, %"@"@5<<K_K_"` -,
 zz%%%ZZ__&
L%":"::ellNbNb"cdd -,s   ;C
Cc                    U R                   $ )zThe "extra" keyword arguments received by `partition_email()`.

These are passed along to delegate partitioners which extract keyword args like
`chunking_strategy` etc. in their decorators to control metadata behaviors, etc.
)r7   rR   s    r*   partitioning_kwargs,EmailPartitioningContext.partitioning_kwargs   s     ||r,   c                    U R                   $ )zWhen True, partition attachments in addition to the email message body.

Any attachment having file-format that cannot be partitioned by unstructured is silently
skipped.
)r6   rR   s    r*   r   ,EmailPartitioningContext.process_attachments   s     (((r,   c                L    U R                   R                  S5      nU(       d  gU$ )z/The value of the Subject: header, when present.SubjectN)rB   rq   )r8   re   s     r*   re    EmailPartitioningContext.subject  s!     ((,,y)r,   c                    U R                   R                  S5      nU(       d  g[        R                  R	                  U5      nU Vs/ s H"  n[        R                  R                  U5      PM$     sn$ s  snf )z!The To: addresses of the message.ToNrA   )r8   tosrI   rJ   s       r*   rh   %EmailPartitioningContext.to_addresses	  rZ   rM   c                R    U R                   (       a  [        U R                   5      $ S$ )zVLast-modified retrieved from filesystem when a file-path was provided, None otherwise.N)r1   r   rR   s    r*   ry   2EmailPartitioningContext._filesystem_last_modified  s     ;?//%doo6StSr,   c                ,   U R                   R                  S5      nU(       d  g [        R                  " U5      nUR                  [        R                  R                  5      R                  SS9$ ! [        R                  [
        [        4 a     gf = f)z?ISO-8601 str representation of message sent-date, if available.DateNseconds)timespec)rB   rq   r   parseParserError	TypeError
ValueError
astimezonedttimezoneutc	isoformat)r8   date_str	sent_dates      r*   rx   #EmailPartitioningContext._sent_date  sy     88<<'	X.I ##BKKOO4>>	>RR ""Iz: 		s   A1 1BBc                   U R                   (       d  U R                  (       d  [        S5      eU R                  (       aT  [        U R                  R	                  S5      [
        5      (       d  [        S5      eU R                  R                  S5        U R                  [        ;  a'  [        [        U R                  5       S[         35      eU $ )z5Raise on first invalid option, return self otherwise.zOno document specified; either a `filename` or `file` argument must be provided.r   z)file object must be opened in binary modez: is not a valid value for content_source; must be one of: )
r1   r2   r   
isinstancer   bytesseekr3   r   reprrR   s    r*   r<   "EmailPartitioningContext._validate$  s    tzza  ::

"E  !!LMMJJOOA'<<,,-. /$$9#:< 
 r,   )r3   r2   r1   r7   r4   r5   r6   )r   
str | Noner   IO[bytes] | Noner   strr    r   r   r   r   boolr!   dict[str, Any])r   r   r   r   r   r   r    r   r   r   r   r   r!   r   returnr$   )r   zlist[str] | None)r   zMIMEPart | None)r   ztuple[str, ...])r   r   r   r   )r   r   )r   r   )r   r   )r   r$   )__name__
__module____qualname____firstlineno____doc__r9   classmethodr%   r   rK   rS   rX   rQ   ri   rg   rf   r    r   rB   r   r   re   rh   ry   rx   r<   __static_attributes__ r,   r*   r$   r$   O   s    ` !%!%))--1$)!#  	
 ' !+ " $   	
 ' !+ "  
" * @ @ N N @ @ ` ` 
 
 " " * * C C a a e e   ) )   @ @ T T 
S 
Sr,   r$   c                  J    \ rS rSrSrS	S jr\S
S j5       rSS jrSS jr	Sr
g)r'   i;  z8Encapsulates the partitioning logic for email documents.c                    Xl         g r0   _ctx)r8   r#   s     r*   r9   _EmailPartitioner.__init__>  s    	r,   c                *    U " US9R                  5       $ )z@Generate the document elements for the email described by `ctx`.r"   _iter_elements)r=   r#   s     r*   r(   _EmailPartitioner.iter_elementsA  s     s|**,,r,   c              #    #    U R                  5        H6  nUR                  R                  U R                  R                  5        Uv   M8     U R                  R
                  (       d  gU R                  R                  R                  5        H*  n[        R                  X R                  5       Sh  vN   M,     g N	7f)zGenerate the document elements for the email described in the partitioning context.

This optionally includes elements generated by partitioning any partitionable attachments
in the message as well.
N)
_iter_email_body_elementsmetadataupdater   ri   r   rB   iter_attachments_AttachmentPartitionerr(   )r8   e
attachments      r*   r    _EmailPartitioner._iter_elementsF  s      //1AJJdii667G 2 yy,,))--88:J-;;J		RRR ;Rs   B0B>2B<3
B>c              #  L  #    U R                   R                  nUc  gUR                  5       nUR                  5       n[	        U[
        5      (       d   eUS:X  ac  [        SUU R                   R                  [        R                  U R                   R                  S.U R                   R                  D6 Sh  vN   g[        SUU R                   R                  [        R                  U R                   R                  S.U R                   R                  D6 Sh  vN   g Nh N7f)z/Generate document elements from the email body.Nr   )textr   metadata_file_typer   r   )r   rS   get_content_typeget_contentr   r   r   r    r   EMLr   r   r   )r8   rS   content_typecontents       r*   r   +_EmailPartitioner._iter_email_body_elementsV  s     II''	  113'')'3'''';&% "&))">">#+<<'+yy'G'G	
 ))//   & "&))">">#+<<'+yy'G'G	
 ))//  s%   B5D$7D 8A"D$D"D$"D$r   N)r#   r$   )r#   r$   r   Iterator[Element]r   r   )r   r   r   r   r   r9   r   r(   r   r   r   r   r,   r*   r'   r'   ;  s)    B - -S r,   r'   c                      \ rS rSrSrSS jr\      SS j5       rSS jr\	SS j5       r
\	SS j5       r\	SS j5       rS	rg
)r   it  z'Partitions an attachment to a MSG file.c                    Xl         X l        g r0   _attachmentr   )r8   r   r#   s      r*   r9   _AttachmentPartitioner.__init__w  s    %	r,   c                .    U " X5      R                  5       $ )zHPartition an attachment MIME-part from a MIME email message (.eml file).r   )r=   r   r#   s      r*   r(   $_AttachmentPartitioner.iter_elements{  s    
 :#2244r,   c              #    #    SSK Jn  [        R                  " U R                  5      n U" SUU R
                  U R                  R                  S.U R                  R                  D6nU H"  nU R                   UR"                  l        Uv   M$     g! [         aZ  n[        U[        5      (       d  e [        R                  " SU R
                  [        U5      R                   SU 35         SnAgSnAff = f7f)zPartition the byte-stream in the attachment MIME-part into elements.

Generates zero elements if the attachment is not partitionable.
r   )	partition)r   r   r   zSkipping attachment %s: %sz: Nr   )unstructured.partition.autor   ioBytesIO_file_bytes_attachment_file_namer   r   r   BaseExceptionr   r   r   warningtyper   _attached_to_filenamer   attached_to_filename)r8   r   r   elementsr   s        r*   r   %_AttachmentPartitioner._iter_elements  s      	:zz$**+	  "&"<"<'+yy'G'G ))//	H  A.2.H.HAJJ+G   	a!;<<NN,**7##$Bqc*
 	s/   'C8>B ()C8
C5AC0+C80C55C8c                t    U R                   R                  nUc  g[        R                  R	                  U5      $ )z@The file-name (no path) of the message. `None` if not available.N)r   r    ospathbasename)r8   r   s     r*   r   ,_AttachmentPartitioner._attached_to_filename  s1     II00	ww	**r,   c                6    U R                   R                  5       $ )zOThe original name of the attached file, `None` if not present in the MIME part.)r   get_filenamerR   s    r*   r   ,_AttachmentPartitioner._attachment_file_name  s     ,,..r,   c                    U R                   R                  5       n[        U[        5      (       a  UR	                  S5      $ [        U[
        5      (       d   eU$ )zThe bytes of the attached file.zutf-8)r   r   r   r   encoder   )r8   r   s     r*   r   "_AttachmentPartitioner._file_bytes  sL     ""..0gs##>>'**'5))))r,   r   N)r   r   r#   r$   )r   r   r#   r$   r   r   r   r   )r   r   )r   r   r   r   r   r9   r   r(   r   r   r   r   r   r   r   r,   r*   r   r   t  sx    1 5%5,D5	5 5B + + / /  r,   r   r0   )r)   r   r   r   r   r   r   r   r   r   r   r   r!   r   r   zlist[Element]),r   
__future__r   datetimer   rD   email.policyemail.utilsr   r   email.messager   r   	functoolsr   typingr   r   r	   r
   r   dateutilr   unstructured.documents.elementsr   r   unstructured.file_utils.modelr   unstructured.loggerr   unstructured.partition.commonr   &unstructured.partition.common.metadatar   unstructured.partition.htmlr   unstructured.partition.textr   r   __annotations__r+   r$   r'   r   r   r,   r*   <module>r     s   
 #     	 	 0 % 1 1  D 2 & D I 6 60K - K  -: "%$()- $-:-: -: 	-:
 "-: '-: -: -: -:`i iX6 6rE Er,   