o
    tBhd                     @   s  d dl mZmZmZ d dlZd dl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 	d(ddZdd Zdd ZG dd deZG dd deZdd Zdd ZG dd deZd)ddZd)ddZG d d! d!Zd"d# Zd$d% Zd&d' ZdS )*    )absolute_importdivisionunicode_literalsN   )compat)util)tags)handlers)numeric_types)jsonFTc                 C   s4   |pt }|pt|||d}|| }|j|||dS )a  Convert a JSON string into a Python object.

    The keyword argument 'keys' defaults to False.
    If set to True then jsonpickle will decode non-string dictionary keys
    into python objects via the jsonpickle protocol.

    The keyword argument 'classes' defaults to None.
    If set to a single class, or a sequence (list, set, tuple) of classes,
    then the classes will be made available when constructing objects.  This
    can be used to give jsonpickle access to local classes that are not
    available through the global module import scope.

    The keyword argument 'safe' defaults to False.
    If set to True, eval() is avoided, but backwards-compatible
    (pre-0.7.0) deserialization of repr-serialized objects is disabled.

    The keyword argument 'backend' defaults to None.
    If set to an instance of jsonpickle.backend.JSONBackend, jsonpickle
    will use that backend for deserialization.

    >>> decode('"my string"') == 'my string'
    True
    >>> decode('36')
    36
    )keysbackendsafe)resetclasses)r   	Unpicklerdecoderestore)stringr   contextr   r   r   r   data r   k/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/jsonpickle/unpickler.pyr      s   
