
    h7                        d Z ddlZddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
 ddlmZmZmZ dZd Z G d	 d
e      Z G d de      Z G d dee      Z G d de      Z edd      Z G d de      Z eej0                        Zd Zy)z(Tools for specifying BSON codec options.    N)abstractmethod)
namedtuple)ABCabcabstractpropertystring_type)UuidRepresentationALL_UUID_REPRESENTATIONSUUID_REPRESENTATION_NAMESe   c                 .    t        | dd      }|t        k(  S )z9Determine if a document_class is a RawBSONDocument class._type_markerN)getattr_RAW_BSON_DOCUMENT_MARKER)document_classmarkers     U/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/bson/codec_options.py_raw_document_classr   !   s    ^^T:F...    c                   0    e Zd ZdZed        Zed        Zy)TypeEncoderaP  Base class for defining type codec classes which describe how a
    custom type can be transformed to one of the types BSON understands.

    Codec classes must implement the ``python_type`` attribute, and the
    ``transform_python`` method to support encoding.

    See :ref:`custom-type-type-codec` documentation for an example.
    c                      y)z<The Python type to be converted into something serializable.N selfs    r   python_typezTypeEncoder.python_type0        	r   c                      y)z<Convert the given Python object into something serializable.Nr   r   values     r   transform_pythonzTypeEncoder.transform_python5   r   r   N)__name__
__module____qualname____doc__r   r   r   r!   r   r   r   r   r   '   /        r   r   c                   0    e Zd ZdZed        Zed        Zy)TypeDecodera6  Base class for defining type codec classes which describe how a
    BSON type can be transformed to a custom type.

    Codec classes must implement the ``bson_type`` attribute, and the
    ``transform_bson`` method to support decoding.

    See :ref:`custom-type-type-codec` documentation for an example.
    c                      y)z0The BSON type to be converted into our own type.Nr   r   s    r   	bson_typezTypeDecoder.bson_typeD   r   r   c                      y)z/Convert the given BSON value into our own type.Nr   r   s     r   transform_bsonzTypeDecoder.transform_bsonI   r   r   N)r"   r#   r$   r%   r   r*   r   r,   r   r   r   r(   r(   ;   r&   r   r(   c                       e Zd ZdZy)	TypeCodeca  Base class for defining type codec classes which describe how a
    custom type can be transformed to/from one of the types :mod:`bson`
    can already encode/decode.

    Codec classes must implement the ``python_type`` attribute, and the
    ``transform_python`` method to support encoding, as well as the
    ``bson_type`` attribute, and the ``transform_bson`` method to support
    decoding.

    See :ref:`custom-type-type-codec` documentation for an example.
    N)r"   r#   r$   r%   r   r   r   r.   r.   O   s    
 	r   r.   c                   *    e Zd ZdZddZd Zd Zd Zy)TypeRegistrya  Encapsulates type codecs used in encoding and / or decoding BSON, as
    well as the fallback encoder. Type registries cannot be modified after
    instantiation.

    ``TypeRegistry`` can be initialized with an iterable of type codecs, and
    a callable for the fallback encoder::

      >>> from bson.codec_options import TypeRegistry
      >>> type_registry = TypeRegistry([Codec1, Codec2, Codec3, ...],
      ...                              fallback_encoder)

    See :ref:`custom-type-type-registry` documentation for an example.

    :Parameters:
      - `type_codecs` (optional): iterable of type codec instances. If
        ``type_codecs`` contains multiple codecs that transform a single
        python or BSON type, the transformation specified by the type codec
        occurring last prevails. A TypeError will be raised if one or more
        type codecs modify the encoding behavior of a built-in :mod:`bson`
        type.
      - `fallback_encoder` (optional): callable that accepts a single,
        unencodable python value and transforms it into a type that
        :mod:`bson` can encode. See :ref:`fallback-encoder-callable`
        documentation for an example.
    Nc                 D   t        |xs g       | _        || _        i | _        i | _        | j                  t        |      st        d|z        | j                  D ]  }d}t        |t              r6| j                  |       d}|j                  | j                  |j                  <   t        |t              r%d}|j                  | j                  |j                  <   |rt        dt        j                  dt        j                  dt         j                  d|d	       y )	Nz%fallback_encoder %r is not a callableFTzExpected an instance of z, z, or z, got z instead)list_TypeRegistry__type_codecs_fallback_encoder_encoder_map_decoder_mapcallable	TypeError
