class AsyncCall(object):
    """
    A class to represent an outstanding asynchronous thrift call currently
    being processed by the handler.

    The main purpose of this class is so that the handler can track all pending
    calls, and cancel them when AsyncLoadHandler.shutdown() is called.
    """

    def __init__(self, handler):
        self.deferred = Deferred()
        self.handler = handler

    def callback(self, *args, **kwargs):
        self.deferred.callback(*args, **kwargs)
        self._done()

    def errback(self, *args, **kwargs):
        self.deferred.errback(*args, **kwargs)
        self._done()

    def start(self):
        raise NotImplementedError()

    def cancel(self):
        raise NotImplementedError()

    def _done(self):
        self.handler.async_call_done(self)
Пример #2
0
class SpoolingSignalProtocol(Protocol):
    def __init__(self, *args, **kwargs):
        self.spooling = SpoolingProtocol()
        self.signaling = SignalProtocol(*args, **kwargs)
        self.finished = None

    def connectionMade(self):
        self.finished = Deferred()

    def connectionLost(self, reason):
        if reason.type is ProcessTerminated:
            self.finished.errback(reason.value)
        else:
            self.finished.callback(reason.value)

    def dataReceived(self, data):
        self.signaling.dataReceived(data)
        self.spooling.dataReceived(data)

    def extReceived(self, fd, data):
        self.signaling.extReceived(fd, data)
        self.spooling.extReceived(fd, data)

    def flush(self):
        return self.spooling.flush()
Пример #3
0
    def test_rewind_stops_on_error(self):
        """
        rewind errbacks it's completion deferred when it encounters an
        error.
        """
        called = [0]

        def op(op_d):
            called[0] += 1
            return op_d

        self.undo.push(op, None)

        op_d1 = Deferred()
        self.undo.push(op, op_d1)

        d = self.undo.rewind()
        self.assertNoResult(d)

        class DummyOpFailure(Exception):
            pass

        op_d1.errback(DummyOpFailure())
        self.assertEqual(called[0], 1)
        self.failureResultOf(d, DummyOpFailure)
Пример #4
0
    def test_process_notifications(self):
        twisted.internet.base.DelayedCall.debug = True
        self._connect()
        self.proto.uaid = str(uuid.uuid4())

        # Swap out fetch_notifications
        self.proto.ap_settings.storage.fetch_notifications = Mock(
            return_value=[]
        )

        self.proto.process_notifications()

        # Grab a reference to it
        notif_d = self.proto._notification_fetch

        # Run it again to trigger the cancel
        self.proto.process_notifications()

        # Tag on our own to follow up
        d = Deferred()

        # Ensure we catch error outs from either call
        notif_d.addErrback(lambda x: d.errback(x))

        def wait(result):
            eq_(self.proto._notification_fetch, None)
            d.callback(True)
        self.proto._notification_fetch.addCallback(wait)
        self.proto._notification_fetch.addErrback(lambda x: d.errback(x))
        return d
