o
    l>js                     @  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	Z	ddl
Z
ddlmZ ddlmZmZ eeZG dd deZd*ddZd+ddZd+ddZd,ddZd-ddZd.ddZd/ddZd0d!d"Zd1d$d%Zd2d(d)ZdS )3z0Pure protocol helpers for the Reminders service.    )annotationsN)urlparse   )reminders_pb2versioned_document_pb2c                   @  s   e Zd ZdZdS )CRDTDecodeErrorz7Raised when a Reminders CRDT payload cannot be decoded.N)__name__
__module____qualname____doc__ r   r   Z/home/thesage/.local/lib/python3.10/site-packages/pyicloud/services/reminders/_protocol.pyr      s    r   keystrreturnc                 C  s,   |  |}|r|jrt|jdr|jjS dS )z8Extract recordName from a REFERENCE field, or return ''.
recordName )	get_fieldvaluehasattrr   )fieldsr   fieldr   r   r   	_ref_name   s   
r   r   prefixc                 C  s4   | s| S t | } | d}| |r| S | |  S )zDReturn a record name with the expected prefix (e.g. ``Alarm/UUID``)./)r   
startswithr   r   tokenr   r   r   _as_record_name   s   

r   c                 C  s8   | s| S t | } | d}| |r| t|d S | S )z5Return a raw UUID/id token without the record prefix.r   N)r   r   lenr   r   r   r   
_as_raw_id*   s   

r    boolc                 C  s>   | sdS t | }|jsdS |jdv rt|jS t|jp|jS )z4Return True for values that already look like a URL.F>   httphttps)r   schemer!   netlocpath)r   parsedr   r   r   _looks_like_url5   s   

r(   c              
   C  s|   | sdS t | r
| S ddt| d  d  }ztj|  | ddd}W n tjttfy5   |  Y S w t |r<|S | S )z=Decode a URL attachment value, falling back to the raw value.r   =   T)validateutf-8)	r(   r   base64	b64decodedecodebinasciiErrorUnicodeDecodeError
ValueError)r   paddingdecodedr   r   r   _decode_attachment_urlB   s$   
r6   dict[str, str]c                 C  s$   t | pddd}d|dS )z?Encode text for CloudKit fields that store UTF-8 payload bytes.r   r,   asciiENCRYPTED_BYTES)typer   )r-   	b64encodeencoder/   )r   encodedr   r   r   _encode_cloudkit_text_fieldW   s   
r>   objectc                 C  s6   | du rdS t | tr| S t | tr| dS t| S )z6Decode a CloudKit text field value into plain ``str``.Nr   r,   )
isinstancer   bytesr/   )r   r   r   r   _decode_cloudkit_text_value]   s   


rB   encrypted_valuestr | bytesc           	      C  s*  | }t |tr5dt|d  }|dkr|d| 7 }zt|}W n tjtfy4 } ztd|d}~ww zt	
|}W n3 t	jyo   zddl}|
|}W n tyl } ztd||dd  W Y d}~nd}~ww Y nw z#t }|| |jr|jd j}t }|| |jpdW S W n ty } ztd	| W Y d}~nd}~ww zt }|| |jrt }||j |jpdW S W n ty } ztd
| W Y d}~nd}~ww zt }|| |jr|jW S W td ty } ztd| W Y d}~tdd}~ww )z8Decode a CRDT document (TitleDocument or NotesDocument).r*   r)   z$Invalid base64-encoded CRDT documentNr   z CRDT decompress skipped: %s (%s)
   r   z,versioned_document.Document parse failed: %sz+versioned_document.Version parse failed: %szbare String parse failed: %szUnable to decode CRDT document)r@   r   r   r-   r.   r0   r1   r3   r   zlib
decompresserrorgzipOSErrorLOGGERdebugr   DocumentParseFromStringversiondatar   Stringstring	ExceptionVersion)	rC   rP   r4   exc_gzipdocumentstring_bytesr   rO   r   r   r   _decode_crdt_documenth   sx   

"



rY   textc                 C  s  | rt | nd}td}d}t }| |_|j }d|j_	d|j_
d|_d|j_	d|j_
|jd |dkrV|j }d|j_	d|j_
||_d|j_	d|j_
|jd |j }d|j_	||j_
d|_d|j_	||j_
|jj
 }||_|j }	||	_
|j }
d|
_
|dkr|j }||_| }t }d|_d|_||_t }d|_|j| | }t|}t|dS )z?Encode a string into an Apple versioned topotext CRDT document.r    d46bcae41b8766c18d75efe35c9145c3l    r      r,   )r   rA   fromhexr   rQ   rR   	substringaddcharID	replicaIDclocklength	timestampchildappendreplicaUUIDreplicaClockattributeRunSerializeToStringr   rT   serializationVersionminimumSupportedVersionrP   rM   rO   rF   compressr-   r;   r/   )rZ   text_lengthreplica_uuid	clock_maxr   sentinelcontentterminaltimestamp_clockcontent_clocksentinel_clockattribute_runrX   rO   rW   	doc_bytes
compressedr   r   r   _encode_crdt_document   s\   







rz   fields_modified	list[str]c                 C  sH   t   d }i }| D ]}d|tt  d||< q
tjd|iddS )z;Generate a ResolutionTokenMap for a set of modified fields.g   @'Ar   )countermodificationTimera   map),:)
separators)timer   uuiduuid4upper_jsondumps)r{   apple_epochtokens
field_namer   r   r   _generate_resolution_token_map   s   r   )r   r   r   r   )r   r   r   r   r   r   )r   r   r   r!   )r   r   r   r   )r   r   r   r7   )r   r?   r   r   )rC   rD   r   r   )rZ   r   r   r   )r{   r|   r   r   )r   
__future__r   r-   r0   jsonr   loggingr   r   rF   urllib.parser   protobufr   r   	getLoggerr   rK   r3   r   r   r   r    r(   r6   r>   rB   rY   rz   r   r   r   r   r   <module>   s.    









6<