<s> """<STR_LIT>""" <EOL> from datetime import datetime <EOL> import re <EOL> from django . conf import settings <EOL> from django . core . serializers import base <EOL> from django . core . serializers import xml_serializer <EOL> from django . db import models <EOL> from google . appengine . api import datastore_types <EOL> from google . appengine . ext import db <EOL> from python import FakeParent <EOL> from python import parse_datetime_with_microseconds <EOL> getInnerText = xml_serializer . getInnerText <EOL> class Serializer ( xml_serializer . Serializer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Serializer , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _objects = [ ] <EOL> def handle_field ( self , obj , field ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def handle_fk_field ( self , obj , field ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def start_object ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def end_object ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> xml = obj . _entity . ToXml ( ) <EOL> xml = xml . replace ( u"""<STR_LIT>""" % obj . _entity . kind ( ) , <EOL> u"""<STR_LIT>""" % unicode ( obj . _meta ) ) <EOL> self . _objects . append ( xml ) <EOL> def getvalue ( self ) : <EOL> """<STR_LIT>""" <EOL> str = u"""<STR_LIT>""" <EOL> str += u"""<STR_LIT>""" <EOL> str += u"<STR_LIT>" . join ( self . _objects ) <EOL> str += u"""<STR_LIT>""" <EOL> return str <EOL> class Deserializer ( xml_serializer . Deserializer ) : <EOL> """<STR_LIT>""" <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> for event , node in self . event_stream : <EOL> if event == "<STR_LIT>" and node . nodeName == "<STR_LIT>" : <EOL> self . event_stream . expandNode ( node ) <EOL> return self . _handle_object ( node ) <EOL> raise StopIteration <EOL> def _handle_object ( self , node ) : <EOL> """<STR_LIT>""" <EOL> Model = self . _get_model_from_node ( node , "<STR_LIT>" ) <EOL> data = { } <EOL> key = db . Key ( node . getAttribute ( "<STR_LIT:key>" ) ) <EOL> if key . name ( ) : <EOL> data [ "<STR_LIT>" ] = key . name ( ) <EOL> parent = None <EOL> if key . parent ( ) : <EOL> parent = FakeParent ( key . parent ( ) ) <EOL> m2m_data = { } <EOL> for field_node in node . getElementsByTagName ( "<STR_LIT>" ) : <EOL> field_name = field_node . getAttribute ( "<STR_LIT:name>" ) <EOL> if not field_name : <EOL> raise base . DeserializationError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> field = Model . properties ( ) [ field_name ] <EOL> field_value = getInnerText ( field_node ) . strip ( ) <EOL> if isinstance ( field , db . Reference ) : <EOL> m = re . match ( "<STR_LIT>" , field_value ) <EOL> if not m : <EOL> raise base . DeserializationError ( u"<STR_LIT>" % <EOL> field_value ) <EOL> key = m . group ( <NUM_LIT:1> ) <EOL> key_obj = db . Key ( key ) <EOL> if not key_obj . name ( ) : <EOL> raise base . DeserializationError ( u"<STR_LIT>" <EOL> "<STR_LIT>" % field_value ) <EOL> data [ field . name ] = key_obj <EOL> else : <EOL> format = '<STR_LIT>' <EOL> if isinstance ( field , db . DateProperty ) : <EOL> field_value = datetime . strptime ( field_value , format ) . date ( ) <EOL> elif isinstance ( field , db . TimeProperty ) : <EOL> field_value = parse_datetime_with_microseconds ( field_value , <EOL> format ) . time ( ) <EOL> elif isinstance ( field , db . DateTimeProperty ) : <EOL> field_value = parse_datetime_with_microseconds ( field_value , format ) <EOL> data [ field . name ] = field . validate ( field_value ) <EOL> object = Model ( ** data ) <EOL> object . _parent = parent <EOL> return base . DeserializedObject ( object , m2m_data ) </s>
