o
    tBhg                     @   s   d dl mZmZ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
 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mZmZmZmZ 															dddZG dd deZdd Zdd Zdd ZdS )    )absolute_importdivisionunicode_literalsN)chainislice   )compat)util)tags)handlers)json)numeric_typesstring_typesPY3PY2TFc                 C   sD   |pt }|pt|||||||	||
||d}|j|j| |d||dS )a  Return a JSON formatted representation of value, a Python object.

    :param unpicklable: If set to False then the output will not contain the
        information necessary to turn the JSON data back into Python objects,
        but a simpler JSON stream is produced.
    :param max_depth: If set to a non-negative integer then jsonpickle will
        not recurse deeper than 'max_depth' steps into the object.  Anything
        deeper than 'max_depth' is represented using a Python repr() of the
        object.
    :param make_refs: If set to False jsonpickle's referencing support is
        disabled.  Objects that are id()-identical won't be preserved across
        encode()/decode(), but the resulting JSON stream will be conceptually
        simpler.  jsonpickle detects cyclical objects and will break the cycle
        by calling repr() instead of recursing when make_refs is set False.
    :param keys: If set to True then jsonpickle will encode non-string
        dictionary keys instead of coercing them into strings via `repr()`.
        This is typically what you want if you need to support Integer or
        objects as dictionary keys.
    :param numeric_keys: Only use this option if the backend supports integer
        dict keys natively.  This flag tells jsonpickle to leave numeric keys
        as-is rather than conforming them to json-friendly strings.
        Using ``keys=True`` is the typical solution for integer keys, so only
        use this if you have a specific use case where you want to allow the
        backend to handle serialization of numeric dict keys.
    :param warn: If set to True then jsonpickle will warn when it
        returns None for an object which it cannot pickle
        (e.g. file descriptors).
    :param max_iter: If set to a non-negative integer then jsonpickle will
        consume at most `max_iter` items when pickling iterators.
    :param use_decimal: If set to True jsonpickle will allow Decimal
        instances to pass-through, with the assumption that the simplejson
        backend will be used in `use_decimal` mode.  In order to use this mode
        you will need to configure simplejson::

            jsonpickle.set_encoder_options('simplejson',
                                           use_decimal=True, sort_keys=True)
            jsonpickle.set_decoder_options('simplejson',
                                           use_decimal=True)
            jsonpickle.set_preferred_backend('simplejson')

        NOTE: A side-effect of the above settings is that float values will be
        converted to Decimal when converting to json.
    :param use_base85:
        If possible, use base85 to encode binary data. Base85 bloats binary data
        by 1/4 as opposed to base64, which expands it by 1/3. This argument is
        ignored on Python 2 because it doesn't support it.
    :param fail_safe: If set to a function exceptions are ignored when pickling
        and if a exception happens the function is called and the return value
        is used as the value for the object that caused the error
    :param indent: When `indent` is a non-negative integer, then JSON array
        elements and object members will be pretty-printed with that indent
        level.  An indent level of 0 will only insert newlines. ``None`` is
        the most compact representation.  Since the default item separator is
        ``(', ', ': ')``,  the output might include trailing whitespace when
        ``indent`` is specified.  You can use ``separators=(',', ': ')`` to
        avoid this.  This value is passed directly to the active JSON backend
        library and not used by jsonpickle directly.
    :param separators:
        If ``separators`` is an ``(item_separator, dict_separator)`` tuple
        then it will be used instead of the default ``(', ', ': ')``
        separators.  ``(',', ':')`` is the most compact JSON representation.
        This value is passed directly to the active JSON backend library and
        not used by jsonpickle directly.

    >>> encode('my string') == '"my string"'
    True
    >>> encode(36) == '36'
    True
    >>> encode({'foo': True}) == '{"foo": true}'
    True
    >>> encode({'foo': [1, 2, [3, 4]]}, max_depth=1)
    '{"foo": "[1, 2, [3, 4]]"}'

    )unpicklable	make_refskeysbackend	max_depthwarnmax_iternumeric_keysuse_decimal
use_base85	fail_safe)reset)indent
separators)r   Picklerencodeflatten)valuer   r   r   r   r   r   r   contextr   r   r   r   r   r   r    r$   i/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/jsonpickle/pickler.pyr       s"   \r    c                   @   s  e Zd Z											d>ddZdd Zdd	 Zd
d Zdd Zdd Zdd Z	d?ddZ
dd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd@d*d+Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= ZdS )Ar   TNFc                 C   s   || _ || _|p	t| _|| _|| _|| _|
ot | _d| _	|| _
i | _g | _|| _|	| _i | _| jr;tj| _tj| _ntj| _tj| _|| _d S N)r   r   r   r   r   r   r   r   r   _depth
_max_depth_objs_seen	_max_iter_use_decimal
_flattenedr
   B85
