Skip to content

Instantly share code, notes, and snippets.

@farcaller
Created January 24, 2010 18:11
Show Gist options
  • Select an option

  • Save farcaller/285346 to your computer and use it in GitHub Desktop.

Select an option

Save farcaller/285346 to your computer and use it in GitHub Desktop.
from StringIO import StringIO
from django.core.serializers.python import Serializer as PythonSerializer
from django.core.serializers.python import Deserializer as PythonDeserializer
import plistlib
try:
import decimal
except ImportError:
from django.utils import _decimal as decimal # Python 2.3 fallback
class Serializer(PythonSerializer):
"""
Convert a queryset to Plist.
"""
internal_use_only = False
def end_serialization(self):
self.options.pop('stream', None)
self.options.pop('fields', None)
self.options.pop('use_natural_keys', None)
plistlib.writePlist(self.objects, self.stream)
def getvalue(self):
if callable(getattr(self.stream, 'getvalue', None)):
return self.stream.getvalue()
def Deserializer(stream_or_string, **options):
"""
Deserialize a stream or string of Plist data.
"""
if isinstance(stream_or_string, basestring):
o = plistlib.readPlistFromString(stream_or_string)
else:
o = plistlib.readPlist(stream_or_string)
for obj in PythonDeserializer(o, **options):
yield obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment