Exemplo n.º 1
0
 def fetch_enabled(self):
     enabled = self._enabled
     if not callable(enabled):
         return defer.succeed(bool(enabled))
     # By default the key is the action name
     maker = IContextMaker(self.model)
     context = maker.make_context(key=self.name, action=self)
     return enabled(None, context)
Exemplo n.º 2
0
    def perform(self, *args, **kwargs):
        parameters = self._parameters # Only once cause it is costly
        value = None
        d = defer.Deferred()

        try:

            if len(args) > 0:
                values = list(args)
                values += [kwargs[u"value"]] if u"value" in kwargs else []
                if len(values) > 1:
                    raise TypeError("Action %s can only have one value: %s"
                                    % (self.name,
                                       ", ".join([repr(v) for v in values])))
                if args:
                    kwargs[u"value"] = args[0]

            params = set(kwargs.keys())
            expected = set(parameters.keys())
            required = set([p.name for p in parameters.itervalues()
                            if p.is_required])

            if not required <= params:
                raise TypeError("Action %s require parameters: %s"
                                % (self.name, ", ".join(required)))

            if not params <= expected:
                if not expected:
                    raise TypeError("Action %s expect no parameter"
                                    % (self.name, ))
                raise TypeError("Action %s expect only parameters: %s"
                                % (self.name, ", ".join(expected)))

            validated = {}
            for param_name, param_value in kwargs.iteritems():
                info = parameters[param_name].value_info
                validated[param_name] = IValidator(info).validate(param_value)

            for param in parameters.itervalues():
                if not param.is_required:
                    info = param.value_info
                    if param.name not in validated and info.use_default:
                        validated[param.name] = info.default

            value = validated.pop(u"value", None)

            # We use the model name instead of the action name as key
            maker = IContextMaker(self.model)
            context = maker.make_context(action=self)

            def log_effect_Error(failure, effect):
                error.handle_failure(None, failure,
                                     "Failure during effect %s execution",
                                     effect.func_name)
                return failure

            for effect in self._effects:
                try:
                    d.addCallback(effect, context, **validated)
                    d.addErrback(log_effect_Error, effect)
                except AssertionError:
                    err = ValueError("Invalid action effect: %r" % (effect, ))
                    return defer.fail(err)

            if self._result_info is not None:
                d.addCallback(IValidator(self._result_info).publish)
            else:
                d.addCallback(defer.override_result, None)

        except ValueError:
            return defer.fail()

        except TypeError:
            return defer.fail()

        else:
            d.callback(value)
            return d
Exemplo n.º 3
0
                                         for p, m in errors.iteritems())
                msg = ("Action %s parameter(s) invalid: %s"
                       % (self.name, param_errors))
                raise InvalidParameters(msg, params=errors)

            for param in parameters:
                if not param.is_required:
                    info = param.value_info
                    if param.name not in validated and info.use_default:
                        validated[param.name] = info.default

            value = validated.pop(u"value", None)

            # We use the model name instead of the action name as key
            maker = IContextMaker(self.model)
            context = maker.make_context(action=self)

            def log_effect_Error(failure, effect):
                error.handle_failure(None, failure,
                                     "Failure during effect %s execution",
                                     effect.func_name)
                return failure

            for effect in self._effects:
                try:
                    d.addCallback(effect, context, **validated)
                    d.addErrback(log_effect_Error, effect)
                except AssertionError:
                    err = ValueError("Invalid action effect: %r" % (effect, ))
                    failure = Failure(err, exc_tb=sys.exc_info()[2])
                    return defer.fail(failure)