r   c                 C   s(   z	t | | W dS  ty   Y dS w )zBWorkaround unreliable hasattr() availability on sqlalchemy objectsTF)object__getattribute__AttributeError)objattrr   r   r   _safe_hasattr5   s   r   c                 C   s   t | tjo| tjS )z<Has this key a special object that has been encoded to JSON?)
isinstancer   string_types
startswithr   JSON_KEYkeyr   r   r   _is_json_key>   s   r%   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_Proxya  Proxies are dummy objects that are later replaced by real instances

    The `restore()` function has to solve a tricky problem when pickling
    objects with cyclical references -- the parent instance does not yet
    exist.

    The problem is that `__getnewargs__()`, `__getstate__()`, custom handlers,
    and cyclical objects graphs are allowed to reference the yet-to-be-created
    object via the referencing machinery.

    In other words, objects are allowed to depend on themselves for
    construction!

    We solve this problem by placing dummy Proxy objects into the referencing
    machinery so that we can construct the child objects before constructing
    the parent.  Objects are initially created with Proxy attribute values
    instead of real references.

    We collect all objects that contain references to proxies and run
    a final sweep over them to swap in the real instance.  This is done
    at the very end of the top-level `restore()`.

    The `instance` attribute below is replaced with the real instance
    after `__new__()` has been used to construct the object and is used
    when swapping proxies with real instances.

    c                 C   s
   d | _ d S Ninstanceselfr   r   r   __init__`      
z_Proxy.__init__c                 C   s   | j S r'   r(   r*   r   r   r   getc   s   z
_Proxy.getc                 C   s
   || _ d S r'   r(   )r+   r)   r   r   r   r   f   r-   z_Proxy.resetN)__name__
__module____qualname____doc__r,   r.   r   r   r   r   r   r&   C   s
    r&   c                   @   s   e Zd Zdd Zdd ZdS )_IDProxyc                 C   s   || _ || _d S r'   )_index_objs)r+   objsindexr   r   r   r,   k   s   
z_IDProxy.__init__c                 C   s   | j | j S r'   )r5   r4   r*   r   r   r   r.   o   s   z_IDProxy.getN)r/   r0   r1   r,   r.   r   r   r   r   r3   j   s    r3   c                 C   s   t | ||  d S r'   )setattrr.   )r   r   proxyr   r   r   _obj_setattrs      r:   c                 C   s   |  | |< d S r'   )r.   )r   idxr9   r   r   r   _obj_setvaluew   s   r=   c                   @   s  e Zd ZdDddZdd ZdEd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! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ ZdFd,d-Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Zd>d? Z d@dA Z!dBdC Z"dS )Gr   NFc                 C   s"   |pt | _|| _|| _|   d S r'   )r   r   r   r   r   )r+   r   r   r   r   r   r   r,   |   s   
zUnpickler.__init__c                 C   s(   i | _ g | _i | _g | _g | _i | _dS )z#Resets the object's internal state.N)	_namedict
_namestack_obj_to_idxr5   _proxies_classesr*   r   r   r   r      s   
zUnpickler.resetTc                 C   s4   |r|    |r| | | |}|r|   |S )a#  Restores a flattened object to its original python state.

        Simply returns any of the basic builtin types

        >>> u = Unpickler()
        >>> u.restore('hello world') == 'hello world'
        True
        >>> u.restore({'key': 'value'}) == {'key': 'value'}
        True

        )r   register_classes_restore_swap_proxies)r+   r   r   r   valuer   r   r   r      s   

zUnpickler.restorec                 C   s<   t |tttfr|D ]}| | q
dS || jt|< dS )zqRegister one or more classes

        :param classes: sequence of classes or a single class to register

        N)r   listtuplesetrC   rB   r   importable_name)r+   r   clsr   r   r   rC      s
   zUnpickler.register_classesc                 C   s*   | j D ]\}}}}|||| qg | _ dS )z2Replace proxies with their corresponding instancesN)rA   )r+   r   r   r9   methodr   r   r   rE      s   
zUnpickler._swap_proxiesc                 C   s6   t |tttttfsdd }||S | |}||S )Nc                 S      | S r'   r   xr   r   r   r         z#Unpickler._restore.<locals>.restore)r   strrG   dictrI   rH   _restore_tagsr+   r   r   r   r   r   rD      s
   
zUnpickler._restorec                 C   s  zt jt|kst|ttfvrdd }|W S W n	 ty!   Y nw t|t jr-| j	}|S t|t j
r8| j}|S t|t jrC| j}|S t|t jrN| j}|S t|t jrY| j}|S t|t jrd| j}|S t|t jro| j}|S t|t jrz| j}|S t|t jr| j}|S t|t jr| j}|S t|t jr| j}|S t|r| j }|S t|t j!r| j"}|S t|t j#r| j$}|S t%|r| j&}|S dd }|S )Nc                 S   rM   r'   r   rN   r   r   r   r      rP   z(Unpickler._restore_tags.<locals>.restorec                 S   rM   r'   r   rN   r   r   r   r      rP   )'r   RESERVEDrI   typerG   rR   	TypeErrorhas_tagB64_restore_base64B85_restore_base85ID_restore_idITERATOR_restore_iteratorTYPE_restore_typeREDUCE_restore_reduceOBJECT_restore_objectFUNCTION_restore_functionBYTES_restore_quopriREF_restore_refREPR_restore_reprr   is_list_restore_listTUPLE_restore_tupleSET_restore_setis_dictionary_restore_dictrT   r   r   r   rS      sn   " 


zUnpickler._restore_tagsc                 C      t |tj dS Nzutf-8)r   	b64decoder   rY   encoder+   r   r   r   r   rZ         zUnpickler._restore_base64c                 C   rw   rx   )r   	b85decoder   r[   rz   r{   r   r   r   r\      r|   zUnpickler._restore_base85c                 C   rw   rx   )quopridecodestringr   ri   rz   r{   r   r   r   rj      r|   zUnpickler._restore_quopric                 C   s   t | |tj S r'   )iterrp   r   r_   r{   r   r   r   r`      r;   zUnpickler._restore_iteratorc                 C   s  t  }| | tt| j|tj }t|dk r%|dgdt|   |\}}}}}|tj	ks9t
|dddkrU|d }	t|	tsG| |	}	|	j|	g|dd R  }
n|| }
|rz|
| W n` ty   z|
j D ]
\}}||| qo||
_W nA ty   z| D ]
\}}t|
|| qW n& ty   |\}}|r|
j| |r| D ]
\}}t|
|| qY nw Y nw Y nw |rz|
| W n ty   |D ]}|
| qY nw |r|D ]
\}}|
|| q||
 | ||
 |
S )z
        Supports restoring with all elements of __reduce__ as per pep 307.
        Assumes that iterator items (the last two) are represented as lists
        as per pickler implementation.
           Nr/    
__newobj__r   r   )r&   _mkrefrG   maprD   r   rc   lenextendNEWOBJgetattrr   rV   __new____setstate__r   __dict__items
setdefaultr8   	Exceptionupdateappend__setitem__r   _swapref)r+   r   r9   
reduce_valfargsstate	listitems	dictitemsrK   stage1kv
dict_stateslots_staterO   r   r   r   rd     sh   




zUnpickler._restore_reducec                 C   s6   z|t j }| j| W S  ty   t| j| Y S w r'   )r   r]   r5   
IndexErrorr3   )r+   r   r<   r   r   r   r^   A  s   
zUnpickler._restore_idc                 C   s   | j |tj S r'   )r>   r.   r   rk   r{   r   r   r   rl   H  s   zUnpickler._restore_refc                 C   s$   t |tj | jd}|d u r|S |S Nr   )	loadclassr   ra   rB   )r+   r   typerefr   r   r   rb   K  s   zUnpickler._restore_typec                 C   s"   | j rd S t|tj }| |S r'   )r   loadreprr   rm   r   r{   r   r   r   rn   Q  s   
zUnpickler._restore_reprc                 C   s   |t j }t|| jd}t|t|}|d ur5t }| | || |}|	| | 
|| |S |d u r>| |S | ||S r   )r   re   r   rB   r	   r.   r&   r   r   r   r   _restore_object_instance)r+   r   
class_namerK   handlerr9   r)   r   r   r   rf   X  s   



zUnpickler._restore_objectc                 C   s   t |tj | jdS r   )r   r   rg   rB   r{   r   r   r   rh   i  r;   zUnpickler._restore_functionc                 C   s2   z|d }W n
 t y   Y d S w |d= | |S )Ndefault_factory)KeyErrorrD   )r+   r   r   r   r   r   _loadfactoryl  s   
zUnpickler._loadfactoryc           	      C   s  t  }| | | |}t|tjr|tj \}}n	t|| jd}i }|r+| |}|r2| |}t	|t
p<t|dd  }z.|sft|drf|rY|j||g|R i |}||_n|j|g|R i |}nt|}W n tyw   d}Y nw |rz|| }W n  ty   zt|}W n ty   | | Y  Y S w Y nw || | || t	|tr|S | ||}t|drt	|jt r|j |_|S )Nr   __meta__r   Tr   )r&   r   r   rX   r   	NEWARGSEXgetargsrB   rD   r   rV   r   hasattrr   r   r   rW   make_blank_classicr   r   r   rH   "_restore_object_instance_variablesr   r.   )	r+   r   rK   r9   factoryr   kwargsis_oldstyler)   r   r   r   r   t  sX   






z"Unpickler._restore_object_instancec              	   C   s  |   }t}i }t|D ]s\}}|r|tjv rqt|tr#| }	n|}	| j	
|	 ||}| |}
t|s>t|rfz|dkrJt|||
 n|
||< W n tye   |dkr^|
||< | j	  Y qw t|||
 t|
tr{| j
|||
|f | j	  q|r||}|S )Nr   )_restore_key_fnr:   r   r   r   rU   r   r
   __str__r?   r   rD   is_noncomplexis_dictionary_subclassr8   rW   popr&   rA   	__class__)r+   r   r)   ignorereservedrestore_keyrL   deferredr   r   str_krF   r   r   r   _restore_from_dict  s>   





zUnpickler._restore_from_dictc                 C   s   |  ||}t|tjr7t|dr"|tj D ]
}|| | qnt|dr7|tj D ]
}|| | q,t|tjrC| 	||}|S )Nr   add)
r   rX   r   SEQr   r   rD   r   STATE_restore_state)r+   r   r)   r   r   r   r   r     s   

z,Unpickler._restore_object_instance_variablesc                 C   s   |  |tj }t|tot|dkot|d t}|o"t|d t}t|dr/|| |S t|tr>| j	||dd}|S |rX| j	|d |dd}|rV| j	|d |dd}|S t|dsdt|dsd|}|S )	N   r   r   r   F)r   __getnewargs____getnewargs_ex__)
rD   r   r   r   rH   r   rR   r   r   r   )r+   r   r)   r   	has_slotshas_slots_and_dictr   r   r   r     s,   "



zUnpickler._restore_statec                    sV   g   fdd|D }| t  fddtD }j| S )Nc                       g | ]}  |qS r   rD   .0r   r*   r   r   
<listcomp>      z+Unpickler._restore_list.<locals>.<listcomp>c                    s&   g | ]\}}t |tr|| fqS r   )r   r&   )r   r<   rF   )rL   parentr   r   r     s    
)r   r   r=   	enumeraterA   )r+   r   childrenproxiesr   )rL   r   r+   r   rp   
  s   

zUnpickler._restore_listc                    s   t  fdd|tj D S )Nc                    r   r   r   r   r*   r   r   r     r   z,Unpickler._restore_tuple.<locals>.<listcomp>)rH   r   rq   r{   r   r*   r   rr     s   zUnpickler._restore_tuplec                    s    fdd|t j D S )Nc                    s   h | ]}  |qS r   r   r   r*   r   r   	<setcomp>  r   z)Unpickler._restore_set.<locals>.<setcomp>)r   rs   r{   r   r*   r   rt     s   zUnpickler._restore_setc                 C   s*  i }| j rkt|D ]'\}}t|rq
t|tr| }n|}| j| | 	|||< | j
  q
t|D ]1\}}t|s@q7| j| | |}| 	| ||< }t|trc| j|||tf | j
  q7|S t|D ]"\}}t|tr~| }n|}| j| | 	|||< | j
  qp|S r'   )r   r   r   r%   r   r
   r   r?   r   rD   r   _restore_pickled_keyr&   rA   r=   )r+   r   r   r   r   r   resultr   r   r   rv     s:   





zUnpickler._restore_dictc                 C   s   | j r| j}|S dd }|S )zReturn a callable that restores keys

        This function is responsible for restoring non-string keys
        when we are decoding with `keys=True`.

        c                 S   rM   r'   r   r#   r   r   r   r   \  rP   z.Unpickler._restore_key_fn.<locals>.restore_key)r   r   )r+   r   r   r   r   r   L  s
   zUnpickler._restore_key_fnc                 C   s.   t |rt|ttjd | j| ddd}|S )zRestore a possibly pickled keyNTF)r   r   r   r   )r%   r   r   r   r"   r   )r+   r$   r   r   r   r   a  s   zUnpickler._restore_pickled_keyc                 C   s   dd | j S )a  Calculates the name of the current location in the JSON stack.

        This is called as jsonpickle traverses the object structure to
        create references to previously-traversed objects.  This allows
        cyclical data structures such as doubly-linked lists.
        jsonpickle ensures that duplicate python references to the same
        object results in only a single JSON object definition and
        special reference tags to represent each reference.

        >>> u = Unpickler()
        >>> u._namestack = []
        >>> u._refname() == '/'
        True
        >>> u._namestack = ['a']
        >>> u._refname() == '/a'
        True
        >>> u._namestack = ['a', 'b']
        >>> u._refname() == '/a/b'
        True

        /)joinr?   r*   r   r   r   _refnamem  s   zUnpickler._refnamec                 C   sX   t |}z| j|  W |S  ty+   t| j| j|< | j| || j|  < Y |S w r'   )idr@   r   r   r5   r   r>   r   )r+   r   obj_idr   r   r   r     s   zUnpickler._mkrefc                 C   sH   t |}t |}| j| }|| j|< | j|= || j|< || j|  < d S r'   )r   r@   r5   r>   r   )r+   r9   r)   proxy_idinstance_idinstance_indexr   r   r   r     s   


zUnpickler._swapref)NFF)TN)T)#r/   r0   r1   r,   r   r   rC   rE   rD   rS   rZ   r\   rj   r`   rd   r^   rl   rb   rn   rf   rh   r   r   r   r   r   rp   rr   rt   rv   r   r   r   r   r   r   r   r   r   r   {   sB    

/?
:..r   c                 C   s   |rz||  W S  t y   Y nw | d}tt|d ddD ]6}td|d| }zt| tj	| }||d D ]}t
||}q>|W   S  tttfyV   Y q w dS )zLoads the module and returns the class.

    >>> cls = loadclass('datetime.datetime')
    >>> cls.__name__
    'datetime'

    >>> loadclass('does.not.exist')

    >>> loadclass('builtins.int')()
    0

    .r   r   N)r   splitranger   r   untranslate_module_namer   
