Source code for bulbs.graph
# -*- coding: utf-8 -*-
#
#James Thornton (http://jamesthornton.com)
# BSD License (see LICENSE for details)
#
"""
Interface for interacting with a graph database through Rexster.
"""
import config
from rest import Resource
from element import Vertex, VertexProxy, Edge, EdgeProxy
from index import IndexProxy
from gremlin import Gremlin
[docs]class Graph(object): """The primary interface to the Rexster.""" def __init__(self,db_url=config.DATABASE_URL): """ Set up proxies for getting and removing elements from the DB. ::param db_url: The URL to the specific database on Rexster. """ self.resource = Resource(db_url) self.vertices = VertexProxy(self.resource) self.edges = EdgeProxy(self.resource) self.indices = IndexProxy(self.resource) self.gremlin = Gremlin(self.resource) @property
[docs] def V(self): """Returns all the vertices of the graph.""" return self.gremlin.query("g.V",Vertex)
@property[docs] def idxV(self,**pair): """ Returns a generator containing the Vertices matching the key and value. ::param pair: The key/value pair to match on. Example: name='James' """ resp = self._idx("vertices",**pair) if resp.results: return (Vertex(self.resource,result) for result in resp.results)
[docs] def idxE(self,**pair): """ Returns a generator containing the Edges matching the key and value. ::param pair: The key/value pair to match on. Example: name='James' """ resp = self._idx("edges",**pair) if resp.results: return (Edge(self.resource,result) for result in resp.results)
def _idx(self,index_name,**pair): """ Returns the Rexster Response object of the index lookup. ::param index_name: The name of the index. ::param pair: The key/value pair to match on. Example: name='James' """ key, value = pair.popitem() target = "%s/indices/%s" % (self.resource.db_name,index_name) params = dict(key=key,value=value) return self.resource.get(target,params)[docs] def clear(self): """Deletes all the elements. WARNING: this will delete all your data!""" target = self.resource.db_name resp = self.resource.delete(target,params=None) return resp