コード例 #1
0
    def __call__(self, instance, *args, **kwargs):
        """
        Intercept attempts to call the GremlinMethod attribute and perform a gremlin query returning the results.

        :param instance: The class instance the method was called on
        :param pool: The RexPro connection pool to execute the query with (optional)
        :type instance: object

        """
        self._setup()

        # pop the optional execute query arguments from kwargs
        query_kwargs = connection.pop_execute_query_kwargs(kwargs)
        query_kwargs['transaction'] = query_kwargs.get('transaction') or self.transaction

        args = list(args)
        if not self.classmethod:
            args = [instance._id] + args

        params = self.defaults.copy()
        if len(args + list(kwargs.values())) > len(self.arg_list):  # pragma: no cover
            raise TypeError('%s() takes %s args, %s given' % (self.attr_name, len(self.arg_list), len(args)))

        #check for and calculate callable defaults
        for k, v in params.items():
            if callable(v):
                params[k] = v()

        arglist = self.arg_list[:]
        for arg in args:
            params[arglist.pop(0)] = arg

        for k, v in kwargs.items():
            if k not in arglist:
                an = self.attr_name
                if k in params:  # pragma: no cover
                    raise TypeError("%s() got multiple values for keyword argument '%s'" % (an, k))
                else:  # pragma: no cover
                    raise TypeError("%s() got an unexpected keyword argument '%s'" % (an, k))
            arglist.pop(arglist.index(k))
            params[k] = v

        params = self.transform_params_to_database(params)

        import_list = []
        for imp in self.imports + self.extra_imports:
            if imp is not None:
                for import_string in imp.import_list:
                    import_list.append(import_string)
        import_string = '\n'.join(import_list)

        script = '\n'.join([import_string, self.function_body])

        try:
            if hasattr(instance, 'get_element_type'):
                context = "vertices.{}".format(instance.get_element_type())
            elif hasattr(instance, 'get_label'):
                context = "edges.{}".format(instance.get_label())
            else:
                context = "other"

            context = "{}.{}".format(context, self.method_name)

            tmp = connection.execute_query(script, params, context=context, **query_kwargs)
        except MogwaiQueryError as pqe:  # pragma: no cover
            import pprint
            msg = "Error while executing Gremlin method\n\n"
            msg += "[Method]\n{}\n\n".format(self.method_name)
            msg += "[Params]\n{}\n\n".format(pprint.pformat(params))
            msg += "[Function Body]\n{}\n".format(self.function_body)
            msg += "[Imports]\n{}\n".format(import_string)
            msg += "\n[Error]\n{}\n".format(pqe)
            if hasattr(pqe, 'raw_response'):
                msg += "\n[Raw Response]\n{}\n".format(pqe.raw_response)
            raise MogwaiGremlinException(msg)
        return tmp
コード例 #2
0
 def create(cls, *args, **kwargs):
     """Create a new element with the given information."""
     # pop the optional execute query arguments from kwargs
     query_kwargs = connection.pop_execute_query_kwargs(kwargs)
     return cls(*args, **kwargs).save(**query_kwargs)