示例#1
0
def hydrate(self, data):
    """ Hydrate a dictionary of data to produce a :class:`.Node`,
    :class:`.Relationship` or other graph object instance. The
    data structure and values expected are those produced by the
    `REST API <http://neo4j.com/docs/stable/rest-api.html>`__.

    :arg data: dictionary of data to hydrate

    """
    if isinstance(data, dict):
        if "self" in data:
            if "type" in data:
                return Relationship.hydrate(data)
            else:
                return Node._find_hydrator(data)
        elif "nodes" in data and "relationships" in data:
            if "directions" not in data:
                from py2neo.batch import Job, Target
                node_uris = data["nodes"]
                relationship_uris = data["relationships"]
                jobs = [Job("GET", Target(uri)) for uri in relationship_uris]
                directions = []
                for i, result in enumerate(self.batch.submit(jobs)):
                    rel_data = result.content
                    start = rel_data["start"]
                    end = rel_data["end"]
                    if start == node_uris[i] and end == node_uris[i + 1]:
                        directions.append("->")
                    else:
                        directions.append("<-")
                data["directions"] = directions
            return Path.hydrate(data)
        elif "columns" in data and "data" in data:
            from py2neo.cypher import RecordList
            return RecordList.hydrate(data, self)
        elif "neo4j_version" in data:
            return self
        elif "exception" in data and ("stacktrace" in data or "stackTrace" in data):
            message = data.pop("message", "The server returned an error")
            raise GraphError(message, **data)
        else:
            warn("Map literals returned over the Neo4j REST interface are ambiguous "
                 "and may be hydrated as graph objects")
            return data
    elif is_collection(data):
        return type(data)(map(self.hydrate, data))
    else:
        return data