_bytes_tagr	   	b85encode_bytes_encoderB64	b64encoder   )selfr   r   r   r   r   r   r   r   r   r   r   r$   r$   r%   __init__   s(   


zPickler.__init__c                 C   s   i | _ d| _g | _i | _d S r&   )r*   r(   r+   r.   r5   r$   r$   r%   r      s   
zPickler.resetc                 C   s   |  j d7  _ dS )z&Steps down one level in the namespace.r   N)r(   r7   r$   r$   r%   _push   s   zPickler._pushc                 C   s$   |  j d8  _ | j dkr|   |S )zzStep up one level in the namespace and return the value.
        If we're at the root, reset the pickler's state.
        r   r'   )r(   r   )r5   r"   r$   r$   r%   _pop   s   
zPickler._popc                 C   s.   t |}|| jv}|rt| j}|| j|< |S )z
        Log a reference to an in-memory object.
        Return True if this object is new and was assigned
        a new ID. Otherwise return False.
        )idr*   len)r5   objobjidis_newnew_idr$   r$   r%   _log_ref   s   


zPickler._log_refc                 C   s"   |  |}| j p| j }|p|S )z~
        Log a reference to an in-memory object, and return
        if that object should be considered newly logged.
        )r@   r   r   )r5   r<   r>   pretend_newr$   r$   r%   _mkref   s   
zPickler._mkrefc                 C   s   t j| jt|iS N)r
   IDr*   getr:   r5   r<   r$   r$   r%   _getref   s   zPickler._getrefc                 C   s   |r|    | |S )a  Takes an object and returns a JSON-safe representation of it.

        Simply returns any of the basic builtin datatypes

        >>> p = Pickler()
        >>> p.flatten('hello world') == 'hello world'
        True
        >>> p.flatten(49)
        49
        >>> p.flatten(350.0)
        350.0
        >>> p.flatten(True)
        True
        >>> p.flatten(False)
        False
        >>> r = p.flatten(None)
        >>> r is None
        True
        >>> p.flatten(False)
        False
        >>> p.flatten([1, 2, 3, 4])
        [1, 2, 3, 4]
        >>> p.flatten((1,2,))[tags.TUPLE]
        [1, 2]
        >>> p.flatten({'key': 'value'}) == {'key': 'value'}
        True
        )r   _flatten)r5   r<   r   r$   r$   r%   r!      s   
zPickler.flattenc                 C   s\   | j r| jr| |}|S z
| jt| }W |S  ty-   | | }| jt|< Y |S w rC   )r   r   _flatten_implr.   r:   KeyError)r5   r<   resultr$   r$   r%   rH     s   
zPickler._flattenc                 C   sj   t rt|tjr| |S t|r| |S t|r|S | j	r)t|t
jr)|S |   | | |S rC   )r   
isinstancetypesFileType_flatten_filer	   is_bytes_flatten_bytestringis_primitiver-   decimalDecimalr8   r9   _flatten_objrF   r$   r$   r%   rI     s   



zPickler._flatten_implc                 C   s   | j | jkS rC   )r(   r)   r7   r$   r$   r%   _max_reached   s   zPickler._max_reachedc              
   C   s   | j | |  }z$t|| j|| j}|rt}n| |}|d u r*| | W d S ||W S  t	t
fy> } z|d }~w ty[ } z| jd u rL|| |W  Y d }~S d }~ww rC   )r+   appendrV   	_in_cycler*   r   repr_get_flattener_pickle_warningKeyboardInterrupt
SystemExit	Exceptionr   )r5   r<   max_reachedin_cycleflatten_funcer$   r$   r%   rU   #  s(   



zPickler._flatten_objc                    s    fdd|D S )Nc                       g | ]}  |qS r$   rH   .0vr7   r$   r%   
<listcomp>@      z)Pickler._list_recurse.<locals>.<listcomp>r$   rF   r$   r7   r%   _list_recurse?  s   zPickler._list_recursec                    s    j }t|r |r|S     jS t|r& js |S  fddS t|r6 js0|S  fddS t	|r> j
S t|rEtS t|rM jS t|rU jS  | d S )Nc                       t j fdd| D iS )Nc                    rc   r$   rd   re   r7   r$   r%   rh   Q  ri   <Pickler._get_flattener.<locals>.<lambda>.<locals>.<listcomp>)r
   TUPLEr<   r7   r$   r%   <lambda>Q      z(Pickler._get_flattener.<locals>.<lambda>c                    rk   )Nc                    rc   r$   rd   re   r7   r$   r%   rh   V  ri   rl   )r
   SETrn   r7   r$   r%   ro   V  rp   )rj   r	   is_listrB   r8   rG   is_tupler   is_setis_dictionary_flatten_dict_objis_type
