B
    ©0H[	   ã               @   s¦   d Z ddlZddlmZ ddlmZ ddlmZ dddd	d
œZi a	G d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S )"aŽ  
Interfaces for serializing Django objects.

Usage::

    from django.core import serializers
    json = serializers.serialize("json", some_queryset)
    objects = list(serializers.deserialize("json", json))

To add your own serializers, use the SERIALIZATION_MODULES setting::

    SERIALIZATION_MODULES = {
        "csv": "path.to.csv.serializer",
        "txt": "path.to.txt.serializer",
    }

é    N)Úapps)Úsettings)ÚSerializerDoesNotExistz&django.core.serializers.xml_serializerzdjango.core.serializers.pythonzdjango.core.serializers.jsonzdjango.core.serializers.pyyaml)ÚxmlÚpythonÚjsonZyamlc               @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚBadSerializera&  
    Stub serializer to hold exception raised during registration

    This allows the serializer registration to cache serializers and if there
    is an error raised in the process of creating a serializer it will be
    raised and passed along to the caller when the serializer is used.
    Fc             C   s
   || _ d S )N)Ú	exception)Úselfr	   © r   údC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\core\serializers\__init__.pyÚ__init__.   s    zBadSerializer.__init__c             O   s
   | j ‚d S )N)r	   )r
   ÚargsÚkwargsr   r   r   Ú__call__1   s    zBadSerializer.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úinternal_use_onlyr   r   r   r   r   r   r   $   s   r   c          
   C   s|   |dkrt stƒ  yt |¡}W n< tk
r\ } zt|ƒ}tdd||dœƒ}W dd}~X Y nX |dkrp|t | < n||| < dS )a�  Register a new serializer.

    ``serializer_module`` should be the fully qualified module name
    for the serializer.

    If ``serializers`` is provided, the registration will be added
    to the provided dictionary.

    If ``serializers`` is not provided, the registration will be made
    directly into the global register of serializers. Adding serializers
    directly is not a thread-safe operation.
    NZBadSerializerModuler   )ÚDeserializerÚ
Serializer)Ú_serializersÚ_load_serializersÚ	importlibÚimport_moduleÚImportErrorr   Útype)ÚformatZserializer_moduleÚserializersÚmoduleÚexcZbad_serializerr   r   r   Úregister_serializer5   s    
r"   c             C   s$   t s
tƒ  | t krt| ƒ‚t | = dS )zCUnregister a given serializer. This is not a thread-safe operation.N)r   r   r   )r   r   r   r   Úunregister_serializerU   s
    r#   c             C   s$   t s
tƒ  | t krt| ƒ‚t |  jS )N)r   r   r   r   )r   r   r   r   Úget_serializer^   s
    r$   c               C   s   t s
tƒ  tt ƒS )N)r   r   Úlistr   r   r   r   Úget_serializer_formatsf   s    r&   c               C   s   t s
tƒ  dd„ t  ¡ D ƒS )Nc             S   s   g | ]\}}|j js|‘qS r   )r   r   )Ú.0ÚkÚvr   r   r   ú
<listcomp>o   s    z1get_public_serializer_formats.<locals>.<listcomp>)r   r   Úitemsr   r   r   r   Úget_public_serializer_formatsl   s    r,   c             C   s$   t s
tƒ  | t krt| ƒ‚t |  jS )N)r   r   r   r   )r   r   r   r   Úget_deserializerr   s
    r-   c             K   s    t | ƒƒ }|j|f|Ž | ¡ S )zn
    Serialize a queryset (or any iterator that returns database objects) using
    a certain serializer.
    )r$   Ú	serializeÚgetvalue)r   ZquerysetÚoptionsÚsr   r   r   r.   z   s    
r.   c             K   s   t | ƒ}||f|ŽS )a  
    Deserialize a stream or a string. Return an iterator that yields ``(obj,
    m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
    object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
    list_of_related_objects}``.
    )r-   )r   Zstream_or_stringr0   Údr   r   r   Údeserialize„   s    r3   c              C   sV   i } xt D ]}t|t | | ƒ q
W ttdƒrNx tjD ]}t|tj| | ƒ q4W | adS )zÈ
    Register built-in and settings-defined serializers. This is done lazily so
    that user code has a chance to (e.g.) set up custom settings without
    needing to be careful of import order.
    ÚSERIALIZATION_MODULESN)ÚBUILTIN_SERIALIZERSr"   Úhasattrr   r4   r   )r   r   r   r   r   r   �   s    

r   c          	      sÄ  g }t ƒ ‰xô| D ]ì\}‰ ˆ dkr(| ¡ ‰ xÒˆ D ]Ê}ˆ |¡ t|dƒrht|jdg ƒ}|rldd„ |D ƒ}ng }x:|jjD ].}|jrv|jj	}t|dƒrv||krv| 
|¡ qvW x@|jjD ]4}|jjjjr²|jj	}t|dƒr²||kr²| 
|¡ q²W | 
||f¡ q.W qW | ¡  g ‰ x²|�r¾g }d}xj|�r†| ¡ \}}d}	x(‡ ‡fdd	„|D ƒD ]}
|
�sJd}	�qJW |	�rtˆ  
|¡ d}n| 
||f¡ �qW |�s¶td
d dd	„ t|dd„ d�D ƒ¡ ƒ‚|}�qW ˆ S )a  Sort a list of (app_config, models) pairs into a single list of models.

    The single list of models is sorted so that any model with a natural key
    is serialized before a normal model, and any model with a natural key
    dependency has it's dependencies serialized first.
    NÚnatural_keyZdependenciesc             S   s   g | ]}t  |¡‘qS r   )r   Z	get_model)r'   Údepr   r   r   r*   ³   s    z%sort_dependencies.<locals>.<listcomp>FTc             3   s   | ]}|ˆkp|ˆ kV  qd S )Nr   )r'   r2   )Ú
model_listÚmodelsr   r   ú	<genexpr>Ü   s    z$sort_dependencies.<locals>.<genexpr>z9Can't resolve dependencies for %s in serialized app list.z, c             s   s&   | ]\}}d |j j|j jf V  qdS )z%s.%sN)Ú_metaZ	app_labelZobject_name)r'   ÚmodelÚdepsr   r   r   r;   è   s   c             S   s
   | d j S )Nr   )r   )Úobjr   r   r   Ú<lambda>é   s    z#sort_dependencies.<locals>.<lambda>)Úkey)ÚsetZ
get_modelsÚaddr6   Úgetattrr7   r<   ÚfieldsZremote_fieldr=   ÚappendZmany_to_manyZthroughZauto_createdÚreverseÚpopÚRuntimeErrorÚjoinÚsorted)Zapp_listZmodel_dependenciesZ
app_configr=   r>   ÚfieldZ	rel_modelZskippedÚchangedÚfoundÚ	candidater   )r9   r:   r   Úsort_dependenciesŸ   s\    


	


rP   )N)r   r   Zdjango.appsr   Zdjango.confr   Zdjango.core.serializers.baser   r5   r   r   r"   r#   r$   r&   r,   r-   r.   r3   r   rP   r   r   r   r   Ú<module>   s(   
 	
