From 35c6ce5365345571832028d5bdb97efd1a4a0435 Mon Sep 17 00:00:00 2001 From: Adam Holmberg Date: Mon, 20 Apr 2015 15:58:44 -0500 Subject: [PATCH] Refactor redundant Connection factory methods to base --- cassandra/connection.py | 18 ++++++++++++++++++ cassandra/io/asyncorereactor.py | 13 ------------- cassandra/io/eventletreactor.py | 13 ------------- cassandra/io/geventreactor.py | 13 ------------- cassandra/io/libevreactor.py | 13 ------------- cassandra/io/twistedreactor.py | 18 ------------------ 6 files changed, 18 insertions(+), 70 deletions(-) diff --git a/cassandra/connection.py b/cassandra/connection.py index 2a020c06..6cbd4346 100644 --- a/cassandra/connection.py +++ b/cassandra/connection.py @@ -239,6 +239,24 @@ class Connection(object): """ pass + @classmethod + def factory(cls, *args, **kwargs): + """ + A factory function which returns connections which have + succeeded in connecting and are ready for service (or + raises an exception otherwise). + """ + timeout = kwargs.pop('timeout', 5.0) + conn = cls(*args, **kwargs) + conn.connected_event.wait(timeout) + if conn.last_error: + raise conn.last_error + elif not conn.connected_event.is_set(): + conn.close() + raise OperationTimedOut("Timed out creating connection") + else: + return conn + def close(self): raise NotImplementedError() diff --git a/cassandra/io/asyncorereactor.py b/cassandra/io/asyncorereactor.py index 2ac7156d..ef687c38 100644 --- a/cassandra/io/asyncorereactor.py +++ b/cassandra/io/asyncorereactor.py @@ -156,19 +156,6 @@ class AsyncoreConnection(Connection, asyncore.dispatcher): cls._loop._cleanup() cls._loop = None - @classmethod - def factory(cls, *args, **kwargs): - timeout = kwargs.pop('timeout', 5.0) - conn = cls(*args, **kwargs) - conn.connected_event.wait(timeout) - if conn.last_error: - raise conn.last_error - elif not conn.connected_event.is_set(): - conn.close() - raise OperationTimedOut("Timed out creating connection") - else: - return conn - def __init__(self, *args, **kwargs): Connection.__init__(self, *args, **kwargs) asyncore.dispatcher.__init__(self) diff --git a/cassandra/io/eventletreactor.py b/cassandra/io/eventletreactor.py index ceac6a95..670d0f18 100644 --- a/cassandra/io/eventletreactor.py +++ b/cassandra/io/eventletreactor.py @@ -57,19 +57,6 @@ class EventletConnection(Connection): def initialize_reactor(cls): eventlet.monkey_patch() - @classmethod - def factory(cls, *args, **kwargs): - timeout = kwargs.pop('timeout', 5.0) - conn = cls(*args, **kwargs) - conn.connected_event.wait(timeout) - if conn.last_error: - raise conn.last_error - elif not conn.connected_event.is_set(): - conn.close() - raise OperationTimedOut("Timed out creating connection") - else: - return conn - def __init__(self, *args, **kwargs): Connection.__init__(self, *args, **kwargs) diff --git a/cassandra/io/geventreactor.py b/cassandra/io/geventreactor.py index 4cd9c681..6e9af0da 100644 --- a/cassandra/io/geventreactor.py +++ b/cassandra/io/geventreactor.py @@ -50,19 +50,6 @@ class GeventConnection(Connection): _write_watcher = None _socket = None - @classmethod - def factory(cls, *args, **kwargs): - timeout = kwargs.pop('timeout', 5.0) - conn = cls(*args, **kwargs) - conn.connected_event.wait(timeout) - if conn.last_error: - raise conn.last_error - elif not conn.connected_event.is_set(): - conn.close() - raise OperationTimedOut("Timed out creating connection") - else: - return conn - def __init__(self, *args, **kwargs): Connection.__init__(self, *args, **kwargs) diff --git a/cassandra/io/libevreactor.py b/cassandra/io/libevreactor.py index db11eaf8..93b4c978 100644 --- a/cassandra/io/libevreactor.py +++ b/cassandra/io/libevreactor.py @@ -236,19 +236,6 @@ class LibevConnection(Connection): cls._libevloop._cleanup() cls._libevloop = None - @classmethod - def factory(cls, *args, **kwargs): - timeout = kwargs.pop('timeout', 5.0) - conn = cls(*args, **kwargs) - conn.connected_event.wait(timeout) - if conn.last_error: - raise conn.last_error - elif not conn.connected_event.is_set(): - conn.close() - raise OperationTimedOut("Timed out creating new connection") - else: - return conn - def __init__(self, *args, **kwargs): Connection.__init__(self, *args, **kwargs) diff --git a/cassandra/io/twistedreactor.py b/cassandra/io/twistedreactor.py index 1a5a64e7..ff81e561 100644 --- a/cassandra/io/twistedreactor.py +++ b/cassandra/io/twistedreactor.py @@ -148,24 +148,6 @@ class TwistedConnection(Connection): if not cls._loop: cls._loop = TwistedLoop() - @classmethod - def factory(cls, *args, **kwargs): - """ - A factory function which returns connections which have - succeeded in connecting and are ready for service (or - raises an exception otherwise). - """ - timeout = kwargs.pop('timeout', 5.0) - conn = cls(*args, **kwargs) - conn.connected_event.wait(timeout) - if conn.last_error: - raise conn.last_error - elif not conn.connected_event.is_set(): - conn.close() - raise OperationTimedOut("Timed out creating connection") - else: - return conn - def __init__(self, *args, **kwargs): """ Initialization method.