Python twisted.internet.defer.Deferred() Examples

The following are 30 code examples of twisted.internet.defer.Deferred(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.internet.defer , or try the search function .
Example #1
Source File: account.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def unsubscribe(self, name):
        """
        Unsubscribe from this mailbox

        :param name: name of the mailbox
        :type name: str
        :rtype: Deferred
        """
        # TODO should raise MailboxException if attempted to unsubscribe
        # from a mailbox that is not currently subscribed.
        # TODO factor out with subscribe method.
        name = normalize_mailbox(name)

        def set_unsubscribed(mbox):
            return mbox.collection.set_mbox_attr("subscribed", False)

        d = self.getMailbox(name)
        d.addCallback(set_unsubscribed)
        return d 
Example #2
Source File: mail.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, store, user_id, ready_cb=None):
        self.store = store
        self.user_id = user_id
        self.adaptor = self.adaptor_class()

        self.mbox_indexer = MailboxIndexer(self.store)

        # This flag is only used from the imap service for the moment.
        # In the future, we should prevent any public method to continue if
        # this is set to True. Also, it would be good to plug to the
        # authentication layer.
        self.session_ended = False

        self.deferred_initialization = defer.Deferred()
        self._ready_cb = ready_cb

        self._init_d = self._initialize_storage()
        self._initialize_sync_hooks() 
Example #3
Source File: soledad.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def initialize_store(self, store):
        """
        Initialize the indexes in the database.

        :param store: store
        :returns: a Deferred that will fire when the store is correctly
                  initialized.
        :rtype: deferred
        """
        # TODO I think we *should* get another deferredLock in here, but
        # global to the soledad namespace, to protect from several points
        # initializing soledad indexes at the same time.
        self._wait_for_indexes()

        d = self._init_indexes(store)
        d.addCallback(self._restore_waiting_methods)
        return d 
Example #4
Source File: Refresh.py    From Timeline with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, penguin):
        self.penguin = penguin
        self.logger = logging.getLogger(TIMELINE_LOGGER)
        
        super(Refresh, self).__init__()

        self.logger.info("Penguin ASync-Refresh service initialized : Penguin - {}".format(self.penguin['nickname']))
        self.RefreshManagerLoop = LoopingCall(self._refresh)
        self.firstTimeCall = True
        self.CacheInitializedDefer = Deferred()
        self.cache = PenguinObject()

        self.penguin.penguin.data = self.cache # for easier access

        self.cacheHandlers = PenguinObject()

        self.logger.info("Penguin ASync-Refresh Loop started, every {}(s) : Penguin - {}".format
                         (self.REFRESH_INTERVAL, self.penguin['nickname']))

        self.RefreshManagerLoop.start(self.REFRESH_INTERVAL) 
Example #5
Source File: sender.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def send(self, recipient, message):
        self.log.info(
            'Connecting to SMTP server %s:%s' % (self._host, self._port))

        # we construct a defer to pass to the ESMTPSenderFactory
        d = defer.Deferred()
        # we don't pass an ssl context factory to the ESMTPSenderFactory
        # because ssl will be handled by reactor.connectSSL() below.
        factory = smtp.ESMTPSenderFactory(
            "",  # username is blank, no client auth here
            "",  # password is blank, no client auth here
            self._from_address,
            recipient.dest.addrstr,
            StringIO(message),
            d,
            heloFallback=True,
            requireAuthentication=False,
            requireTransportSecurity=True)
        factory.domain = bytes('leap.bitmask.mail-' + __version__)
        reactor.connectSSL(
            self._host, self._port, factory,
            contextFactory=SSLContextFactory(self._key, self._key))
        d.addCallback(lambda result: result[1][0][0])
        d.addErrback(self._send_errback)
        return d 
Example #6
Source File: openpgp.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def _repair_key_docs(self, doclist):
        """
        If there is more than one key for a key id try to self-repair it

        :return: a Deferred that will be fired with the valid key doc once all
                 the deletions are completed
        :rtype: Deferred
        """

        def log_key_doc(doc):
            self.log.error("\t%s: %s" % (doc.content[KEY_UIDS_KEY],
                                         doc.content[KEY_FINGERPRINT_KEY]))

        def cmp_key(d1, d2):
            return cmp(d1.content[KEY_REFRESHED_AT_KEY],
                       d2.content[KEY_REFRESHED_AT_KEY])

        return self._repair_docs(doclist, cmp_key, log_key_doc) 
