Ejemplo n.º 1
0
Archivo: rpc.py Proyecto: zaheerm/feat
 def got_reply(self, reply):
     if reply.payload['succeed']:
         return reply.payload['result']
     exc = reply.payload['exception']
     msg = reply.payload['message']
     if issubclass(exc, RPCException):
         return defer.fail(exc(msg))
     return defer.fail(exc("REMOTE: " + str(msg)))
Ejemplo n.º 2
0
def maybe_fiber(_function, *args, **kwargs):
    try:
        result = _function(*args, **kwargs)
    except Exception:
        return defer.fail(failure.Failure())
    else:
        if IFiber.providedBy(result):
            return result.start()
        if isinstance(result, defer.Deferred):
            return result
        elif isinstance(result, failure.Failure):
            return defer.fail(result)
        else:
            return defer.succeed(result)
Ejemplo n.º 3
0
Archivo: base.py Proyecto: sylane/feat
    def start_listening(self):
        assert self._port is None, "Already listening"

        if self._factory is None:
            self._factory = self._create_factory()

        if self._security.use_ssl:
            ssl_ctx_factory = self._security.get_ssl_context_factory()
            setup = self._create_ssl_setup(self._factory, ssl_ctx_factory)
        else:
            setup = self._create_tcp_setup(self._factory)

        for port in self._port_range:
            try:
                setup(port)
                break
            except tw_error.CannotListenError:
                continue

        if self._port is None:
            msg = ("Couldn't listen on any of the %d port(s) "
                   "from range starting with %d"
                   % (len(self._port_range), self._port_range[0]))
            return defer.fail(tw_error.CannotListenError(msg))

        self._on_listening()

        return defer.succeed(self)
Ejemplo n.º 4
0
    def testSolveConflictAlerts(self):
        solve = Method()
        self.patch(conflicts, 'solve', solve)

        solve.reset(defer.succeed('id'))
        yield self.agent.conflict_cb('id', 'rev', False, False)
        self.assertTrue(solve.called)
        # this should not resolve alert, this would make nagios blow up
        self.assertNotIn('conflict-id', self.state.alert_statuses)

        # now fail solving conflict
        r = defer.fail(conflicts.UnsolvableConflict('bum', {'_id': 'id'}))
        solve.reset(r)
        yield self.agent.conflict_cb('id', 'rev', False, False)
        self.assertTrue(solve.called)
        # this should raise the alert
        self.assertIn('couchdb-conflicts', self.state.alert_statuses)
        self.assertEqual(1, self.state.alert_statuses['couchdb-conflicts'][0])

        solve.reset(defer.succeed('id'))
        yield self.agent.conflict_cb('id', 'rev', False, False)
        self.assertTrue(solve.called)
        # this should resolve alert
        self.assertIn('couchdb-conflicts', self.state.alert_statuses)
        self.assertEqual(0, self.state.alert_statuses['couchdb-conflicts'][0])
Ejemplo n.º 5
0
 def notify_finish(self):
     if self._failed:
         return defer.fail(self._result)
     elif self._finished:
         return defer.succeed(self._result)
     else:
         return self._notifier.wait('finished')
Ejemplo n.º 6
0
Archivo: common.py Proyecto: f3at/feat
 def notify_finish(self):
     if self._finalize_called:
         if isinstance(self._result, (Exception, failure.Failure)):
             return defer.fail(self._result)
         else:
             return defer.succeed(self._result)
     return self._fnotifier.wait('finish')
Ejemplo n.º 7
0
 def wait_created(self):
     if self._created:
         return defer.succeed(self)
     if self._failure is not None:
         return defer.fail(self._failure)
     d = defer.Deferred()
     self._waiters.append(d)
     return d
Ejemplo n.º 8
0
 def expire_now(self):
     if self._expiration_call and self._expiration_call.active():
         self._expiration_call.reset(0)
         d = self.notify_finish()
         return d
     self.error('Expiration call %r is None or was already called '
                'or cancelled', self._expiration_call)
     return defer.fail(ProtocolExpired())
