示例#1
0
 def save(self):
     """
     Base class save method. Performs basic validation and error handling.
     """
     if self.__abstract__:
         raise GoblinException('cant save abstract elements')
     self.pre_save()
     return self
示例#2
0
 def __init__(self, data):
     # this class is cached, so no performance hit here.
     if isinstance(data, element.klass):
         data = data.as_dict()
     elif not isinstance(data, dict):
         raise GoblinException("Result data is not tabular!")
     self.__data = data
     for k, v in data.items():
         if isinstance(k, string_types):
             setattr(self, k, v)
     self.__position = 0
     self.__ready = True
示例#3
0
    def _simple_deletion(self, operation, labels, **kwargs):
        """
        Perform simple bulk graph deletion operation.

        :param operation: The operation to be performed
        :type operation: str
        :param labels: The edge label to be used
        :type labels: str or Edge

        """
        from goblin.models.edge import Edge

        label_strings = []
        for label in labels:
            if inspect.isclass(label) and issubclass(label, Edge):
                label_string = label.get_label()
            elif isinstance(label, Edge):
                label_string = label.get_label()
            elif isinstance(label, string_types):
                label_string = label
            else:
                raise GoblinException("traversal labels must be edge " +
                                      "classes, instances, or strings")
            label_strings.append(label_string)

        future = connection.get_future(kwargs)
        future_result = self._delete_related(operation, label_strings)

        def on_read(f2):
            try:
                result = f2.result()
            except Exception as e:
                future.set_exception(e)
            else:
                future.set_result(result)

        def on_save(f):
            try:
                stream = f.result()
            except Exception as e:
                future.set_exception(e)
            else:
                future_read = stream.read()
                future_read.add_done_callback(on_read)

        future_result.add_done_callback(on_save)

        return future
示例#4
0
    def update(self, **values):
        """
        performs an update of this element with the given values and returns
        the saved object
        """
        if self.__abstract__:
            raise GoblinException('cant update abstract elements')
        self.pre_update(**values)
        for key in values.keys():
            if key not in self._properties:
                raise TypeError(
                    "unrecognized attribute name: '{}'".format(key))

        for k, v in values.items():
            setattr(self, k, v)

        return self.save()
示例#5
0
 def __delattr__(self, item):
     raise GoblinException("Row is not editable")
示例#6
0
 def __setattr__(self, key, value):
     # only allow 'private' fields to be set
     if not self.__ready or key.startswith(self.__okay_setattr):
         super(Row, self).__setattr__(key, value)
     else:
         raise GoblinException("Row is not editable")
示例#7
0
 def __delitem__(self, key):
     raise GoblinException("Row is not editable")
示例#8
0
 def __setitem__(self, key, value):
     raise GoblinException("Row is not editable")
示例#9
0
 def __delslice__(self, i, j):
     raise GoblinException("Row is not editable")
示例#10
0
 def __setslice__(self, i, j, sequence):
     raise GoblinException("Row is not editable")
示例#11
0
 def __delslice__(self, i, j):
     raise GoblinException("Cannot edit Table result")
示例#12
0
 def __setslice__(self, i, j, sequence):
     raise GoblinException("Cannot edit Table result")
示例#13
0
 def __delitem__(self, key):
     raise GoblinException("Cannot edit Table result")
示例#14
0
 def __setitem__(self, key, value):
     raise GoblinException("Cannot edit Table result")
示例#15
0
    def _simple_traversal(self,
                          operation,
                          labels,
                          limit=None,
                          offset=None,
                          types=None,
                          **kwargs):
        """
        Perform simple graph database traversals with ubiquitous pagination.

        :param operation: The operation to be performed
        :type operation: str
        :param labels: The edge labels to be used
        :type labels: list of Edges or strings
        :param start: The starting offset
        :type start: int
        :param max_results: The maximum number of results to return
        :type max_results: int
        :param types: The list of allowed result elements
        :type types: list

        """
        from goblin.models.edge import Edge
        label_strings = []
        for label in labels:
            if inspect.isclass(label) and issubclass(label, Edge):
                label_string = label.get_label()
            elif isinstance(label, Edge):
                label_string = label.get_label()
            elif isinstance(label, string_types):
                label_string = label
            else:
                raise GoblinException("traversal labels must be edge " +
                                      "classes, instances, or strings")
            label_strings.append(label_string)

        allowed_elts = None
        if types is not None:
            allowed_elts = []
            for e in types:
                if issubclass(e, Vertex):
                    allowed_elts += [e.get_label()]
                elif issubclass(e, Edge):
                    allowed_elts += [e.get_label()]

        if limit is not None and offset is not None:
            start = offset
            end = offset + limit
        else:
            start = end = None
        future = connection.get_future(kwargs)
        future_result = self._traversal(operation,
                                        label_strings,
                                        start,
                                        end,
                                        allowed_elts)

        def traversal_handler(data):
            if data is None:
                data = []
            return data

        def on_traversal(f):
            try:
                stream = f.result()
            except Exception as e:
                future.set_exception(e)
            else:
                stream.add_handler(traversal_handler)
                future.set_result(stream)

        future_result.add_done_callback(on_traversal)
        return future