
    Ng=                       d Z ddlmZ ddlZddlZddlZddlZddlZddl	m
Z
 ddlmZ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  G d d          Z G d d          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z  G d de          Z! G d de          Z" G d d e          Z#d(d'Z$dS ))aR  Provides access to all properties of a top-level object (message, recipient, attachment).

This object is accessed using the `.properties` property of a top-level object. The properties for
each top-level object are segregated into its own properties object.

Many property-ids (PIDs) and property-types (PTYPs) are available as constants in
`oxml.domain.contants`.

```python
>>> from oxmsg import Message
>>> from oxmsg.domain import constants as c

>>> msg = Message.load("message.msg")
>>> properties = msg.properties
>>> properties.str_prop_value(c.PID_MESSAGE_CLASS).value
'IPM.Note'
```
    )annotationsN)defaultdict)FinalIteratorcast)	constants)	encodings)model)	reference)lazypropertyc                      e Zd ZdZd'dZd(d	Zd)dZed*d            Zd+dZ	d,dZ
d-dZed.d            Zd/dZd0dZed*d            Zed1d            Zed2d!            Zed3d#            Zed4d%            Zd&S )5
Propertiesz4Provides access to properties from an OXMSG storage.storagem.PropStorageTproperties_header_offsetintc                "    || _         || _        d S N)_storage_properties_header_offset)selfr   r   s      L/var/www/html/ai-engine/env/lib/python3.11/site-packages/oxmsg/properties.py__init__zProperties.__init__(   s     *B&&&    returnIterator[m.PropertyT]c                *    t          | j                  S r   )iter_property_sequencer   s    r   __iter__zProperties.__iter__.   s    D+,,,r   pidbytes | Nonec                ~    |                      |t          j                  }|dS t          t          |          j        S )zRetrieve bytes of PtypBinary property identified by `pid`.

        Returns `None` if property is not present in this collection.
        N)_get_propertycPTYP_BINARYr   BinaryPropertyvaluer   r"   propertys      r   binary_prop_valuezProperties.binary_prop_value1   s9    
 %%c1=994NH--33r   strc                    |                      t          j                  }|t          j        |j                  S | j        S )a	  The encoding used for a PidTagBody or PidTagHtml property of PtypString8/Binary.

        Must be cherry-picked because it is required before constructing the properties collection.

        Note when these are PtypString they unconditionally use UTF-16LE.
        )_cherry_pick_int_propr&   PID_INTERNET_CODEPAGEr	   encoding_from_codepager)   _str_prop_encoding)r   internet_codepages     r   body_encodingzProperties.body_encoding=   sA     !66q7NOO(34E4KLLL &&r   dt.datetime | Nonec                r    | j                             |          }|dS t          t          |          j        S )zRead datetime property value from the properties stream.

        - Microseconds are truncated.
        - Returns `None` when no `pid` property is present in properties stream.
        N)_properties_mappinggetr   TimePropertyr)   r*   s      r   date_prop_valuezProperties.date_prop_valueM   s8     +//444L(++11r   
int | Nonec                r    | j                             |          }|dS t          t          |          j        S )zRetrieve int value of PtypInteger32 property identified by `pid`.

        Returns `None` if no `pid` property is present in this collection.
        N)r7   r8   r   Int32Propertyr)   r*   s      r   int_prop_valuezProperties.int_prop_valueZ   s8    
 +//444M8,,22r   