Пример #5
0
        def indx_cb(indx):
            self.logger.debug("Created INDXClient.")
            prep_d = Deferred()

            if self.try_nike_login():
                self.logger.debug("Successfully logged in to Nike.com.")
            else:
                self.logger.error("Failed to log in to Nike.com.")
                prep_d.errback("Failed to log in to Nike.com.")
                
            def objects_cb(harvester, indx=indx):
                self.logger.debug("Found or created all 4 time series.")
                self.logger.debug("Found or created harvester. {0}".format(harvester))

                def wait(x):
                    self.logger.debug("Harvested! Suspending execution for 6 hours at {0}.".format(datetime.now().isoformat()))
                    sleep(21600)
                    prep_d.callback(None)

                if harvester:
                    if "zeros_from" in harvester :
                        self.zeros = datetime.strptime(harvester["zeros_from"][0]["@value"], "%Y-%m-%dT%H:%M:%S")
                    if "retrieved_to" in harvester :
                        self.retrieved = datetime.strptime(harvester["retrieved_to"][0]["@value"], "%Y-%m-%dT%H:%M:%S")
                    self.harvest(indx, harvester).addCallbacks(wait, prep_d.errback)                    

            self.find_create(indx, self.steps_ts_id, {"http://www.w3.org/2000/01/rdf-schema#label":"Nike+ Steps Time Series", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type":"http://purl.org/linked-data/cube#Dataset"}).addCallbacks(
                lambda x: self.find_create(indx, self.calories_ts_id, {"http://www.w3.org/2000/01/rdf-schema#label":"Nike+ Calories Time Series", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type":"http://purl.org/linked-data/cube#Dataset"}), prep_d.errback).addCallbacks(
                lambda x: self.find_create(indx, self.stars_ts_id, {"http://www.w3.org/2000/01/rdf-schema#label":"Nike+ Stars Time Series", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type":"http://purl.org/linked-data/cube#Dataset"}), prep_d.errback).addCallbacks(
                lambda x: self.find_create(indx, self.fuel_ts_id, {"http://www.w3.org/2000/01/rdf-schema#label":"Nike+ Fuel Time Series", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type":"http://purl.org/linked-data/cube#Dataset"}), prep_d.errback).addCallbacks(
                lambda x: self.find_create(indx, self.harvester_id, {"http://www.w3.org/2000/01/rdf-schema#label":"INDX Nike+ Harvester extra info"}), prep_d.errback).addCallbacks(objects_cb, prep_d.errback)   
            return prep_d
Пример #6
0
class Engine:
    def __init__(self, controller):
        self._ctrl = controller
        self._ctrl.add_engine(self)
        self.deferred = Deferred()

    def _signal(self, name, *args, **keywords):
        self._ctrl.signal(name, *args, **keywords)

    def __stop(self):
        self._ctrl.remove_engine(self)

    def _success(self, result=None):
        self.__stop()
        self.deferred.callback(result)

    def _failure(self, fail='Engine failed'):
        self.__stop()
        self.deferred.errback(fail)

    def abort(self):
        """Aborts this engine, does not emit a signal."""
        self.__stop()

    def send(self, something):
        return self._ctrl.send(something)

    def sendall(self, something):
        return self._ctrl.sendall(something)
Пример #7
0
 def join(self, _):
     def send(_, node):
         factory = NodeClientFactory(OverlayService(self), {"command" : \
                 "join","node":self.my_node})
         reactor.connectTCP(node["host"], node["tcp_port"], factory)
         factory.deferred.addCallback(lambda _: node)
         def sendcallback(_):
             return node
         def senderrback(_):
             raise Exception()
         factory.deferred.addCallbacks(sendcallback,senderrback)
         return factory.deferred
     def success(node):
         coordinator = node
     def error(e):
         self.is_coordinator = True
         self.my_node["id"] = 0
         self.nextid = 1
         self.members[self.my_node["host"]] = self.my_node
         send_log("Notice", "I am coordinator")
         return e
     # search for running loadbalancers and join the overlay network
     initialized = False
     d = Deferred()
     for node in self.config["nodes"]:
         d.addErrback(send, node)
     d.addCallbacks(success, error)
     d.errback(0)
     return d
Пример #8
0
class StartStopProcessProtocol(ProcessProtocol):
    """
    An L{IProcessProtocol} with a Deferred for events where the subprocess
    starts and stops.
    """

    def __init__(self):
        self.started = Deferred()
        self.stopped = Deferred()
        self.output = ''
        self.errors = ''

    def connectionMade(self):
        self.started.callback(self.transport)

    def outReceived(self, data):
        self.output += data

    def errReceived(self, data):
        self.errors += data

    def processEnded(self, reason):
        if reason.check(ProcessDone):
            self.stopped.callback(self.output)
        else:
            self.stopped.errback(ExitedWithStderr(
                    self.errors, self.output))
Пример #9
0
class txDBInterface:
    def __init__(self, *query):
        self.dbpool = dbpool
        self.resultdf = Deferred()
        self.query = query

    def runResultQuery(self):
        df = self.dbpool.runQuery(*self.query)
        df.addCallbacks(self.onResult, self.onFail)
        return self.resultdf

    def runActionQuery(self):
        df = self.dbpool.runOperation(*self.query)
        df.addCallbacks(self.onResult, self.onFail)
        log.debug("running query: %s" % self.query)
        return self.resultdf

    def onResult(self, result):
        self.resultdf.callback(result)

    def onFail(self, error):
        if isinstance(error, adbapi.ConnectionLost):
            log.info("We lost connection to db. re-running the query")
            return self.runQuery()
        self.resultdf.errback(error)
Пример #10
0
 def stop(self, name):
     """ Stop the container.
         
         @param name:        Name of the container which should be stopped.
         @type  name:        str
         
         @param command:     Deferred whose callback is triggered on success
                             or whose errback is triggered on failure with
                             an error message.
         @type  command:     twisted::Deferred
     """
     log.msg("Stop container '{0}'".format(name))
     deferred = Deferred()
     
     try:
         dfrd = getProcessValue('/usr/bin/lxc-stop', ('-n', name),
                                env=os.environ, reactor=self._reactor)
         
         def cb(retVal):
             if retVal == 0:
                 deferred.callback('Container successfully stopped.')
             else:
                 e = ContainerError('Container could not be stopped: '
                                    'Received exit code {0} from '
                                    'lxc-stop.'.format(retVal))
                 deferred.errback(Failure(e))
         
         dfrd.addCallback(cb)
     except OSError:
         e = ContainerError('Insufficient system resources to stop a '
                            'process.')
         deferred.errback(Failure(e))
     
     return deferred
Пример #11
0
class TorIRC(IRCClient):
    nickname = 'txsocksx-tor-irc'
    nickservPassword = ''

    def connectionMade(self):
        self.sendLine('CAP REQ :sasl')
        self.deferred = Deferred()
        IRCClient.connectionMade(self)

    def irc_CAP(self, prefix, params):
        if params[1] != 'ACK' or params[2].split() != ['sasl']:
            print 'sasl not available'
            self.quit('')
        sasl = ('{0}\0{0}\0{1}'.format(self.nickname, self.nickservPassword)).encode('base64').strip()
        self.sendLine('AUTHENTICATE PLAIN')
        self.sendLine('AUTHENTICATE ' + sasl)

    def irc_903(self, prefix, params):
        self.sendLine('CAP END')

    def irc_904(self, prefix, params):
        print 'sasl auth failed', params
        self.quit('')
    irc_905 = irc_904

    def connectionLost(self, reason):
        self.deferred.errback(reason)

    def signedOn(self):
        print 'signed on successfully'
        self.quit('')
Пример #12
0
class _Query(object):
    def __init__(self, sql, raiseOnZeroRowCount, args):
        self.sql                 = sql
        self.args                = args
        self.results             = []
        self.deferred            = Deferred()
        self.raiseOnZeroRowCount = raiseOnZeroRowCount


    def row(self, row):
        """
        A row was received.
        """
        self.results.append(row)


    def done(self, norows, derived, noneResult):
        """
        The query is complete.

        @param norows: A boolean.  True if there were not any rows.

        @param derived: either C{None} or a C{list} of L{IDerivedParameter}
            providers initially passed into the C{execSQL} that started this
            query.  The values of these object swill mutate the original input
            parameters to resemble them.  Although L{IDerivedParameter.preQuery}
            and L{IDerivedParameter.postQuery} are invoked on the other end of
            the wire, the local objects will be made to appear as though they
            were called here.

        @param noneResult: should the result of the query be C{None} (i.e. did
            it not have a C{description} on the cursor).
        """
        if noneResult and not self.results:
            results = None
        else:
            results = self.results
        if derived is not None:
            # 1) Bleecchh.
            # 2) FIXME: add some direct tests in test_adbapi2, the unit test for
            # this crosses some abstraction boundaries so it's a little
            # integration-y and in the tests for twext.enterprise.dal
            for remote, local in zip(derived, self._deriveDerived()):
                local.__dict__ = remote.__dict__

        if norows and (self.raiseOnZeroRowCount is not None):
            exc = self.raiseOnZeroRowCount()
            self.deferred.errback(Failure(exc))
        else:
            self.deferred.callback(results)


    def _deriveDerived(self):
        derived = None
        for param in self.args:
            if IDerivedParameter.providedBy(param):
                if derived is None:
                    derived = []
                derived.append(param)
        return derived
Пример #13
0
    def auth_plain(self, username, password):
        """ Plain authentication. """
        return_d = Deferred()
        try:
            self.is_authed = False

            url = "{0}auth/login".format(self.address)
            values = {"username": username, "password": password}

            self._debug("Calling auth_plain")     

            # TODO change client.post etc to be async using twisted web clients
            def responded_cb(status):
                if status['code'] != 200:
                    errmsg = "Authentication failed"
                    self._error(errmsg)
                    raise Exception(errmsg)

                self._debug("Authentication successful")
                self.is_authed = True
                return_d.callback(status)
            
            self.client.post(url, values).addCallbacks(responded_cb, return_d.errback)

        except Exception as e:
            return_d.errback(Failure(e))

        return return_d
Пример #14
0
class JsonResponseProtocol(Protocol):

    def __init__(self, d):
        self._upstream = d
        self._finished = Deferred()
        self.buf = []

    def getData(self):
        dl = DeferredList([self._finished, self._upstream],
                          fireOnOneErrback=True)

        @dl.addCallback
        def cb(l):
            return l[0][1]

        @dl.addErrback
        def eb(fail):
            return fail.value.subFailure

        return dl

    def dataReceived(self, data):
        self.buf.append(data)

    def connectionLost(self, reason):
        try:
            data = json.loads("".join(self.buf))
        except Exception, e:
            self._finished.errback(e)
        else:
Пример #15
0
def test():
    d = Deferred()
    d.addCallbacks(gotPoets, gotFailed)
    # traceback.print_stack()
    # d.callback('tips poetm short')
    d.errback(Exception('tips poetm shorts'))
    print "....end....."
Пример #16
0
def execute(cmd, env=None, path=None, reactor=None):
    """ Execute a command using twisted's Process Protocol and returns a
        Deferred firing when the command as terminated.

        @param cmd:         Command which should be executed. It has to be a
                            tuple containing the executable as first argument
                            and all additional arguments which will be passed
                            to the executable.
        @type  cmd:         tuple

        @param env:         Can be used to use custom environment variables to
                            execute the command. If argument is omitted the
                            environment of os.environ is used.
        @type  env:         dict

        @param path:        Path which will be used as working directory to
                            execute the command. If argument is omitted the
                            current directory is used.
        @type  path:        str

        @param reactor:     Reference to twisted's reactor. If argument is
                            omitted the standard twisted reactor is imported and
                            used.
        @type  reactor:     twisted::reactor
    """
    deferred = Deferred()
    protocol = _ProcessProtocol(' '.join(cmd), deferred)

    try:
        reactor.spawnProcess(protocol, cmd[0], cmd, env, path)
    except OSError:
        e = ExecutionError('Command could not be executed.')
        deferred.errback(Failure(e))

    return deferred
Пример #17
0
    def create_root_box(self, box_name, username, password):
        """ Create a new root box for the user name specified. """
        logging.debug("indx_pg2 create_root_box {0} for user {1}".format(box_name, username))
        result_d = Deferred()

        try:
            if username is None or username == "":
                raise Exception("Username cannot be blank, value was {0}".format(username))
            if box_name is None or box_name == "":
                raise Exception("Box Name cannot be blank, value was {0}".format(box_name))

            def created_cb(empty):
                logging.debug("indx_pg2 create_root_box created_cb")

                def connected_cb(conn):
                    logging.debug("indx_pg2 create_root_box connected_cb")

                    def do_acl(empty):
                        logging.debug("indx_pg2 create_root_box do_acl")
                        user = IndxUser(self, username)

                        user.set_acl(box_name, "@indx", {"read": True, "write": False, "control": False, "owner": False}).addCallbacks(result_d.callback, result_d.errback)

                    conn.runOperation("UPDATE tbl_users SET root_box = %s WHERE username = %s", [box_name, username]).addCallbacks(do_acl, result_d.errback)

                self.connect_indx_db().addCallbacks(connected_cb, result_d.errback)

            self.create_box(box_name, username, password).addCallbacks(created_cb, result_d.errback)

        except Exception as e:
            failure = Failure(e)
            logging.error("indx_pg2 create_root_box error, calling errback. Error is: {0}".format(e))
            result_d.errback(failure)

        return result_d
Пример #18
0
 def test_addActionFinishFailure(self):
     """
     When the L{Deferred} referred to in L{DeferredContext.addActionFinish}
     fires with an exception, a finish message is logged.
     """
     d = Deferred()
     logger = MemoryLogger()
     action = Action(logger, "uuid", TaskLevel(level=[1]), "sys:me")
     with action.context():
         DeferredContext(d).addActionFinish()
     exception = RuntimeError("because")
     d.errback(exception)
     assertContainsFields(
         self,
         logger.messages[0],
         {
             "task_uuid": "uuid",
             "task_level": [1, 1],
             "action_type": "sys:me",
             "action_status": "failed",
             "reason": "because",
             "exception": "%s.RuntimeError" % (RuntimeError.__module__,),
         },
     )
     d.addErrback(lambda _: None)  # don't let Failure go to Twisted logs
Пример #19
0
def r_getPage(url, *args, **kwargs):
    if FuturesSession != None:
        session = FuturesSession()
        method = kwargs.pop("method", "GET")
        user_agent = kwargs.pop("agent", None)
        if user_agent:
            headers = kwargs.pop("headers", {})
            headers["User-Agent"] = user_agent
            kwargs["headers"] = headers

        def cb_err_ret(err):
            return err

        d = Deferred()
        d.addErrback(cb_err_ret)
        try:

            def bg_cb(sess, resp):
                c = resp.content
                d.callback(c)

            if method == "GET":
                session.get(url, background_callback=bg_cb, *args, **kwargs)
            elif method == "POST":
                kwargs["data"] = kwargs.pop("postdata")
                session.post(url, background_callback=bg_cb, *args, **kwargs)
            return d
        except Exception, err:
            printl("Error: " + str(err), None, "E")
            d.errback(failure.Failure())
class BodyCollector(Protocol):
    """
    If you want to accumulate the body of an HTTP response until it is
    all finished and then get the result in a string, then create an
    instance of BodyCollector, call its .start() method to get a
    deferred, and pass the instance of BodyCollector as the argument
    to Response.deliverBody(). When the deferred that it gave you
    calls your callback, it will pass the string containing the HTTP
    response body.
    """
    def __init__(self):
        self.finished = Deferred()
        self.bytesl = []

    def start(self):
        return self.finished

    def dataReceived(self, bytes):
        self.bytesl.append(bytes)

    def connectionLost(self, reason):
        self.reason = reason
        self.bytes = ''.join(self.bytesl)
        if isinstance(reason, ResponseDone):
            self.finished.callback(self)
        else:
            self.finished.errback(reason)
Пример #21
0
    def test_wb_connect_after_timeout(self):
        """
        Test an odd error scenario. If the zookeeper client succeeds in
        connecting after a timeout, the connection should be closed, as
        the connect deferred has already fired.
        """
        mock_client = self.mocker.patch(self.client)
        mock_client.close()

        def close_state():
            # Ensure the client state variable is correct after the close call.
            self.client.connected = False

        self.mocker.call(close_state)
        self.mocker.replay()

        task = DelayedCall(1, lambda: 1, None, None, None, None)
        task.called = True

        d = Deferred()
        d.errback(ConnectionTimeoutException())

        self.client._cb_connected(
            task, d, None, zookeeper.CONNECTED_STATE, "/")

        self.failUnlessFailure(d, ConnectionTimeoutException)
        return d
Пример #22
0
 def decorator(self, *args, **kwargs):
     if not self.initialized:
         reactor.callLater(5, INVERSE_SHARED[id(f)], self, *args, **kwargs)
         return
     if self.server_mode:
         if "_remote_call" in kwargs:
             del kwargs["_remote_call"]
             d = maybeDeferred(f, self, *args, **kwargs)
             d.addErrback(lambda x:{"_pb_failure":dumps(x)})
             return d
         return maybeDeferred(f, self, *args, **kwargs)
     try:
         remote_obj = choice(self.remote_objs.values())
     except IndexError:
         d = Deferred()
         d.errback(ComponentException("No active %s "
             "connections." % self.__class__.__name__))
         return d
     kwargs["_remote_call"] = True
     try:
         d = remote_obj.callRemote(
             self.server_methods[id(f)],
             *args,
             **kwargs)
     except pb.DeadReferenceError:
         self.server.disconnect_by_remote_obj(remote_obj)
         return INVERSE_SHARED[id(f)](self, *args, **kwargs)
     d.addCallback(check_response, self, time.time())
     d.addErrback(
         self.trap_shared_disconnect,
         remote_obj,
         INVERSE_SHARED[id(f)],
         *args,
         **kwargs)
     return d
Пример #23
0
class HTTP11ClientProtocol(_newclient.HTTP11ClientProtocol):
    def request(self, request):
        if self._state != 'QUIESCENT':
            return fail(RequestNotSent())

        self._state = 'TRANSMITTING'
        _requestDeferred = maybeDeferred(request.writeTo, self.transport)
        self._finishedRequest = Deferred()

        self._currentRequest = request

        self._transportProxy = TransportProxyProducer(self.transport)
        self._parser = HTTPClientParser(request, self._finishResponse)
        self._parser.makeConnection(self._transportProxy)
        self._responseDeferred = self._parser._responseDeferred

        def cbRequestWrotten(ignored):
            if self._state == 'TRANSMITTING':
                self._state = 'WAITING'
                self._responseDeferred.chainDeferred(self._finishedRequest)

        def ebRequestWriting(err):
            if self._state == 'TRANSMITTING':
                self._state = 'GENERATION_FAILED'
                self.transport.loseConnection()
                self._finishedRequest.errback(
                    Failure(RequestGenerationFailed([err])))
            else:
                log.err(err, 'Error writing request, but not in valid state '
                             'to finalize request: %s' % self._state)

        _requestDeferred.addCallbacks(cbRequestWrotten, ebRequestWriting)

        return self._finishedRequest
Пример #24
0
    def get_token(self, boxid):
        """ Get a token for this box. """
        return_d = Deferred()

        try:
            if not self.is_authed:
                return_d.errback(Failure(Exception("Must authenticate before getting token.")))
                return return_d

            url = "{0}auth/get_token".format(self.address)
            values = {"box": boxid, "app": self.appid}

            self._debug("Getting token")

            def responded_cb(status):
                if status['code'] != 200:
                    errmsg = "Getting a token failed"
                    self._error(errmsg)
                    raise Exception(errmsg)

                self._debug("Getting a token was successful: {0}".format(status['token']))
                return_d.callback(status['token'])

            self.client.post(url, values).addCallbacks(responded_cb, return_d.errback)

        except Exception as e:
            return_d.errback(Failure(e))

        return return_d
Пример #25
0
class FakeConnection(object):
    """
    Fake server connection.
    """

    def __init__(self, server):
        self.server = server
        self.client_protocol = None
        self.server_protocol = None

        self._accept_d = Deferred()
        self._connected_d = Deferred()
        self._finished_d = Deferred()

    @property
    def connected(self):
        return self._connected_d.called and not self._finished_d.called

    @property
    def pending(self):
        return not self._accept_d.called

    def await_connected(self):
        """
        Wait for a client to finish connecting.
        """
        return self._connected_d

    def accept_connection(self):
        """
        Accept a pending connection.
        """
        assert self.pending, "Connection is not pending."
        self.server_protocol = self.server.server_factory.buildProtocol(None)
        self._accept_d.callback(
            FakeServerProtocolWrapper(self, self.server_protocol))
        return self.await_connected()

    def reject_connection(self, reason=None):
        """
        Reject a pending connection.
        """
        assert self.pending, "Connection is not pending."
        if reason is None:
            reason = ConnectionRefusedError()
        self._accept_d.errback(reason)

    def await_finished(self):
        """
        Wait for the both sides of the connection to close.
        """
        return self._finished_d

    # Internal stuff.

    def _finish_connecting(self, client_protocol, finished_d):
        self.client_protocol = client_protocol
        finished_d.chainDeferred(self._finished_d)
        self._connected_d.callback(None)
Пример #26
0
def localLoad(url, delaySecs=0):
    """deferred to file.read, with optional delay to simulate a load time"""
    try:
        contents = open(url[len("file://"):]).read()
    except IOError, e:
        d = Deferred()
        d.errback(e)
        return d
Пример #27
0
class HTTP11ClientProtocolPatched(HTTP11ClientProtocol):

    def request(self, request):
        if self._state != 'QUIESCENT':
            return fail(RequestNotSent())

        self._state = 'TRANSMITTING'
        _requestDeferred = maybeDeferred(request.writeTo, self.transport)

        def cancelRequest(ign):
            if self.timedOut:
                # Request timeout was hit
                self._giveUp(Failure(TimeoutError("Request took longer than %s seconds" % request.timeout)))
            else:
                # Explicitly cancel the request's deferred if it's still trying to
                # write when this request is cancelled.
                if self._state in ('TRANSMITTING', 'TRANSMITTING_AFTER_RECEIVING_RESPONSE'):
                    _requestDeferred.cancel()
                else:
                    self._giveUp(Failure(CancelledError()))
        self._finishedRequest = Deferred(cancelRequest)

        if self.timeout > 0:
            self.timedOut = False
            def timeout():
                self.timedOut = True
                self._finishedRequest.cancel()
            td = reactor.callLater(self.timeout, timeout)
            #td.cancel()
            #self._finishedRequest.addBoth(td.cancel) # where/how to cancel?

        # Keep track of the Request object in case we need to call stopWriting
        # on it.
        self._currentRequest = request

        self._transportProxy = TransportProxyProducer(self.transport)
        self._parser = HTTPClientParser(request, self._finishResponse)
        self._parser.makeConnection(self._transportProxy)
        self._responseDeferred = self._parser._responseDeferred

        def cbRequestWrotten(ignored):
            if self._state == 'TRANSMITTING':
                self._state = 'WAITING'
                self._responseDeferred.chainDeferred(self._finishedRequest)

        def ebRequestWriting(err):
            if self._state == 'TRANSMITTING':
                self._state = 'GENERATION_FAILED'
                self.transport.loseConnection()
                self._finishedRequest.errback(
                    Failure(RequestGenerationFailed([err])))
            else:
                log.err(err, 'Error writing request, but not in valid state '
                             'to finalize request: %s' % self._state)

        _requestDeferred.addCallbacks(cbRequestWrotten, ebRequestWriting)

        return self._finishedRequest
Пример #28
0
class BOBI2PClientFactory(ClientFactory):
    protocol = I2PClientTunnelCreatorBOBClient
    bobProto = None
    canceled = False
    removeTunnelWhenFinished = True

    def _cancel(self, d):
        self.bobProto.sender.transport.abortConnection()
        self.canceled = True

    def __init__(self, reactor, clientFactory, bobEndpoint, dest,
                 tunnelNick=None,
                 inhost='localhost',
                 inport=None,
                 options=None):
        self._reactor = reactor
        self._clientFactory = clientFactory
        self._bobEndpoint = bobEndpoint
        self.dest = dest
        self.tunnelNick = tunnelNick
        self.inhost = inhost
        self.inport = inport
        self.options = options
        self.deferred = Deferred(self._cancel);

    def buildProtocol(self, addr):
        proto = self.protocol()
        proto.factory = self
        self.bobProto = proto
        return proto

    def bobConnectionFailed(self, reason):
        if not self.canceled:
            self.deferred.errback(reason)

    # This method is not called if an endpoint deferred errbacks
    def clientConnectionFailed(self, connector, reason):
        self.bobConnectionFailed(reason)

    def i2pTunnelCreated(self):
        # BOB is now listening for a tunnel.
        # BOB only listens on TCP4 (for now).
        clientEndpoint = TCP4ClientEndpoint(self._reactor, self.inhost, self.inport)
        # Wrap the client Factory.
        wrappedFactory = BOBClientFactoryWrapper(self._clientFactory,
                                                 self._bobEndpoint,
                                                 I2PAddress(self.localDest),
                                                 self.tunnelNick,
                                                 self.removeTunnelWhenFinished)
        wrappedFactory.setDest(self.dest)
        d = clientEndpoint.connect(wrappedFactory)
        def checkProto(proto):
            if proto is None:
                self.deferred.cancel()
            return proto
        d.addCallback(checkProto)
        # When the Deferred returns an IProtocol, pass it on.
        d.chainDeferred(self.deferred)
Пример #29
0
class ReceiveFileDescriptor(ConnectableProtocol):
    """
    L{ReceiveFileDescriptor} provides an API for waiting for file descriptors to
    be received.

    @ivar reason: The reason the connection was lost, after C{connectionLost}
        is called.

    @ivar waiting: A L{Deferred} which fires with a file descriptor once one is
        received, or with a failure if the connection is lost with no descriptor
        arriving.
    """

    reason = None
    waiting = None

    def waitForDescriptor(self):
        """
        Return a L{Deferred} which will fire with the next file descriptor
        received, or with a failure if the connection is or has already been
        lost.
        """
        if self.reason is None:
            self.waiting = Deferred()
            return self.waiting
        else:
            return fail(self.reason)

    def fileDescriptorReceived(self, descriptor):
        """
        Fire the waiting Deferred, initialized by C{waitForDescriptor}, with the
        file descriptor just received.
        """
        self.waiting.callback(descriptor)
        self.waiting = None

    def dataReceived(self, data):
        """
        Fail the waiting Deferred, if it has not already been fired by
        C{fileDescriptorReceived}.  The bytes sent along with a file descriptor
        are guaranteed to be delivered to the protocol's C{dataReceived} method
        only after the file descriptor has been delivered to the protocol's
        C{fileDescriptorReceived}.
        """
        if self.waiting is not None:
            self.waiting.errback(Failure(Exception("Received bytes (%r) before descriptor." % (data,))))
            self.waiting = None

    def connectionLost(self, reason):
        """
        Fail the waiting Deferred, initialized by C{waitForDescriptor}, if there
        is one.
        """
        ConnectableProtocol.connectionLost(self, reason)
        if self.waiting is not None:
            self.waiting.errback(reason)
            self.waiting = None
        self.reason = reason
Пример #30
0
 def test_pooled_deferred_errbbacks_not_obscured(self):
     """
     The errbacks of pooled deferreds are not obscured by removing them
     from the pool.
     """
     holdup = Deferred()
     self.pool.add(holdup)
     holdup.errback(DummyException('hey'))
     self.failureResultOf(holdup, DummyException)
Пример #31
0
class VNCDoToolFactory(rfb.RFBFactory):
    password = None

    protocol = VNCDoToolClient
    shared = True

    pseudocursor = False
    nocursor = False
    pseudodesktop = True
    force_caps = False

    def __init__(self):
        self.deferred = Deferred()

    def clientConnectionLost(self, connector, reason):
        pass

    def clientConnectionFailed(self, connector, reason):
        self.deferred.errback(reason)

    def clientConnectionMade(self, protocol):
        self.deferred.callback(protocol)
Пример #32
0
class Command:
    """
    Wrap a client action into an object, that holds the values used in the
    protocol.

    @ivar _deferred: the L{Deferred} object that will be fired when the result
        arrives.
    @type _deferred: L{Deferred}

    @ivar command: name of the command sent to the server.
    @type command: L{bytes}
    """

    def __init__(self, command, **kwargs):
        """
        Create a command.

        @param command: the name of the command.
        @type command: L{bytes}

        @param kwargs: this values will be stored as attributes of the object
            for future use
        """
        self.command = command
        self._deferred = Deferred()
        for k, v in kwargs.items():
            setattr(self, k, v)

    def success(self, value):
        """
        Shortcut method to fire the underlying deferred.
        """
        self._deferred.callback(value)

    def fail(self, error):
        """
        Make the underlying deferred fails.
        """
        self._deferred.errback(error)
Пример #33
0
    def test_fire_when_all_fired(self, logger):
        """
        The ``Deferred`` returned by ``gather_deferreds`` does not fire until
        all the supplied ``deferreds`` have either erred back or called back.
        """
        d1 = Deferred()
        d2 = Deferred()
        d3 = Deferred()
        gathering = gather_deferreds([d1, d2, d3])

        # The second deferred fires first, with an error
        d2.errback(ZeroDivisionError('test_consume_errors1'))

        # But the gathered list does not fire...
        self.assertNoResult(gathering)

        # The remaining deferreds then callback...
        d1.callback(None)
        d3.callback(None)

        # ...and the gathered list has now fired.
        self.failureResultOf(gathering)
Пример #34
0
def makeDeferredWithProcessProtocol():
    """Returns a (`Deferred`, `ProcessProtocol`) tuple.

    The Deferred's `callback()` will be called (with None) if the
    `ProcessProtocol` is called back indicating that no error occurred.
    Its `errback()` will be called with the `Failure` reason otherwise.
    """
    done = Deferred()
    protocol = ProcessProtocol()
    # Call the errback if the "failure" object indicates a non-zero exit.
    protocol.processEnded = lambda reason: (done.errback(reason) if (
        reason and not reason.check(ProcessDone)) else done.callback(None))
    return done, protocol
Пример #35
0
class Engine:
    def __init__(self, client):
        self._client = client
        self._client.add_engine(self)
        self.deferred = Deferred()

    def _signal(self, name, *args, **keywords):
        self._client.signal(name, *args, **keywords)

    def __stop(self):
        self._client.remove_engine(self)

    def _success(self, result=None):
        self.__stop()
        self.deferred.callback(result)

    def _failure(self, fail='Engine failed'):
        self.__stop()
        self.deferred.errback(fail)

    def abort(self):
        """Aborts this engine, does not emit a signal."""
        self.__stop()
Пример #36
0
    def test_asyncDeferredFailureTraceback(self):
        """
        When a Deferred is awaited upon that later fails with a Failure that
        has a traceback, both the place that the synchronous traceback comes
        from and the awaiting line are shown in the traceback.
        """
        def returnsFailure():
            try:
                raise SampleException()
            except SampleException:
                return Failure()

        it = Deferred()

        async def doomed():
            return await it

        started = Deferred.fromCoroutine(doomed())
        self.assertNoResult(started)
        it.errback(returnsFailure())
        failure = self.failureResultOf(started)
        self.assertIn(", in doomed\n", failure.getTraceback())
        self.assertIn(", in returnsFailure\n", failure.getTraceback())
Пример #37
0
class RegistrationResponse(object):
    """A helper for dealing with the response of a single registration request.

    @ivar deferred: The L{Deferred} that will be fired as per
        L{RegistrationHandler.register}.
    """
    def __init__(self, reactor):
        self._reactor = reactor
        self._done_id = reactor.call_on("registration-done", self._done)
        self._failed_id = reactor.call_on("registration-failed", self._failed)
        self.deferred = Deferred()

    def _cancel_calls(self):
        self._reactor.cancel_call(self._done_id)
        self._reactor.cancel_call(self._failed_id)

    def _done(self):
        self.deferred.callback(None)
        self._cancel_calls()

    def _failed(self, reason=None):
        self.deferred.errback(RegistrationError(reason))
        self._cancel_calls()
Пример #38
0
 def ftp_CWD(self, path):
     try:
         segments = toSegments(self.workingDirectory, path)
     except InvalidPath:
         # XXX Eh, what to fail with here?
         return defer.fail(FileNotFoundError(path))
     pth = '/'.join(segments)
     d = Deferred()
     d.addCallback(lambda r: self._accessGrantedResponse(r, segments))
     if not pth or pth == '/':
         d.callback(None)
         return d
     ret = api.file_info(pth,
                         include_uploads=False,
                         include_downloads=False)
     if ret['status'] != 'OK':
         d.errback(FileNotFoundError(path))
         return d
     if ret['result'][0]['type'] == 'dir':
         d.callback(None)
     else:
         d.errback(FileNotFoundError(path))
     return d
Пример #39
0
 def test_addActionFinishFailure(self):
     """
     When the L{Deferred} referred to in L{DeferredContext.addActionFinish}
     fires with an exception, a finish message is logged.
     """
     d = Deferred()
     logger = MemoryLogger()
     action = Action(logger, "uuid", TaskLevel(level=[1]), "sys:me")
     with action.context():
         DeferredContext(d).addActionFinish()
     exception = RuntimeError("because")
     d.errback(exception)
     assertContainsFields(
         self, logger.messages[0], {
             "task_uuid": "uuid",
             "task_level": [1, 1],
             "action_type": "sys:me",
             "action_status": "failed",
             "reason": "because",
             "exception": "%s.RuntimeError" % (RuntimeError.__module__, )
         }
     )
     d.addErrback(lambda _: None)  # don't let Failure go to Twisted logs
Пример #40
0
class _DumpOutputProtocol(ProcessProtocol):
    """
    Internal helper.
    """
    def __init__(self, f):
        self.done = Deferred()
        self._out = f if f is not None else sys.stdout

    def processEnded(self, reason):
        if not self.done.called:
            self.done.callback(None)

    def processExited(self, reason):
        if not isinstance(reason.value, ProcessDone):
            self.done.errback(reason)

    def outReceived(self, data):
        data = str(data, sys.stdout.encoding)
        self._out.write(data)

    def errReceived(self, data):
        data = str(data, sys.stdout.encoding)
        self._out.write(data)
Пример #41
0
    def _newConnections(self, conn_str):
        """ Make a pool of new connections. """
        # lock with the semaphore before calling this
        logging.debug("IndxConnectionPool _newConnections")
        return_d = Deferred()

        self.connections[conn_str] = DBConnectionPool(conn_str)

        try:
            d_list = []
            for i in range(MIN_CONNS):
                connection_d = self._newConnection(conn_str)
                d_list.append(connection_d)

            dl = DeferredList(d_list)
            dl.addCallbacks(return_d.callback, return_d.errback)

        except Exception as e:
            logging.error(
                "IndxConnectionPool error in _newConnections: {0}".format(e))
            return_d.errback(Failure(e))

        return return_d
Пример #42
0
def GetLatest(idurl):
    """
    Returns latest copy from cache or fire `immediatelyCaching`,
    result is a `Deferred` object.
    """
    known = FromCache(idurl)
    result = Deferred()
    if known:
        result.callback(known)
    else:
        d = immediatelyCaching(idurl)
        d.addCallback(lambda _: result.callback(FromCache(idurl)))
        d.addErrback(lambda _: result.errback(None))
    return result
Пример #43
0
class ContentRequest(RandomNumberCache):
    """
    This request cache keeps track of all outstanding search requests.
    """
    def __init__(self, request_cache, search_type, query):
        super(ContentRequest, self).__init__(request_cache, u"request")
        self.query = query
        self.search_type = search_type
        self.response = None
        self.deferred = Deferred()

    @property
    def timeout_delay(self):
        return 30.0

    def append_response(self, response):
        self.response.extend(response)

    def finish(self):
        self.deferred.callback(self.response)

    def on_timeout(self):
        self.deferred.errback(self.response)
Пример #44
0
def with_timeout(timeout, d, reactor=reactor):
    """Returns a `Deferred` that is in all respects equivalent to `d`, e.g. when `cancel()` is called on it `Deferred`,
    the wrapped `Deferred` will also be cancelled; however, a `Timeout` will be fired after the `timeout` number of
    seconds if `d` has not fired by that time.

    When a `Timeout` is raised, `d` will be cancelled. It is up to the caller to worry about how `d` handles
    cancellation, i.e. whether it has full/true support for cancelling, or does cancelling it just prevent its callbacks
    from being fired but doesn't cancel the underlying operation.

    """
    if timeout is None or not isinstance(d, Deferred):
        return d

    ret = Deferred(canceller=lambda _: (
        d.cancel(),
        timeout_d.cancel(),
    ))

    timeout_d = sleep(timeout, reactor)
    timeout_d.addCallback(lambda _: (
        d.cancel(),
        ret.errback(Failure(Timeout())) if not ret.called else None,
    ))

    timeout_d.addErrback(lambda f: f.trap(CancelledError))

    d.addCallback(lambda result: (
        timeout_d.cancel(),
        ret.callback(result),
    ))

    d.addErrback(lambda f: (if_(not f.check(CancelledError), lambda: (
        timeout_d.cancel(),
        ret.errback(f),
    )), ))

    return ret
Пример #45
0
def transfer_private_key(key_id, idurl):
    if _Debug:
        lg.out(_DebugLevel,
               'key_ring.transfer_private_key  %s -> %s' % (key_id, idurl))
    result = Deferred()
    recipient_id_obj = identitycache.FromCache(idurl)
    if not recipient_id_obj:
        lg.warn('not found "%s" in identity cache' % idurl)
        result.errback(Exception('not found "%s" in identity cache' % idurl))
        return result
    key_alias, creator_idurl = my_keys.split_key_id(key_id)
    if not key_alias or not creator_idurl:
        lg.warn('wrong key_id')
        result.errback(Exception('wrong key_id'))
        return result
    key_object = my_keys.known_keys().get(key_id)
    if key_object is None:
        lg.warn('unknown key: "%s"' % key_id)
        result.errback(Exception('unknown key: "%s"' % key_id))
        return result
    key_json = {
        'key_id': key_id,
        'alias': key_alias,
        'creator': creator_idurl,
        'fingerprint': str(key_object.fingerprint()),
        'type': str(key_object.type()),
        'ssh_type': str(key_object.sshType()),
        'size': str(key_object.size()),
        'public': str(key_object.public().toString('openssh')),
        'private': str(key_object.toString('openssh')),
    }
    key_data = json.dumps(key_json)
    block = encrypted.Block(
        BackupID=key_id,
        Data=key_data,
        SessionKey=key.NewSessionKey(),
        # encrypt data using public key of recipient
        EncryptKey=lambda inp: recipient_id_obj.encrypt(inp),
    )
    encrypted_key_data = block.Serialize()
    p2p_service.SendKey(
        remote_idurl=recipient_id_obj.getIDURL(),
        encrypted_key_data=encrypted_key_data,
        packet_id=key_id,
        callbacks={
            commands.Ack():
            lambda response, info: result.callback(response),
            commands.Fail():
            lambda response, info: result.errback(Exception(response)),
        },
    )
    return result
Пример #46
0
class Engine:
    def __init__(self, client):
        assert client is not None

        self._client = client
        self._client.add_engine(self)
        self.deferred = Deferred(canceller=self.__cancel)

    def _disconnect(self):
        if self._client is not None:
            self._client._client.transport.loseConnection()
            self._client = None

    def _signal(self, name, *args, **keywords):
        assert self._client is not None

        self._client.signal(name, *args, **keywords)

    def __stop(self):
        if self._client is not None:
            self._client.remove_engine(self)
            self._client = None

    def _success(self, result=None):
        self.__stop()
        self.deferred.callback(result)

    def _failure(self, fail='Engine failed'):
        self.__stop()
        self.deferred.errback(fail)

    def abort(self):
        """Aborts this engine, does not emit a signal."""
        self.__stop()

    def __cancel(self, d):
        self.abort()
Пример #47
0
class Request(RandomNumberCache):
    """
    This request cache keeps track of all outstanding requests within the DHTCommunity.
    """
    def __init__(self,
                 community,
                 msg_type,
                 node,
                 params=None,
                 consume_errors=False):
        super(Request, self).__init__(community.request_cache, msg_type)
        self.msg_type = msg_type
        self.node = node
        self.params = params
        self.deferred = Deferred()
        self.start_time = time.time()
        self.consume_errors = consume_errors

    @property
    def timeout_delay(self):
        return 5.0

    def on_timeout(self):
        if not self.deferred.called:
            self._logger.debug('Timeout for %s to %s', self.msg_type,
                               self.node)
            self.node.failed += 1
            if not self.consume_errors:
                self.deferred.errback(
                    Failure(
                        RuntimeError('Timeout for {} to {}'.format(
                            self.msg_type, self.node))))

    def on_complete(self):
        self.node.last_response = time.time()
        self.node.failed = 0
        self.node.rtt = time.time() - self.start_time
Пример #48
0
class FailureLoggingSubprocessProtocol(ProcessProtocol):
    """Simple Twisted protocol which logs a process'es output."""

    capBuf = None

    def __init__(self, file):
        """Constructor."""
        self.file = file
        self.d = Deferred()
        self.capBuf = deque('', 1024 * 1024)

    def outReceived(self, data):
        """Triggered upon program `stdout` data arriving."""
        text = data.decode('utf-8')
        self.file.write(text)
        self.capBuf.append(text)

    def errReceived(self, data):
        """Triggered upon program `stderr` data arriving."""
        text = data.decode('utf-8')
        self.file.write(text)

    def processEnded(self, reason):
        """Triggered upon the end of a program's execution."""
        if reason.check(ProcessDone):
            self.d.callback(''.join(self.capBuf))
        else:  # noqa: no-cover
            # Rewind to the start of our log file.
            self.file.seek(0)

            # Display the entire log.
            for line in self.file:
                LOGGER.warning("| %s" % line)

            # Resolve the deferred.
            self.d.errback(reason)
Пример #49
0
    def open_session(self, window_id=0):
        """Open a unique session for the caller application."""
        d = Deferred()
        try:
            self.window_id = str(window_id)
            self.bus = dbus.SessionBus()
            service_object = self.bus.get_object(BUS_NAME, SECRETS_SERVICE)
            self.service = dbus.Interface(service_object,
                                          dbus_interface=SERVICE_IFACE)
            self.properties = dbus.Interface(service_object,
                                             dbus_interface=PROPERTIES_IFACE)

            def session_opened(result, session):
                """The session was successfully opened."""
                self.session = self.bus.get_object(BUS_NAME, session)
                d.callback(self)

            parameters = dbus.String(ALGORITHM_PARAMS, variant_level=1)
            self.service.OpenSession(ALGORITHM, parameters,
                                     reply_handler=session_opened,
                                     error_handler=d.errback)
        except dbus.exceptions.DBusException as e:
            d.errback(e)
        return d
Пример #50
0
def request_service_keys_registry(key_id, idurl):
    result = Deferred()
    p2p_service.SendRequestService(
        idurl,
        'service_keys_registry',
        callbacks={
            commands.Ack():
            lambda response, indo: on_service_keys_registry_response(
                response, indo, key_id, idurl, result),
            commands.Fail():
            lambda response, indo: result.errback(
                Exception('"service_keys_registry" not started on remote node')
            )
        })
    return result
Пример #51
0
class _CollectOutputProtocol(ProcessProtocol):
    """
    Internal helper. Collects all output (stdout + stderr) into
    self.output, and callback's on done with all of it after the
    process exits (for any reason).
    """
    def __init__(self):
        self.done = Deferred()
        self.output = StringIO()

    def processEnded(self, reason):
        if not self.done.called:
            self.done.callback(self.output.getvalue())

    def processExited(self, reason):
        if not isinstance(reason.value, ProcessDone):
            self.done.errback(reason)

    def outReceived(self, data):
        self.output.write(data)

    def errReceived(self, data):
        print("ERR: {}".format(data))
        self.output.write(data)
Пример #52
0
class StartStopProcessProtocol(ProcessProtocol):
    """
    An L{IProcessProtocol} with a Deferred for events where the subprocess
    starts and stops.

    @ivar started: A L{Deferred} which fires with this protocol's
        L{IProcessTransport} provider when it is connected to one.

    @ivar stopped: A L{Deferred} which fires with the process output or a
        failure if the process produces output on standard error.

    @ivar output: A C{str} used to accumulate standard output.

    @ivar errors: A C{str} used to accumulate standard error.
    """

    def __init__(self):
        self.started = Deferred()
        self.stopped = Deferred()
        self.output = b""
        self.errors = b""

    def connectionMade(self):
        self.started.callback(self.transport)

    def outReceived(self, data):
        self.output += data

    def errReceived(self, data):
        self.errors += data

    def processEnded(self, reason):
        if reason.check(ProcessDone):
            self.stopped.callback(self.output)
        else:
            self.stopped.errback(ExitedWithStderr(self.errors, self.output))
Пример #53
0
class DataClientFactory(ClientFactory, LoggerInterface):
    protocol = TickDataClient

    def __init__(self, parent=None):
        super().__init__()
        LoggerInterface.__init__(self, parent=parent)
        self.done = Deferred()
        self._tick_event = dict()

    def clientConnectionFailed(self, connector, reason):
        self.logger.error('connection failed:', reason.getErrorMessage())
        self.done.errback(reason)
        ReconnectingClientFactory.clientConnectionFailed(connector, reason)

    def clientConnectionLost(self, connector, reason):
        self.logger.warning('connection lost:', reason.getErrorMessage())
        self.done.callback(None)
        ReconnectingClientFactory.clientConnectionLost(connector, reason)

    def register_event(self, symbol, handle):
        self._tick_event[symbol] = handle

    def unregister_event(self, symbol):
        del self._tick_event[symbol]
Пример #54
0
def send_to_miner(coins):
    """
    """
    if not is_connected():
        return succeed(None)
    result = Deferred()
    p2p_service.SendCoin(
        contract_chain_consumer.A().connected_miner,
        coins,
        callbacks={
            commands.Ack(): lambda response, info: result.callback(response),
            commands.Fail(): lambda response, info: result.errback(Exception(response)),
        }
    )
    return result
Пример #55
0
        def add(request):
            print "starting add()"
            try:
                args = self.getMembraneArgs(request)
            except KeyError:
                # Failed to correctly supply all arguments
                d = Deferred()
                d.errback(
                    Exception(
                        "Invalid request; not all required arguments were supplied"
                    ))
                return d
            except ValueError:
                # Failed to correctly supply all arguments
                d = Deferred()
                d.errback(
                    Exception(
                        "Invalid data: the molecular weight cut-off must be an integer, and the pressures, temperatures, and pHs must be real numbers."
                    ))
                return d

            # Sanity check data
            print "staring sanity check"
            error = self.sanityCheckMembraneArgs(args)

            # Fail if sanity checks failed
            if error is not None:
                d = Deferred()
                d.errback(Exception(error))
                return d

            # Write data into the database
            def writeToDatabase(cursor):
                print "requesting membranes table"
                cursor.execute("SELECT * FROM Membranes WHERE Name = ?",
                               (args["name"], ))
                print "checking if there are any matching names"
                if cursor.fetchone() is not None:
                    raise Exception("A membrane named '" + args["name"] +
                                    "' already exists in the database")
                print "inserting membrane data"
                cursor.execute(
                    "INSERT INTO Membranes(Name, Description, MWCO, SurfaceArea, Retired, MaxInletPressure, MaxAlongMembranePressure, MaxTransMembranePressure, MaxBackPressure, MinTemperature, MaxTemperature, MinPH, MaxPH) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)",
                    (args["name"], args["description"], args["mwco"],
                     args["surfaceArea"], args["retired"],
                     args["maxInletPressure"],
                     args["maxAlongMembranePressure"],
                     args["maxTransMembranePressure"], args["maxBackPressure"],
                     args["minTemperature"], args["maxTemperature"],
                     args["minPH"], args["maxPH"]))
                print "done"

            print "writing to database"
            return dbpool.runInteraction(writeToDatabase)
Пример #56
0
        def edit():
            try:
                args = self.getMembraneArgs(request)
            except KeyError:
                # Failed to correctly supply all arguments
                d = Deferred()
                d.errback(
                    Exception(
                        "Invalid request; not all required arguments were supplied"
                    ))
                return d
            except ValueError:
                # Failed to correctly supply all arguments
                d = Deferred()
                d.errback(
                    Exception(
                        "Invalid data: the molecular weight cut-off must be an integer, and the pressures, temperatures, and pHs must be real numbers."
                    ))
                return d

            # Sanity check data
            error = self.sanityCheckMembraneArgs(args)

            # Fail if sanity checks failed
            if error is not None:
                d = Deferred()
                d.errback(Exception(error))
                return d

            # Edit data in database
            def writeToDatabase(cursor):
                print "testing to see if the membrane id exists"
                cursor.execute("SELECT * FROM Membranes WHERE MembraneID = ?",
                               (args["membraneID"], ))
                if cursor.fetchone() is None:
                    raise Exception("The MembraneID '" +
                                    str(args["membraneID"]) +
                                    "' does not exist in the database")
                print "attempting to update db"
                cursor.execute(
                    "UPDATE Membranes SET Name=?, Description=?, MWCO=?, SurfaceArea=?, Retired=?, MaxInletPressure=?, MaxAlongMembranePressure=?, MaxTransMembranePressure=?, MaxBackPressure=?, MinTemperature=?, MaxTemperature=?, MinPH=?, MaxPH=? WHERE MembraneID = ?",
                    (args["name"], args["description"], args["mwco"],
                     args["surfaceArea"], args["retired"],
                     args["maxInletPressure"],
                     args["maxAlongMembranePressure"],
                     args["maxTransMembranePressure"], args["maxBackPressure"],
                     args["minTemperature"], args["maxTemperature"],
                     args["minPH"], args["maxPH"], args["membraneID"]))

            print "calling writeToDatabase"
            return dbpool.runInteraction(writeToDatabase)
Пример #57
0
	def downloadPhoto(self, photo, thumbnail=False):
		if not photo: return

		cache = os.path.join(self.cache, 'thumb', photo.albumid.text) if thumbnail else os.path.join(self.cache, photo.albumid.text)
		try: os.makedirs(cache)
		except OSError: pass

		url = photo.media.thumbnail[0].url if thumbnail else photo.media.content[0].url
		filename = url.split('/')[-1]
		fullname = os.path.join(cache, filename)
		d = Deferred()
		# file exists, assume it's valid...
		if os.path.exists(fullname):
			reactor.callLater(0, d.callback, (fullname, photo))
		else:
			downloadPage(url, fullname).addCallbacks(
				lambda value:d.callback((fullname, photo)),
				lambda error:d.errback((error, photo)))
		return d
Пример #58
0
def is_healthy(service_name):
    result = Deferred()
    svc = services().get(service_name, None)
    if svc is None:
        result.errback(Exception('service %s not found' % service_name))
        return result
    if not svc.installed():
        result.errback(Exception('service %s is not installed' % service_name))
        return result
    if not svc.enabled():
        result.errback(Exception('service %s is disabled' % service_name))
        return result
    if not is_started(service_name):
        result.errback(Exception('service %s is not started' % service_name))
        return result
    service_health = svc.health_check()
    if isinstance(service_health, Deferred):
        return service_health
    if service_health is True:
        result.callback(True)
    else:
        result.callback(False)
    return result
Пример #59
0
    def call(self, procedure, *args, **kwargs):
        assert (type(procedure) == str)

        invocation = Invocation()
        if 'options' in kwargs:
            options = kwargs['options']
            del kwargs['options']
            assert (isinstance(options, CallOptions))
            if options.discloseMe:
                invocation.caller = newid()
            if options.onProgress:
                invocation.progress = options.onProgress

        d = Deferred()
        if procedure == "com.myapp.echo":
            if len(args) != 1 or len(kwargs) != 0 or type(args[0]) != str:
                d.errback(
                    ApplicationError(
                        ApplicationError.INVALID_ARGUMENT,
                        "procedure takes exactly 1 positional argument of type string"
                    ))
            else:
                d.callback(args[0])
        elif procedure == "com.myapp.complex":
            d.callback(CallResult(23, 7, foo="bar"))

        elif self._registrations.has_key(procedure):
            try:
                kwargs['invocation'] = invocation
                res = self._registrations[procedure]._endpoint(*args, **kwargs)
            except TypeError as err:
                d.errback(
                    ApplicationError(ApplicationError.INVALID_ARGUMENT,
                                     str(err)))
            else:
                d.callback(res)

        else:
            d.errback(
                ApplicationError(ApplicationError.NO_SUCH_PROCEDURE,
                                 "no procedure with URI {}".format(procedure)))
        return d
Пример #60
0
        def add(request):
            try:
                args = self.getChemicalArgs(request)
            except KeyError:
                # Failed to correctly supply all arguments
                d = Deferred()
                d.errback(
                    Exception(
                        "Invalid request; not all required arguments were supplied"
                    ))
                return d
            except ValueError:
                # Failed to correctly supply all arguments
                d = Deferred()
                d.errback(
                    Exception(
                        "Invalid data: the temperatures must be real numbers.")
                )
                return d

            # Sanity check data
            error = self.sanityCheckChemicalArgs(args)

            # Fail if sanity checks failed
            if error is not None:
                d = Deferred()
                d.errback(Exception(error))
                return d

            # Write data into the database
            def writeToDatabase(cursor):
                cursor.execute("SELECT * FROM Chemicals WHERE Name = ?",
                               (args["name"], ))
                if cursor.fetchone() is not None:
                    raise Exception("A chemical named '" + args["name"] +
                                    "' already exists in the database")
                cursor.execute(
                    "INSERT INTO Chemicals(Name, Description, MinTemperature, MaxTemperature, DosedManually) VALUES (?,?,?,?,?)",
                    (args["name"], args["description"], args["minTemperature"],
                     args["maxTemperature"], args["dosedManually"]))

            return dbpool.runInteraction(writeToDatabase)