示例#1
0
文件: graph.py 项目: gemius/pyArango
	def __init__(self, database, jsonInit) :
		self.database = database
		self.connection = self.database.connection
		try :
			self._key = jsonInit["_key"]
		except KeyError :
			self._key = jsonInit["name"]
		except KeyError :
			raise KeyError("'jsonInit' must have a field '_key' or a field 'name'")

		self.name = self._key
		self._rev = jsonInit["_rev"]
		self._id = jsonInit["_id"]
	
		self.definitions = {}
		for de in self._edgeDefinitions :
			if de.name not in self.database.collections and not COL.isEdgeCollection(de.name) :
				raise KeyError("'%s' is not a valid edge collection" % de.name)
			self.definitions[de.name] = de
		
		# for e in jsonInit["edgeDefinitions"] :
		# 	if e["collection"] not in self._edgeDefinitions :
		# 		raise CreationError("Collection '%s' is not mentioned in the definition of graph '%s'" % (e["collection"], self.__class__,__name__))
		# 	if e["from"] != self._edgeDefinitions[e["collection"]]["from"] :
		# 		vals = (e["collection"], self.__class__,__name__, self._edgeDefinitions[e["collection"]]["from"], e["from"])
		# 		raise CreationError("Edge definition '%s' of graph '%s' mismatch for 'from':\npython:%s\narangoDB:%s" % vals)
		# 	if e["to"] != self._edgeDefinitions[e["collection"]]["to"] :
		# 		vals = (e["collection"], self.__class__,__name__, self._edgeDefinitions[e["collection"]]["to"], e["to"])
		# 		raise CreationError("Edge definition '%s' of graph '%s' mismatch for 'to':\npython:%s\narangoDB:%s" % vals )
		# 	defs.append(e["collection"])

		# if jsonInit["orphanCollections"] != self._orphanCollections :
		# 	raise CreationError("Orphan collection '%s' of graph '%s' mismatch:\npython:%s\narangoDB:%s" (e["collection"], self.__class__,__name__, self._orphanCollections, jsonInit["orphanCollections"]))
			
		self.URL = "%s/%s" % (self.database.graphsURL, self._key)
示例#2
0
	def __init__(self, database, jsonInit) :
		self.database = database
		try :
			self._key = jsonInit["_key"]
		except KeyError :
			self._key = jsonInit["name"]
		except KeyError :
			raise KeyError("'jsonInit' must have a field '_key' or a field 'name'")

		self.name = self._key
		self._rev = jsonInit["_rev"]
		self._id = jsonInit["_id"]
	
		self.definitions = {}
		for de in self._edgeDefinitions :
			if de.name not in self.database.collections and not COL.isEdgeCollection(de.name) :
				raise KeyError("'%s' is not a valid edge collection" % de.name)
			self.definitions[de.name] = de
		
		# for e in jsonInit["edgeDefinitions"] :
		# 	if e["collection"] not in self._edgeDefinitions :
		# 		raise CreationError("Collection '%s' is not mentioned in the definition of graph '%s'" % (e["collection"], self.__class__,__name__))
		# 	if e["from"] != self._edgeDefinitions[e["collection"]]["from"] :
		# 		vals = (e["collection"], self.__class__,__name__, self._edgeDefinitions[e["collection"]]["from"], e["from"])
		# 		raise CreationError("Edge definition '%s' of graph '%s' mismatch for 'from':\npython:%s\narangoDB:%s" % vals)
		# 	if e["to"] != self._edgeDefinitions[e["collection"]]["to"] :
		# 		vals = (e["collection"], self.__class__,__name__, self._edgeDefinitions[e["collection"]]["to"], e["to"])
		# 		raise CreationError("Edge definition '%s' of graph '%s' mismatch for 'to':\npython:%s\narangoDB:%s" % vals )
		# 	defs.append(e["collection"])

		# if jsonInit["orphanCollections"] != self._orphanCollections :
		# 	raise CreationError("Orphan collection '%s' of graph '%s' mismatch:\npython:%s\narangoDB:%s" (e["collection"], self.__class__,__name__, self._orphanCollections, jsonInit["orphanCollections"]))
			
		self.URL = "%s/%s" % (self.database.graphsURL, self._key)