str | Nonec                    |                      |t          j        t          j        f          }|dS t	          t
          |          j        S )zRetrieve str value of PtypString or PtypString8 property identified by `pid`.

        Returns the empty str if property is not present in this collection.
        N)r%   r&   PTYP_STRINGPTYP_STRING8r   StringPropertyr)   r*   s      r   str_prop_valuezProperties.str_prop_valuef   s@    
 %%cAM1>+JKK4NH--33r   boolc                    |                      t          j                  }|dS t          |t          j        z            S )zMTrue indicates PtypString properties in this message are encoded "utf-16-le".NF)r>   r&   PID_STORE_SUPPORT_MASKrE   mSTORE_UNICODE_OK)r   store_support_masks     r   string_props_are_unicodez#Properties.string_props_are_unicoder   s=     "001IJJ%5&);;<<<r   Int32Property | Nonec                    t          j        d          }| j        D ]<}|                    |dd                   d         }||k    rt	          |          c S =dS )zGet an Int32 property without triggering broader property load.

        Used to solve chicken-and-egg problem of determining encoding required by string
        properties before atomically loading all properties.
        <2xHN   r   )structStruct_prop_segment_sequenceunpackr=   )r   r"   PIDsegmentpid_s        r   r/   z Properties._cherry_pick_int_prop|   sj     mF##2 	. 	.G::gbqbk**1-Ds{{$W----- tr   ptypsint | tuple[int, ...]m.PropertyT | Nonec                x    t          |t                    r|fn|}| j        |         }|D ]}|j        |v r|c S dS )a  Retrieve the first property with `pid` and one of `ptyps`.

        The general expectation is that at most one property with `pid` and one of `ptyps` will be
        present in the collection. In the unusual case there could be more than one this method
        may need to be called once for each possible type to get them all or in a particular order
        of preference.
        N)
isinstancer   _properties_by_pidptyp)r   r"   rW   acceptable_ptypscandidate_propsps         r   r%   zProperties._get_property   s\     (2%'='=HE8851#6  	 	Av))) *tr   c                z    |                      t          j                  }|t          j        |j                  S dS )zThe encoding used for non-body properties of PtypString8.

        Must be cherry-picked because it is required before constructing the properties collection.

        Note when PtypString properties are unconditionally encoded with UTF-16LE.
        Nziso-8859-15)r/   r&   PID_MESSAGE_CODEPAGEr	   r1   r)   )r   message_codepages     r   r2   zProperties._str_prop_encoding   s=      55a6LMM'34D4JKKK }r   tuple[bytes, ...]c                ~    t          d t          | j        j        | j        d         d          D                       S )zR16-byte segments comprising property blocks from the attachment properties stream.c              3  @   K   | ]}t          |          d k    |V  dS )   N)len).0rU   s     r   	<genexpr>z4Properties._prop_segment_sequence.<locals>.<genexpr>   sC       
 
 7||r!! 
 "!!!
 
r   Nrg   )tuple_batched_bytesr   properties_stream_bytesr   r    s    r   rR   z!Properties._prop_segment_sequence   sU      
 
)5d6T6V6VWY[ 
 
 
 
 
 	
r   #defaultdict[int, list[m.PropertyT]]c                    t          t                    }| j        D ]"}||j                                     |           #|S )a  Properties in this collection grouped by property-id (PID).

        Not sure if this solves an actual problem in practice, but it's at least
        theoretically possible that the same PID could appear twice in a property collection
        with different PTYPs.
        )r   listr   r"   append)r   properties_by_pidr`   s      r   r\   zProperties._properties_by_pid   sJ     BMTARAR( 	/ 	/Aae$++A....  r   (types.MappingProxyType[int, m.PropertyT]c                H    t          j        d | j        D                       S )z5The property objects in this collection keyed by pid.c                    i | ]
}|j         |S  )r"   )ri   r`   s     r   
<dictcomp>z2Properties._properties_mapping.<locals>.<dictcomp>   s    &Q&Q&QAqua&Q&Q&Qr   )typesMappingProxyTyper   r    s    r   r7   zProperties._properties_mapping   s'     %&Q&Q9P&Q&Q&QRRRr   tuple[m.PropertyT, ...]c                     t          j        d          t           j        fd          }t	           fd|D                       S )zrProperty object for each property in this collection.

        Properties are in property-id (PID) order.
        rN   c                J                         | d d                   d         S )NrO   r   )rS   )xrT   s    r   <lambda>z/Properties._property_sequence.<locals>.<lambda>   s&    SZZPQRTSTRTPUEVEVWXEY r   )keyc              3  r   K   | ]1}t                               |j        j        j                   V  2dS )rU   r   str_prop_encodingr4   N)BasePropertyfactoryr   r2   r4   )ri   rU   r   s     r   rj   z0Properties._property_sequence.<locals>.<genexpr>   sb       
 
    "&"9"0	 !  
 
 
 
 
 
