o
    l>j                     @  s   U 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mZm	Z	m
Z
mZ eeZeh dZded< dddZddd ddZd!ddZd"ddZdS )#a  Codecs for base64-JSON Invites fields.

Several `EventDetails` and `cloudkit.share` fields are typed as ``BYTES`` (or
``ENCRYPTED_BYTES``) on the wire but carry base64-encoded JSON. CloudKit Web
Services hands the inner JSON back decrypted to authenticated sessions, so
decoding is just ``base64 -> utf-8 -> json``.

The decoder is permissive (returns ``None`` for malformed input rather than
raising) so a single malformed record can't poison a page of results.
    )annotationsN)AnyMappingOptionalUnion>   timeplacestyle
backgroundintegrationszfrozenset[str]JSON_BYTES_FIELDSvalue&Optional[Union[str, bytes, bytearray]]returnOptional[Any]c              	   C  s   | du rdS t | tr-ztj| dd}W t
|S  tjtfy,   tdt	|  Y dS w t | ttfrft| }t
|dd}|durD|S ztj|dd}W t
|S  tjtfye   tdt	| Y dS w dS )a  Decode a base64-encoded JSON blob to its Python value.

    Accepts either a base64 ``str`` (the wire form) or ``bytes``/``bytearray``.
    For ``bytes`` we try parsing as raw UTF-8 JSON first (Pydantic's
    ``Base64Bytes`` decodes the wire base64 for us, so the value reaches
    this codec as already-decoded JSON bytes) and fall back to a second
    base64-decode attempt if that fails (handles callers passing the wire
    base64 form as ``bytes``). Returns ``None`` when ``value`` is missing
    or cannot be decoded.
    NT)validatez%invites.codecs.b64_decode_fail len=%dFlog_failuresz+invites.codecs.json_parse_fail bytes_len=%d)
isinstancestrbase64	b64decodebinasciiError
ValueErrorLOGGERdebuglen_parse_json_bytesbytes	bytearray)r   rawparseddecoded r$   U/home/thesage/.local/lib/python3.10/site-packages/pyicloud/services/invites/codecs.pydecode_json_bytes   s.   
r&   Tr   r!   r   r   boolc                C  sp   z|  d}W n ty   |rtdt|  Y d S w zt|W S  tjy7   |r4tdt| Y d S w )Nutf-8z&invites.codecs.utf8_decode_fail len=%dz*invites.codecs.json_parse_fail text_len=%d)decodeUnicodeDecodeErrorr   r   r   jsonloadsJSONDecodeError)r!   r   textr$   r$   r%   r   C   s   r   r   r   c                 C  s&   t j| ddd}t|ddS )zCEncode a Python value as a base64-encoded JSON string for the wire.),:F)
separatorsensure_asciir(   ascii)r+   dumpsr   	b64encodeencoder)   )r   r.   r$   r$   r%   encode_json_bytesR   s   r7   blobOptional[Mapping[str, Any]]tuple[str, ...]c                 C  sd   t | tsdS | d}t |tsdS g }|D ]}t |tr-|d}t |tr-|| qt|S )zExtract the list of widget type strings from a decoded ``integrations`` blob.

    Observed shape: ``{"version": "1", "data": [{"type": "com.apple.widget.weather"}, ...]}``.
    Returns an empty tuple when the blob is missing or malformed.
    r$   datatype)r   r   getlistr   appendtuple)r8   r;   outentrytr$   r$   r%   decode_integrationsX   s   






rD   )r   r   r   r   )r!   r   r   r'   r   r   )r   r   r   r   )r8   r9   r   r:   )__doc__
__future__r   r   r   r+   loggingtypingr   r   r   r   	getLogger__name__r   	frozensetr   __annotations__r&   r   r7   rD   r$   r$   r$   r%   <module>   s    

&
