o
    C©ÌhR©  ã                   @  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ZddlZddl	m
Z
mZmZmZmZmZmZmZmZmZmZ ddlmZmZmZmZ ddlmZ ddlmZmZ ddlm Z m!Z!m"Z"m#Z#m$Z$ dd	l%m&Z& dd
l'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 ej9ej:ej;ej<ej=ej>dœZ?G dd„ dƒZ@G dd„ dƒZAe
rÇeeeBef  ZCneZCdZDG dd„ deCƒZEeEeAjFd�ZGdeHd< 	 eEeAjId�ZJdeHd< 	 eEeAjKd�ZLdeHd< 	 eLZMdeHd< 	 dÕd&d'„ZNdÖd*d+„ZOeMfd×d-d.„ZPeMfdØd1d2„ZQeMfdÙd5d6„ZRdÚd9d:„ZSdÛd<d=„ZTdÜdAdB„ZUdÛdCdD„ZVdÛdEdF„ZWdÝdHdI„ZXdÞdKdL„ZYdßdMdN„ZZdàdPdQ„Z[dádSdT„Z\dÚdUdV„Z]dÚdWdX„Z^dâdYdZ„Z_dãd\d]„Z`däd_d`„Zadådbdc„Zbdædedf„Zcdçdhdi„ZddÛdjdk„Zedèdmdn„Zfi doeY“dpe]“dqeX“dreS“dsec“dted“duee“dve[“dweT“dxdydz„ “d{e`“d|ef“d}eb“d~e^“de\“d€eZ“d�e_“d‚eai¥ZgdƒeHd„< ehegƒZidéd†d‡„Zjdêd‰dŠ„Zkdëd‹dŒ„Zldìd�dŽ„Zmdíd�d�„Zndêd‘d’„Zodîd“d”„Zpdïd•d–„Zqdðd˜d™„Zrdñdšd›„Zsdòd�dž„Ztdód d¡„Zudôd¢d£„Zvdõd¤d¥„Zwdõd¦d§„Zxdöd©dª„Zyd÷d¬d­„Zzd÷d®d¯„Z{i e|en“e}es“ejer“e"ek“e~eq“eep“eBen“e€dƒen“ej�eu“eet“e*em“eel“e&ey“e,e{“e.ez“e0ev“e2eo“e4eoe6ewe(exi¥Z‚d°eHd±< i Zƒd²eHd³< e‚D ]Z„e…e„d´ƒ�rwe‚e„ eƒe„j†< �qhe‡dµd¶„ e‚D ƒƒZˆeMfd×d·d¸„Z‰død¹dº„ZŠdùd»d¼„Z‹dúd¾d¿„ZŒdûdÀdÁ„Z�e0dÂedÃe*dÃe(dÃe6dÄe.dÅe,dÅiZŽdÆeHdÇ< eBeŠe}eŠeje‹e2eŒe&e�iZ�dÈeHdÉ< düdýdÌdÍ„Z�dþdÐdÑ„Z‘dÿdÓdÔ„Z’dS (   a\  Tools for using Python's :mod:`json` module with BSON documents.

This module provides two helper methods `dumps` and `loads` that wrap the
native :mod:`json` methods and provide explicit BSON conversion to and from
JSON. :class:`~bson.json_util.JSONOptions` provides a way to control how JSON
is emitted and parsed, with the default being the Relaxed Extended JSON format.
:mod:`~bson.json_util` can also generate Canonical or legacy `Extended JSON`_
when :const:`CANONICAL_JSON_OPTIONS` or :const:`LEGACY_JSON_OPTIONS` is
provided, respectively.

.. _Extended JSON: https://github.com/mongodb/specifications/blob/master/source/extended-json/extended-json.md

Example usage (deserialization):

.. doctest::

   >>> from bson.json_util import loads
   >>> loads(
   ...     '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$scope": {}, "$code": "function x() { return 1; }"}}, {"bin": {"$type": "80", "$binary": "AQIDBA=="}}]'
   ... )
   [{'foo': [1, 2]}, {'bar': {'hello': 'world'}}, {'code': Code('function x() { return 1; }', {})}, {'bin': Binary(b'...', 128)}]

Example usage with :const:`RELAXED_JSON_OPTIONS` (the default):

.. doctest::

   >>> from bson import Binary, Code
   >>> from bson.json_util import dumps
   >>> dumps(
   ...     [
   ...         {"foo": [1, 2]},
   ...         {"bar": {"hello": "world"}},
   ...         {"code": Code("function x() { return 1; }")},
   ...         {"bin": Binary(b"")},
   ...     ]
   ... )
   '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }"}}, {"bin": {"$binary": {"base64": "AQIDBA==", "subType": "00"}}}]'

Example usage (with :const:`CANONICAL_JSON_OPTIONS`):

.. doctest::

   >>> from bson import Binary, Code
   >>> from bson.json_util import dumps, CANONICAL_JSON_OPTIONS
   >>> dumps(
   ...     [
   ...         {"foo": [1, 2]},
   ...         {"bar": {"hello": "world"}},
   ...         {"code": Code("function x() { return 1; }")},
   ...         {"bin": Binary(b"")},
   ...     ],
   ...     json_options=CANONICAL_JSON_OPTIONS,
   ... )
   '[{"foo": [{"$numberInt": "1"}, {"$numberInt": "2"}]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }"}}, {"bin": {"$binary": {"base64": "AQIDBA==", "subType": "00"}}}]'

