
    =[j                        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j        e          Z eh d          Zded<   ddZddddZddZ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                h   | dS t          | t                    rl	 t          j        | d          }nE# t          j        t          f$ r, t                              dt          |                      Y dS w xY wt          |          S t          | t          t          f          rt          |           }t          |d          }||S 	 t          j        |d          }nE# t          j        t          f$ r, t                              dt          |                     Y dS w xY wt          |          S 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decodeds       h/home/thesage/.hermes/hermes-agent/venv/lib/python3.11/site-packages/pyicloud/services/invites/codecs.pydecode_json_bytesr'      sD    }t% &	"54888CC
+ 	 	 	LL@#e**MMM44	 !%%%%%+,, *Ell"3U;;;M	&sT:::GG
+ 	 	 	LLFCQQQ44	 !)))4s!   2 >A43A4C >D D Tr   r#   r!   r   boolc               P   	 |                      d          }n;# t          $ r. |r(t                              dt	          |                      Y d S w xY w	 t          j        |          S # t
          j        $ r. |r(t                              dt	          |                     Y d S w xY 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   texts      r&   r    r    C   s    zz'""    	MLLA3s88LLLttz$    	RLLEs4yyQQQtts!    4AAA( (9B%$B%r   r   c                    t          j        | dd          }t          j        |                    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   r0   s     r&   encode_json_bytesr:   R   sC    :e
GGGDDKK001188AAA    blobOptional[Mapping[str, Any]]tuple[str, ...]c                \   t          | t                    sdS |                     d          }t          |t                    sdS g }|D ]V}t          |t                    r?|                    d          }t          |t                    r|                    |           Wt          |          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.
     datatype)r   r   getlistr   appendtuple)r<   rA   outentryts        r&   decode_integrationsrJ   X   s     dG$$ r88FDdD!! rC  eW%% 			&!!A!S!! 

1::r;   )r   r   r   r   )r#   r!   r   r(   r   r   )r   r   r   r   )r<   r=   r   r>   )__doc__
__future__r   r   r   r-   loggingtypingr   r   r   r   	getLogger__name__r   	frozensetr   __annotations__r'   r    r:   rJ   r@   r;   r&   <module>rS      s!  	 	 	 # " " " " "     0 0 0 0 0 0 0 0 0 0 0 0		8	$	$ %.I<<<% %     
# # # #L ;?      B B B B     r;   