o
    hF1                     @   s4  d dl mZ d dlmZ d dlmZmZ g dZdZG dd de	Z
G dd	 d	ZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd dZG dd deZG dd deZG dd deedZG dd deZG dd  d ZG d!d" d"ZG d#d$ d$Ze ZejZejZd%S )&    )forms)MediaDefiningClass)cached_propertyPromise)_type_args_dict_list_val_id_ref   c                   @   s   e Zd ZdS )UnpackableTypeErrorN)__name__
__module____qualname__ r   r   Q/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/telepath/__init__.pyr   
   s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )Nodeao  
    Intermediate representation of a packed value. Subclasses represent a particular value
    type, and implement emit_verbose (returns a dict representation of a value that can have
    an _id attached) and emit_compact (returns a compact representation of the value, in any
    JSON-serialisable type).

    If this node is assigned an id, emit() will return the verbose representation with the
    id attached on first call, and a reference on subsequent calls. To disable this behaviour
    (e.g. for small primitive values where the reference representation adds unwanted overhead),
    set self.use_id = False.
    c                 C   s   d | _ d| _d| _d S )NFT)idseenuse_idselfr   r   r   __init__   s   
zNode.__init__c                 C   sT   | j r| jr| jd urd| jiS d| _| j r&| jd ur&|  }| j|d< |S |  S )Nr   Tr   )r   r   r   emit_verboseemit_compact)r   resultr   r   r   emit   s   

z	Node.emitN)r   r   r   __doc__r   r   r   r   r   r   r      s    r   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )	ValueNodez+Represents a primitive value; int, bool etcc                    s   t    || _d| _d S )NF)superr   valuer   r   r"   	__class__r   r   r   0      

zValueNode.__init__c                 C   
   d| j iS Nr
   r"   r   r   r   r   r   5      
zValueNode.emit_verbosec                 C      | j S Nr)   r   r   r   r   r   8      zValueNode.emit_compact)r   r   r   r   r   r   r   __classcell__r   r   r$   r   r    .   s
    r    c                       ,   e Zd Z fddZdd Zdd Z  ZS )
StringNodec                    s"   t    || _t|tk| _d S r,   )r!   r   r"   lenSTRING_REF_MIN_LENGTHr   r#   r$   r   r   r   =   s   
zStringNode.__init__c                 C   r'   r(   r)   r   r   r   r   r   B   r*   zStringNode.emit_verbosec                 C   r+   r,   r)   r   r   r   r   r   E   r-   zStringNode.emit_compactr   r   r   r   r   r   r.   r   r   r$   r   r0   <   s    r0   c                       r/   )ListNodec                       t    || _d S r,   r!   r   r"   r#   r$   r   r   r   J      

zListNode.__init__c                 C   s   ddd | j D iS )Nr	   c                 S      g | ]}|  qS r   r   .0itemr   r   r   
<listcomp>O       z)ListNode.emit_verbose.<locals>.<listcomp>r)   r   r   r   r   r   N   s   zListNode.emit_verbosec                 C   s   dd | j D S )Nc                 S   r8   r   r9   r:   r   r   r   r=   R   r>   z)ListNode.emit_compact.<locals>.<listcomp>r)   r   r   r   r   r   Q   s   zListNode.emit_compactr3   r   r   r$   r   r4   I       r4   c                       r/   )DictNodec                    r5   r,   r6   r#   r$   r   r   r   V   r7   zDictNode.__init__c                 C   s   ddd | j  D iS )Nr   c                 S      i | ]	\}}||  qS r   r9   r;   keyvalr   r   r   
<dictcomp>[       z)DictNode.emit_verbose.<locals>.<dictcomp>)r"   itemsr   r   r   r   r   Z   s   zDictNode.emit_verbosec                    s2   t  fddtD r  S dd  j D S )Nc                 3   s    | ]}| j v V  qd S r,   r)   )r;   reserved_keyr   r   r   	<genexpr>^   s    z(DictNode.emit_compact.<locals>.<genexpr>c                 S   rA   r   r9   rB   r   r   r   rE   c   rF   z)DictNode.emit_compact.<locals>.<dictcomp>)anyDICT_RESERVED_KEYSr   r"   rG   r   r   r   r   r   ]   s   zDictNode.emit_compactr3   r   r   r$   r   r@   U   r?   r@   c                       r/   )
ObjectNodec                    s   t    || _|| _d S r,   )r!   r   constructorargs)r   rM   rN   r$   r   r   r   g   r&   zObjectNode.__init__c                 C   s   | j dd | jD dS )Nc                 S   r8   r   r9   r;   argr   r   r   r=   o   r>   z+ObjectNode.emit_verbose.<locals>.<listcomp>)r   r   )rM   rN   r   r   r   r   r   l   s   zObjectNode.emit_verbosec                 C   s   |   S r,   )r   r   r   r   r   r   r   s   zObjectNode.emit_compactr3   r   r   r$   r   rL   f   s    rL   c                   @      e Zd ZdZdd ZdS )BaseAdapterz/Handles serialisation of a specific object typec                 C      t |S )ay  
        Translates obj into a node that we can call emit() on to obtain the final serialisable
        form. Any media declarations that will be required for deserialisation of the object should
        be passed to context.add_media().

        This base implementation handles simple JSON-serialisable values such as integers, and
        wraps them as a ValueNode.
        )r    r   objcontextr   r   r   
