'Internal helper -- Populate self._entity or create a new one
if that one does not exist.  Does not change any state of the instance
other than the internal state of the entity.
This method is separate from _populate_internal_entity so that it is
possible to call to_xml without changing the state of an unsaved entity
to saved.
Returns:
self._entity or a new Entity which is not stored on the instance.'

def _populate_entity(self, _entity_class=datastore.Entity):

    if self.is_saved():
        entity = self._entity
    else:
        kwds = {'_app': self._app, 'namespace': self.__namespace, 'unindexed_properties': self._unindexed_properties}
        if (self._key is not None):
            if self._key.id():
                kwds['id'] = self._key.id()
            else:
                kwds['name'] = self._key.name()
            if self._key.parent():
                kwds['parent'] = self._key.parent()
        else:
            if (self._key_name is not None):
                kwds['name'] = self._key_name
            if (self._parent_key is not None):
                kwds['parent'] = self._parent_key
            elif (self._parent is not None):
                kwds['parent'] = self._parent._entity
        entity = _entity_class(self.kind(), **kwds)
    self._to_entity(entity)
    return entity
