'Internal helper: extracts the entity group from a key or entity.
Supports both v3 and v1 protobufs.
Args:
value: an entity_pb.{Reference, EntityProto} or
googledatastore.{Key, Entity}.
Returns:
A tuple consisting of:
- kind
- name, id, or (\'new\', unique id)'

def _extract_entity_group(self, value):

    if (_CLOUD_DATASTORE_ENABLED and isinstance(value, googledatastore.Entity)):
        value = value.key
    if isinstance(value, entity_pb.EntityProto):
        value = value.key()
    if (_CLOUD_DATASTORE_ENABLED and isinstance(value, googledatastore.Key)):
        elem = value.path[0]
        elem_id = elem.id
        elem_name = elem.name
        kind = elem.kind
    else:
        elem = value.path().element(0)
        kind = elem.type()
        elem_id = elem.id()
        elem_name = elem.name()
    return (kind, (elem_id or elem_name or ('new', id(elem))))