Example usage (with :const:`LEGACY_JSON_OPTIONS`):

.. doctest::

   >>> from bson import Binary, Code
   >>> from bson.json_util import dumps, LEGACY_JSON_OPTIONS
   >>> dumps(
   ...     [
   ...         {"foo": [1, 2]},
   ...         {"bar": {"hello": "world"}},
   ...         {"code": Code("function x() { return 1; }", {})},
   ...         {"bin": Binary(b"")},
   ...     ],
   ...     json_options=LEGACY_JSON_OPTIONS,
   ... )
   '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }", "$scope": {}}}, {"bin": {"$binary": "AQIDBA==", "$type": "00"}}]'

Alternatively, you can manually pass the `default` to :func:`json.dumps`.
It won't handle :class:`~bson.binary.Binary` and :class:`~bson.code.Code`
instances (as they are extended strings you can't provide custom defaults),
but it will be faster as there is less recursion.

.. note::
   If your application does not need the flexibility offered by
   :class:`JSONOptions` and spends a large amount of time in the `json_util`
   module, look to
   `python-bsonjs <https://pypi.python.org/pypi/python-bsonjs>`_ for a nice
   performance improvement. `python-bsonjs` is a fast BSON to MongoDB
   Extended JSON converter for Python built on top of
   `libbson <https://github.com/mongodb/libbson>`_. `python-bsonjs` works best
   with PyMongo when using :class:`~bson.raw_bson.RawBSONDocument`.
é    )ÚannotationsN)ÚTYPE_CHECKINGÚAnyÚCallableÚMappingÚMutableMappingÚOptionalÚSequenceÚTupleÚTypeÚUnionÚcast)ÚALL_UUID_SUBTYPESÚUUID_SUBTYPEÚBinaryÚUuidRepresentation)ÚCode)ÚCodecOptionsÚDatetimeConversion)Ú_MAX_UTC_MSÚEPOCH_AWAREÚ
DatetimeMSÚ_datetime_to_millisÚ_millis_to_datetime)ÚDBRef)Ú
Decimal128)ÚInt64)ÚMaxKey)ÚMinKey)ÚObjectId)ÚRegex)ÚRE_TYPE©Ú	Timestamp)Úutc)ÚiÚlÚmÚsÚuÚxc                   @  ó   e Zd ZdZ	 dZ	 dZdS )ÚDatetimeRepresentationr   é   é   N)Ú__name__Ú
__module__Ú__qualname__ÚLEGACYÚ
NUMBERLONGÚISO8601© r5   r5   ú]/var/www/html/Moveonwheels/move_on_wheels/venv/lib/python3.10/site-packages/bson/json_util.pyr,   ›   s    

r,   c                   @  r+   )ÚJSONModer   r-   r.   N)r/   r0   r1   r2   ÚRELAXEDÚ	CANONICALr5   r5   r5   r6   r7   ¿   s    
r7   l        c                      sˆ   e Zd ZU ded< ded< ded< ded< ded< d‡ fdd„Zdddejfd‡ fdd„Zd ‡ fdd„Zd!‡ fdd„Z	d"dd„Z
‡  ZS )#ÚJSONOptionsÚintÚ	json_modeÚboolÚstrict_number_longÚdatetime_representationÚstrict_uuidzType[MutableMapping[str, Any]]Údocument_classÚargsr   Úkwargsc                   s   t ƒ  ¡  dS )a/  Encapsulates JSON options for :func:`dumps` and :func:`loads`.

        :param strict_number_long: If ``True``, :class:`~bson.int64.Int64` objects
            are encoded to MongoDB Extended JSON's *Strict mode* type
            `NumberLong`, ie ``'{"$numberLong": "<number>" }'``. Otherwise they
            will be encoded as an `int`. Defaults to ``False``.
        :param datetime_representation: The representation to use when encoding
            instances of :class:`datetime.datetime`. Defaults to
            :const:`~DatetimeRepresentation.LEGACY`.
        :param strict_uuid: If ``True``, :class:`uuid.UUID` object are encoded to
            MongoDB Extended JSON's *Strict mode* type `Binary`. Otherwise it
            will be encoded as ``'{"$uuid": "<hex>" }'``. Defaults to ``False``.
        :param json_mode: The :class:`JSONMode` to use when encoding BSON types to
            Extended JSON. Defaults to :const:`~JSONMode.LEGACY`.
        :param document_class: BSON documents returned by :func:`loads` will be
            decoded to an instance of this class. Must be a subclass of
            :class:`collections.MutableMapping`. Defaults to :class:`dict`.
        :param uuid_representation: The :class:`~bson.binary.UuidRepresentation`
            to use when encoding and decoding instances of :class:`uuid.UUID`.
            Defaults to :const:`~bson.binary.UuidRepresentation.UNSPECIFIED`.
        :param tz_aware: If ``True``, MongoDB Extended JSON's *Strict mode* type
            `Date` will be decoded to timezone aware instances of
            :class:`datetime.datetime`. Otherwise they will be naive. Defaults
            to ``False``.
        :param tzinfo: A :class:`datetime.tzinfo` subclass that specifies the
            timezone from which :class:`~datetime.datetime` objects should be
            decoded. Defaults to :const:`~bson.tz_util.utc`.
        :param datetime_conversion: Specifies how UTC datetimes should be decoded
            within BSON. Valid options include 'datetime_ms' to return as a
            DatetimeMS, 'datetime' to return as a datetime.datetime and
            raising a ValueError for out-of-range values, 'datetime_auto' to
            return DatetimeMS objects when the underlying datetime is
            out-of-range and 'datetime_clamp' to clamp to the minimum and
            maximum possible datetimes. Defaults to 'datetime'. See
            `handling out of range datetimes <https://www.mongodb.com/docs/languages/python/pymongo-driver/current/data-formats/dates-and-times/#handling-out-of-range-datetimes>`_ for details.
        :param args: arguments to :class:`~bson.codec_options.CodecOptions`
        :param kwargs: arguments to :class:`~bson.codec_options.CodecOptions`

        .. seealso:: The specification for Relaxed and Canonical `Extended JSON`_.

        .. versionchanged:: 4.0
           The default for `json_mode` was changed from :const:`JSONMode.LEGACY`
           to :const:`JSONMode.RELAXED`.
           The default for `uuid_representation` was changed from
           :const:`~bson.binary.UuidRepresentation.PYTHON_LEGACY` to
           :const:`~bson.binary.UuidRepresentation.UNSPECIFIED`.

        .. versionchanged:: 3.5
           Accepts the optional parameter `json_mode`.

        .. versionchanged:: 4.0
           Changed default value of `tz_aware` to False.
        N)ÚsuperÚ__init__)ÚselfrB   rC   ©Ú	__class__r5   r6   rE   ù   s   6zJSONOptions.__init__NÚclsúType[JSONOptions]úOptional[bool]úOptional[int]Úreturnc                   sz  |  dd¡|d< |d r|  dt¡|d< |tjtjtjd fvr#tdƒ‚ttt	ƒ j
