a
    ¡˜ëjõ  ã                   @  s€   d Z ddlmZ ddlZddlmZmZ ddlm	Z	 ddl
mZmZmZ ddlmZ ejG dd	„ d	ƒƒZejG d
d„ dƒƒZdS )aF  A "folder" in Microsoft Compound File Binary (CFB) format.

The CFB/OLE file format encloses a filesystem, to a first appoximation, much like a Zip
archive does. In this format, a "storage" corresponds to a directory and a "stream"
corresponds to a file. A storage can contain both streams and other storages.

Each MSG file has a "root" storage, represented in this package by `MessageStorage`.
Each attachment and recipient has their own storage in the root, with a pre-defined
name, and there are other top-level objects than can appear in a MSG file that get their
own storage.
é    )ÚannotationsN)ÚIteratorÚMapping)Ú	OleFileIO)ÚSTGTY_STORAGEÚSTGTY_STREAMÚOleDirectoryEntry)Úlazypropertyc                   @  s¶   e Zd ZU dZded< ded< ded< ddœd	d
„Zed#dddd dœdd„ƒZddœdd„Zddœdd„Z	e
ddœdd„ƒZe
ddœdd„ƒZddddœdd„Ze
d dœd!d"„ƒZdS )$ÚStoragez'Container for streams and sub-storages.ÚstrÚpathztuple[Stream, ...]Ústreamsztuple[Storage, ...]Ústorages©Úreturnc                 C  s*   dt | jƒ› dt| jƒ› dt| jƒ› d�S )NzStorage(path=ú, z
 streams, z
 storages))Úreprr   Úlenr   r   ©Úself© r   úL/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/oxmsg/storage.pyÚ__repr__    s    ÿÿzStorage.__repr__NÚ r   zOleDirectoryEntry | None©ÚoleÚnodeÚprefixr   c                   s\   |r|nˆj }dddddœdd„}t|ˆ|ˆƒƒ}t‡ ‡‡fdd	„|jD ƒƒ}ˆ ˆ||d
�S )zPReturn a Storage loaded from `node` and containing its streams and sub-storages.r   r   r   zIterator[Stream]r   c              	   s  sr   dd„ |j D ƒD ]\}|r(|› d|j› �n|j}|  |¡�}| ¡ }W d  ƒ n1 sV0    Y  t||ƒV  qdS )z4Generate `Stream` object for each stream in `nodes`.c                 s  s   | ]}|j tkr|V  qd S )N)Ú
entry_typer   ©Ú.0Úkr   r   r   Ú	<genexpr>0   ó    z:Storage.from_ole.<locals>._iter_streams.<locals>.<genexpr>ú/N)ÚkidsÚnameZ
openstreamÚreadÚStream)r   r   r   Zstream_noder   ÚfÚbytes_r   r   r   Ú_iter_streams.   s
    &z'Storage.from_ole.<locals>._iter_streamsc                 3  s<   | ]4}|j tkrˆ  ˆ|ˆr,ˆ› d |j› �n|j¡V  qdS )r$   N)r   r   Úfrom_oler&   r   ©Úclsr   r   r   r   r"   7   s   
þz#Storage.from_ole.<locals>.<genexpr>)r   r   r   )ÚrootÚtupler%   )r.   r   r   r   r+   r   Zsub_storagesr   r-   r   r,   &   s    þzStorage.from_olezIterator[Storage]c                 c  s"   | j D ]}|j d¡r|V  qdS )zDGenerate storage object specific to each attachment in this message.z__attach_version1.0_#N©r   r&   Ú
startswith©r   Úsr   r   r   Úiter_attachment_storages>   s    
z Storage.iter_attachment_storagesc                 c  s"   | j D ]}|j d¡r|V  qdS )zBGenerate storage object specific to each recipent in this message.z__recip_version1.0_#Nr1   r3   r   r   r   Úiter_recipient_storagesD   s    
zStorage.iter_recipient_storagesc                 C  s   | j  d¡d S )z:The "directory-name" of this storage, with no path-prefix.r$   éÿÿÿÿ©r   Úsplitr   r   r   r   r&   J   s    zStorage.nameÚbytesc                 C  s   | j d jS )zDThe bytes of the one-and-only-one properties stream in this storage.z__properties_version1.0©Ú_streams_by_namer*   r   r   r   r   Úproperties_stream_bytesO   s    zStorage.properties_stream_bytesÚint)ÚpidÚptypr   c                 C  s   | j d|d›|d›� jS )zCRead variable-length property bytes from the stream it's stored in.z__substg1.0_Z04Xr;   )r   r?   r@   r   r   r   Úproperty_stream_bytesU   s    zStorage.property_stream_byteszMapping[str, Stream]c                 C  s   dd„ | j D ƒS )z*dict semantics on streams of this storage.c                 S  s   i | ]}|j |“qS r   )r&   )r    r4   r   r   r   Ú
<dictcomp>_   r#   z,Storage._streams_by_name.<locals>.<dictcomp>)r   r   r   r   r   r<   \   s    zStorage._streams_by_name)Nr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   Úclassmethodr,   r5   r6   r	   r&   r=   rA   r<   r   r   r   r   r
      s"   
 ÿr
   c                   @  sB   e Zd ZU dZded< ded< ddœdd„Zeddœd	d
„ƒZdS )r(   z;Bytes of a property of a top-level object in an OXMSG file.r   r   r:   r*   r   c                 C  s    dt | jƒ› dt| jƒd›d�S )NzStream(path=r   ú,z bytes))r   r   r   r*   r   r   r   r   r   i   s    zStream.__repr__c                 C  s   | j  d¡d S )z3The "filename" of this stream, with no path-prefix.r$   r7   r8   r   r   r   r   r&   l   s    zStream.nameN)rC   rD   rE   rF   rG   r   r	   r&   r   r   r   r   r(   b   s   
r(   )rF   Ú
__future__r   ZdataclassesÚdcÚtypingr   r   Zolefiler   Zolefile.olefiler   r   r   Z
oxmsg.utilr	   Z	dataclassr
   r(   r   r   r   r   Ú<module>   s   I