Ejemplo n.º 9
0
 def _call_async(self, method, args, kwargs):
     try:
         r = method(*args, **kwargs)
     except:
         self._defer_queue.put(defer.fail())
         return
     if not isinstance(r, defer.Deferred):
         self._defer_queue.put(defer.succeed(r))
         return
     self._defer_queue.put(r)
Ejemplo n.º 10
0
 def _call_async(self, method, args, kwargs):
     try:
         r = method(*args, **kwargs)
     except:
         self._defer_queue.put(defer.fail())
         return
     if not isinstance(r, defer.Deferred):
         self._defer_queue.put(defer.succeed(r))
         return
     self._defer_queue.put(r)
Ejemplo n.º 11
0
    def read(self, doc, *args, **kwargs):
        try:

            d = self._read_fun(doc, *args, **kwargs)

            if not isinstance(d, defer.Deferred):
                return defer.succeed(d)

            return d

        except:

            return defer.fail()
Ejemplo n.º 12
0
Archivo: emu.py Proyecto: f3at/feat
 def save_attachment(self, doc_id, revision, attachment):
     attachment = IAttachmentPrivate(attachment)
     doc = self._documents.get(doc_id)
     if not doc:
         return defer.fail(NotFoundError(doc_id))
     if '_attachments' not in doc:
         doc['_attachments'] = dict()
     doc['_attachments'][attachment.name] = dict(
         stub=True,
         content_type=attachment.content_type,
         length=attachment.length)
     self._attachments[doc['_id']][attachment.name] = attachment.get_body()
     self._set_id_and_revision(doc, doc_id)
     r = Response(ok=True, id=doc['_id'], rev=doc['_rev'])
     return defer.succeed(r)
Ejemplo n.º 13
0
 def read(self, document, iface, *args, **kwargs):
     """
     Returns a Deferred that fire the read object.
     """
     try:
         document = IReadableDocument(document)
         mime_type = document.mime_type
         reader = self.lookup_reader(mime_type, iface)
         if not reader:
             msg = ("No adapter found to read object %s from %s document"
                    % (iface.__class__.__name__, mime_type))
             raise NoReaderFoundError(msg)
         return reader.read(document, *args, **kwargs)
     except:
         return defer.fail(Failure())
Ejemplo n.º 14
0
    def write(self, doc, obj, *args, **kwargs):
        try:

            d = self._write_fun(doc, obj, *args, **kwargs)

            if (d is None) or (d is doc):
                return defer.succeed(doc)

            if isinstance(d, defer.Deferred):
                return d.addCallback(self._check_writer_result, doc)

            return self.registry.write(doc, d)

        except:

            return defer.fail()
Ejemplo n.º 15
0
 def write(self, document, obj, *args, **kwargs):
     """
     Returns a Deferred that fire the factory result
     that should be the document.
     """
     try:
         document = IWritableDocument(document)
         mime_type = document.mime_type
         writer = self.lookup_writer(mime_type, obj)
         if not writer:
             msg = ("No adapter found to write object %s to %s document"
                    % (obj.__class__.__name__, mime_type))
             raise NoWriterFoundError(msg)
         return writer.write(document, obj, *args, **kwargs)
     except:
         return defer.fail(Failure())
Ejemplo n.º 16
0
    def defer_to_thread(self, func, *args, **kw):
        if self.joined:
            return defer.fail(ThreadPoolError("Pool already stopped"))

        job_explanation = kw.pop('job_explanation', None) or \
                          reflect.canonical_name(func)
        job_id = self._next_job_id(job_explanation)
        cb = JobCallback(self, job_id)
        if self._stats:
            self._stats.new_item(job_id, job_explanation)
        ctx = context.theContextTracker.currentContext().contexts[-1]

        self.q.put((ctx, func, args, kw, cb, job_id))

        if self.started:
            self._start_some_workers()

        return cb.deferred
Ejemplo n.º 17
0
    def defer_to_thread(self, func, *args, **kw):
        if self.joined:
            return defer.fail(ThreadPoolError("Pool already stopped"))

        job_explanation = kw.pop('job_explanation', None) or \
                          reflect.canonical_name(func)
        job_id = self._next_job_id(job_explanation)
        cb = JobCallback(self, job_id)
        if self._stats:
            self._stats.new_item(job_id, job_explanation)
        ctx = context.theContextTracker.currentContext().contexts[-1]

        self.q.put((ctx, func, args, kw, cb, job_id))

        if self.started:
            self._start_some_workers()

        return cb.deferred