isinstancer   _validate_type_encoderr!   r   r(   r,   r*   r"   r.   )r   type_codecsfallback_encodercodecis_valid_codecs        r   __init__zTypeRegistry.__init__x   s   !+"34!1!!-,- G$!& ' ' '' 	4E"N%-++E2!%7<7M7M!!%"3"34%-!%5:5I5I!!%//2!#,,k.B.B!**E34 4	4r   c                 r    ddl m} |D ],  }t        |j                  |      sd|d|d}t	        |       y )Nr   )_BUILT_IN_TYPESzCTypeEncoders cannot change how built-in types are encoded (encoder z transforms type ))bsonrA   
issubclassr   r8   )r   r=   rA   pytypeerr_msgs        r   r:   z#TypeRegistry._validate_type_encoder   s?    (% 	)F%++V4 "6+  ((	)r   c                 h    | j                   j                  d| j                  d| j                  dS )Nz(type_codecs=z, fallback_encoder=rB   )	__class__r"   r3   r4   r   s    r   __repr__zTypeRegistry.__repr__   s*    NN##T%7%7""$ 	%r   c                     t        |t        |             st        S | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j
                  |j
                  k(  S )N)r9   typeNotImplementedr6   r5   r4   )r   others     r   __eq__zTypeRegistry.__eq__   sd    %d,!!""e&8&88 D""e&8&88D''5+B+BB	Er   )NN)r"   r#   r$   r%   r?   r:   rI   rN   r   r   r   r0   r0   ^   s    244)%
Er   r0   CodecOptionsr   tz_awareuuid_representationunicode_decode_error_handlertzinfotype_registryc                   <    e Zd ZdZedddddfdZd Zd Zd Zd	 Z	y)
rO   a  Encapsulates options used encoding and / or decoding BSON.

    The `document_class` option is used to define a custom type for use
    decoding BSON documents. Access to the underlying raw BSON bytes for
    a document is available using the :class:`~bson.raw_bson.RawBSONDocument`
    type::

      >>> from bson.raw_bson import RawBSONDocument
      >>> from bson.codec_options import CodecOptions
      >>> codec_options = CodecOptions(document_class=RawBSONDocument)
      >>> coll = db.get_collection('test', codec_options=codec_options)
      >>> doc = coll.find_one()
      >>> doc.raw
      '\x16\x00\x00\x00\x07_id\x00[0\x165\x91\x10\xea\x14\xe8\xc5\x8b\x93\x00'

    The document class can be any type that inherits from
    :class:`~collections.MutableMapping`::

      >>> class AttributeDict(dict):
      ...     # A dict that supports attribute access.
      ...     def __getattr__(self, key):
      ...         return self[key]
      ...     def __setattr__(self, key, value):
      ...         self[key] = value
      ...
      >>> codec_options = CodecOptions(document_class=AttributeDict)
      >>> coll = db.get_collection('test', codec_options=codec_options)
      >>> doc = coll.find_one()
      >>> doc._id
      ObjectId('5b3016359110ea14e8c58b93')

    See :doc:`/examples/datetimes` for examples using the `tz_aware` and
    `tzinfo` options.

    See :class:`~bson.binary.UUIDLegacy` for examples using the
    `uuid_representation` option.

    :Parameters:
      - `document_class`: BSON documents returned in queries will be decoded
        to an instance of this class. Must be a subclass of
        :class:`~collections.MutableMapping`. Defaults to :class:`dict`.
      - `tz_aware`: If ``True``, BSON datetimes will be decoded to timezone
        aware instances of :class:`~datetime.datetime`. Otherwise they will be
        naive. Defaults to ``False``.
      - `uuid_representation`: The BSON representation to use when encoding
        and decoding instances of :class:`~uuid.UUID`. Defaults to
        :data:`~bson.binary.UuidRepresentation.PYTHON_LEGACY`. New
        applications should consider setting this to
        :data:`~bson.binary.UuidRepresentation.STANDARD` for cross language
        compatibility. See :ref:`handling-uuid-data-example` for details.
      - `unicode_decode_error_handler`: The error handler to apply when
        a Unicode-related error occurs during BSON decoding that would
        otherwise raise :exc:`UnicodeDecodeError`. Valid options include
        'strict', 'replace', and 'ignore'. Defaults to 'strict'.
      - `tzinfo`: A :class:`~datetime.tzinfo` subclass that specifies the
        timezone to/from which :class:`~datetime.datetime` objects should be
        encoded/decoded.
      - `type_registry`: Instance of :class:`TypeRegistry` used to customize
        encoding and decoding behavior.

    .. versionadded:: 3.8
       `type_registry` attribute.

    .. warning:: Care must be taken when changing
       `unicode_decode_error_handler` from its default value ('strict').
       The 'replace' and 'ignore' modes should not be used when documents
       retrieved from the server will be modified in the client application
       and stored back to the server.
    FNstrictc           	         t        |t        j                        st        |      st	        d      t        |t              st	        d      |t        j                  }n|t        vrt        d      t        |t        d f      st        d      |2t        |t        j                        st	        d      |st        d      |xs
 t               }t        |t              st	        d      t        j!                  | ||||||f      S )Nztdocument_class must be dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a sublass of collections.MutableMappingztz_aware must be True or FalsezGuuid_representation must be a value from bson.binary.UuidRepresentationz5unicode_decode_error_handler must be a string or Nonez-tzinfo must be an instance of datetime.tzinfoz8cannot specify tzinfo without also setting tz_aware=Truez1type_registry must be an instance of TypeRegistry)rD   r   MutableMappingr   r8   r9   boolr	   PYTHON_LEGACYr
   
