2007-03-12 22:01:44 +08:00
|
|
|
"""
|
|
|
|
YAML serializer.
|
|
|
|
|
|
|
|
Requires PyYaml (http://pyyaml.org/), but that's checked for in __init__.
|
|
|
|
"""
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
import decimal
|
2008-07-30 18:40:37 +08:00
|
|
|
import yaml
|
2012-06-15 20:41:16 +08:00
|
|
|
from io import StringIO
|
2008-07-30 18:40:37 +08:00
|
|
|
|
2007-12-05 05:35:28 +08:00
|
|
|
from django.db import models
|
2012-02-10 02:56:58 +08:00
|
|
|
from django.core.serializers.base import DeserializationError
|
2007-03-12 22:01:44 +08:00
|
|
|
from django.core.serializers.python import Serializer as PythonSerializer
|
|
|
|
from django.core.serializers.python import Deserializer as PythonDeserializer
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import smart_bytes
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2012-06-11 01:53:35 +08:00
|
|
|
|
2007-03-12 22:01:44 +08:00
|
|
|
|
2009-02-12 10:54:39 +08:00
|
|
|
class DjangoSafeDumper(yaml.SafeDumper):
|
|
|
|
def represent_decimal(self, data):
|
|
|
|
return self.represent_scalar('tag:yaml.org,2002:str', str(data))
|
|
|
|
|
|
|
|
DjangoSafeDumper.add_representer(decimal.Decimal, DjangoSafeDumper.represent_decimal)
|
|
|
|
|
2007-03-12 22:01:44 +08:00
|
|
|
class Serializer(PythonSerializer):
|
|
|
|
"""
|
2007-03-15 15:48:36 +08:00
|
|
|
Convert a queryset to YAML.
|
2007-03-12 22:01:44 +08:00
|
|
|
"""
|
2009-12-14 20:39:20 +08:00
|
|
|
|
2007-12-17 14:53:15 +08:00
|
|
|
internal_use_only = False
|
2009-12-14 20:39:20 +08:00
|
|
|
|
2007-12-05 05:35:28 +08:00
|
|
|
def handle_field(self, obj, field):
|
|
|
|
# A nasty special case: base YAML doesn't support serialization of time
|
|
|
|
# types (as opposed to dates or datetimes, which it does support). Since
|
|
|
|
# we want to use the "safe" serializer for better interoperability, we
|
|
|
|
# need to do something with those pesky times. Converting 'em to strings
|
|
|
|
# isn't perfect, but it's better than a "!!python/time" type which would
|
|
|
|
# halt deserialization under any other language.
|
|
|
|
if isinstance(field, models.TimeField) and getattr(obj, field.name) is not None:
|
|
|
|
self._current[field.name] = str(getattr(obj, field.name))
|
|
|
|
else:
|
|
|
|
super(Serializer, self).handle_field(obj, field)
|
2009-12-14 20:39:20 +08:00
|
|
|
|
2007-03-12 22:01:44 +08:00
|
|
|
def end_serialization(self):
|
2009-02-12 10:54:39 +08:00
|
|
|
yaml.dump(self.objects, self.stream, Dumper=DjangoSafeDumper, **self.options)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2007-03-12 22:01:44 +08:00
|
|
|
def getvalue(self):
|
2012-06-28 22:28:25 +08:00
|
|
|
# Grand-parent super
|
|
|
|
return super(PythonSerializer, self).getvalue()
|
2007-03-12 22:01:44 +08:00
|
|
|
|
|
|
|
def Deserializer(stream_or_string, **options):
|
|
|
|
"""
|
2007-03-15 15:48:36 +08:00
|
|
|
Deserialize a stream or string of YAML data.
|
2007-03-12 22:01:44 +08:00
|
|
|
"""
|
2012-06-11 01:53:35 +08:00
|
|
|
if isinstance(stream_or_string, bytes):
|
2012-06-15 20:41:16 +08:00
|
|
|
stream_or_string = stream_or_string.decode('utf-8')
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(stream_or_string, six.string_types):
|
2012-06-15 20:41:16 +08:00
|
|
|
stream = StringIO(stream_or_string)
|
2007-03-12 22:01:44 +08:00
|
|
|
else:
|
|
|
|
stream = stream_or_string
|
2012-02-10 02:56:58 +08:00
|
|
|
try:
|
|
|
|
for obj in PythonDeserializer(yaml.safe_load(stream), **options):
|
|
|
|
yield obj
|
2012-02-10 09:13:38 +08:00
|
|
|
except GeneratorExit:
|
|
|
|
raise
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
2012-02-10 02:56:58 +08:00
|
|
|
# Map to deserializer error
|
|
|
|
raise DeserializationError(e)
|