__import__sysmodulesr   r   ImportError
ValueError)module_and_namer   namesup_tomoduler   r   r   r   r   r     s&   



r   c                 C   s   t | tjr
tdt | tjr| tj S t | tjr | tj S z| tj }| tj }W n ty7   g  Y S w t	||d}|sBg S t
|drRt|jt|krR|S g S )z'Return arguments suitable for __new__()z+__newargs_ex__ returns both args and kwargsr   _fields)rX   r   r   r   NEWARGSINITARGSr   re   r   r   r   r   r   )r   r   seq_listobj_dictr   r   r   r   r     s&   



r   c                   @   s   e Zd ZdZdS )_trivialclassicz?
    A trivial class that can be instantiated with no args
    N)r/   r0   r1   r2   r   r   r   r   r     s    r   c                 C   s   t  }| |_|S )z
    Implement the mandated strategy for dealing with classic classes
    which cannot be instantiated without __getinitargs__ because they
    take parameters
    )r   r   )rK   r)   r   r   r   r     s   r   c                 C   sD   |  d\}}t }|}d|v r| ddd }t|||< t|S )zReturns an instance of the object from the object's repr() string.
    It involves the dynamic specification of code.

    >>> obj = loadrepr('datetime/datetime.datetime.now()')
    >>> obj.__class__.__name__
    'datetime'

    r   r   r   r   )r   localsr   eval)reprstrr   evalstrmylocals	localnamer   r   r   r     s   	r   c                 C   s   t | tu o	|| v S )zHelper class that tests to see if the obj is a dictionary
    and contains a particular key/tag.

    >>> obj = {'test': 1}
    >>> has_tag(obj, 'test')
    True
    >>> has_tag(obj, 'fail')
    False

    >>> has_tag(42, 'fail')
    False

    )rV   rR   )r   tagr   r   r   rX     s   rX   )NNFTFNr'   )
__future__r   r   r   r~   r   r   r   r   r   r	   r
   r   r   r   r   r%   r   r&   r3   r:   r=   r   r   r   r   r   r   rX   r   r   r   r   <module>   s8   
"	'	    
&
%