From ab850b5a99d2a696161f4bf851d538916a06a1ad Mon Sep 17 00:00:00 2001 From: meejah Date: Sun, 22 Mar 2015 18:23:39 -0600 Subject: [PATCH] cleanup: remove dead code, flake8 tox env, and flake8 fixes --- test/test_as_future.py | 44 ----------------------------------- test/test_call_later.py | 6 ++--- test/test_callback.py | 2 -- test/test_errback.py | 3 +-- test/test_gather.py | 4 +--- test/util.py | 2 +- tox.ini | 10 +++++++- txaio/__init__.py | 18 ++++++++------- txaio/aio.py | 29 +++-------------------- txaio/aio_py3.py | 37 ------------------------------ txaio/interfaces.py | 51 ----------------------------------------- txaio/tx.py | 7 +++--- 12 files changed, 30 insertions(+), 183 deletions(-) delete mode 100644 txaio/aio_py3.py diff --git a/test/test_as_future.py b/test/test_as_future.py index 758a42e..e956cfd 100644 --- a/test/test_as_future.py +++ b/test/test_as_future.py @@ -1,5 +1,3 @@ -from six import StringIO -import pytest import txaio from util import run_once @@ -71,7 +69,6 @@ def test_as_future_recursive(): errors = [] results = [] calls = [] - exception = RuntimeError("sadness") f1 = txaio.create_future_success(42) def method(*args, **kw): @@ -93,44 +90,3 @@ def test_as_future_recursive(): assert len(errors) == 0 assert results[0] == 42 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')) diff --git a/test/test_call_later.py b/test/test_call_later.py index 09847ee..4a1dc08 100644 --- a/test/test_call_later.py +++ b/test/test_call_later.py @@ -1,13 +1,9 @@ -from six import StringIO import pytest import txaio -from mock import patch -from util import run_once, await # we need implementation-specific tests because we have to do # implementation-specific mocking of the event-loops - def test_call_later_twisted(): ''' Wait for two Futures. @@ -29,6 +25,7 @@ def test_call_later_twisted(): delay = txaio.call_later(1, foo, 5, 6, 7, foo="bar") assert len(calls) == 0 + assert hasattr(delay, 'cancel') reactor.advance(2) assert len(calls) == 1 @@ -71,6 +68,7 @@ def test_call_later_asio(): calls.append((args, kw)) delay = txaio.call_later(1, foo, 5, 6, 7, foo="bar") + assert hasattr(delay, 'cancel') assert len(calls) == 0 # advance time in the asyncio event-loop past our diff --git a/test/test_callback.py b/test/test_callback.py index 860892d..4b31609 100644 --- a/test/test_callback.py +++ b/test/test_callback.py @@ -1,5 +1,3 @@ -from six import StringIO -import pytest import txaio from util import run_once diff --git a/test/test_errback.py b/test/test_errback.py index 2e5a746..4c4dc2d 100644 --- a/test/test_errback.py +++ b/test/test_errback.py @@ -1,8 +1,7 @@ from six import StringIO -import pytest import txaio -from util import run_once, await +from util import run_once def test_errback(): diff --git a/test/test_gather.py b/test/test_gather.py index 46d8089..d5bf5ca 100644 --- a/test/test_gather.py +++ b/test/test_gather.py @@ -1,8 +1,6 @@ -from six import StringIO -import pytest import txaio -from util import run_once, await +from util import await def test_gather_two(): diff --git a/test/util.py b/test/util.py index c30221c..254df70 100644 --- a/test/util.py +++ b/test/util.py @@ -30,7 +30,7 @@ except ImportError as e: try: # XXX fixme hack better way to detect twisted # (has to work on py3 where asyncio exists always, though) - import twisted + import twisted # noqa def await(_): return diff --git a/tox.ini b/tox.ini index c9ad567..2958ae8 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,8 @@ envlist = py{27,py}-{twisted,asyncio}, py{34}-asyncio, py34-twisted, -# flake8 + flake8 + [testenv] deps = @@ -22,3 +23,10 @@ commands = py.test -s --basetemp={envtmpdir} --cov txaio --cov-report term-missing # coverage report --show-missing # coverage html + +[testenv:flake8] +deps = + flake8 +changedir=. +commands = + flake8 txaio/ test/ diff --git a/txaio/__init__.py b/txaio/__init__.py index c08c9fa..27cf31c 100644 --- a/txaio/__init__.py +++ b/txaio/__init__.py @@ -6,6 +6,7 @@ from .interfaces import IFailedFuture # see tx.py for Twisted implementation # see aio.py for asyncio/trollius implementation + class _Config: """ This holds all valid configuration options, accessed as @@ -35,16 +36,17 @@ __all__ = ( 'config', # the config instance, access via attributes - 'create_future', # create a Future (can be already resolved/errored) - 'as_future', # call a method, and always return a Future - 'reject', # errback a Future - 'resolve', # callback a Future - 'add_callbacks', # add callback and/or errback - 'gather', # return a Future waiting for several other Futures + 'create_future', # create a Future (can be already resolved/errored) + 'as_future', # call a method, and always return a Future + 'reject', # errback a Future + 'resolve', # callback a Future + 'add_callbacks', # add callback and/or errback + 'gather', # return a Future waiting for several other Futures 'IFailedFuture', # describes API for arg to errback()s ) + def use_twisted(): from txaio import tx import txaio @@ -70,11 +72,11 @@ def use_asyncio(): try: - from .tx import * + from txaio.tx import * # noqa using_twisted = True except ImportError: try: - from .aio import * + from txaio.aio import * # noqa using_asyncio = True except ImportError: raise ImportError("Neither asyncio nor Twisted found.") diff --git a/txaio/aio.py b/txaio/aio.py index b4490d0..c972c2f 100644 --- a/txaio/aio.py +++ b/txaio/aio.py @@ -1,6 +1,5 @@ from __future__ import absolute_import, print_function -import six import sys import traceback import functools @@ -12,21 +11,6 @@ try: import asyncio from asyncio import iscoroutine 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: # Trollius >= 0.3 was renamed @@ -34,15 +18,6 @@ except ImportError: import trollius as asyncio from trollius import iscoroutine 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() @@ -128,6 +103,7 @@ def create_future_error(error=None): f.set_exception(error.value) return f + # XXX maybe rename to call()? def as_future(fun, *args, **kwargs): try: @@ -142,6 +118,7 @@ def as_future(fun, *args, **kwargs): else: return create_future_success(res) + def call_later(delay, fun, *args, **kwargs): # loop.call_later doesns't support kwargs real_call = functools.partial(fun, *args, **kwargs) @@ -190,7 +167,7 @@ def add_callbacks(future, callback, errback): x = callback(res) if x is not None: f._result = x - except Exception as e: + except Exception: if errback: errback(create_failure()) return future.add_done_callback(done) diff --git a/txaio/aio_py3.py b/txaio/aio_py3.py deleted file mode 100644 index c26f370..0000000 --- a/txaio/aio_py3.py +++ /dev/null @@ -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 diff --git a/txaio/interfaces.py b/txaio/interfaces.py index ff08412..3432c61 100644 --- a/txaio/interfaces.py +++ b/txaio/interfaces.py @@ -1,57 +1,6 @@ import abc 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) class IFailedFuture(object): diff --git a/txaio/tx.py b/txaio/tx.py index abb2fef..b9d944c 100644 --- a/txaio/tx.py +++ b/txaio/tx.py @@ -1,8 +1,6 @@ from twisted.python.failure import Failure from twisted.internet.defer import maybeDeferred, Deferred, DeferredList 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 txaio.interfaces import IFailedFuture @@ -26,12 +24,13 @@ def create_future(result=None, error=None): raise ValueError("Cannot have both result and error.") f = Deferred() - if result != None: + if result is not None: resolve(f, result) - elif error != None: + elif error is not None: reject(f, error) return f + # maybe delete, just use create_future() def create_future_success(result): return succeed(result)