build_nodey   s   	zBaseAdapter.build_nodeNr   r   r   r   rW   r   r   r   r   rR   w       rR   c                   @   s   e Zd Zdd ZdS )StringAdapterc                 C   rS   r,   )r0   rT   r   r   r   rW      s   zStringAdapter.build_nodeN)r   r   r   rW   r   r   r   r   rZ      s    rZ   c                   @   rQ   )DictAdapterzHandles serialisation of dictsc                    s   t  fdd| D S )Nc                    s    i | ]\}}t | |qS r   )strrW   rB   rV   r   r   rE      s    z*DictAdapter.build_node.<locals>.<dictcomp>)r@   rG   rT   r   r]   r   rW      s   zDictAdapter.build_nodeNrX   r   r   r   r   r[      rY   r[   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	Adaptera  
    Handles serialisation of custom types.
    Subclasses should define:
    - js_constructor: namespaced identifier for the JS constructor function that will unpack this
        object
    - js_args(obj): returns a list of (telepath-packable) arguments to be passed to the constructor
    - get_media(obj) or class Media: media definitions necessary for unpacking

    The adapter should then be registered with register(adapter, cls).
    c                 C   r+   r,   )mediar   rU   r   r   r   	get_media   r-   zAdapter.get_mediac                 C   s    | | | | j| |fS r,   )	add_mediara   js_constructorjs_argsrT   r   r   r   pack   s   zAdapter.packc                    s(   |  | \}}t| fdd|D S )Nc                       g | ]}  |qS r   rW   rO   r]   r   r   r=          z&Adapter.build_node.<locals>.<listcomp>)re   rL   )r   rU   rV   rM   rN   r   r]   r   rW      s   zAdapter.build_nodeN)r   r   r   r   ra   re   rW   r   r   r   r   r^      s
    r^   )	metaclassc                   @   rQ   )AutoAdapterzh
    Adapter for objects that define their own telepath_pack method that we can simply delegate to.
    c                 C   s
   | |S r,   )telepath_packrT   r   r   r   re      r*   zAutoAdapter.packN)r   r   r   r   re   r   r   r   r   rj      s    rj   c                   @   s6   e Zd ZdZdd Zedd ZdddZd	d
 ZdS )JSContextBasea6  
    Base class for JSContext classes obtained through AdapterRegistry.js_context_class.
    Subclasses of this are assigned the following class attributes:
    registry - points to the associated AdapterRegistry
    telepath_js_path - path to telepath.js (as per standard Django staticfiles conventions)

    A JSContext handles packing a set of values to be used in the same request; calls to
    JSContext.pack will return the packed representation and also update the JSContext's media
    property to include all JS needed to unpack the values seen so far.
    c                 C   s   | j | _tt| jg| _d S r,   )