ValueErrorr   datetimerT   r0   tuple__new__)clsr   rQ   rR   rS   rT   rU   s          r   r_   zCodecOptions.__new__   s$   
 >3+=+=>#N3 D E E (D)<==&"4"B"B (@@ C D D6d8KL ' ( (fhoo6CE E NP P &7-6OPP}}.(,?.GH 	Hr   c                     | j                   t        u rdnt        | j                         }t        j                  | j
                  | j
                        }d|d| j                  d|d| j                  d| j                  d| j                  S )z;Representation of the arguments used to create this object.dictzdocument_class=z, tz_aware=z, uuid_representation=z, unicode_decode_error_handler=z	, tzinfo=z, type_registry=)
r   rb   reprr   getrR   rQ   rS   rT   rU   )r   document_class_repruuid_rep_reprs      r   _arguments_reprzCodecOptions._arguments_repr  sz     ))T1Fd))* 	 255d6N6N6:6N6NP %dmm]22DKK##% 	&r   c                     | j                   | j                  | j                  | j                  | j                  | j
                  dS )z7Dictionary of the arguments used to create this object.rP   rP   r   s    r   _options_dictzCodecOptions._options_dict*  s@     #11#'#;#;,0,M,Mkk!//1 	1r   c                 V    | j                   j                  d| j                         dS )N(rB   )rH   r"   rg   r   s    r   rI   zCodecOptions.__repr__5  s     >>22D4H4H4JKKr   c                 Z    | j                         }|j                  |       t        di |S )ah  Make a copy of this CodecOptions, overriding some options::

            >>> from bson.codec_options import DEFAULT_CODEC_OPTIONS
            >>> DEFAULT_CODEC_OPTIONS.tz_aware
            False
            >>> options = DEFAULT_CODEC_OPTIONS.with_options(tz_aware=True)
            >>> options.tz_aware
            True

        .. versionadded:: 3.5
        r   )ri   updaterO   )r   kwargsoptss      r   with_optionszCodecOptions.with_options8  s,     !!#F#d##r   )
r"   r#   r$   r%   rb   r_   rg   ri   rI   rp   r   r   r   rO   rO      s8    DL %)$(-54	#HJ& 	1L$r   )rR   c                 n   t        | j                  dt        j                        | j                  dt        j                        | j                  d      | j                  dt        j
                        | j                  dt        j                        | j                  dt        j                              S )zParse BSON codec options.r   rQ   uuidrepresentationrS   rT   rU   rP   )rO   rd   DEFAULT_CODEC_OPTIONSr   rQ   rS   rT   rU   )optionss    r   _parse_codec_optionsru   M  s    {{3BBD-668#KK(<=%,[[*!>>&@ {{8%:%A%ABkk2@@BC Cr   )r%   r]   warningsr   r   collectionsr   bson.py3compatr   r   r   bson.binaryr	   r
   r   r   r   r   r(   r.   objectr0   _options_baserO   r[   rs   ru   r   r   r   <module>r|      s    /    " B B4 4
   /# (# (	[ 	GE6 GET @AX$= X$v %*88: Cr   