示例#3
0
	def createGraph(self, name, createCollections = True) :
		"""Creates a graph and returns it. 'name' must be the name of a class inheriting from Graph.
		You can decide weither or not you want non existing collections to be created by setting the value of 'createCollections'.
		If the value if 'false' checks will be performed to make sure that every collection mentionned in the edges definition exist. Raises a ValueError in case of
		a non-existing collection."""

		def _checkCollectionList(lst) :
			for colName in lst :
				if not COL.isCollection(colName) :
					raise ValueError("'%s' is not a defined Collection" % colName)

		graphClass = GR.getGraphClass(name)

		ed = []
		for e in graphClass._edgeDefinitions :
			if not createCollections :
				if not COL.isEdgeCollection(e.edgesCollection) :
					raise ValueError("'%s' is not a defined Edge Collection" % e.edgesCollection)
				_checkCollectionList(e.fromCollections)
				_checkCollectionList(e.toCollections)

			ed.append(e.toJson())
		
		if not createCollections :
			_checkCollectionList(graphClass._orphanedCollections)

		payload = {
				"name": name,
				"edgeDefinitions": ed,
				"orphanCollections": graphClass._orphanedCollections
			}
		

		payload = json.dumps(payload)
		r = self.connection.session.post(self.graphsURL, data = payload)
		data = r.json()

		if r.status_code == 201 or r.status_code == 202 :
			self.graphs[name] = graphClass(self, data["graph"])
		else :
			raise CreationError(data["errorMessage"], data)		
		return self.graphs[name]
示例#4
0
    def createGraph(self, name, createCollections=True):
        """Creates a graph and returns it. 'name' must be the name of a class inheriting from Graph.
		You can decide weither or not you want non existing collections to be created by setting the value of 'createCollections'.
		If the value if 'false' checks will be performed to make sure that every collection mentionned in the edges definition exist. Raises a ValueError in case of
		a non-existing collection."""
        def _checkCollectionList(lst):
            for colName in lst:
                if not COL.isCollection(colName):
                    raise ValueError("'%s' is not a defined Collection" %
                                     colName)

        graphClass = GR.getGraphClass(name)

        ed = []
        for e in graphClass._edgeDefinitions:
            if not createCollections:
                if not COL.isEdgeCollection(e.edgesCollection):
                    raise ValueError("'%s' is not a defined Edge Collection" %
                                     e.edgesCollection)
                _checkCollectionList(e.fromCollections)
                _checkCollectionList(e.toCollections)

            ed.append(e.toJson())

        if not createCollections:
            _checkCollectionList(graphClass._orphanedCollections)

        payload = {
            "name": name,
            "edgeDefinitions": ed,
            "orphanCollections": graphClass._orphanedCollections
        }

        payload = json.dumps(payload)
        r = requests.post(self.graphsURL, data=payload)
        data = r.json()
        if r.status_code == 201:
            self.graphs[name] = graphClass(self, data["graph"])
        else:
            raise CreationError(data["errorMessage"], data)
        return self.graphs[name]
示例#5
0
	def __init__(self, database, jsonInit) :
		self.database = database
		self.connection = self.database.connection
		try :
			self._key = jsonInit["_key"]
		except KeyError :
			self._key = jsonInit["name"]
		except KeyError :
			raise KeyError("'jsonInit' must have a field '_key' or a field 'name'")

		self.name = self._key
		self._rev = jsonInit["_rev"]
		self._id = jsonInit["_id"]
	
		self.definitions = {}
		for de in self._edgeDefinitions :
			if de.name not in self.database.collections and not COL.isEdgeCollection(de.name) :
				raise KeyError("'%s' is not a valid edge collection" % de.name)
			self.definitions[de.name] = de
		
		self.URL = "%s/%s" % (self.database.graphsURL, self._key)