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