Example #7
Source File: test_gateway.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def getReply(self, line, proto, transport):
        proto.lineReceived(line)

        if line[:4] not in ['HELO', 'MAIL', 'RCPT', 'DATA']:
            return succeed("")

        def check_transport(_):
            reply = transport.value()
            if reply:
                transport.clear()
                return succeed(reply)

            d = Deferred()
            d.addCallback(check_transport)
            reactor.callLater(0, lambda: d.callback(None))
            return d

        return check_transport(None) 
Example #8
Source File: imapclient.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def main():
    hostname = raw_input('IMAP4 Server Hostname: ')
    port = raw_input('IMAP4 Server Port (the default is 143): ')
    username = raw_input('IMAP4 Username: ')
    password = util.getPassword('IMAP4 Password: ')

    onConn = defer.Deferred(
    ).addCallback(cbServerGreeting, username, password
                  ).addErrback(ebConnection
                               ).addBoth(cbClose)

    factory = SimpleIMAP4ClientFactory(username, onConn)

    from twisted.internet import reactor
    conn = reactor.connectTCP(hostname, int(port), factory)
    reactor.run() 
Example #9
Source File: account.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def isSubscribed(self, name):
        """
        Returns True if user is subscribed to this mailbox.

        :param name: the mailbox to be checked.
        :type name: str

        :rtype: Deferred (will fire with bool)
        """
        name = normalize_mailbox(name)

        def get_subscribed(mbox):
            return mbox.collection.get_mbox_attr("subscribed")

        d = self.getMailbox(name)
        d.addCallback(get_subscribed)
        return d 
Example #10
Source File: account.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def subscribe(self, name):
        """
        Subscribe to this mailbox if not already subscribed.

        :param name: name of the mailbox
        :type name: str
        :rtype: Deferred
        """
        name = normalize_mailbox(name)

        def set_subscribed(mbox):
            return mbox.collection.set_mbox_attr("subscribed", True)

        d = self.getMailbox(name)
        d.addCallback(set_subscribed)
        return d 
Example #11
Source File: mail.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def get_message_by_uid(self, uid, absolute=True, get_cdocs=False):
        """
        Retrieve a message by its Unique Identifier.

        If this is a Mailbox collection, that is the message UID, unique for a
        given mailbox, or a relative sequence number depending on the absolute
        flag. For now, only absolute identifiers are supported.
        :rtype: Deferred
        """
        # TODO deprecate absolute flag, it doesn't make sense UID and
        # !absolute. use _by_sequence_number instead.
        if not absolute:
            raise NotImplementedError("Does not support relative ids yet")

        get_doc_fun = self.mbox_indexer.get_doc_id_from_uid

        def get_msg_from_mdoc_id(doc_id):
            if doc_id is None:
                return None
            return self.adaptor.get_msg_from_mdoc_id(
                self.messageklass, self.store,
                doc_id, uid=uid, get_cdocs=get_cdocs)

        d = get_doc_fun(self.mbox_uuid, uid)
        d.addCallback(get_msg_from_mdoc_id)
        return d 
Example #12
Source File: test_tx.py    From prometheus-async with Apache License 2.0 5 votes vote down vote up
def test_deferred(self, fo, patch_timer):
        """
        time works with Deferreds.
        """
        d = tx.time(fo, Deferred())

        assert [] == fo._observed

        d.callback(42)

        assert 42 == (yield d)
        assert [1] == fo._observed 
Example #13
Source File: _decorators.py    From prometheus-async with Apache License 2.0 5 votes vote down vote up
def time(metric, deferred=None):
    r"""
    Call ``metric.observe(time)`` with runtime in seconds.

    Can be used as a decorator as well as on ``Deferred``\ s.

    Works with both sync and async results.

    :returns: function or ``Deferred``.
    """
    if deferred is None:

        @decorator
        def time_decorator(f, _, args, kw):
            def observe(value):
                metric.observe(get_time() - start_time)
                return value

            start_time = get_time()
            rv = f(*args, **kw)
            if isinstance(rv, Deferred):
                return rv.addBoth(observe)
            else:
                return observe(rv)

        return time_decorator
    else:

        def observe(value):
            metric.observe(get_time() - start_time)
            return value

        start_time = get_time()
        return deferred.addBoth(observe) 
Example #14
Source File: TTwisted.py    From galaxy-sdk-python with Apache License 2.0 5 votes vote down vote up
def _receiveSASLMessage(self):
        self._sasl_negotiation_deferred = defer.Deferred()
        self._sasl_negotiation_status = None
        return self._sasl_negotiation_deferred 