| g|¢R i |¤Žƒ}|tjtjtjfvrAtdƒ‚||_|jtjkro|rPtdƒ‚|d tjfvr[tdƒ‚|dvrctd	ƒ‚d|_tj|_d
|_|S |jtjkrœ|dvr}tdƒ‚|d tjfvrˆtdƒ‚|dvr�td	ƒ‚d
|_tj|_d
|_|S d|_tj|_d|_|d ur­||_|d ur´||_|d ur»||_|S )NÚtz_awareFÚtzinfoznJSONOptions.datetime_representation must be one of LEGACY, NUMBERLONG, or ISO8601 from DatetimeRepresentation.zQJSONOptions.json_mode must be one of LEGACY, RELAXED, or CANONICAL from JSONMode.z<Cannot specify strict_number_long=True with JSONMode.RELAXEDz_datetime_representation must be DatetimeRepresentation.ISO8601 or omitted with JSONMode.RELAXED)NTz6Cannot specify strict_uuid=False with JSONMode.RELAXEDTz=Cannot specify strict_number_long=False with JSONMode.RELAXEDzbdatetime_representation must be DatetimeRepresentation.NUMBERLONG or omitted with JSONMode.RELAXED)Úgetr$   r,   r2   r3   r4   Ú
ValueErrorr   r:   rD   Ú__new__r7   r8   r9   r<   r>   r?   r@   )rI   r>   r?   r@   r<   rB   rC   rF   rG   r5   r6   rR   1  sl   	üÿ ÿÿéÿ÷zJSONOptions.__new__Ústrc                   s    d  | j| j| j| jtƒ  ¡ ¡S )Nz[strict_number_long={!r}, datetime_representation={!r}, strict_uuid={!r}, json_mode={!r}, {})Úformatr>   r?   r@   r<   rD   Ú_arguments_repr)rF   rG   r5   r6   rU   t  s   ûýzJSONOptions._arguments_reprúdict[Any, Any]c                   s*   t ƒ  ¡ }| | j| j| j| jdœ¡ |S )N©r>   r?   r@   r<   )rD   Ú_options_dictÚupdater>   r?   r@   r<   )rF   Úoptions_dictrG   r5   r6   rX   �  s   
üÿzJSONOptions._options_dictc                 K  s@   |   ¡ }dD ]}| |t| |ƒ¡||< q| |¡ tdi |¤ŽS )aˆ  
        Make a copy of this JSONOptions, overriding some options::

            >>> from bson.json_util import CANONICAL_JSON_OPTIONS
            >>> CANONICAL_JSON_OPTIONS.tz_aware
            True
            >>> json_options = CANONICAL_JSON_OPTIONS.with_options(tz_aware=False, tzinfo=None)
            >>> json_options.tz_aware
            False

        .. versionadded:: 3.12
        rW   Nr5   )rX   rP   ÚgetattrrY   r:   )rF   rC   ÚoptsÚoptr5   r5   r6   Úwith_optionsŽ  s
   
