<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from django . conf import settings <EOL> from django . core . serializers import base <EOL> from django . db import models , DEFAULT_DB_ALIAS <EOL> from django . utils . xmlutils import SimplerXMLGenerator <EOL> from django . utils . encoding import smart_text <EOL> from xml . dom import pulldom <EOL> from xml . sax import handler <EOL> from xml . sax . expatreader import ExpatParser as _ExpatParser <EOL> class Serializer ( base . Serializer ) : <EOL> """<STR_LIT>""" <EOL> def indent ( self , level ) : <EOL> if self . options . get ( '<STR_LIT>' , None ) is not None : <EOL> self . xml . ignorableWhitespace ( '<STR_LIT:\n>' + '<STR_LIT:U+0020>' * self . options . get ( '<STR_LIT>' , None ) * level ) <EOL> def start_serialization ( self ) : <EOL> """<STR_LIT>""" <EOL> self . xml = SimplerXMLGenerator ( self . stream , self . options . get ( "<STR_LIT>" , settings . DEFAULT_CHARSET ) ) <EOL> self . xml . startDocument ( ) <EOL> self . xml . startElement ( "<STR_LIT>" , { "<STR_LIT:version>" : "<STR_LIT:1.0>" } ) <EOL> def end_serialization ( self ) : <EOL> """<STR_LIT>""" <EOL> self . indent ( <NUM_LIT:0> ) <EOL> self . xml . endElement ( "<STR_LIT>" ) <EOL> self . xml . endDocument ( ) <EOL> def start_object ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( obj , "<STR_LIT>" ) : <EOL> raise base . SerializationError ( "<STR_LIT>" % type ( obj ) ) <EOL> self . indent ( <NUM_LIT:1> ) <EOL> obj_pk = obj . _get_pk_val ( ) <EOL> if obj_pk is None : <EOL> attrs = { "<STR_LIT>" : smart_text ( obj . _meta ) , } <EOL> else : <EOL> attrs = { <EOL> "<STR_LIT>" : smart_text ( obj . _get_pk_val ( ) ) , <EOL> "<STR_LIT>" : smart_text ( obj . _meta ) , <EOL> } <EOL> self . xml . startElement ( "<STR_LIT:object>" , attrs ) <EOL> def end_object ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> self . indent ( <NUM_LIT:1> ) <EOL> self . xml . endElement ( "<STR_LIT:object>" ) <EOL> def handle_field ( self , obj , field ) : <EOL> """<STR_LIT>""" <EOL> self . indent ( <NUM_LIT:2> ) <EOL> self . xml . startElement ( "<STR_LIT>" , { <EOL> "<STR_LIT:name>" : field . name , <EOL> "<STR_LIT:type>" : field . get_internal_type ( ) <EOL> } ) <EOL> if getattr ( obj , field . name ) is not None : <EOL> self . xml . characters ( field . value_to_string ( obj ) ) <EOL> else : <EOL> self . xml . addQuickElement ( "<STR_LIT:None>" ) <EOL> self . xml . endElement ( "<STR_LIT>" ) <EOL> def handle_fk_field ( self , obj , field ) : <EOL> """<STR_LIT>""" <EOL> self . _start_relational_field ( field ) <EOL> related_att = getattr ( obj , field . get_attname ( ) ) <EOL> if related_att is not None : <EOL> if self . use_natural_keys and hasattr ( field . rel . to , '<STR_LIT>' ) : <EOL> related = getattr ( obj , field . name ) <EOL> related = related . natural_key ( ) <EOL> for key_value in related : <EOL> self . xml . startElement ( "<STR_LIT>" , { } ) <EOL> self . xml . characters ( smart_text ( key_value ) ) <EOL> self . xml . endElement ( "<STR_LIT>" ) <EOL> else : <EOL> self . xml . characters ( smart_text ( related_att ) ) <EOL> else : <EOL> self . xml . addQuickElement ( "<STR_LIT:None>" ) <EOL> self . xml . endElement ( "<STR_LIT>" ) <EOL> def handle_m2m_field ( self , obj , field ) : <EOL> """<STR_LIT>""" <EOL> if field . rel . through . _meta . auto_created : <EOL> self . _start_relational_field ( field ) <EOL> if self . use_natural_keys and hasattr ( field . rel . to , '<STR_LIT>' ) : <EOL> def handle_m2m ( value ) : <EOL> natural = value . natural_key ( ) <EOL> self . xml . startElement ( "<STR_LIT:object>" , { } ) <EOL> for key_value in natural : <EOL> self . xml . startElement ( "<STR_LIT>" , { } ) <EOL> self . xml . characters ( smart_text ( key_value ) ) <EOL> self . xml . endElement ( "<STR_LIT>" ) <EOL> self . xml . endElement ( "<STR_LIT:object>" ) <EOL> else : <EOL> def handle_m2m ( value ) : <EOL> self . xml . addQuickElement ( "<STR_LIT:object>" , attrs = { <EOL> '<STR_LIT>' : smart_text ( value . _get_pk_val ( ) ) <EOL> } ) <EOL> for relobj in getattr ( obj , field . name ) . iterator ( ) : <EOL> handle_m2m ( relobj ) <EOL> self . xml . endElement ( "<STR_LIT>" ) <EOL> def _start_relational_field ( self , field ) : <EOL> """<STR_LIT>""" <EOL> self . indent ( <NUM_LIT:2> ) <EOL> self . xml . startElement ( "<STR_LIT>" , { <EOL> "<STR_LIT:name>" : field . name , <EOL> "<STR_LIT>" : field . rel . __class__ . __name__ , <EOL> "<STR_LIT:to>" : smart_text ( field . rel . to . _meta ) , <EOL> } ) <EOL> class Deserializer ( base . Deserializer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stream_or_string , ** options ) : <EOL> super ( Deserializer , self ) . __init__ ( stream_or_string , ** options ) <EOL> self . event_stream = pulldom . parse ( self . stream , self . _make_parser ( ) ) <EOL> self . db = options . pop ( '<STR_LIT>' , DEFAULT_DB_ALIAS ) <EOL> def _make_parser ( self ) : <EOL> """<STR_LIT>""" <EOL> return DefusedExpatParser ( ) <EOL> def __next__ ( self ) : <EOL> for event , node in self . event_stream : <EOL> if event == "<STR_LIT>" and node . nodeName == "<STR_LIT:object>" : <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> if node . hasAttribute ( "<STR_LIT>" ) : <EOL> pk = node . getAttribute ( "<STR_LIT>" ) <EOL> else : <EOL> pk = None <EOL> data = { Model . _meta . pk . attname : Model . _meta . pk . to_python ( pk ) } <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> field = Model . _meta . get_field ( field_name ) <EOL> if field . rel and isinstance ( field . rel , models . ManyToManyRel ) : <EOL> m2m_data [ field . name ] = self . _handle_m2m_field_node ( field_node , field ) <EOL> elif field . rel and isinstance ( field . rel , models . ManyToOneRel ) : <EOL> data [ field . attname ] = self . _handle_fk_field_node ( field_node , field ) <EOL> else : <EOL> if field_node . getElementsByTagName ( '<STR_LIT:None>' ) : <EOL> value = None <EOL> else : <EOL> value = field . to_python ( getInnerText ( field_node ) . strip ( ) ) <EOL> data [ field . name ] = value <EOL> return base . DeserializedObject ( Model ( ** data ) , m2m_data ) <EOL> def _handle_fk_field_node ( self , node , field ) : <EOL> """<STR_LIT>""" <EOL> if node . getElementsByTagName ( '<STR_LIT:None>' ) : <EOL> return None <EOL> else : <EOL> if hasattr ( field . rel . to . _default_manager , '<STR_LIT>' ) : <EOL> keys = node . getElementsByTagName ( '<STR_LIT>' ) <EOL> if keys : <EOL> field_value = [ getInnerText ( k ) . strip ( ) for k in keys ] <EOL> obj = field . rel . to . _default_manager . db_manager ( self . db ) . get_by_natural_key ( * field_value ) <EOL> obj_pk = getattr ( obj , field . rel . field_name ) <EOL> if field . rel . to . _meta . pk . rel : <EOL> obj_pk = obj_pk . pk <EOL> else : <EOL> field_value = getInnerText ( node ) . strip ( ) <EOL> obj_pk = field . rel . to . _meta . get_field ( field . rel . field_name ) . to_python ( field_value ) <EOL> return obj_pk <EOL> else : <EOL> field_value = getInnerText ( node ) . strip ( ) <EOL> return field . rel . to . _meta . get_field ( field . rel . field_name ) . to_python ( field_value ) <EOL> def _handle_m2m_field_node ( self , node , field ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( field . rel . to . _default_manager , '<STR_LIT>' ) : <EOL> def m2m_convert ( n ) : <EOL> keys = n . getElementsByTagName ( '<STR_LIT>' ) <EOL> if keys : <EOL> field_value = [ getInnerText ( k ) . strip ( ) for k in keys ] <EOL> obj_pk = field . rel . to . _default_manager . db_manager ( self . db ) . get_by_natural_key ( * field_value ) . pk <EOL> else : <EOL> obj_pk = field . rel . to . _meta . pk . to_python ( n . getAttribute ( '<STR_LIT>' ) ) <EOL> return obj_pk <EOL> else : <EOL> m2m_convert = lambda n : field . rel . to . _meta . pk . to_python ( n . getAttribute ( '<STR_LIT>' ) ) <EOL> return [ m2m_convert ( c ) for c in node . getElementsByTagName ( "<STR_LIT:object>" ) ] <EOL> def _get_model_from_node ( self , node , attr ) : <EOL> """<STR_LIT>""" <EOL> model_identifier = node . getAttribute ( attr ) <EOL> if not model_identifier : <EOL> raise base . DeserializationError ( <EOL> "<STR_LIT>" % ( node . nodeName , attr ) ) <EOL> try : <EOL> Model = models . get_model ( * model_identifier . split ( "<STR_LIT:.>" ) ) <EOL> except TypeError : <EOL> Model = None <EOL> if Model is None : <EOL> raise base . DeserializationError ( <EOL> "<STR_LIT>" % ( node . nodeName , model_identifier ) ) <EOL> return Model <EOL> def getInnerText ( node ) : <EOL> """<STR_LIT>""" <EOL> inner_text = [ ] <EOL> for child in node . childNodes : <EOL> if child . nodeType == child . TEXT_NODE or child . nodeType == child . CDATA_SECTION_NODE : <EOL> inner_text . append ( child . data ) <EOL> elif child . nodeType == child . ELEMENT_NODE : <EOL> inner_text . extend ( getInnerText ( child ) ) <EOL> else : <EOL> pass <EOL> return "<STR_LIT>" . join ( inner_text ) <EOL> class DefusedExpatParser ( _ExpatParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> _ExpatParser . __init__ ( self , * args , ** kwargs ) <EOL> self . setFeature ( handler . feature_external_ges , False ) <EOL> self . setFeature ( handler . feature_external_pes , False ) <EOL> def start_doctype_decl ( self , name , sysid , pubid , has_internal_subset ) : <EOL> raise DTDForbidden ( name , sysid , pubid ) <EOL> def entity_decl ( self , name , is_parameter_entity , value , base , <EOL> sysid , pubid , notation_name ) : <EOL> raise EntitiesForbidden ( name , value , base , sysid , pubid , notation_name ) <EOL> def unparsed_entity_decl ( self , name , base , sysid , pubid , notation_name ) : <EOL> raise EntitiesForbidden ( name , None , base , sysid , pubid , notation_name ) <EOL> def external_entity_ref_handler ( self , context , base , sysid , pubid ) : <EOL> raise ExternalReferenceForbidden ( context , base , sysid , pubid ) <EOL> def reset ( self ) : <EOL> _ExpatParser . reset ( self ) <EOL> parser = self . _parser <EOL> parser . StartDoctypeDeclHandler = self . start_doctype_decl <EOL> parser . EntityDeclHandler = self . entity_decl <EOL> parser . UnparsedEntityDeclHandler = self . unparsed_entity_decl <EOL> parser . ExternalEntityRefHandler = self . external_entity_ref_handler <EOL> class DefusedXmlException ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> class DTDForbidden ( DefusedXmlException ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , sysid , pubid ) : <EOL> super ( DTDForbidden , self ) . __init__ ( ) <EOL> self . name = name <EOL> self . sysid = sysid <EOL> self . pubid = pubid <EOL> def __str__ ( self ) : <EOL> tpl = "<STR_LIT>" <EOL> return tpl . format ( self . name , self . sysid , self . pubid ) <EOL> class EntitiesForbidden ( DefusedXmlException ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , value , base , sysid , pubid , notation_name ) : <EOL> super ( EntitiesForbidden , self ) . __init__ ( ) <EOL> self . name = name <EOL> self . value = value <EOL> self . base = base <EOL> self . sysid = sysid <EOL> self . pubid = pubid <EOL> self . notation_name = notation_name <EOL> def __str__ ( self ) : <EOL> tpl = "<STR_LIT>" <EOL> return tpl . format ( self . name , self . sysid , self . pubid ) <EOL> class ExternalReferenceForbidden ( DefusedXmlException ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , context , base , sysid , pubid ) : <EOL> super ( ExternalReferenceForbidden , self ) . __init__ ( ) <EOL> self . context = context <EOL> self . base = base <EOL> self . sysid = sysid <EOL> self . pubid = pubid <EOL> def __str__ ( self ) : <EOL> tpl = "<STR_LIT>" <EOL> return tpl . format ( self . sysid , self . pubid ) </s>
