cleanup: remove dead code, flake8 tox env, and flake8 fixes
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
from six import StringIO
|
|
||||||
import pytest
|
|
||||||
import txaio
|
import txaio
|
||||||
|
|
||||||
from util import run_once
|
from util import run_once
|
||||||
@@ -71,7 +69,6 @@ def test_as_future_recursive():
|
|||||||
errors = []
|
errors = []
|
||||||
results = []
|
results = []
|
||||||
calls = []
|
calls = []
|
||||||
exception = RuntimeError("sadness")
|
|
||||||
f1 = txaio.create_future_success(42)
|
f1 = txaio.create_future_success(42)
|
||||||
|
|
||||||
def method(*args, **kw):
|
def method(*args, **kw):
|
||||||
@@ -93,44 +90,3 @@ def test_as_future_recursive():
|
|||||||
assert len(errors) == 0
|
assert len(errors) == 0
|
||||||
assert results[0] == 42
|
assert results[0] == 42
|
||||||
assert calls[0] == ((1, 2, 3), dict(key='word'))
|
assert calls[0] == ((1, 2, 3), dict(key='word'))
|
||||||
|
|
||||||
|
|
||||||
def test_as_future_generator():
|
|
||||||
'''
|
|
||||||
Return a coroutine to as_future
|
|
||||||
'''
|
|
||||||
errors = []
|
|
||||||
results = []
|
|
||||||
calls = []
|
|
||||||
|
|
||||||
@txaio.future_generator
|
|
||||||
def codependant(*args, **kw):
|
|
||||||
calls.append((args, kw))
|
|
||||||
yield txaio.create_future_success(42)
|
|
||||||
txaio.returnValue(42)
|
|
||||||
|
|
||||||
def method(*args, **kw):
|
|
||||||
calls.append((args, kw))
|
|
||||||
return codependant(*args, **kw)
|
|
||||||
f = txaio.as_future(method, 1, 2, 3, key='word')
|
|
||||||
|
|
||||||
def cb(x):
|
|
||||||
results.append(x)
|
|
||||||
|
|
||||||
def errback(f):
|
|
||||||
errors.append(f)
|
|
||||||
|
|
||||||
txaio.add_callbacks(f, cb, errback)
|
|
||||||
|
|
||||||
# XXX really need to figure out something better here :(
|
|
||||||
run_once()
|
|
||||||
run_once()
|
|
||||||
run_once()
|
|
||||||
run_once()
|
|
||||||
|
|
||||||
assert len(results) == 1
|
|
||||||
assert len(errors) == 0
|
|
||||||
assert results[0] == 42
|
|
||||||
assert len(calls) == 2
|
|
||||||
assert calls[0] == ((1, 2, 3), dict(key='word'))
|
|
||||||
assert calls[1] == ((1, 2, 3), dict(key='word'))
|
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
from six import StringIO
|
|
||||||
import pytest
|
import pytest
|
||||||
import txaio
|
import txaio
|
||||||
from mock import patch
|
|
||||||
|
|
||||||
from util import run_once, await
|
|
||||||
|
|
||||||
# we need implementation-specific tests because we have to do
|
# we need implementation-specific tests because we have to do
|
||||||
# implementation-specific mocking of the event-loops
|
# implementation-specific mocking of the event-loops
|
||||||
|
|
||||||
def test_call_later_twisted():
|
def test_call_later_twisted():
|
||||||
'''
|
'''
|
||||||
Wait for two Futures.
|
Wait for two Futures.
|
||||||
@@ -29,6 +25,7 @@ def test_call_later_twisted():
|
|||||||
|
|
||||||
delay = txaio.call_later(1, foo, 5, 6, 7, foo="bar")
|
delay = txaio.call_later(1, foo, 5, 6, 7, foo="bar")
|
||||||
assert len(calls) == 0
|
assert len(calls) == 0
|
||||||
|
assert hasattr(delay, 'cancel')
|
||||||
reactor.advance(2)
|
reactor.advance(2)
|
||||||
|
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
@@ -71,6 +68,7 @@ def test_call_later_asio():
|
|||||||
calls.append((args, kw))
|
calls.append((args, kw))
|
||||||
|
|
||||||
delay = txaio.call_later(1, foo, 5, 6, 7, foo="bar")
|
delay = txaio.call_later(1, foo, 5, 6, 7, foo="bar")
|
||||||
|
assert hasattr(delay, 'cancel')
|
||||||
assert len(calls) == 0
|
assert len(calls) == 0
|
||||||
|
|
||||||
# advance time in the asyncio event-loop past our
|
# advance time in the asyncio event-loop past our
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
from six import StringIO
|
|
||||||
import pytest
|
|
||||||
import txaio
|
import txaio
|
||||||
|
|
||||||
from util import run_once
|
from util import run_once
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
from six import StringIO
|
from six import StringIO
|
||||||
import pytest
|
|
||||||
import txaio
|
import txaio
|
||||||
|
|
||||||
from util import run_once, await
|
from util import run_once
|
||||||
|
|
||||||
|
|
||||||
def test_errback():
|
def test_errback():
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
from six import StringIO
|
|
||||||
import pytest
|
|
||||||
import txaio
|
import txaio
|
||||||
|
|
||||||
from util import run_once, await
|
from util import await
|
||||||
|
|
||||||
|
|
||||||
def test_gather_two():
|
def test_gather_two():
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ except ImportError as e:
|
|||||||
try:
|
try:
|
||||||
# XXX fixme hack better way to detect twisted
|
# XXX fixme hack better way to detect twisted
|
||||||
# (has to work on py3 where asyncio exists always, though)
|
# (has to work on py3 where asyncio exists always, though)
|
||||||
import twisted
|
import twisted # noqa
|
||||||
|
|
||||||
def await(_):
|
def await(_):
|
||||||
return
|
return
|
||||||
|
|||||||
10
tox.ini
10
tox.ini
@@ -3,7 +3,8 @@ envlist =
|
|||||||
py{27,py}-{twisted,asyncio},
|
py{27,py}-{twisted,asyncio},
|
||||||
py{34}-asyncio,
|
py{34}-asyncio,
|
||||||
py34-twisted,
|
py34-twisted,
|
||||||
# flake8
|
flake8
|
||||||
|
|
||||||
|
|
||||||
[testenv]
|
[testenv]
|
||||||
deps =
|
deps =
|
||||||
@@ -22,3 +23,10 @@ commands =
|
|||||||
py.test -s --basetemp={envtmpdir} --cov txaio --cov-report term-missing
|
py.test -s --basetemp={envtmpdir} --cov txaio --cov-report term-missing
|
||||||
# coverage report --show-missing
|
# coverage report --show-missing
|
||||||
# coverage html
|
# coverage html
|
||||||
|
|
||||||
|
[testenv:flake8]
|
||||||
|
deps =
|
||||||
|
flake8
|
||||||
|
changedir=.
|
||||||
|
commands =
|
||||||
|
flake8 txaio/ test/
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from .interfaces import IFailedFuture
|
|||||||
# see tx.py for Twisted implementation
|
# see tx.py for Twisted implementation
|
||||||
# see aio.py for asyncio/trollius implementation
|
# see aio.py for asyncio/trollius implementation
|
||||||
|
|
||||||
|
|
||||||
class _Config:
|
class _Config:
|
||||||
"""
|
"""
|
||||||
This holds all valid configuration options, accessed as
|
This holds all valid configuration options, accessed as
|
||||||
@@ -35,16 +36,17 @@ __all__ = (
|
|||||||
|
|
||||||
'config', # the config instance, access via attributes
|
'config', # the config instance, access via attributes
|
||||||
|
|
||||||
'create_future', # create a Future (can be already resolved/errored)
|
'create_future', # create a Future (can be already resolved/errored)
|
||||||
'as_future', # call a method, and always return a Future
|
'as_future', # call a method, and always return a Future
|
||||||
'reject', # errback a Future
|
'reject', # errback a Future
|
||||||
'resolve', # callback a Future
|
'resolve', # callback a Future
|
||||||
'add_callbacks', # add callback and/or errback
|
'add_callbacks', # add callback and/or errback
|
||||||
'gather', # return a Future waiting for several other Futures
|
'gather', # return a Future waiting for several other Futures
|
||||||
|
|
||||||
'IFailedFuture', # describes API for arg to errback()s
|
'IFailedFuture', # describes API for arg to errback()s
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def use_twisted():
|
def use_twisted():
|
||||||
from txaio import tx
|
from txaio import tx
|
||||||
import txaio
|
import txaio
|
||||||
@@ -70,11 +72,11 @@ def use_asyncio():
|
|||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from .tx import *
|
from txaio.tx import * # noqa
|
||||||
using_twisted = True
|
using_twisted = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
try:
|
try:
|
||||||
from .aio import *
|
from txaio.aio import * # noqa
|
||||||
using_asyncio = True
|
using_asyncio = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise ImportError("Neither asyncio nor Twisted found.")
|
raise ImportError("Neither asyncio nor Twisted found.")
|
||||||
|
|||||||
29
txaio/aio.py
29
txaio/aio.py
@@ -1,6 +1,5 @@
|
|||||||
from __future__ import absolute_import, print_function
|
from __future__ import absolute_import, print_function
|
||||||
|
|
||||||
import six
|
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
import functools
|
import functools
|
||||||
@@ -12,21 +11,6 @@ try:
|
|||||||
import asyncio
|
import asyncio
|
||||||
from asyncio import iscoroutine
|
from asyncio import iscoroutine
|
||||||
from asyncio import Future
|
from asyncio import Future
|
||||||
from asyncio import async
|
|
||||||
from asyncio import ALL_COMPLETED, FIRST_COMPLETED, FIRST_EXCEPTION
|
|
||||||
|
|
||||||
from asyncio import coroutine
|
|
||||||
|
|
||||||
if six.PY2:
|
|
||||||
future_generator = coroutine
|
|
||||||
|
|
||||||
def returnValue(x):
|
|
||||||
# inject the return value into the function-just-called
|
|
||||||
raise Return(x)
|
|
||||||
|
|
||||||
else:
|
|
||||||
from .aio_py3 import *
|
|
||||||
|
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# Trollius >= 0.3 was renamed
|
# Trollius >= 0.3 was renamed
|
||||||
@@ -34,15 +18,6 @@ except ImportError:
|
|||||||
import trollius as asyncio
|
import trollius as asyncio
|
||||||
from trollius import iscoroutine
|
from trollius import iscoroutine
|
||||||
from trollius import Future
|
from trollius import Future
|
||||||
from trollius import async
|
|
||||||
from trollius import ALL_COMPLETED, FIRST_COMPLETED, FIRST_EXCEPTION
|
|
||||||
|
|
||||||
from trollius import coroutine as future_generator
|
|
||||||
|
|
||||||
from trollius import Return
|
|
||||||
|
|
||||||
def returnValue(x):
|
|
||||||
raise Return(x)
|
|
||||||
|
|
||||||
|
|
||||||
config = _Config()
|
config = _Config()
|
||||||
@@ -128,6 +103,7 @@ def create_future_error(error=None):
|
|||||||
f.set_exception(error.value)
|
f.set_exception(error.value)
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
|
||||||
# XXX maybe rename to call()?
|
# XXX maybe rename to call()?
|
||||||
def as_future(fun, *args, **kwargs):
|
def as_future(fun, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
@@ -142,6 +118,7 @@ def as_future(fun, *args, **kwargs):
|
|||||||
else:
|
else:
|
||||||
return create_future_success(res)
|
return create_future_success(res)
|
||||||
|
|
||||||
|
|
||||||
def call_later(delay, fun, *args, **kwargs):
|
def call_later(delay, fun, *args, **kwargs):
|
||||||
# loop.call_later doesns't support kwargs
|
# loop.call_later doesns't support kwargs
|
||||||
real_call = functools.partial(fun, *args, **kwargs)
|
real_call = functools.partial(fun, *args, **kwargs)
|
||||||
@@ -190,7 +167,7 @@ def add_callbacks(future, callback, errback):
|
|||||||
x = callback(res)
|
x = callback(res)
|
||||||
if x is not None:
|
if x is not None:
|
||||||
f._result = x
|
f._result = x
|
||||||
except Exception as e:
|
except Exception:
|
||||||
if errback:
|
if errback:
|
||||||
errback(create_failure())
|
errback(create_failure())
|
||||||
return future.add_done_callback(done)
|
return future.add_done_callback(done)
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
import six
|
|
||||||
|
|
||||||
if six.PY3:
|
|
||||||
from asyncio import coroutine, async
|
|
||||||
from functools import wraps
|
|
||||||
from types import GeneratorType
|
|
||||||
|
|
||||||
class Return(Exception):
|
|
||||||
def __init__(self, v):
|
|
||||||
self.value = v
|
|
||||||
|
|
||||||
def returnValue(x):
|
|
||||||
# inject the return value into the function-just-called
|
|
||||||
raise Return(x)
|
|
||||||
|
|
||||||
@coroutine
|
|
||||||
def unwind_generator(gen):
|
|
||||||
future = gen.send(None)
|
|
||||||
try:
|
|
||||||
while future:
|
|
||||||
res = yield from async(future)
|
|
||||||
future = gen.send(res)
|
|
||||||
except Return as e:
|
|
||||||
# print("Return via exception! joys!", e)
|
|
||||||
res = e.value
|
|
||||||
return res
|
|
||||||
|
|
||||||
def future_generator(f):
|
|
||||||
@wraps(f)
|
|
||||||
@coroutine
|
|
||||||
def unwrap_return(*args, **kw):
|
|
||||||
r = f(*args, **kw)
|
|
||||||
if type(r) is not GeneratorType:
|
|
||||||
raise RuntimeError("{0} must return a generator".format(f))
|
|
||||||
real = yield from unwind_generator(r)
|
|
||||||
return real
|
|
||||||
return unwrap_return
|
|
||||||
@@ -1,57 +1,6 @@
|
|||||||
import abc
|
import abc
|
||||||
import six
|
import six
|
||||||
|
|
||||||
@six.add_metaclass(abc.ABCMeta)
|
|
||||||
class ILoopMixin(object):
|
|
||||||
"""
|
|
||||||
Some ideas for a Mixin-style API, similar to the FutureMixin that
|
|
||||||
was in Autobahn.
|
|
||||||
"""
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def future_create(self, value=None, exception=None):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def future_call(self, fun, *args, **kw):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def future_resolve(self, value):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def future_reject(self, exception=None):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def future_gather(self, futures, **kw):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class FutureWrapper(object):
|
|
||||||
"""
|
|
||||||
Writing down some ideas for a Thing That Wraps A Future or a
|
|
||||||
Deferred, as per some #twisted feedback
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, future):
|
|
||||||
self.future = future
|
|
||||||
|
|
||||||
def add_callbacks(self, callback, errback):
|
|
||||||
"""
|
|
||||||
Same as txaio.add_callbacks(future, callback, errback) put we provide the future.
|
|
||||||
"""
|
|
||||||
add_callbacks(self.future, callback, errback)
|
|
||||||
|
|
||||||
def reject(self, exception=None):
|
|
||||||
reject(self.future, exception=exception)
|
|
||||||
|
|
||||||
@abc.abstractmethod
|
|
||||||
def resolve(self, value):
|
|
||||||
resolve(self.future, value)
|
|
||||||
|
|
||||||
|
|
||||||
@six.add_metaclass(abc.ABCMeta)
|
@six.add_metaclass(abc.ABCMeta)
|
||||||
class IFailedFuture(object):
|
class IFailedFuture(object):
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
from twisted.python.failure import Failure
|
from twisted.python.failure import Failure
|
||||||
from twisted.internet.defer import maybeDeferred, Deferred, DeferredList
|
from twisted.internet.defer import maybeDeferred, Deferred, DeferredList
|
||||||
from twisted.internet.defer import succeed, fail
|
from twisted.internet.defer import succeed, fail
|
||||||
from twisted.internet.defer import inlineCallbacks as future_generator
|
|
||||||
from twisted.internet.defer import returnValue # XXX how to do in asyncio?
|
|
||||||
from twisted.internet.interfaces import IReactorTime
|
from twisted.internet.interfaces import IReactorTime
|
||||||
|
|
||||||
from txaio.interfaces import IFailedFuture
|
from txaio.interfaces import IFailedFuture
|
||||||
@@ -26,12 +24,13 @@ def create_future(result=None, error=None):
|
|||||||
raise ValueError("Cannot have both result and error.")
|
raise ValueError("Cannot have both result and error.")
|
||||||
|
|
||||||
f = Deferred()
|
f = Deferred()
|
||||||
if result != None:
|
if result is not None:
|
||||||
resolve(f, result)
|
resolve(f, result)
|
||||||
elif error != None:
|
elif error is not None:
|
||||||
reject(f, error)
|
reject(f, error)
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
|
||||||
# maybe delete, just use create_future()
|
# maybe delete, just use create_future()
|
||||||
def create_future_success(result):
|
def create_future_success(result):
|
||||||
return succeed(result)
|
return succeed(result)
|
||||||
|
|||||||
Reference in New Issue
Block a user