base_mediar_   setr\   media_fragmentsr   r   r   r   r      s   zJSContextBase.__init__c                 C   s   t j| jgdS )N)js)r   Mediatelepath_js_pathr   r   r   r   rm      s   zJSContextBase.base_mediaNc                 C   sx   g }|r	| | |s|rt|tr|g}| tj||d |D ]}t|}|| jvr9|  j|7  _| j| q!d S )N)rp   css)append
isinstancer\   r   rq   ro   r_   add)r   r_   rp   rs   media_objects	media_obj	media_strr   r   r   rb      s   


zJSContextBase.add_mediac                 C   s   t | | S r,   )ValueContextrW   r   r`   r   r   r   re      s   zJSContextBase.pack)NNN)	r   r   r   r   r   propertyrm   rb   re   r   r   r   r   rl      s    


rl   c                   @   s:   e Zd ZdZeZdddZdd Zdd Ze	d	d
 Z
dS )AdapterRegistryz]
    Manages the mapping of Python types to their corresponding adapter implementations.
    telepath/js/telepath.jsc                 C   s8   || _ td t tt tt tt tt tt	 i| _
d S r,   )rr   typerR   boolintfloatr\   rZ   dictr[   adapters)r   rr   r   r   r   r      s   

zAdapterRegistry.__init__c                    s   t |dkr|s|\ }t tstd   j|< d S |s<|dp't  t ts3td   fdd}|S t |dkrUt|d trU|d }t j|< |S td)	N   z0register expected a BaseAdapter instance, got %radapterc                    s    j | < | S r,   )r   )clsr   r   r   r   wrapper  s   
z)AdapterRegistry.register.<locals>.wrapper   r   zwregister must be called as register(adapter, cls) or as a class decorator - @register or @register(adapter=MyAdapter()))r1   ru   rR   	TypeErrorr   getrj   r~   )r   rN   kwargsr   r   r   r   r   register   s$   

zAdapterRegistry.registerc                 C   s,   |j D ]}| j|}|d ur|  S qd S r,   )__mro__r   r   )r   r   baser   r   r   r   find_adapter  s   
zAdapterRegistry.find_adapterc                 C   s   t d| jf| | jdS )N	JSContext)registryrr   )r~   js_context_base_classrr   r   r   r   r   js_context_class   s   
z AdapterRegistry.js_context_classN)r}   )r   r   r   r   rl   r   r   r   r   r   r   r   r   r   r   r|      s    
%r|   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )rz   a  
    A context instantiated for each top-level value that JSContext.pack is called on. Results from
    this context's build_node method will be kept in a lookup table. If, over the course of
    building the node tree for the top level value, we encounter multiple references to the same
    value, a reference to the existing node will be generated rather than building it again. Calls
    to add_media are passed back to the parent context so that multiple calls to pack() will have
    their media combined in a single bundle.
    c                 C   s$   || _ |j| _i | _i | _d| _d S )Nr   )parent_contextr   
raw_valuesnodesnext_id)r   r   r   r   r   r   1  s
   
zValueContext.__init__c                 O   s   | j j|i | d S r,   )r   rb   )r   rN   r   r   r   r   rb   8  s   zValueContext.add_mediac                 C   sp   t |}z| j| }W n ty%   | |}|| j|< || j|< | Y S w |j d u r6| j|_ |  jd7  _|S )Nr   )r   r   KeyError_build_new_noder   r   )r   rD   obj_idexisting_nodenoder   r   r   rW   ;  s   




zValueContext.build_nodec                    st    j t|}|r|| S t|trtt|S zt|}W n t	y.   t
d| w t fdd|D S )Nz!don't know how to pack object: %rc                    rf   r   rg   r:   r   r   r   r=   b  rh   z0ValueContext._build_new_node.<locals>.<listcomp>)r   r   r~   rW   ru   r   r0   r\   iterr   r   r4   )r   rU   r   rG   r   r   r   r   P  s   
zValueContext._build_new_nodeN)r   r   r   r   r   rb   rW   r   r   r   r   r   rz   (  s    rz   N)djangor   django.formsr   django.utils.functionalr   r   rK   r2   r   r   r   r    r0   r4   r@   rL   rR   rZ   r[   r^   rj   rl   r|   rz   r   r   r   r   r   r   r   r   <module>   s.     	*IA