r   )rP   rQ   sortedrR   rk   )r   segmentsrT   s   ` @r   r   zProperties._property_sequence   sn     mF##$5;Y;Y;Y;YZZZ 
 
 
 
 $
 
 
 
 
 	
r   N)r   r   r   r   )r   r   )r"   r   r   r#   r   r-   )r"   r   r   r5   )r"   r   r   r;   )r"   r   r   r?   r   rE   )r"   r   r   rL   )r"   r   rW   rX   r   rY   )r   rd   )r   rn   )r   rs   )r   rz   )__name__
__module____qualname____doc__r   r!   r,   r   r4   r:   r>   rD   rK   r/   r%   r2   rR   r\   r7   r   rv   r   r   r   r   %   s       >>B B B B- - - -
4 
4 
4 
4 ' ' ' \'2 2 2 2
3 
3 
3 
3
4 
4 
4 
4 = = = \=          \  	
 	
 	
 \	
 
! 
! 
! \
! S S S \S 
 
 
 \
 
 
r   r   c                      e Zd ZU dZ ej        d          Zded<    ej        d          Zded<   dd	Z	e
dd            Zedd            Zedd            Zedd            Zedd            Zedd            ZdS )r   z6Base class for properties, providing common behaviors.rN   Final[struct.Struct]rT   <HPTYPrU   bytesc                    || _         d S r   _segment)r   rU   s     r   r   zBaseProperty.__init__   s    r   r   r   r   r-   r4   r   m.PropertyTc                (   | j                             |dd                   d         }|t          j        k    rt	          ||          S |t          j        k    rt          |          S |t          j        k    rt          |          S |t          j	        k    rt          ||          S |t          j        k    rt          |          S |t          j        k    rt          |          S |t          j        k    rt!          ||          S |t          j        k    rt%          ||||          S |t          j        k    rt)          |          S t+          dt-          |                     t+          dt/          |                     t          |          S )zFConstruct a property object of the appropriate sub-type for `segment`.N   r   r   zlen(segment)=zrepr(segment)=)r   rS   r&   r'   r(   PTYP_BOOLEANBooleanPropertyPTYP_FLOATING_64Float64Property	PTYP_GUIDGuidPropertyPTYP_INTEGER_16Int16PropertyPTYP_INTEGER_32r=   rA   rC   rB   String8Property	PTYP_TIMEr9   printrh   repr)clsrU   r   r   r4   r]   s         r   r   zBaseProperty.factory   s   
 xwrr{++A.1=  !'73331>!!"7+++1%%%"7+++1;1111$$$ )))1$$$ )))1=  !'73331>!!""3+	    1;((( 	 W  !!!!g!!"""W%%%r   c                `    t           j                            | j                  }||j        ndS )z?The Microsft name for this property, like "PidTagMessageClass".Nznot recorded in model)refproperty_descriptorsr8   r"   ms_name)r   	prop_descs     r   namezBaseProperty.name  s0     ,00::	$-$9y  ?VVr   r   c                \    | j                             | j        dd                   d         S )zJThe property-id (PID) for this property, like 0x3701 for attachment bytes.NrO   r   )rT   rS   r   r    s    r   r"   zBaseProperty.pid  s(     xt}RaR011!44r   c                \    | j                             | j        dd                   d         S )zGThe property-type (PTYP) for this property, like 0x0102 for PtypBinary.Nr   r   )r   rS   r   r    s    r   r]   zBaseProperty.ptyp  s*     ybqb 122155r   c                r    t           j                            | j                  }|r|j        n
| j        ddS )zCThe Microsft name for the type of this property, like "PtypString".04Xz not recorded in model)r   property_type_descriptorsr8   r]   r   )r   prop_type_descs     r   	ptyp_namezBaseProperty.ptyp_name  s?     6::49EE&4bN""TY:b:b:b:b	
r   c                     | j         dd         S )zOThe latter 8 bytes of the property segment, where the property value is stored.   Nr   r    s    r   _payloadzBaseProperty._payload'  s     }QRR  r   N)rU   r   )
rU   r   r   r   r   r-   r4   r-   r   r   r   r   r   r   r   )r   r   r   r   rP   rQ   rT   __annotations__r   r   classmethodr   r+   r   r"   r]   r   r   r   rv   r   r   r   r      s/        @@ -f 5 5C5555!.t!4!4D4444        )& )& )& [)&V W W W XW
 5 5 5 X5 6 6 6 X6 
 
 
 X
 ! ! ! \! ! !r   r   c                  <     e Zd ZdZd	 fdZed