zJSONOptions.with_options)rB   r   rC   r   )rI   rJ   r>   rK   r?   rL   r@   rK   r<   r;   rB   r   rC   r   rM   r:   )rM   rS   )rM   rV   )rC   r   rM   r:   )r/   r0   r1   Ú__annotations__rE   r7   r8   rR   rU   rX   r^   Ú__classcell__r5   r5   rG   r6   r:   ò   s   
 :ûCr:   )r<   ÚLEGACY_JSON_OPTIONSÚCANONICAL_JSON_OPTIONSÚRELAXED_JSON_OPTIONSÚDEFAULT_JSON_OPTIONSÚobjr   rB   rC   rM   rS   c                 O  s*   |  dt¡}tjt| |ƒg|¢R i |¤ŽS )aQ  Helper function that wraps :func:`json.dumps`.

    Recursive function that handles all BSON types including
    :class:`~bson.binary.Binary` and :class:`~bson.code.Code`.

    :param json_options: A :class:`JSONOptions` instance used to modify the
        encoding of MongoDB Extended JSON types. Defaults to
        :const:`DEFAULT_JSON_OPTIONS`.

    .. versionchanged:: 4.0
       Now outputs MongoDB Relaxed Extended JSON by default (using
       :const:`DEFAULT_JSON_OPTIONS`).

    .. versionchanged:: 3.4
       Accepts optional parameter `json_options`. See :class:`JSONOptions`.
    Újson_options)Úpoprd   ÚjsonÚdumpsÚ_json_convert)re   rB   rC   rf   r5   r5   r6   ri   Ç  s   ri   r(   úUnion[str, bytes, bytearray]c                   sP   |  dt¡‰ ˆ jtu r‡ fdd„|d< n‡ fdd„|d< tj| g|¢R i |¤ŽS )aþ  Helper function that wraps :func:`json.loads`.

    Automatically passes the object_hook for BSON type conversion.

    Raises ``TypeError``, ``ValueError``, ``KeyError``, or
    :exc:`~bson.errors.InvalidId` on invalid MongoDB Extended JSON.

    :param json_options: A :class:`JSONOptions` instance used to modify the
        decoding of MongoDB Extended JSON types. Defaults to
        :const:`DEFAULT_JSON_OPTIONS`.

    .. versionchanged:: 4.0
       Now loads :class:`datetime.datetime` instances as naive by default. To
       load timezone aware instances utilize the `json_options` parameter.
       See :ref:`tz_aware_default_change` for an example.

    .. versionchanged:: 3.5
       Parses Relaxed and Canonical Extended JSON as well as PyMongo's legacy
       format. Now raises ``TypeError`` or ``ValueError`` when parsing JSON
       type wrappers with values of the wrong type or any extra keys.

    .. versionchanged:: 3.4
       Accepts optional parameter `json_options`. See :class:`JSONOptions`.
    rf   c                   ó
   t | ˆ ƒS ©N)Úobject_hook©re   ©rf   r5   r6   Ú<lambda>ø  ó   
 zloads.<locals>.<lambda>rn   c                   rl   rm   )Úobject_pairs_hook)Úpairsrp   r5   r6   rq   ú  rr   rs   )rg   rd   rA   Údictrh   Úloads)r(   rB   rC   r5   rp   r6   rv   Ü  s
   
rv   rf   c                   sn   t | dƒr‡ fdd„|  ¡ D ƒS t | dƒr%t| ttfƒs%‡ fdd„| D ƒS zt| ˆ ƒW S  ty6   |  Y S w )z]Recursive helper method that converts BSON types so they can be
    converted into json.
    Úitemsc                   s   i | ]
\}}|t |ˆ ƒ“qS r5   ©rj   )Ú.0ÚkÚvrp   r5   r6   Ú
<dictcomp>  s    z!_json_convert.<locals>.<dictcomp>Ú__iter__c                   s   g | ]}t |ˆ ƒ‘qS r5   rx   )ry   r{   rp   r5   r6   Ú
<listcomp>  s    z!_json_convert.<locals>.<listcomp>)Úhasattrrw   Ú
isinstancerS   ÚbytesÚdefaultÚ	TypeError©re   rf   r5   rp   r6   rj   þ  s   
ÿrj   rt   úSequence[Tuple[str, Any]]c                 C  s   t | | ¡|ƒS rm   )rn   rA   )rt   rf   r5   r5   r6   rs     s   rs   ÚdctúMapping[str, Any]c                 C  s4   d }| D ]
}|t v r|} nq|rt| | |ƒS | S rm   )Ú_PARSERS_SETÚ_PARSERS)r†   rf   Úmatchrz   r5   r5   r6   rn     s   þrn   ÚdocÚdummy0c                 C  sJ   | d }t |ttfƒs| S d}|  dd¡D ]
}|t |d¡O }qt||ƒS )Nú$regexr   ú$optionsÚ )r€   rS   r�   rP   Ú_RE_OPT_TABLEr    )r‹   rŒ   ÚpatternÚflagsr]   r5   r5   r6   Ú_parse_legacy_regex  s   
r“   úUnion[Binary, uuid.UUID]c                 C  sd   t | ƒdkrtd| › �ƒ‚t| d tƒstd| › �ƒ‚|jtjkr+t t	 
| d ¡¡S t	 
| d ¡S )z*Decode a JSON legacy $uuid to Python UUID.r-   zBad $uuid, extra field(s): ú$uuidz$uuid must be a string: )Úlenrƒ   r€   rS   Úuuid_representationr   ÚUNSPECIFIEDr   Ú	from_uuidÚuuidÚUUID©r‹   rf   r5   r5   r6   Ú_parse_legacy_uuid)  s   r�   ÚdataÚsubtyper;   c                 C  sn   |t v r(|j}t| |ƒ}|tjkr|S |tkrtj}n|tjkr#tj}| |¡S |dkr2t	t
j| ƒS t| |ƒS ©Nr   )r   r—   r   r   r˜   r   ÚSTANDARDÚPYTHON_LEGACYÚas_uuidr   rš   r›   )rž   rŸ   rf   r—   Úbinary_valuer5   r5   r6   Ú_binary_or_uuid5  s   