Example #15
Source File: TTwisted.py    From galaxy-sdk-python with Apache License 2.0 5 votes vote down vote up
def __init__(self, client_class, iprot_factory, oprot_factory=None):
        self._client_class = client_class
        self._iprot_factory = iprot_factory
        if oprot_factory is None:
            self._oprot_factory = iprot_factory
        else:
            self._oprot_factory = oprot_factory

        self.recv_map = {}
        self.started = defer.Deferred() 
Example #16
Source File: dispatcher.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def do_POLL(self, _, *parts, **kw):
        if not self.queue.empty():
            return self.queue.get()

        d = defer.Deferred()
        self.waiting.append(d)
        return d 
Example #17
Source File: _decorators.py    From prometheus-async with Apache License 2.0 5 votes vote down vote up
def count_exceptions(metric, deferred=None, exc=BaseException):
    """
    Call ``metric.inc()`` whenever *exc* is caught.

    Can be used as a decorator or on a ``Deferred``.

    :returns: function (if decorator) or ``Deferred``.
    """

    def inc(fail):
        fail.trap(exc)
        metric.inc()
        return fail

    if deferred is None:

        @decorator
        def count_exceptions_decorator(f, _, args, kw):
            try:
                rv = f(*args, **kw)
            except exc:
                metric.inc()
                raise

            if isinstance(rv, Deferred):
                return rv.addErrback(inc)
            else:
                return rv

        return count_exceptions_decorator
    else:
        return deferred.addErrback(inc) 
Example #18
Source File: soledad.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def update(self, store):
        """
        Update the documents for this wrapper.

        :return: a deferred that will fire when the underlying
                 Soledad document has been updated.
        :rtype: Deferred
        """
        # the deferred lock guards against revision conflicts
        return self._lock.run(self._update, store) 
Example #19
Source File: mail.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def count_unseen(self):
        """
        Count the unseen messages in this collection.
        :return: a Deferred that will fire with the integer for the count.
        :rtype: Deferred
        """
        if not self.is_mailbox_collection():
            raise NotImplementedError()
        return self.adaptor.get_count_unseen(self.store, self.mbox_uuid) 
Example #20
Source File: mail.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def count_recent(self):
        """
        Count the recent messages in this collection.
        :return: a Deferred that will fire with the integer for the count.
        :rtype: Deferred
        """
        if not self.is_mailbox_collection():
            raise NotImplementedError()
        return self.adaptor.get_count_recent(self.store, self.mbox_uuid) 
Example #21
Source File: mail.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def count(self):
        """
        Count the messages in this collection.
        :return: a Deferred that will fire with the integer for the count.
        :rtype: Deferred
        """
        if not self.is_mailbox_collection():
            raise NotImplementedError()

        d = self.mbox_indexer.count(self.mbox_uuid)
        return d 
Example #22
Source File: mailbox.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def _bound_seq(self, messages_asked, uid):
        """
        Put an upper bound to a messages sequence if this is open.

        :param messages_asked: IDs of the messages.
        :type messages_asked: MessageSet
        :return: a Deferred that will fire with a MessageSet
        """

        def set_last_uid(last_uid):
            messages_asked.last = last_uid
            return messages_asked

        def set_last_seq(all_uid):
            messages_asked.last = len(all_uid)
            return messages_asked

        if not messages_asked.last:
            try:
                iter(messages_asked)
            except TypeError:
                # looks like we cannot iterate
                if uid:
                    d = self.collection.get_last_uid()
                    d.addCallback(set_last_uid)
                else:
                    d = self.collection.all_uid_iter()
                    d.addCallback(set_last_seq)
                return d
        return defer.succeed(messages_asked) 
Example #23
Source File: mail.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def get_message_by_sequence_number(self, msn, get_cdocs=False):
        """
        Retrieve a message by its Message Sequence Number.
        :rtype: Deferred
        """
        def get_uid_for_msn(all_uid):
            return all_uid[msn - 1]
        d = self.all_uid_iter()
        d.addCallback(get_uid_for_msn)
        d.addCallback(
            lambda uid: self.get_message_by_uid(
                uid, get_cdocs=get_cdocs))
        d.addErrback(self.log.error('Error getting msg by seq'))
        return d 
Example #24
Source File: mailbox.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def getUIDNext(self):
        """
        Return the likely UID for the next message added to this
        mailbox. Currently it returns the higher UID incremented by
        one.

        :return: deferred with int
        :rtype: Deferred
        """
        d = self.collection.get_uid_next()
        return d 
