2007-03-12 22:01:44 +08:00
|
|
|
"""
|
|
|
|
YAML serializer.
|
|
|
|
|
2018-09-26 14:48:47 +08:00
|
|
|
Requires PyYaml (https://pyyaml.org/), but that's checked for in __init__.
|
2007-03-12 22:01:44 +08:00
|
|
|
"""
|
|
|
|
|
2015-04-02 03:42:09 +08:00
|
|
|
import collections
|
2009-12-22 23:18:51 +08:00
|
|
|
import decimal
|
2012-06-15 20:41:16 +08:00
|
|
|
from io import StringIO
|
2008-07-30 18:40:37 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
import yaml
|
|
|
|
|
2012-02-10 02:56:58 +08:00
|
|
|
from django.core.serializers.base import DeserializationError
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.serializers.python import (
|
|
|
|
Deserializer as PythonDeserializer, Serializer as PythonSerializer,
|
|
|
|
)
|
|
|
|
from django.db import models
|
2012-06-11 01:53:35 +08:00
|
|
|
|
2013-01-06 01:01:16 +08:00
|
|
|
# Use the C (faster) implementation if possible
|
|
|
|
try:
|
|
|
|
from yaml import CSafeLoader as SafeLoader
|
|
|
|
from yaml import CSafeDumper as SafeDumper
|
|
|
|
except ImportError:
|
|
|
|
from yaml import SafeLoader, SafeDumper
|
2007-03-12 22:01:44 +08:00
|
|
|
|
2013-01-06 01:01:16 +08:00
|
|
|
|
|
|
|
class DjangoSafeDumper(SafeDumper):
|
2009-02-12 10:54:39 +08:00
|
|
|
def represent_decimal(self, data):
|
|
|
|
return self.represent_scalar('tag:yaml.org,2002:str', str(data))
|
|
|
|
|
2015-04-02 03:42:09 +08:00
|
|
|
def represent_ordered_dict(self, data):
|
|
|
|
return self.represent_mapping('tag:yaml.org,2002:map', data.items())
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2009-02-12 10:54:39 +08:00
|
|
|
DjangoSafeDumper.add_representer(decimal.Decimal, DjangoSafeDumper.represent_decimal)
|
2015-04-02 03:42:09 +08:00
|
|
|
DjangoSafeDumper.add_representer(collections.OrderedDict, DjangoSafeDumper.represent_ordered_dict)
|
2019-02-05 19:22:08 +08:00
|
|
|
# Workaround to represent dictionaries in insertion order.
|
|
|
|
# See https://github.com/yaml/pyyaml/pull/143.
|
|
|
|
DjangoSafeDumper.add_representer(dict, DjangoSafeDumper.represent_ordered_dict)
|
2009-02-12 10:54:39 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2007-03-12 22:01:44 +08:00
|
|
|
class Serializer(PythonSerializer):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Convert a queryset to YAML."""
|
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:
|
2017-01-21 21:13:44 +08:00
|
|
|
super().handle_field(obj, field)
|
2009-12-14 20:39:20 +08:00
|
|
|
|
2007-03-12 22:01:44 +08:00
|
|
|
def end_serialization(self):
|
2020-04-24 04:14:32 +08:00
|
|
|
self.options.setdefault('allow_unicode', True)
|
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):
|
2017-01-21 21:13:44 +08:00
|
|
|
# Grandparent super
|
2012-06-28 22:28:25 +08:00
|
|
|
return super(PythonSerializer, self).getvalue()
|
2007-03-12 22:01:44 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2007-03-12 22:01:44 +08:00
|
|
|
def Deserializer(stream_or_string, **options):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Deserialize a stream or string of YAML data."""
|
2012-06-11 01:53:35 +08:00
|
|
|
if isinstance(stream_or_string, bytes):
|
2017-02-08 01:05:47 +08:00
|
|
|
stream_or_string = stream_or_string.decode()
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(stream_or_string, str):
|
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:
|
2017-02-24 09:06:01 +08:00
|
|
|
yield from PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options)
|
2017-01-08 03:13:29 +08:00
|
|
|
except (GeneratorExit, DeserializationError):
|
2012-02-10 09:13:38 +08:00
|
|
|
raise
|
2017-01-08 03:13:29 +08:00
|
|
|
except Exception as exc:
|
|
|
|
raise DeserializationError() from exc
|