d            Z xZS )r(   z'Property for PtypBinary OLE properties.rU   r   r   r   c                X    t                                          |           || _        d S r   superr   r   r   rU   r   	__class__s      r   r   zBinaryProperty.__init__0  &    !!!r   r   c                L    | j                             | j        | j                  S )z"The bytes of this binary property.)r   property_stream_bytesr"   r]   r    s    r   r)   zBinaryProperty.value4  s      }2248TYGGGr   rU   r   r   r   r   r   r   r   r   r   r   r)   __classcell__r   s   @r   r(   r(   -  sk        11            H H H \H H H H Hr   r(   c                  V    e Zd ZU dZ ej        d          Zded<   ed	d            Z	dS )
r   z(Property for PtypBoolean OLE properties.z<br   SIGNED_CHARr   rE   c                d    | j                             | j        dd                   d         dk    S )z#The boolean value of this property.N   r   )r   rS   r   r    s    r   r)   zBooleanProperty.value?  s0     &&t}RaR'899!<AAr   Nr   )
r   r   r   r   rP   rQ   r   r   r   r)   rv   r   r   r   r   :  s]         22(5d(;(;K;;;;B B B \B B Br   r   c                  V    e Zd ZU dZ ej        d          Zded<   ed	d            Z	dS )
r   z+Property for PtypFloating64 OLE properties.z<dr   FLOAT64r   floatc                L    | j                             | j                  d         S )z1The 64-bit floating-point value of this property.r   )r   rS   r   r    s    r   r)   zFloat64Property.valueJ  s!     |""4=11!44r   N)r   r   )
r   r   r   r   rP   rQ   r   r   r   r)   rv   r   r   r   r   E  sW         55$1FM$$7$7G77775 5 5 \5 5 5r   r   c                  p     e Zd ZU dZ ej        d          Zded<   d fd	ZddZ	e
dd            Z xZS )r   z%Property for PtypGuid OLE properties.z<IHH8sr   GUIDrU   r   r   r   c                X    t                                          |           || _        d S r   r   r   s      r   r   zGuidProperty.__init__U  r   r   r   r-   c                *    t          | j                  S )zPHex str representation of this UUID like '9d947746-9662-40a8-a526-abd4faec9737'.)r-   r)   r    s    r   __str__zGuidProperty.__str__Y  s    4:r   	uuid.UUIDc                    t          j        | j                            | j        | j                  dd                   S )zThe value of this property as a uuid.UUID object.

        The `str` value of this object is the standard-form string for the UUID, like:
        '9d947746-9662-40a8-a526-abd4faec9737'.
        Nrg   )bytes_le)uuidUUIDr   r   r"   r]   r    s    r   r)   zGuidProperty.value]  s;     y$-"E"EdhPTPY"Z"Z[^\^[^"_````r   r   r   )r   r   )r   r   r   r   rP   rQ   r   r   r   r   r   r)   r   r   s   @r   r   r   P  s         //!.x!8!8D8888               a a a \a a a a ar   r   c                  V    e Zd ZU dZ ej        d          Zded<   ed	d            Z	dS )