r¥   c                 C  sh   t | d tƒrd| d  | d< t| d dƒ}|dkr%t| d dd … dƒ}t | d  ¡ ¡}t|||ƒS )Nú$typeú%02xé   l   €ÿ é   ú$binary)r€   r;   Úbase64Ú	b64decodeÚencoder¥   )r‹   rf   rŸ   rž   r5   r5   r6   Ú_parse_legacy_binaryJ  s   r®   c                 C  sŽ   | d }|d }|d }t |tƒstd| › �ƒ‚t |tƒr#t|ƒdkr*td| › �ƒ‚t|ƒdkr7td| › �ƒ‚t | ¡ ¡}t|t|dƒ|ƒS )	Nrª   r«   ÚsubTypez!$binary base64 must be a string: r.   z7$binary subType must be a string at most 2 characters: z=$binary must include only "base64" and "subType" components: r¨   )	r€   rS   rƒ   r–   r«   r¬   r­   r¥   r;   )r‹   rf   ÚbinaryÚb64rŸ   rž   r5   r5   r6   Ú_parse_canonical_binaryT  s   
r²   ú$Union[datetime.datetime, DatetimeMS]c              
   C  sŒ  | d }t | ƒdkrtd| › �ƒ‚t|tƒ�r<zT|d dkr'|dd… }d}nC|d dv r@|d	 d
kr@|dd… }|dd… }n*|d dv rS|dd… }|dd… }n|d	 dv rf|dd	… }|d	d… }n|}d}W n ty€ } z	td|›d�ƒ|‚d}~ww | d¡}d}|dkržtt||d… ƒd ƒ}|d|… }t	j	 
|d¡j|td�}|�r|dk�rt |ƒdkrÐ|dd…  d
¡\}	}
t|	ƒd t|
ƒd  }n+t |ƒdkrët|dd… ƒd t|dd… ƒd  }nt |ƒdkrût|dd… ƒd }|d dk�r|d9 }|t	j|d� }|j�r)|j�r| |j¡}|jtjk�r't|ƒS |S |jdd�}|jtjk�r:t|ƒS |S tt|ƒtd|ƒƒS )z3Decode a JSON datetime to python datetime.datetime.ú$dater-   zBad $date, extra field(s): éÿÿÿÿÚZNiúÿÿÿ)ú+ú-éýÿÿÿú:éûÿÿÿr�   z
time data z( does not match ISO-8601 datetime formatÚ.r   i@B ú%Y-%m-%dT%H:%M:%S)ÚmicrosecondrO   r©   i  é<   é   é   r¸   )Úseconds©rO   zCodecOptions[Any])r–   rƒ   r€   rS   Ú
IndexErrorrQ   Úrfindr;   ÚfloatÚdatetimeÚstrptimeÚreplacer$   ÚsplitÚ	timedeltarN   rO   Ú
astimezoneÚdatetime_conversionr   ÚDATETIME_MSr   r   r   )r‹   rf   ÚdtmÚdtÚoffsetÚexcÚ	dot_indexr¾   ÚawareÚhoursÚminutesÚsecsÚaware_tzinfo_noner5   r5   r6   Ú_parse_canonical_datetimec  sl   €€ÿ
ÿ*rÙ   r   c                 C  s&   t | ƒdkrtd| › �ƒ‚t| d ƒS )z1Decode a JSON ObjectId to bson.objectid.ObjectId.r-   zBad $oid, extra field(s): ú$oid)r–   rƒ   r   ©r‹   rŒ   r5   r5   r6   Ú_parse_canonical_oid¨  s   rÜ   c                 C  ó*   | d }t | ƒdkrtd| › �ƒ‚t|ƒS )z&Decode a JSON symbol to Python string.ú$symbolr-   zBad $symbol, extra field(s): )r–   rƒ   rS   )r‹   rŒ   Úsymbolr5   r5   r6   Ú_parse_canonical_symbol¯  ó   rà   r   c                 C  s6   | D ]}|dvrt d| › �ƒ‚qt| d |  d¡d�S )z%Decode a JSON code to bson.code.Code.©ú$codeú$scopezBad $code, extra field(s): rã   rä   )Úscope)rƒ   r   rP   )r‹   rŒ   Úkeyr5   r5   r6   Ú_parse_canonical_code·  s
   ÿrç   ú
