Пример #1
0
    def from_entity(cls, entity):

        if(_PATH_KEY_PROPERTY in entity and
           tuple(entity[_PATH_KEY_PROPERTY]) != cls.path_key()):
            key = entity[_PATH_KEY_PROPERTY].split(':')
            try:
                abspath = os.path.abspath(os.path.dirname(__file__))
                if abspath not in sys.path:
                    sys.path.insert(0,abspath)
                    
                    imported_class = DataController.import_model(key)
                    obj = imported_class()
                    
                    _class_map[obj.class_key()] = obj.__class__
                    
                    return obj.from_entity(entity)

            except ImportError:
                raise db.KindError('Could not import model hierarchy \'%s\'' % str(key))

        if (_CLASS_KEY_PROPERTY in entity and
            tuple(entity[_CLASS_KEY_PROPERTY]) != cls.class_key()):
            key = tuple(entity[_CLASS_KEY_PROPERTY])
            try:
                poly_class = _class_map[key]
            except KeyError:
                raise db.KindError('No implementation for class \'%s\'' % key)
            return poly_class.from_entity(entity)
        return super(PolyModel, cls).from_entity(entity)
Пример #2
0
    def _model_from_protobuf(cls, pb, _entity_class=datastore.Entity):
        
        logging.info('=========== Model from Protobuf ===========')
        logging.info('Pb: '+str(pb))
        
        entity = _entity_class.FromPb(pb)
        
        logging.info('Entity: '+str(entity))

        _n_obj = NormalizedObject.from_entity(entity)

        if _n_obj.data_class_path is not None:

            classpath = _n_obj.data_class_path.split('.')
            model = DataController.import_model(classpath)
            
            logging.info('Model: '+str(model))
        else:
            logging.critical('No data class path pointer found.') ##@TODO: fallback to regular from entity

        if _n_obj.parent() is not None:
            if _n_obj.is_saved():
                if _n_obj.key().name() is not None:
                    obj = model(_n_obj.key().parent(),key_name=_n_obj.key().name())
                else:
                    obj = model(_n_obj.key().parent())
            else:
                obj = model(_n_obj.parent())
        else:
            if _n_obj.is_saved():
                if _n_obj.key().name() is not None:
                    obj = model(key_name=_n_obj.key().name())
                else:
                    obj = model()
            else:
                obj = model()

        for item in entity:
            logging.info('Setting model property '+str(item) +' to value '+str(entity[item]))
            setattr(obj, item, entity[item])
        
        #res = model.from_entity(entity)
        logging.info('Finishing up and returning '+str(obj)+'.')
        return obj
Пример #3
0
    def from_entity(cls, entity):
    
        #logging.info(' ')
        #logging.info('========== PRE CLASS MAP DUMP ==========')
        
        #if len(_class_map) > 0:
                        
        #    for key in _class_map.keys():

        #        if key is None:
        #            logging.info('-- :: NONE KEY ::  =>  '+str(_class_map[key]))
        #        else:
        #            logging.info('--'+str(key)+'  =>  '+str(_class_map[key]))
             
        #logging.info('======== END PRE CLASS MAP DUMP ========')
        #logging.info(' ')
        
        logging.info(' ')        
        logging.info('============= PolyModel Entity Unpack ============= ')
        logging.info('class_key: '+str(cls.class_key()))
        logging.info('path_key: '+str(cls.path_key()))
        logging.info('cls: '+str(cls))
    
        if _CLASS_KEY_PROPERTY in entity:
            
            #if tuple(entity[_CLASS_KEY_PROPERTY]) != cls.class_key():
            #    logging.info('Model is not of base poly type E.')

            key = tuple(entity[_CLASS_KEY_PROPERTY])

            #if key in _class_map:

            #    logging.info('Key found in classmap. ('+str(_class_map[key])+')')
                
            #    poly_class = _class_map[key]
                
            #    rez = poly_class().from_entity(entity)
            #    logging.info('Resulting Entity: '+str(rez))
                
            #    return rez

        
            #else:
            #    logging.info('Key NOT found in classmap.')
                
            if(_PATH_KEY_PROPERTY in entity and entity[_PATH_KEY_PROPERTY] != cls.path_key()):

                logging.info('Model path is not equal to default.')

                key = entity[_PATH_KEY_PROPERTY]
                
                try:

                    logging.info('Attempting lazy-load.')
            
                    ### split away path from class name
                    class_path_t = key.split(_PATH_SEPERATOR)
        
                    #### build and execute an import statement to lazy-load the implementation class
                    if _IMPORT_PREFIX is not False:
                        if _IMPORT_PATH[-1] != '.': _IMPORT_PATH = _IMPORT_PATH+'.'
                        prefix = _IMPORT_PATH
                    else:
                        prefix = ''
                        
                    logging.info('Executing import request: '+str(class_path_t)+'.')

                    if _LOG_IMPORTS == True: logging.debug('[PolyModel]: Importing data class "'+str(class_path_t[-1])+'" from "'+str(prefix.join(class_path_t[0:-1]))+'".')
                    imported_class = DataController.import_model(class_path_t)

                    #### instantiate an empty class of the requested type
                    logging.info('Imported Class: '+str(imported_class))
                    logging.info('Attribute Request: '+str(class_path_t[-1]))
                    
            
                    #obj = getattr(imported_class,class_path_t[-1])()
                    obj = imported_class()

                    logging.info('Creating object... '+str(obj))
                    logging.info('Adding to Classmap: "'+str(obj.__class__)+'" with key "'+str(obj.class_key())+'".')
        
                    #### add the class to the _class_map... polymodel will do the rest
                    _class_map[obj.class_key()] = obj.__class__

                    ## Return Entity
                    logging.info('Lazy load successful. Creating entity...')
                                    
                    found_entity = obj.from_entity(entity)
        
                    logging.info('Resulting Entity: '+str(found_entity))
                    
                    return found_entity
                        
#                        else:
#                            logging.critical('Could not find attribute "'+str(class_path_t[-1])+'" on class "'+str(imported_class)+'".')
#                            logging.critical('PolyModel expansion resulted in failure. Terminating.')
#                            logging.info(' ')
            
                except ImportError:
                    logging.error('[PolyModel]: Error importing data class "'+str(class_path_t)+'".')
                    raise db.KindError('Could not import model hierarchy \'%s\'' % str(key))
        
                except NameError:
                    logging.error('[PolyModel]: Error instantiating data class "'+str(class_path_t)+'".')
                    raise db.KindError('Could not instantiate model implementation of type \'%s\'' % str(class_path_t[-1]))