web/lib/django/core/serializers/pyyaml.py
changeset 38 77b6da96e6f1
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 """
       
     2 YAML serializer.
       
     3 
       
     4 Requires PyYaml (http://pyyaml.org/), but that's checked for in __init__.
       
     5 """
       
     6 
       
     7 from StringIO import StringIO
       
     8 import decimal
       
     9 import yaml
       
    10 
       
    11 from django.db import models
       
    12 from django.core.serializers.python import Serializer as PythonSerializer
       
    13 from django.core.serializers.python import Deserializer as PythonDeserializer
       
    14 
       
    15 class DjangoSafeDumper(yaml.SafeDumper):
       
    16     def represent_decimal(self, data):
       
    17         return self.represent_scalar('tag:yaml.org,2002:str', str(data))
       
    18 
       
    19 DjangoSafeDumper.add_representer(decimal.Decimal, DjangoSafeDumper.represent_decimal)
       
    20 
       
    21 class Serializer(PythonSerializer):
       
    22     """
       
    23     Convert a queryset to YAML.
       
    24     """
       
    25 
       
    26     internal_use_only = False
       
    27 
       
    28     def handle_field(self, obj, field):
       
    29         # A nasty special case: base YAML doesn't support serialization of time
       
    30         # types (as opposed to dates or datetimes, which it does support). Since
       
    31         # we want to use the "safe" serializer for better interoperability, we
       
    32         # need to do something with those pesky times. Converting 'em to strings
       
    33         # isn't perfect, but it's better than a "!!python/time" type which would
       
    34         # halt deserialization under any other language.
       
    35         if isinstance(field, models.TimeField) and getattr(obj, field.name) is not None:
       
    36             self._current[field.name] = str(getattr(obj, field.name))
       
    37         else:
       
    38             super(Serializer, self).handle_field(obj, field)
       
    39 
       
    40     def end_serialization(self):
       
    41         self.options.pop('stream', None)
       
    42         self.options.pop('fields', None)
       
    43         self.options.pop('use_natural_keys', None)
       
    44         yaml.dump(self.objects, self.stream, Dumper=DjangoSafeDumper, **self.options)
       
    45 
       
    46     def getvalue(self):
       
    47         return self.stream.getvalue()
       
    48 
       
    49 def Deserializer(stream_or_string, **options):
       
    50     """
       
    51     Deserialize a stream or string of YAML data.
       
    52     """
       
    53     if isinstance(stream_or_string, basestring):
       
    54         stream = StringIO(stream_or_string)
       
    55     else:
       
    56         stream = stream_or_string
       
    57     for obj in PythonDeserializer(yaml.load(stream), **options):
       
    58         yield obj
       
    59