Regex[str]c                 C  sl   | d }t | ƒdkrtd| › �ƒ‚t |ƒdkrtd| › �ƒ‚|d }t|tƒs/tdt|ƒ ƒ‚t|d |ƒS )	z(Decode a JSON regex to bson.regex.Regex.ú$regularExpressionr-   z(Bad $regularExpression, extra field(s): r.   zLBad $regularExpression must include only "pattern and "options" components: ÚoptionszCBad $regularExpression options, options must be string, was type %sr‘   )r–   rƒ   r€   rS   Útyper    )r‹   rŒ   Úregexr\   r5   r5   r6   Ú_parse_canonical_regex¿  s   ÿ

ÿrí   c                 C  s^   t |  d¡tƒr-d| v r-t |  d¡ttdƒfƒr-t|  d¡|  d¡fd|  dd¡i| ¤ŽS | S )z(Decode a JSON DBRef to bson.dbref.DBRef.ú$refz$idz$dbNÚdatabase)r€   rP   rS   rë   r   rg   rÛ   r5   r5   r6   Ú_parse_canonical_dbrefÐ  s   ÿÿ*rð   c                 C  s’   | d }t | ƒdkrtd| › �ƒ‚t|tƒrB| ¡ }|jdur&td|› �ƒ‚t|jtƒs3td|› �ƒ‚t |ƒdkr@td|› �ƒ‚|S td	| › �ƒ‚)
z9Decode a JSON (deprecated) DBPointer to bson.dbref.DBRef.ú
$dbPointerr-   z Bad $dbPointer, extra field(s): Nz!Bad $dbPointer, extra field $db: z)Bad $dbPointer, $id must be an ObjectId: r.   z)Bad $dbPointer, extra field(s) in DBRef: z"Bad $dbPointer, expected a DBRef: )r–   rƒ   r€   r   Úas_docrï   Úidr   )r‹   rŒ   ÚdbrefÚ	dbref_docr5   r5   r6   Ú_parse_canonical_dbpointerÛ  s   

rö   c                 C  óB   | d }t | ƒdkrtd| › �ƒ‚t|tƒstd| › �ƒ‚t|ƒS )z"Decode a JSON int32 to python int.ú
$numberIntr-   z Bad $numberInt, extra field(s): z$numberInt must be string: )r–   rƒ   r€   rS   r;   )r‹   rŒ   Úi_strr5   r5   r6   Ú_parse_canonical_int32î  ó   
rú   r   c                 C  rÝ   )z(Decode a JSON int64 to bson.int64.Int64.ú$numberLongr-   z!Bad $numberLong, extra field(s): )r–   rƒ   r   )r‹   rŒ   Úl_strr5   r5   r6   Ú_parse_canonical_int64ø  rá   rþ   rÆ   c                 C  r÷   )z%Decode a JSON double to python float.ú$numberDoubler-   z#Bad $numberDouble, extra field(s): z$numberDouble must be string: )r–   rƒ   r€   rS   rÆ   ©r‹   rŒ   Úd_strr5   r5   r6   Ú_parse_canonical_double   rû   r  r   c                 C  r÷   )z7Decode a JSON decimal128 to bson.decimal128.Decimal128.ú$numberDecimalr-   z$Bad $numberDecimal, extra field(s): z$numberDecimal must be string: )r–   rƒ   r€   rS   r   r   r5   r5   r6   Ú_parse_canonical_decimal128
  rû   r  r   c                 C  sJ   t | d ƒtus| d dkrtd| › �ƒ‚t| ƒdkr"td| › �ƒ‚tƒ S )z,Decode a JSON MinKey to bson.min_key.MinKey.ú$minKeyr-   z$minKey value must be 1: úBad $minKey, extra field(s): )rë   r;   rƒ   r–   r   rÛ   r5   r5   r6   Ú_parse_canonical_minkey  s
   r  r   c                 C  sH   t | d ƒtus| d dkrtd| fƒ‚t| ƒdkr!td| › �ƒ‚tƒ S )z,Decode a JSON MaxKey to bson.max_key.MaxKey.ú$maxKeyr-   z$maxKey value must be 1: %sr  )rë   r;   rƒ   r–   r   rÛ   r5   r5   r6   Ú_parse_canonical_maxkey  s
   r	  c                 C  s   d| v r	t | |ƒS t| |ƒS )Nr¦   )r®   r²   rœ   r5   r5   r6   Ú_parse_binary&  s   

r
  r#   c                 C  s   | d }t |d |d ƒS )Nú