_mktyperef	is_object_ref_obj_instanceis_module_function_flatten_functionr[   )r5   r<   list_recurser$   r7   r%   rZ   B  s0   








zPickler._get_flattenerc                 C   sX   | j r| |r| |S | |S |  }t|| j|d}|r"dS | | | |S )z.Reference an existing object or flatten if newFN)r   rB   _flatten_obj_instancerG   rV   rX   r*   )r5   r<   r_   r`   r$   r$   r%   rz   h  s   




zPickler._ref_obj_instancec                 C   s   t st|tjs
J dS )z+
        Special case file objects
        N)r   rL   rM   rN   rF   r$   r$   r%   rO   }  s   zPickler._flatten_filec                 C   s4   t rz|dW S  ty   Y nw | j| |iS )Nzutf-8)r   decodeUnicodeDecodeErrorr0   r2   rF   r$   r$   r%   rQ     s   zPickler._flatten_bytestringc                 C   sT  i }t |d}t |d}| ot |d}t|d}t|d}t|d}t|\}	}
t |d}|r8|j}nt|}t|}t|t|}|dur^| j	rV||t
j< || ||S d}| j	rL|	rx|
sxz| }W n tyw   Y nw |
rz|d	}W n	 ty   Y nw |rt|trz!t|d
}tjt| }|D ]}t||}| |  W S W n ty   Y nw |rLt|}dt| }|r|dg| 7 }t|d dddkrt
j|d< |\}}}}}|rt |drt |dst|trL|d rt|d |d< |d rt|d |d< tt| j|}t|d }|d	kr?|| du r?|d8 }|d	kr?|| du s/|d|d  |t
j < |S |rt!|s| j	r^||t
j< |rntt| j|" |t
j#< |r~|s~| |$ |t
j%< |r| |& |t
j'< |rz|( }W n ty   | )| Y dS w | *||S t!|r| j	rdj+|j,d|t
j-< |S t./|}|S t0|r| 1|| |S t2|r| 3||S t4|rtt| jt5|| j6|t
j7< |S |rt2|r| 3||S t|dd | 1|j8|S |r| 9||S |r#|S | )| dS )z?Recursively flatten an instance and return a json-friendly dict	__class____dict__	__slots____getnewargs____getnewargs_ex____getinitargs____getstate__N   .   r   __name__ 
__newobj____setstate__      r   z{name}/{name})name_):hasattrr	   
has_method
has_reducer   typeimportable_namer   rE   r   r
   OBJECTr!   
__reduce__	TypeError__reduce_ex__rL   r   itersplitsysmodulesnextgetattrrH   rJ   listr;   NEWOBJdicttuplemapREDUCE	is_moduler   	NEWARGSEXr   NEWARGSr   INITARGSr   r[   	_getstateformatr   REPRr   ustris_dictionary_subclassrv   is_sequence_subclass_flatten_sequence_objis_iteratorr   r,   ITERATORr   _flatten_newstyle_with_slots)r5   r<   data	has_classhas_dict	has_slotshas_getnewargshas_getnewargs_exhas_getinitargsr   has_reduce_exhas_getstatecls
class_namehandler
reduce_valvarpathcurmodmodname
rv_as_listinsufficiencyfargsstate	listitems	dictitemsreduce_args
last_indexr$   r$   r%   r~     s   












zPickler._flatten_obj_instancec                 C   s"   | j rtjt|i}|S d }|S rC   )r   r
   FUNCTIONr	   r   )r5   r<   r   r$   r$   r%   r|   1  s
   zPickler._flatten_functionc           	      C   s  |du r|  }| jr2| j}t|D ]
\}}|||| q| j}t|D ]
\}}|||| q&n| j}t|D ]
\}}|||| q:t|drtt|j	rt|j	}t
|r\t|}n| |rk| t| }n| |}||d< t|dr| jri }| |j| ||d< |S )z8Recursively call flatten() and return json-friendly dictNdefault_factoryr   )r   r   _flatten_string_key_value_pairr	   items"_flatten_non_string_key_value_pair_flatten_key_value_pairr   callabler   rw   rx   rB   r~   r   CloneFactoryrG   r   rv   r   )	r5   r<   r   r!   krg   factoryr"   	dict_datar$   r$   r%   rv   9  s4   



