コード例 #1
0
ファイル: stack_network.py プロジェクト: sah/tricle
 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
ファイル: __init__.py プロジェクト: smartree/monocle
 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
ファイル: http.py プロジェクト: smartree/monocle
 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
ファイル: callback.py プロジェクト: sah/tricle
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
ファイル: __init__.py プロジェクト: ChrisWren/monocle
 def read_some(self):
     cb = Callback()
     self.read_cb = cb
     self.iostream.read_some(self._read_complete)
     return cb
コード例 #10
0
ファイル: __init__.py プロジェクト: ChrisWren/monocle
 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
ファイル: __init__.py プロジェクト: ChrisWren/monocle
 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
ファイル: callback.py プロジェクト: sah/tricle
def test_result():
    cb = Callback()
    assert not hasattr(cb, 'result')
    cb('ok')
    assert cb.result == 'ok'
コード例 #19
0
ファイル: __init__.py プロジェクト: ChrisWren/monocle
 def read_until(self, s):
     cb = Callback()
     self.read_cb = cb
     self.iostream.read_until(s, self._read_complete)
     return cb
コード例 #20
0
ファイル: __init__.py プロジェクト: smartree/monocle
 def resume(self):
     self.read_cb = Callback()
     self.transport.resumeProducing()
コード例 #21
0
ファイル: repl.py プロジェクト: steenzout/monocle
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()