o
    l>j
                     @  s   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	m
Z
mZ ddlmZmZ ddlmZ eeZdddZdddZG dd dZdS )z?Decoding of compressed note data from TextDataEncrypted format.    )annotationsN)ListOptionalUnion   )AttachmentIdNoteBody)	notes_pb2val&Optional[Union[str, bytes, bytearray]]returnOptional[bytes]c                 C  s   | du rdS t | ttfrtdt|  t| S t | trMztj| dd}tdt| t| |W S  t	j
yL   tdt|  | jddd	 Y S w dS )
z9Accepts base64 string OR raw bytes and returns raw bytes.Nz notes.decoder.input_bytes len=%dT)validatez*notes.decoder.input_b64 len=%d -> bytes=%dz%notes.decoder.input_str_nonb64 len=%dzutf-8replace)errors)
isinstancebytes	bytearrayLOGGERdebuglenstrbase64	b64decodebinasciiErrorencode)r
   out r   U/home/thesage/.local/lib/python3.10/site-packages/pyicloud/services/notes/decoding.py_b64_to_bytes   s"   
r    blobr   c                 C  s`   t | dkr| d dkr| d dkrt| S zt| W S  tjy/   t| tj  Y S w )N   r      r      )r   gzip
decompresszliberror	MAX_WBITS)r!   r   r   r   _decompress&   s   $
r*   c                   @  s   e Zd ZdZd	ddZdS )
BodyDecoderz:Decode TextDataEncrypted (base64, compressed) to NoteBody.text_data_encrypted_b64r   r   Optional[NoteBody]c              
   C  s  |d u rd S t |}|sd S zt|}W n ty- } ztd| W Y d }~d S d }~ww z{t }|| tt|dd dd }|rJt|dd nd }g }|rt	 }	t|dg D ]=}
t|
dd }|rt|dd sot|dd rt|dd	pvd	}t|dd p~d }||f}||	v rqY|	
| |t||d
 qYtdt|t| t|||dW S  ty } ztd| W Y d }~d S d }~ww )Nz notes.decoder.decompress_fail %sdocumentnote	note_textattribute_runattachment_infoattachment_identifiertype_uti )
identifierr4   z-notes.decoder.attachments ids=%d note_text=%s)r   textattachment_idsz!notes.decoder.proto_parse_fail %s)r    r*   	Exceptionr   r   r	   NoteStoreProtoParseFromStringgetattrsetaddappendr   r   boolr   )selfr,   rawdocemsgr/   r7   idsseenrunaiidentr4   keyr   r   r   decode2   s\   



zBodyDecoder.decodeN)r,   r   r   r-   )__name__
__module____qualname____doc__rL   r   r   r   r   r+   /   s    r+   )r
   r   r   r   )r!   r   r   r   )rP   
__future__r   r   r   r%   loggingr'   typingr   r   r   domainr   r   protobufr	   	getLoggerrM   r   r    r*   r+   r   r   r   r   <module>   s    


	