zPickler._flatten_dict_objc              	   C   sJ   | j }d}|D ]}zt||}|||| W n	 ty   Y qw d}q|S )NFT)r   r   AttributeError)r5   r<   attrsr   r!   okr   r"   r$   r$   r%   _flatten_obj_attrsl  s   
zPickler._flatten_obj_attrsc                 C   sJ   dd |j  D }| |t| |s#dd t|D }| ||| |S )zAReturn a json-friendly dict for new-style objects with __slots__.c                 S   s   g | ]}t t|d t qS )r   )_wrap_string_slotr   r   )rf   r   r$   r$   r%   rh   {  s    z8Pickler._flatten_newstyle_with_slots.<locals>.<listcomp>c                 S   s$   g | ]}| d s|d s|qS )__)
startswithendswith)rf   xr$   r$   r%   rh     s    
)r   mror   r   dir)r5   r<   r   allslotsr   r$   r$   r%   r   y  s   z$Pickler._flatten_newstyle_with_slotsc                 C   st   t ||s|S |du rd}| jrt|trnt|ts1zt|}W n ty0   t	|}Y nw | 
|||< |S )z7Flatten a key/value pair into the passed-in dictionary.Nnull)r	   is_picklabler   rL   r   r   rY   r^   r   r   rH   r5   r   rg   r   r$   r$   r%   r     s   
zPickler._flatten_key_value_pairc                 C   s<   t ||s|S | jrt|ts| |}| |||< |S )z'Flatten only non-string key/value pairs)r	   r   r   rL   r   _escape_keyrH   r   r$   r$   r%   r     s   
z*Pickler._flatten_non_string_key_value_pairc                 C   s   t ||s|S | jrt|ts|S |tjr| |}n)|du r$d}| j	r-t|t
r-nt|tsGzt|}W n tyF   t|}Y nw | |||< |S )z$Flatten string key/value pairs only.Nr   )r	   r   r   rL   r   r   r
   JSON_KEYr   r   r   rY   r^   r   r   rH   r   r$   r$   r%   r     s(   


z&Pickler._flatten_string_key_value_pairc                    sB   t |dr |j|  fdd|D } jr||tj< |S |S )z4Return a json-friendly dict for a sequence subclass.r   c                    rc   r$   rd   re   r7   r$   r%   rh     ri   z1Pickler._flatten_sequence_obj.<locals>.<listcomp>)r   rv   r   r   r
   SEQ)r5   r<   r   r"   r$   r7   r%   r     s   

zPickler._flatten_sequence_objc              	   C   s   t jt|dd| | j| jd S )NFT)r   r   r#   r   r   )r
   r   r    r   r   )r5   r   r$   r$   r%   r     s   zPickler._escape_keyc                 C   s&   |  |}| jr||tj< |S |}|S rC   )rH   r   r
   STATE)r5   r<   r   r   r$   r$   r%   r     s   

zPickler._getstatec                 C   s    | j rd| }t | d S d S )Nz/jsonpickle cannot pickle %r: replaced with None)r   warnings)r5   r<   msgr$   r$   r%   r[     s   zPickler._pickle_warning)TTNNFFNFFFN)TrC   ) r   
__module____qualname__r6   r   r8   r9   r@   rB   rG   r!   rH   rI   rV   rU   rj   rZ   rz   rO   rQ   r~   r|   rv   r   r   r   r   r   r   r   r   r[   r$   r$   r$   r%   r      sT    
.	

 
& &
3	
r   c                 C   s.   |s| ot | |v ot|  ot|  S )z>Detect cyclic structures that would lead to infinite recursion)r:   r	   rR   is_enum)r<   objsr_   r   r$   r$   r%   rX     s
   

rX   c                 C   s   t jt| iS )zxReturn a typeref dictionary

    >>> _mktyperef(AssertionError) == {'py/type': 'builtins.AssertionError'}
    True

    )r
   TYPEr	   r   rn   r$   r$   r%   rx     s   rx   c                 C   s   t | tr| fS | S )z0Converts __slots__ = 'a' into __slots__ = ('a',))rL   r   )stringr$   r$   r%   r     s   
r   )TTFNTNFNNFFFNNN)
__future__r   r   r   rS   r   r   rM   	itertoolsr   r   r   r   r	   r
   r   r   r   r   r   r   r   r    objectr   rX   rx   r   r$   r$   r$   r%   <module>   sH   
o    ^	