r   z*Property for PtypInteger16 OLE properties.r   r   INT16r   r   c                \    | j                             | j        dd                   d         S )#The integer value of this property.Nr   r   )r   rS   r   r    s    r   r)   zInt16Property.valuen  *     z  rr!233A66r   Nr   )
r   r   r   r   rP   rQ   r   r   r   r)   rv   r   r   r   r   i  W         44"/&-"5"5E55557 7 7 \7 7 7r   r   c                  V    e Zd ZU dZ ej        d          Zded<   ed	d            Z	dS )
r=   z*Property for PtypInteger32 OLE properties.z<Ir   INT32r   r   c                \    | j                             | j        dd                   d         S )r   NrO   r   )r   rS   r   r    s    r   r)   zInt32Property.valuey  r   r   Nr   )
r   r   r   r   rP   rQ   r   r   r   r)   rv   r   r   r=   r=   t  r   r   r=   c                  <     e Zd ZdZd
 fdZedd	            Z xZS )rC   z'Property for PtypString OLE properties.rU   r   r   r   c                X    t                                          |           || _        d S r   r   r   s      r   r   zStringProperty.__init__  r   r   r   r-   c                r    | j                             | j        | j                                      d          S )z*The decoded str from this string property.z	utf-16-le)r   r   r"   r]   decoder    s    r   r)   zStringProperty.value  s.     }2248TYGGNN{[[[r   r   r   r   r   s   @r   rC   rC     sk        11            \ \ \ \\ \ \ \ \r   rC   c                  <     e Zd ZdZd fd	Zedd            Z xZS )r   zHProperty for PtypString8 (8-bit characters, not Unicode) OLE properties.rU   r   r   r   r   r-   r4   c                t    t                                          |           || _        || _        || _        d S r   )r   r   r   r2   _body_encoding)r   rU   r   r   r4   r   s        r   r   zString8Property.__init__  s:     	!!!"3+r   r   c                    | j                             | j        | j                                      | j        t
          j        k    r| j        n| j                  S )zThe encoded bytes of this string property.

        The caller is responsible for determining the encoding and applying it to get a str value.
        )	r   r   r"   r]   r   r&   PID_BODYr   r2   r    s    r   r)   zString8Property.value  sN     }2248TYGGNN#'8qz#9#9Dt?V
 
 	
r   )rU   r   r   r   r   r-   r4   r-   r   r   r   s   @r   r   r     sc        RR, , , , , , 
 
 
 \
 
 
 
 
r   r   c                  V    e Zd ZU dZ ej        d          Zded<   ed	d            Z	dS )
r9   z%Property for PtypTime OLE properties.z<Qr   TIMEr   dt.datetimec                    | j                             | j                  d         }t          j        dddt          j        j                  }|dz  }|t          j        |          z   S )z:The value of this property as a timezone-aware `datetime`.r   iA  r   )tzinfog    cA)seconds)r   rS   r   dtdatetimetimezoneutc	timedelta)r   (hundred_nanosecond_intervals_since_epochepochseconds_since_epochs       r   r)   zTimeProperty.value  sb     4893C3CDM3R3RST3U0D!Qr{???F#Mr|,?@@@@@r   N)r   r   )
r   r   r   r   rP   rQ   r   r   r   r)   rv   r   r   r9   r9     s]         //!.t!4!4D4444A A A \A A Ar   r9   blockr   nr   r   Iterator[bytes]c              #     K   |dk     rt          d          t          |           }t          t          j        ||                    x}r*|V  t          t          j        ||                    x}(dS dS )zBatch bytes from `block` into segments of `n` bytes each.

    Last batch is shorter than `n` when `block` is not evenly divisible by `n`.
    r   zn must be at least oneN)
ValueErrorr   r   	itertoolsislice)r   r   
iter_bytesbatchs       r   rl   rl     s      
 	1uu1222eJ)*a8899
9%  )*a8899
9%     r   )r   r   r   r   r   r   )%r   
__future__r   r   r   r   rP   rx   r   collectionsr   typingr   r   r   oxmsg.domainr   r&   r	   r
   rH   r   r   
oxmsg.utilr   r   r   r(   r   r   r   r   r=   rC   r   r9   rl   rv   r   r   <module>r     s   & # " " " " "            # # # # # # ( ( ( ( ( ( ( ( ( ( ' ' ' ' ' ' " " " " " " # # # # # # ) ) ) ) ) ) # # # # # #r
 r
 r
 r
 r
 r
 r
 r
jP! P! P! P! P! P! P! P!f
H 
H 
H 
H 
H\ 
H 
H 
HB B B B Bl B B B5 5 5 5 5l 5 5 5a a a a a< a a a27 7 7 7 7L 7 7 77 7 7 7 7L 7 7 7
\ 
\ 
\ 
\ 
\\ 
\ 
\ 
\
 
 
 
 
l 
 
 
,A A A A A< A A A	 	 	 	 	 	r   