<s> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> from google . appengine . ext . db import polymodel <EOL> import datetime <EOL> import pyamf <EOL> from pyamf . adapters import util <EOL> class ModelStub ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , klass ) : <EOL> self . klass = klass <EOL> def properties ( self ) : <EOL> return self . klass . properties ( ) <EOL> def dynamic_properties ( self ) : <EOL> return [ ] <EOL> class GAEReferenceCollection ( dict ) : <EOL> """<STR_LIT>""" <EOL> def _getClass ( self , klass ) : <EOL> if not issubclass ( klass , ( db . Model , db . Expando ) ) : <EOL> raise TypeError ( '<STR_LIT>' % ( klass , ) ) <EOL> return self . setdefault ( klass , { } ) <EOL> def getClassKey ( self , klass , key ) : <EOL> """<STR_LIT>""" <EOL> d = self . _getClass ( klass ) <EOL> return d [ key ] <EOL> def addClassKey ( self , klass , key , obj ) : <EOL> """<STR_LIT>""" <EOL> d = self . _getClass ( klass ) <EOL> d [ key ] = obj <EOL> class DataStoreClassAlias ( pyamf . ClassAlias ) : <EOL> """<STR_LIT>""" <EOL> KEY_ATTR = '<STR_LIT>' <EOL> def _compile_base_class ( self , klass ) : <EOL> if klass in ( db . Model , polymodel . PolyModel ) : <EOL> return <EOL> pyamf . ClassAlias . _compile_base_class ( self , klass ) <EOL> def getCustomProperties ( self ) : <EOL> props = [ self . KEY_ATTR ] <EOL> self . reference_properties = { } <EOL> self . properties = { } <EOL> reverse_props = [ ] <EOL> for name , prop in self . klass . properties ( ) . iteritems ( ) : <EOL> self . properties [ name ] = prop <EOL> props . append ( name ) <EOL> if isinstance ( prop , db . ReferenceProperty ) : <EOL> self . reference_properties [ name ] = prop <EOL> if issubclass ( self . klass , polymodel . PolyModel ) : <EOL> del self . properties [ '<STR_LIT>' ] <EOL> props . remove ( '<STR_LIT>' ) <EOL> for name , value in self . klass . __dict__ . iteritems ( ) : <EOL> if isinstance ( value , db . _ReverseReferenceProperty ) : <EOL> reverse_props . append ( name ) <EOL> self . encodable_properties . update ( self . properties . keys ( ) ) <EOL> self . decodable_properties . update ( self . properties . keys ( ) ) <EOL> self . readonly_attrs . update ( reverse_props ) <EOL> if not self . reference_properties : <EOL> self . reference_properties = None <EOL> if not self . properties : <EOL> self . properties = None <EOL> self . no_key_attr = self . KEY_ATTR in self . exclude_attrs <EOL> def getEncodableAttributes ( self , obj , codec = None ) : <EOL> attrs = pyamf . ClassAlias . getEncodableAttributes ( self , obj , codec = codec ) <EOL> gae_objects = getGAEObjects ( codec . context ) if codec else None <EOL> if self . reference_properties and gae_objects : <EOL> for name , prop in self . reference_properties . iteritems ( ) : <EOL> klass = prop . reference_class <EOL> key = prop . get_value_for_datastore ( obj ) <EOL> if not key : <EOL> continue <EOL> try : <EOL> attrs [ name ] = gae_objects . getClassKey ( klass , key ) <EOL> except KeyError : <EOL> ref_obj = getattr ( obj , name ) <EOL> gae_objects . addClassKey ( klass , key , ref_obj ) <EOL> attrs [ name ] = ref_obj <EOL> for k in attrs . keys ( ) [ : ] : <EOL> if k . startswith ( '<STR_LIT:_>' ) : <EOL> del attrs [ k ] <EOL> for attr in obj . dynamic_properties ( ) : <EOL> attrs [ attr ] = getattr ( obj , attr ) <EOL> if not self . no_key_attr : <EOL> attrs [ self . KEY_ATTR ] = str ( obj . key ( ) ) if obj . is_saved ( ) else None <EOL> return attrs <EOL> def createInstance ( self , codec = None ) : <EOL> return ModelStub ( self . klass ) <EOL> def getDecodableAttributes ( self , obj , attrs , codec = None ) : <EOL> key = attrs . setdefault ( self . KEY_ATTR , None ) <EOL> attrs = pyamf . ClassAlias . getDecodableAttributes ( self , obj , attrs , codec = codec ) <EOL> del attrs [ self . KEY_ATTR ] <EOL> new_obj = None <EOL> if key and codec : <EOL> new_obj = loadInstanceFromDatastore ( self . klass , key , codec ) <EOL> if isinstance ( obj , ModelStub ) and hasattr ( obj , '<STR_LIT>' ) : <EOL> del obj . klass <EOL> if new_obj : <EOL> obj . __dict__ = new_obj . __dict__ . copy ( ) <EOL> obj . __class__ = self . klass <EOL> apply_init = True <EOL> if self . properties : <EOL> for k in [ k for k in attrs . keys ( ) if k in self . properties . keys ( ) ] : <EOL> prop = self . properties [ k ] <EOL> v = attrs [ k ] <EOL> if isinstance ( prop , db . FloatProperty ) and isinstance ( v , ( int , long ) ) : <EOL> attrs [ k ] = float ( v ) <EOL> elif isinstance ( prop , db . IntegerProperty ) and isinstance ( v , float ) : <EOL> x = long ( v ) <EOL> if x == v : <EOL> attrs [ k ] = x <EOL> elif isinstance ( prop , db . ListProperty ) and v is None : <EOL> attrs [ k ] = [ ] <EOL> elif isinstance ( v , datetime . datetime ) : <EOL> if isinstance ( prop , db . DateProperty ) : <EOL> attrs [ k ] = v . date ( ) <EOL> elif isinstance ( prop , db . TimeProperty ) : <EOL> attrs [ k ] = v . time ( ) <EOL> if new_obj is None and isinstance ( v , ModelStub ) and prop . required and k in self . reference_properties : <EOL> apply_init = False <EOL> del attrs [ k ] <EOL> if new_obj is None and apply_init is True : <EOL> obj . __init__ ( ** attrs ) <EOL> return attrs <EOL> def getGAEObjects ( context ) : <EOL> """<STR_LIT>""" <EOL> return context . extra . setdefault ( '<STR_LIT>' , GAEReferenceCollection ( ) ) <EOL> def loadInstanceFromDatastore ( klass , key , codec = None ) : <EOL> """<STR_LIT>""" <EOL> if not issubclass ( klass , ( db . Model , db . Expando ) ) : <EOL> raise TypeError ( '<STR_LIT>' % ( klass , ) ) <EOL> if not isinstance ( key , basestring ) : <EOL> raise TypeError ( '<STR_LIT>' , ( repr ( key ) , ) ) <EOL> key = str ( key ) <EOL> if codec is None : <EOL> return klass . get ( key ) <EOL> gae_objects = getGAEObjects ( codec . context ) <EOL> try : <EOL> return gae_objects . getClassKey ( klass , key ) <EOL> except KeyError : <EOL> pass <EOL> obj = klass . get ( key ) <EOL> gae_objects . addClassKey ( klass , key , obj ) <EOL> return obj <EOL> def writeGAEObject ( obj , encoder = None ) : <EOL> """<STR_LIT>""" <EOL> if not obj . is_saved ( ) : <EOL> encoder . writeObject ( obj ) <EOL> return <EOL> context = encoder . context <EOL> kls = obj . __class__ <EOL> s = obj . key ( ) <EOL> gae_objects = getGAEObjects ( context ) <EOL> try : <EOL> referenced_object = gae_objects . getClassKey ( kls , s ) <EOL> except KeyError : <EOL> referenced_object = obj <EOL> gae_objects . addClassKey ( kls , s , obj ) <EOL> encoder . writeObject ( referenced_object ) <EOL> pyamf . register_alias_type ( DataStoreClassAlias , db . Model ) <EOL> pyamf . add_type ( db . Query , util . to_list ) <EOL> pyamf . add_type ( db . Model , writeGAEObject ) </s>