Example #25
Source File: mailbox.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def getMessageCount(self):
        """
        Returns the total count of messages in this mailbox.

        :return: deferred with int
        :rtype: Deferred
        """
        return self.collection.count() 
Example #26
Source File: sender.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def send(self, recipient, message):
        """
        Send a messages to recipient

        :type recipient: string
        :type message: string

        :return: A Deferred that will be called with the recipient address
        :raises SendError: in case of failure in send
        """ 
Example #27
Source File: mailbox.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def copy(self, message):
        """
        Copy the given message object into this mailbox.

        :param message: an IMessage implementor
        :type message: LeapMessage
        :return: a deferred that will be fired with the message
                 uid when the copy succeed.
        :rtype: Deferred
        """
        d = self.collection.copy_msg(
            message.message, self.collection.mbox_uuid)
        return d

    # convenience fun 
Example #28
Source File: mailbox.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def store(self, messages_asked, flags, mode, uid):
        """
        Sets the flags of one or more messages.

        :param messages: The identifiers of the messages to set the flags
        :type messages: A MessageSet object with the list of messages requested

        :param flags: The flags to set, unset, or add.
        :type flags: sequence of str

        :param mode: If mode is -1, these flags should be removed from the
                     specified messages.  If mode is 1, these flags should be
                     added to the specified messages.  If mode is 0, all
                     existing flags should be cleared and these flags should be
                     added.
        :type mode: -1, 0, or 1

        :param uid: If true, the IDs specified in the query are UIDs;
                    otherwise they are message sequence IDs.
        :type uid: bool

        :return: A deferred, that will be called with a dict mapping message
                 sequence numbers to sequences of str representing the flags
                 set on the message after this operation has been performed.
        :rtype: deferred

        :raise ReadOnlyMailbox: Raised if this mailbox is not open for
                                read-write.
        """
        if not self.isWriteable():
            self.log.info('Read only mailbox!')
            raise imap4.ReadOnlyMailbox

        d = defer.Deferred()
        reactor.callLater(0, self._do_store, messages_asked, flags,
                          mode, uid, d)

        d.addCallback(self.collection.cb_signal_unread_to_ui)
        d.addErrback(lambda f: self.log.error('Error on store'))
        return d 
Example #29
Source File: mailbox.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def fetch_flags(self, messages_asked, uid):
        """
        A fast method to fetch all flags, tricking just the
        needed subset of the MIME interface that's needed to satisfy
        a generic FLAGS query.

        Given how LEAP Mail is supposed to work without local cache,
        this query is going to be quite common, and also we expect
        it to be in the form 1:* at the beginning of a session, so
        it's not bad to fetch all the FLAGS docs at once.

        :param messages_asked: IDs of the messages to retrieve information
                               about
        :type messages_asked: MessageSet

        :param uid: If 1, the IDs are UIDs. They are message sequence IDs
                    otherwise.
        :type uid: int

        :return: A tuple of two-tuples of message sequence numbers and
                flagsPart, which is a only a partial implementation of
                MessagePart.
        :rtype: tuple
        """
        # is_sequence = True if uid == 0 else False
        # XXX FIXME -----------------------------------------------------
        # imap/tests, or muas like mutt, it will choke until we implement
        # sequence numbers. This is an easy hack meanwhile.
        is_sequence = False
        # ---------------------------------------------------------------

        if is_sequence:
            raise NotImplementedError(
                "FETCH FLAGS NOT IMPLEMENTED FOR MESSAGE SEQUENCE NUMBERS YET")

        d = defer.Deferred()
        reactor.callLater(0, self._do_fetch_flags, messages_asked, uid, d)
        return d 
Example #30
Source File: mailbox.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def _get_notify_count(self):
        """
        Get message count and recent count for this mailbox.

        :return: a deferred that will fire with a tuple, with number of
                 messages and number of recent messages.
        :rtype: Deferred
        """
        # XXX this is way too expensive in cases like multiple APPENDS.
        # We should have a way of keep a cache or do a self-increment for that
        # kind of calls.
        d_exists = defer.maybeDeferred(self.getMessageCount)
        d_recent = defer.maybeDeferred(self.getRecentCount)
        d_list = [d_exists, d_recent]

        def log_num_msg(result):
            exists, recent = tuple(result)
            self.log.debug(
                'NOTIFY (%r): there are %s messages, %s recent' % (
                    self.mbox_name, exists, recent))
            return result

        d = defer.gatherResults(d_list)
        d.addCallback(log_num_msg)
        return d

    # commands, do not rename methods