Пример #1
0
 def __init__(self):
     self.buffer = ""
     self.is_closed = False
     self.is_reading = False
     self.read_cb = Callback()
     self.connect_cb = Callback()
     self.out = []
     self.disconnect_called = 0
Пример #2
0
 def __init__(self, reader, writer):
     self._reader = reader
     self._writer = writer
     self.writing = False
     self.flush_cb = Callback()
     self.timeout = None
     self._closed = False
Пример #3
0
 def __init__(self, stack_conn=None):
     self._stack_conn = stack_conn
     self.writing = False
     self.flush_cb = Callback()
     self.write_encoding = 'utf-8'
     self.timeout = None
     self._current_timeout = None
Пример #4
0
 def connect(self, host, port):
     self._connection_timeout = self.timeout
     self._stack_conn = _Connection()
     self._stack_conn.attach(self)
     self._stack_conn.connect_cb = Callback()
     factory = ClientFactory()
     factory.protocol = lambda: self._stack_conn
     factory.clientConnectionFailed = self.clientConnectionFailed
     self._connect_to_reactor(host, port, factory, self._connection_timeout)
     yield self._stack_conn.connect_cb
Пример #5
0
 def query(self, url, headers=None, method='GET', body=None):
     _http_client = tornado.httpclient.AsyncHTTPClient()
     req = tornado.httpclient.HTTPRequest(url,
                                          method=method,
                                          headers=headers or {},
                                          body=body,
                                          # XXX: TODO
                                          #request_timeout=self.timeout
                                          )
     cb = Callback()
     _http_client.fetch(req, cb)
     response = yield cb
     yield Return(response)
Пример #6
0
    def _mock_call(self, *args, **kwargs):
        ''' Mock seems to run all returns through this method, so we intercept
        the return value and wrap it in monocle magic. '''
        result = super(_MonocleCallableMixin, self)._mock_call(*args, **kwargs)
        # Special case for when a person tries to print a MonocleMock directly.
        # In this case the result is a string, and the content is something
        # like <MonocleMock id='4470907728'>. So we look for that particular
        # string and do NOT wrap it in a Monocle callback.
        if (isinstance(result, str) and 
            (result.startswith('<MonocleMock ') or
             result.startswith('<MagicMonocleMock '))):
            return result

        cb = Callback()
        cb(result)
        return cb
Пример #7
0
def test_add():
    cb = Callback()
    calls = []
    assert cb._handlers == []
    cb.add(calls.append)
    assert cb._handlers == [calls.append]
    pytest.raises(TypeError, cb.add, False)
    assert cb._handlers == [calls.append]
    assert calls == []
    cb('ok')
    assert calls == ['ok']
    cb.add(calls.append)
    assert calls == ['ok', 'ok']
    pytest.raises(TypeError, cb.add, False)
Пример #8
0
def test_add():
    cb = Callback()
    calls = []
    assert cb.future._callbacks == []
    cb.add(calls.append)
    assert len(cb.future._callbacks) == 1
    pytest.raises(TypeError, cb.add, False)
    assert len(cb.future._callbacks) == 1
    assert calls == []
    cb('ok')
    yield sleep(0)
    assert calls == ['ok']
    cb.add(calls.append)
    yield sleep(0)
    assert calls == ['ok', 'ok']
    pytest.raises(TypeError, cb.add, False)
Пример #9
0
 def read_some(self):
     cb = Callback()
     self.read_cb = cb
     self.iostream.read_some(self._read_complete)
     return cb
Пример #10
0
 def connect(self, address):
     cb = Callback()
     self.connect_cb = cb
     self.iostream.connect(address, self._connect_complete)
     return cb
Пример #11
0
def next_tick():
    cb = Callback()
    cb(None)
    return cb
Пример #12
0
def delayed(seconds, val):
    cb = Callback()
    queue_task(seconds, cb, val)
    return cb
Пример #13
0
def immediate(val):
    cb = Callback()
    cb(val)
    return cb
Пример #14
0
 def resume(self):
     self.read_cb = Callback()
Пример #15
0
def sleep(seconds):
    cb = Callback()
    queue_task(seconds, cb, None)
    return cb
Пример #16
0
 def read(self, size):
     cb = Callback()
     self.read_cb = cb
     self.iostream.read_bytes(size, self._read_complete)
     return cb
Пример #17
0
 def _write_flushed(self, result=None):
     self.writing = False
     cb = self.flush_cb
     self.flush_cb = Callback()
     if cb:
         cb(result)
Пример #18
0
def test_result():
    cb = Callback()
    assert not hasattr(cb, 'result')
    cb('ok')
    assert cb.result == 'ok'
Пример #19
0
 def read_until(self, s):
     cb = Callback()
     self.read_cb = cb
     self.iostream.read_until(s, self._read_complete)
     return cb
Пример #20
0
 def resume(self):
     self.read_cb = Callback()
     self.transport.resumeProducing()
Пример #21
0
def main():
    print "Monocle", monocle.VERSION, "/", "Python", sys.version
    print 'Type "help", "copyright", "credits" or "license" for more information.'
    print "You can yield to Monocle oroutines at the prompt."
    ic = HistoryConsole()
    gs = dict(globals())
    ls = {}
    source = ""
    while True:
        try:
            if source:
                source += "\n"

            cb = Callback()

            def wait_for_input():
                try:
                    prompt = ">>> "
                    if source:
                        prompt = "... "
                    s = ic.raw_input(prompt)
                except EOFError:
                    eventloop.queue_task(0, eventloop.halt)
                    return
                eventloop.queue_task(0, cb, s)

            Thread(target=wait_for_input).start()
            source += yield cb

            if "\n" in source and not source.endswith("\n"):
                continue

            try:
                _c = code.compile_command(source)
                if not _c:
                    continue
                eval(_c, gs, ls)
            except SyntaxError, e:
                if "'yield' outside function" not in str(e):
                    raise

                # it's a yield!

                try:
                    core_hack_source = "    __r = (" + source.replace(
                        "\n", "\n    ") + ")"
                    hack_source = "def __g():\n" + core_hack_source + "\n    yield Return(locals())\n\n"
                    _c = code.compile_command(hack_source)
                except SyntaxError:
                    # maybe the return value assignment wasn't okay
                    core_hack_source = "    " + source.replace("\n", "\n    ")
                    hack_source = "def __g():\n" + core_hack_source + "\n    yield Return(locals())\n\n"
                    _c = code.compile_command(hack_source)

                if not _c:
                    continue

                # make the locals global so __g can reach them
                g_gs = dict(gs)
                g_gs.update(ls)
                eval(_c, g_gs, ls)

                # now monoclize it and get the callback
                _c = code.compile_command("monocle.o(__g)()", symbol="eval")
                cb = eval(_c, gs, ls)
                ls.pop('__g')
                #print "=== waiting for %s ===" % cb
                g_ls = yield cb
                if '__r' in g_ls:
                    r = g_ls.pop('__r')
                    if r:
                        print r
                ls.update(g_ls)
        except Exception:
            traceback.print_exc()

        source = ""
Пример #22
0
 def __init__(self, sock=None, evlp=evlp):
     asyncore.dispatcher_with_send.__init__(self, sock=sock, map=evlp._map)
     self.max_buffer_size = 104857600
     self.buffer = ""
     self.read_cb = None
     self.connect_cb = Callback()