def __response_iterator(self): producer = None columns = [] record_data = None for key, value in self.__response: key_len = len(key) if key_len > 0: section = key[0] if section == "columns": if key_len > 1: columns.append(value) elif section == "data": if key_len == 1: producer = RecordProducer(columns) yield tuple(columns) elif key_len == 2: if record_data is not None: yield producer.produce( self.graph.hydrate(assembled(record_data))) record_data = [] else: record_data.append((key[2:], value)) if record_data is not None: yield producer.produce(self.graph.hydrate(assembled(record_data))) self.close()
def get_or_create_index(self, content_type, index_name, config=None): """ Fetch a specific index from the current database, returning an :py:class:`Index` instance. If an index with the supplied `name` and content `type` does not exist, one is created with either the default configuration or that supplied in `config`:: # get or create a node index called "People" people = graph.get_or_create_index(neo4j.Node, "People") # get or create a relationship index called "Friends" friends = graph.get_or_create_index(neo4j.Relationship, "Friends") :param content_type: either :py:class:`neo4j.Node` or :py:class:`neo4j.Relationship` :param index_name: the name of the required index :return: an :py:class:`Index` instance .. seealso:: :py:func:`get_index` .. seealso:: :py:class:`Index` """ index = self.get_index(content_type, index_name) if index: return index index_manager = self._index_manager(content_type) rs = index_manager.post({"name": index_name, "config": config or {}}) index = Index(content_type, assembled(rs)["template"]) self._indexes[content_type].update({index_name: index}) return index
def get_layer(self, layer_name): resource = self.resources['getLayer'] spatial_data = dict(layer=layer_name, **EXTENSION_CONFIG) raw = resource.post(spatial_data) layer = assembled(raw) return layer
def get_or_create(self, key, value, abstract): """ Fetch a single entity from the index which is associated with the `key`:`value` pair supplied, creating a new entity with the supplied details if none exists:: # obtain a reference to the "Contacts" node index and # ensure that Alice exists therein contacts = graph.get_or_create_index(neo4j.Node, "Contacts") alice = contacts.get_or_create("name", "SMITH, Alice", { "given_name": "Alice Jane", "family_name": "Smith", "phone": "01234 567 890", "mobile": "07890 123 456" }) # obtain a reference to the "Friendships" relationship index and # ensure that Alice and Bob's friendship is registered (`alice` # and `bob` refer to existing nodes) friendships = graph.get_or_create_index(neo4j.Relationship, "Friendships") alice_and_bob = friendships.get_or_create( "friends", "Alice & Bob", (alice, "KNOWS", bob) ) .. """ return self.graph.hydrate( assembled(self._create_unique(key, value, abstract)))
def stream(self, batch): response = self.post(batch) try: for i, result_data in grouped(response): result = JobResult.hydrate(assembled(result_data), batch) log.info("<<< %s", result) yield result finally: response.close()
def __init__(self, response): assert response.status_code // 100 == 4 try: self.__cause__ = response except TypeError: pass if isinstance(response, JSONResponse): self._server_exception = ServerException(assembled(response)) Exception.__init__(self, self._server_exception.message) else: self._server_exception = None Exception.__init__(self, *response.args)
def create_layer(self, layer_name): """ Create a Layer to add geometries to. If a Layer with the name property value of ``layer_name`` already exists, nothing happens. """ resource = self.resources['addEditableLayer'] spatial_data = dict(layer=layer_name, **EXTENSION_CONFIG) raw = resource.post(spatial_data) layer = assembled(raw) return layer
def __init__(self, response): assert response.status_code // 100 == 5 try: self.__cause__ = response except TypeError: pass # TODO: check for unhandled HTML errors (on 500) if response.is_json: self._server_exception = ServerException(assembled(response)) Exception.__init__(self, self._server_exception.message) else: self._server_exception = None Exception.__init__(self, *response.args)
def get(self, key, value): """ Fetch a list of all entities from the index which are associated with the `key`:`value` pair supplied:: # obtain a reference to the "People" node index and # get all nodes where `family_name` equals "Smith" people = graph.get_or_create_index(neo4j.Node, "People") smiths = people.get("family_name", "Smith") .. """ return [ self.graph.hydrate(assembled(result)) for i, result in grouped( self._searcher.expand(key=key, value=value).get()) ]
def stream(self, batch): """ Execute a collection of jobs and yield results as received. :arg batch: A :class:`.Batch` of jobs. :rtype: generator """ response = self.post(batch) try: for i, result_data in grouped(response): result = JobResult.hydrate(assembled(result_data), batch) log.info("< %s", result) yield result finally: response.close()
def get(self, key, value): """ Fetch a list of all entities from the index which are associated with the `key`:`value` pair supplied:: # obtain a reference to the "People" node index and # get all nodes where `family_name` equals "Smith" people = graph.get_or_create_index(neo4j.Node, "People") smiths = people.get("family_name", "Smith") .. """ return [ self.graph.hydrate(assembled(result)) for i, result in grouped(self._searcher.expand(key=key, value=value).get()) ]
def __response_iterator(self): producer = None columns = [] record_data = None for key, value in self.__response: key_len = len(key) if key_len > 0: section = key[0] if section == "columns": if key_len > 1: columns.append(value) elif section == "data": if key_len == 1: producer = RecordProducer(columns) yield tuple(columns) elif key_len == 2: if record_data is not None: yield producer.produce(self.graph.hydrate(assembled(record_data))) record_data = [] else: record_data.append((key[2:], value)) if record_data is not None: yield producer.produce(self.graph.hydrate(assembled(record_data))) self.close()
def query(self, query): """ Query the index according to the supplied query criteria, returning a list of matched entities:: # obtain a reference to the "People" node index and # get all nodes where `family_name` equals "Smith" people = graph.get_or_create_index(neo4j.Node, "People") s_people = people.query("family_name:S*") The query syntax used should be appropriate for the configuration of the index being queried. For indexes with default configuration, this should be Apache Lucene query syntax. """ resource = self._query_template.expand(query=query) for i, result in grouped(resource.get()): yield self.graph.hydrate(assembled(result))
def _execute_spatial_request(self, resource, spatial_payload): try: json_stream = resource.post(spatial_payload) except GraphError as exc: if 'NullPointerException' in exc.full_name: # no results leads to a NullPointerException. # this is probably a bug on the Java side, but this # happens with some resources and must be managed. return [] raise if json_stream.status_code == 204: # no content return [] geometry_nodes = map(Node.hydrate, assembled(json_stream)) nodes = self._get_data_nodes(geometry_nodes) return nodes
def create_if_none(self, key, value, abstract): """ Create a new entity with the specified details within the current index, under the `key`:`value` pair supplied, if no such entity already exists. If creation occurs, the new entity will be returned, otherwise :py:const:`None` will be returned:: # obtain a reference to the "Contacts" node index and # create a node for Alice if one does not already exist contacts = graph.get_or_create_index(neo4j.Node, "Contacts") alice = contacts.create_if_none("name", "SMITH, Alice", { "given_name": "Alice Jane", "family_name": "Smith", "phone": "01234 567 890", "mobile": "07890 123 456" }) .. """ rs = self._create_unique(key, value, abstract) if rs.status_code == CREATED: return self.graph.hydrate(assembled(rs)) else: return None
def get_or_create(self, key, value, abstract): """ Fetch a single entity from the index which is associated with the `key`:`value` pair supplied, creating a new entity with the supplied details if none exists:: # obtain a reference to the "Contacts" node index and # ensure that Alice exists therein contacts = graph.get_or_create_index(neo4j.Node, "Contacts") alice = contacts.get_or_create("name", "SMITH, Alice", { "given_name": "Alice Jane", "family_name": "Smith", "phone": "01234 567 890", "mobile": "07890 123 456" }) # obtain a reference to the "Friendships" relationship index and # ensure that Alice and Bob's friendship is registered (`alice` # and `bob` refer to existing nodes) friendships = graph.get_or_create_index(neo4j.Relationship, "Friendships") alice_and_bob = friendships.get_or_create( "friends", "Alice & Bob", (alice, "KNOWS", bob) ) .. """ return self.graph.hydrate(assembled(self._create_unique(key, value, abstract)))
def __iter__(self): for i, response in grouped(self.__response): yield BatchResponse.hydrate(assembled(response), self.body_hydrator) self.close()
def _query_with_score(self, query, order): resource = self._query_template.expand(query=query, order=order) for i, result in grouped(resource.get()): meta = assembled(result) yield self.graph.hydrate(meta), meta["score"]