2007-03-12 22:01:44 +08:00
|
|
|
"""
|
|
|
|
YAML serializer.
|
|
|
|
|
|
|
|
Requires PyYaml (http://pyyaml.org/), but that's checked for in __init__.
|
|
|
|
"""
|
|
|
|
|
2008-07-30 18:40:37 +08:00
|
|
|
from StringIO import StringIO
|
2009-12-22 23:18:51 +08:00
|
|
|
import decimal
|
2008-07-30 18:40:37 +08:00
|
|
|
import yaml
|
|
|
|
|
2007-12-05 05:35:28 +08:00
|
|
|
from django.db import models
|
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
|
|
|
|
|
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):
|
2007-06-01 21:39:08 +08:00
|
|
|
self.options.pop('stream', None)
|
|
|
|
self.options.pop('fields', None)
|
2009-12-14 20:39:20 +08:00
|
|
|
self.options.pop('use_natural_keys', None)
|
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):
|
|
|
|
return self.stream.getvalue()
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
if isinstance(stream_or_string, basestring):
|
|
|
|
stream = StringIO(stream_or_string)
|
|
|
|
else:
|
|
|
|
stream = stream_or_string
|
2009-12-22 23:18:51 +08:00
|
|
|
for obj in PythonDeserializer(yaml.load(stream), **options):
|
2007-03-12 22:01:44 +08:00
|
|
|
yield obj
|
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
|
|
|
|