o a!@sdZddlZddlmZddlmZddlmZdddd d d Zia Gd d d Z d#ddZ ddZ ddZ ddZddZddZddZddZddZd$d!d"ZdS)%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_serializerzdjango.core.serializers.pythonzdjango.core.serializers.jsonzdjango.core.serializers.pyyamlzdjango.core.serializers.jsonl)xmlpythonjsonyamljsonlc@s$eZdZdZdZddZddZdS) 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. FcCs ||_dSN exception)selfr rB/usr/lib/python3/dist-packages/django/core/serializers/__init__.py__init__/s zBadSerializer.__init__cOs|jr r )rargskwargsrrr__call__2szBadSerializer.__call__N)__name__ __module__ __qualname____doc__internal_use_onlyrrrrrrr %s  r c Cs|dur ts tzt|}Wnty/}zt|}tdd||d}WYd}~nd}~ww|dur:|t|<dS|||<dS)aRegister 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. NBadSerializerModuler) Deserializer Serializer) _serializers_load_serializers importlib import_module ImportErrorr type)formatserializer_module serializersmoduleexcbad_serializerrrrregister_serializer6s   r)cCs$tst|tvr t|t|=dS)zCUnregister a given serializer. This is not a thread-safe operation.N)rrrr#rrrunregister_serializerVs  r+cC$tst|tvr t|t|jSr )rrrrr*rrrget_serializer_  r-cCststttSr )rrlistrrrrget_serializer_formatsgsr0cCststddtDS)NcSsg|] \}}|jjs|qSr)rr).0kvrrr psz1get_public_serializer_formats..)rritemsrrrrget_public_serializer_formatsmsr6cCr,r )rrrrr*rrrget_deserializersr.r7cKs$t|}|j|fi||S)zn Serialize a queryset (or any iterator that returns database objects) using a certain serializer. )r- serializegetvalue)r#querysetoptionssrrrr8{s r8cKst|}||fi|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}``. )r7)r#stream_or_stringr;drrr deserializesr?cCsNi}tD] }t|t||qttdr#tjD] }t|tj||q|adS)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)hasattrrr@r)r%r#rrrrs  rFc sg}t|D]p\}dur|D]a}|t|dr2t|jdg}|r1dd|D}ng}|jjD]}|jrO|jj }t|drO||krO| |q8|jj D]}|jj jj rn|jj }t|drn||krn| |qT| ||fqq|g|rg}d} |r|\}}tfdd|Dr |d } n| ||f|s| s|r|\}} |ntd d d dt|d ddD|}|sS)aSort 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. If allow_cycles is True, return the best-effort ordering that will respect most of dependencies but ignore some of them to break the cycles. N natural_key dependenciescSsg|]}t|qSr)r get_model)r1deprrrr4sz%sort_dependencies..Fc3s |] }|vp |vVqdSr r)r1r> model_listmodelsrr sz$sort_dependencies..Tz9Can't resolve dependencies for %s in serialized app list.z, css|] \}}|jjVqdSr )_metalabel)r1modeldepsrrrrJs  cSs |djS)Nr)r)objrrrs z#sort_dependencies..)key)set get_modelsaddrBgetattrrCrKfields remote_fieldrMappend many_to_manythrough auto_createdreversepopall RuntimeErrorjoinsorted) app_list allow_cyclesmodel_dependencies app_configrMrNfield rel_modelskippedchanged_rrGrsort_dependenciessj                rkr )F)rr django.appsr django.confrdjango.core.serializers.baserrArr r)r+r-r0r6r7r8r?rrkrrrrs.