Added yaml directly into BUILTIN_SERIALIZERS.
The serializer definitely exists, but the dependent yaml module may not be installed. The register_serializer() function will catch exceptions and will stub in a fake serializer object that will raise the exception when the serializer is used.
This commit is contained in:
parent
d8d61d8260
commit
c72392dab4
|
@ -17,6 +17,7 @@ To add your own serializers, use the SERIALIZATION_MODULES setting::
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import importlib
|
import importlib
|
||||||
|
import sys
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
|
@ -27,17 +28,29 @@ BUILTIN_SERIALIZERS = {
|
||||||
"xml" : "django.core.serializers.xml_serializer",
|
"xml" : "django.core.serializers.xml_serializer",
|
||||||
"python" : "django.core.serializers.python",
|
"python" : "django.core.serializers.python",
|
||||||
"json" : "django.core.serializers.json",
|
"json" : "django.core.serializers.json",
|
||||||
|
"yaml" : "django.core.serializers.pyyaml",
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check for PyYaml and register the serializer if it's available.
|
|
||||||
try:
|
|
||||||
import yaml
|
|
||||||
BUILTIN_SERIALIZERS["yaml"] = "django.core.serializers.pyyaml"
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
_serializers = {}
|
_serializers = {}
|
||||||
|
|
||||||
|
|
||||||
|
class BadSerializer(object):
|
||||||
|
"""
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
internal_use_only = False
|
||||||
|
|
||||||
|
def __init__(self, exception):
|
||||||
|
self.exception = exception
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
raise self.exception
|
||||||
|
|
||||||
|
|
||||||
def register_serializer(format, serializer_module, serializers=None):
|
def register_serializer(format, serializer_module, serializers=None):
|
||||||
"""Register a new serializer.
|
"""Register a new serializer.
|
||||||
|
|
||||||
|
@ -53,12 +66,23 @@ def register_serializer(format, serializer_module, serializers=None):
|
||||||
"""
|
"""
|
||||||
if serializers is None and not _serializers:
|
if serializers is None and not _serializers:
|
||||||
_load_serializers()
|
_load_serializers()
|
||||||
module = importlib.import_module(serializer_module)
|
|
||||||
|
try:
|
||||||
|
module = importlib.import_module(serializer_module)
|
||||||
|
except ImportError, exc:
|
||||||
|
bad_serializer = BadSerializer(exc)
|
||||||
|
|
||||||
|
module = type('BadSerializerModule', (object,), {
|
||||||
|
'Deserializer': bad_serializer,
|
||||||
|
'Serializer': bad_serializer,
|
||||||
|
})
|
||||||
|
|
||||||
if serializers is None:
|
if serializers is None:
|
||||||
_serializers[format] = module
|
_serializers[format] = module
|
||||||
else:
|
else:
|
||||||
serializers[format] = module
|
serializers[format] = module
|
||||||
|
|
||||||
|
|
||||||
def unregister_serializer(format):
|
def unregister_serializer(format):
|
||||||
"Unregister a given serializer. This is not a thread-safe operation."
|
"Unregister a given serializer. This is not a thread-safe operation."
|
||||||
if not _serializers:
|
if not _serializers:
|
||||||
|
|
Loading…
Reference in New Issue