$timestampÚtr%   r"   )r‹   rŒ   Útspr5   r5   r6   Ú_parse_timestamp-  s   r  rÚ   rî   r´   r�   r  r  rª   rã   r•   z
$undefinedc                 C  s   d S rm   r5   )Ú_Ú_1r5   r5   r6   rq   <  s    rq   rü   r  r  rñ   ré   rÞ   rø   rÿ   z,dict[str, Callable[[Any, JSONOptions], Any]]r‰   r�   c                 C  s@   |j tjkrt | ¡ ¡ d| dœS dt | ¡ ¡ d| dœiS )Nr§   )rª   r¦   rª   )r«   r¯   )r<   r7   r2   r«   Ú	b64encodeÚdecode)rž   rŸ   rf   r5   r5   r6   Ú_encode_binaryI  s   r  ru   c                 C  sb   |j tjkrdt| ƒ  krtkrn nt|  ¡ |ƒS |j tjkr'dt| ƒiS ddtt| ƒƒiiS )Nr   r´   rü   )	r?   r,   r4   r;   r   Ú_encode_datetimeÚas_datetimer2   rS   r„   r5   r5   r6   Ú_encode_datetimemsO  s   r  c                 C  s,   | j d u rdt| ƒiS t| ƒt| j |ƒdœS )Nrã   râ   )rå   rS   rj   r„   r5   r5   r6   Ú_encode_codeZ  s   
r  c                 C  s   |j r	dt| ƒiS t| ƒS )Nrü   )r>   rS   r;   r„   r5   r5   r6   Ú_encode_int64a  s   r  c                 C  s   | S rm   r5   ©re   rŒ   r5   r5   r6   Ú_encode_nooph  s   r  c                 C  sÀ   d}| j tj@ r|d7 }| j tj@ r|d7 }| j tj@ r |d7 }| j tj@ r*|d7 }| j tj@ r4|d7 }| j tj@ r>|d7 }t| j	t
ƒrH| j	}n| j	 d¡}|jtjkrY||d	œS d
||dœiS )Nr�   r%   r&   r'   r(   r)   r*   zutf-8)r�   rŽ   ré   )r‘   rê   )r’   ÚreÚ
IGNORECASEÚLOCALEÚ	MULTILINEÚDOTALLÚUNICODEÚVERBOSEr€   r‘   rS   r  r<   r7   r2   )re   rf   r’   r‘   r5   r5   r6   Ú_encode_regexl  s&   
r"  c                 C  sB   |j tjkrt |   krtk rn ndt| ƒiS dt| ƒiS | S )Nrø   rü   )r<   r7   r9   Ú
_INT32_MAXrS   r„   r5   r5   r6   Ú_encode_intƒ  s
   r$  c                 C  s`   |j tjkr.t | ¡rddiS t | ¡r | dkrdnd}d|iS |j tjkr.dtt| ƒƒiS | S )Nrÿ   ÚNaNr   ÚInfinityz	-Infinity)	r<   r7   r2   ÚmathÚisnanÚisinfr9   rS   Úrepr)re   rf   Úrepresentationr5   r5   r6   Ú_encode_float‹  s   

r,  údatetime.datetimec                 C  sÈ   |j tjkrN| js| jtd�} | jd usJ ‚| tkrN| j | ¡}|j|j	|j
fdkr-d}n|  d¡}t| jd ƒ}|r@d|f nd}dd	 |  d
¡||¡iS t| ƒ}|j tjkr\d|iS ddt|ƒiiS )NrÃ   )r   r   r   r¶   z%ziè  z.%03dr�   r´   z{}{}{}r½   rü   )r?   r,   r4   rO   rÉ   r$   r   Ú	utcoffsetÚdaysrÂ   ÚmicrosecondsÚstrftimer;   r¾   rT   r   r2   rS   )re   rf   ÚoffÚ	tz_stringÚmillisÚfracsecsr5   r5   r6   r  ™  s"   
ÿr  c                 C  s   t | d|ƒS r    )r  r„   r5   r5   r6   Ú_encode_bytes°  ó   r6  r   c                 C  s   t | | j|ƒS rm   )r  rŸ   r„   r5   r5   r6   Ú_encode_binary_obj´  ó   r8  ú	uuid.UUIDc                 C  s.   |j rtj| |jd�}t||j|ƒS d| jiS )N)r—   r•   )r@   r   r™   r—   r  rŸ   Úhex)re   rf   Úbinvalr5   r5   r6   Ú_encode_uuid¸  s   
r=  c                 C  ó   dt | ƒiS )NrÚ   ©rS   r  r5   r5   r6   Ú_encode_objectidÀ  r7  r@  c                 C  s   d| j | jdœiS )Nr  )r  r%   )ÚtimeÚincr  r5   r5   r6   Ú_encode_timestampÄ  s   rC  c                 C  r>  )Nr  r?  r  r5   r5   r6   Ú_encode_decimal128È  r7  rD  r   c                 C  s   t |  ¡ |d�S )Nrp   )rj   rò   r„   r5   r5   r6   Ú_encode_dbrefÌ  s   rE  Údummy1c                 C  ó   ddiS )Nr  r-   r5   ©rŒ   rF  r5   r5   r6   Ú_encode_minkeyÐ  ó   rI  c                 C  rG  )Nr  r-   r5   rH  r5   r5   r6   Ú_encode_maxkeyÔ  rJ  rK  z-dict[Type, Callable[[Any, JSONOptions], Any]]Ú	_ENCODERSz,dict[int, Callable[[Any, JSONOptions], Any]]Ú_MARKERSÚ_type_markerc                 c  s   � | ]}|V  qd S rm   r5   )ry   r  r5   r5   r6   Ú	<genexpr>ù  s   € rO  c                 C  s    z
t t| ƒ | |ƒW S  ty   Y nw t| dƒr/| j}|tv r/t| }|t t| ƒ< || |ƒS tD ]}t| |ƒrIt | }|t t| ƒ< || |ƒ  S q1td|  ƒ‚)NrN  z%r is not JSON serializable)	rL  rë   ÚKeyErrorr   rN  rM  Ú_BUILT_IN_TYPESr€   rƒ   )re   rf   ÚmarkerÚfuncÚbaser5   r5   r6   r‚   ü  s$   ÿ


ür‚   c                 C  s   t | ƒS rm   )r–   ro   r5   r5   r6   Ú_get_str_size  rJ  rU  c                 C  s   dt t|  ¡ ƒƒ S )NrÀ   )r–   rS   rA  ro   r5   r5   r6   Ú_get_datetime_size  s   rV  r    c                 C  ó   dt | jƒ S )Né   )r–   r‘   ro   r5   r5   r6   Ú_get_regex_size#  r9  rY  c                 C  rW  )Né"   )r–   Ú
collectionro   r5   r5   r6   Ú_get_dbref_size'  r9  r\  é   é   é   é   zdict[Any, int]Ú_CONSTANT_SIZE_TABLEzdict[Any, Callable[[Any], int]]Ú_VARIABLE_SIZE_TABLEÚmax_sizeÚcurrent_sizec                 C  s(  ||kr|S t | ƒ}zt| W S  ty   Y nw zt| | ƒW S  ty)   Y nw |tkrQ| jrG|dt| j||ƒ t| ƒ t| jƒ 7 }|S |dt| ƒ 7 }|S |tkrx|  	¡ D ]\}}|t|||ƒ7 }|t|||ƒ7 }||kru|  S qY|S t
| dƒr’| D ]}|t|||ƒ7 }||kr‘|  S q|S )z!Recursively finds size of objectsrÀ   r}   )rë   ra  rP  rb  r   rå   Úget_sizer–   ru   rw   r   )re   rc  rd  Úobj_typerz   r{   r%   r5   r5   r6   re  >  sH   
ÿÿ"ÿôõÿ
ûÿre  Ú
max_lengthúTuple[Any, int]c                 C  sÊ   |dkrdS |}t | dƒr2i }|  ¡ D ]\}}t||ƒ\}}|r$|||< |dkr- ||fS q||fS t | dƒr`t| ttfƒs`g }| D ]}t||ƒ\}}|rR| |¡ |dkr[ ||fS qB||fS t| |ƒS )zMRecursively truncate documents as needed to fit inside max_length characters.r   r    rw   r}   )r   rw   Ú_truncate_documentsr€   rS   r�   ÚappendÚ	_truncate)re   rg  Ú	remainingÚ	truncatedrz   r{   Útruncated_vr5   r5   r6   ri  g  s4   
þ
þ
ri  rl  c                 C  sR   t | |ƒ}||kr| || fS z| d |… }W n ty"   | }Y nw ||| fS rm   )re  rƒ   )re   rl  Úsizerm  r5   r5   r6   rk  ‚  s   
ÿrk  )re   r   rB   r   rC   r   rM   rS   )r(   rk   rB   r   rC   r   rM   r   )re   r   rf   r:   rM   r   )rt   r…   rf   r:   rM   r   )r†   r‡   rf   r:   rM   r   )r‹   r   rŒ   r   rM   r   )r‹   r   rf   r:   rM   r”   )rž   r   rŸ   r;   rf   r:   rM   r”   )r‹   r   rf   r:   rM   r³   )r‹   r   rŒ   r   rM   r   )r‹   r   rŒ   r   rM   rS   )r‹   r   rŒ   r   rM   r   )r‹   r   rŒ   r   rM   rè   )r‹   r   rŒ   r   rM   r;   )r‹   r   rŒ   r   rM   r   )r‹   r   rŒ   r   rM   rÆ   )r‹   r   rŒ   r   rM   r   )r‹   r   rŒ   r   rM   r   )r‹   r   rŒ   r   rM   r   )r‹   r   rŒ   r   rM   r#   )rž   r�   rŸ   r;   rf   r:   rM   r   )re   r   rf   r:   rM   ru   )re   r   rf   r:   rM   ru   )re   r   rf   r:   rM   r   )re   r   rŒ   r   rM   r   )re   r;   rf   r:   rM   r   )re   rÆ   rf   r:   rM   r   )re   r-  rf   r:   rM   ru   )re   r�   rf   r:   rM   ru   )re   r   rf   r:   rM   ru   )re   r:  rf   r:   rM   ru   )re   r   rŒ   r   rM   ru   )re   r#   rŒ   r   rM   ru   )re   r   rf   r:   rM   ru   )rŒ   r   rF  r   rM   ru   )re   r   rM   r;   )re   r-  rM   r;   )re   r    rM   r;   )re   r   rM   r;   )r   )re   r   rc  r;   rd  r;   rM   r;   )re   r   rg  r;   rM   rh  )re   r   rl  r;   rM   rh  )“Ú__doc__Ú
__future__r   r«   rÇ   rh   r'  r  rš   Útypingr   r   r   r   r   r   r	   r
   r   r   r   Úbson.binaryr   r   r   r   Ú	bson.coder   Úbson.codec_optionsr   r   Úbson.datetime_msr   r   r   r   r   Ú
bson.dbrefr   Úbson.decimal128r   Ú
bson.int64r   Úbson.max_keyr   Úbson.min_keyr   Úbson.objectidr   Ú
bson.regexr    Úbson.sonr!   Úbson.timestampr#   Úbson.tz_utilr$   ÚIÚLÚMÚSÚUÚXr�   r,   r7   rS   Ú_BASE_CLASSr#  r:   r2   ra   r_   r9   rb   r8   rc   rd   ri   rv   rj   rs   rn   r“   r�   r¥   r®   r²   rÙ   rÜ   rà   rç   rí   rð   rö   rú   rþ   r  r  r  r	  r
  r  r‰   Úsetrˆ   r  r  r  r  r  r"  r$  r,  r  r6  r8  r=  r@  rC  rD  rE  rI  rK  r=   r�   rÆ   r;   rë   r›   rL  rM  Ú_typr   rN  ÚtuplerQ  r‚   rU  rV  rY  r\  ra  rb  re  ri  rk  r5   r5   r5   r6   Ú<module>   s†  W4ú
$+ 1

"ÿ







E













	
	
ÿþýüûúùø	÷
öõôóòñðïî

















ÿþýüûúùø	÷
öõôóòñðïì€



ùû	
)