Ejemplo n.º 18
0
    def exit(self, result=None):
        self._cleanup()

        if not self._is_root:
            # If not a root section just return the result as-is
            return result

        # We are a root sections
        if IFiber.providedBy(result):
            # If the result is a fiber, we initialize and start it
            fiber = IFiber(result)
            self.descriptor.attach(fiber)
            return fiber.start()

        if isinstance(result, defer.Deferred):
            return result
        elif isinstance(result, failure.Failure):
            return defer.fail(result)
        else:
            return defer.succeed(result)
Ejemplo n.º 19
0
    def query(self, query, address, timeout=None):
        '''Interpret and delegate the query to the parent labour.
        The interface has been modified to take the address as an
        extra parameter, it need a modified factory to work.'''

        def package_response(response, name, rec_type):
            results = []

            if not isinstance(response, list):
                response = [response] if response is not None else []

            for rec in response:
                record = rec_type(*rec)
                header = dns.RRHeader(name, record.TYPE, dns.IN,
                                      record.ttl, record, auth=True)
                results.append(header)

            if not results:
                raise dns.DomainError(name)

            return results, [], []

        name = str(query.name)

        if query.type == dns.A:
            d = defer.succeed(name)
            d.addCallback(self._patron.lookup_address, address[0])
            d.addCallback(package_response, name, dns.Record_A)
            return d

        if query.type == dns.NS:
            d = defer.succeed(name)
            d.addCallback(self._patron.lookup_ns)
            d.addCallback(package_response, name, dns.Record_NS)
            return d

        return defer.fail(failure.Failure(dns.DomainError(query.name)))
Ejemplo n.º 20
0
            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)

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

        except:
            return defer.fail()

        else:
            d.callback(value)
            return d

    ### annotations ###
Ejemplo n.º 21
0
Archivo: emu.py Proyecto: f3at/feat
 def get_attachment(self, doc_id, name):
     if doc_id not in self._attachments:
         return defer.fail(NotFoundError(doc_id))
     if name not in self._attachments[doc_id]:
         return defer.fail(NotFoundError('%s/%s' % (doc_id, name)))
     return defer.succeed(self._attachments[doc_id][name])
Ejemplo n.º 22
0
Archivo: model.py Proyecto: sylane/feat
 def _notsup(self, feature_desc):
     msg = ("%s model %s does not support %s"
            % (self.identity, self.name, feature_desc, ))
     return defer.fail(NotSupported(msg))
Ejemplo n.º 23
0
Archivo: model.py Proyecto: sylane/feat
 def query_items(self, **kwargs):
     return defer.fail(NotSupported("Model do not support item queries"))
Ejemplo n.º 24
0
 def get_list_for_room(self, name):
     if self._should_work:
         res = {'session1': u'some.ip', 'session2': u'other.ip'}
         return defer.succeed(res)
     else:
         return defer.fail(ValueError('blah!'))
Ejemplo n.º 25
0
Archivo: model.py Proyecto: sylane/feat
 def query_items(self, **kwargs):
     return defer.fail(NotSupported("%s model %s do not support "
                                    "item queries" % (self.identity,
                                                      self.name)))
Ejemplo n.º 26
0
 def get_url_for_room(self, name):
     if self._should_work:
         res = {'url': u'20.30.10.30:1003', 'session_id': u'sth'}
         return defer.succeed(res)
     else:
         return defer.fail(ValueError('blah!'))
Ejemplo n.º 27
0
 def give_defer(result):
     if isinstance(result, Exception):
         return defer.fail(result)
     else:
         return defer.succeed(result)
Ejemplo n.º 28
0
def _solve_alert(connection, doc, conflicts):
    f = Failure(UnsolvableConflict("alert strategy", doc))
    return defer.fail(f)
Ejemplo n.º 29
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
Ejemplo n.º 30
0
 def give_defer(result):
     if isinstance(result, Exception):
         return defer.fail(result)
     else:
         return defer.succeed(result)