Commits

Jason R. Coombs committed 46dfb06

Added ServerConnection.reconnect() method. Fixes #3515580.

Comments (0)

Files changed (3)

+0.8
+===
+
+* Added ServerConnection.reconnect method. Fixes #3515580.
+
 0.7.1
 =====
 
 except ImportError:
     pass
 
+from . import functools as irc_functools
+
 DEBUG = False
 
 # set the version tuple
 
     def __init__(self, irclibobj):
         super(ServerConnection, self).__init__(irclibobj)
-        self.connected = 0  # Not connected yet.
+        self.connected = False
         self.socket = None
         self.ssl = None
 
+    # save the method args to allow for easier reconnection.
+    @irc_functools.save_method_args
     def connect(self, server, port, nickname, password=None, username=None,
             ircname=None, localaddress="", localport=0, ssl=False, ipv6=False):
         """Connect/reconnect to a server.
         Arguments:
 
             server -- Server name.
-
             port -- Port number.
-
             nickname -- The nickname.
-
             password -- Password (if any).
-
             username -- The username.
-
             ircname -- The IRC name ("realname").
-
             localaddress -- Bind the connection to a specific local IP address.
-
             localport -- Bind the connection to a specific local port.
-
             ssl -- Enable support for ssl.
-
             ipv6 -- Enable support for ipv6.
 
         This function can be called to reconnect a closed connection.
         self.localaddress = localaddress
         self.localport = localport
         self.localhost = socket.gethostname()
+        self.ipv6 = ipv6
         if ipv6:
             self.socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
         else:
             self.socket.connect((self.server, self.port))
             if ssl:
                 self.ssl = ssl_mod.wrap_socket(self.socket)
-        except socket.error, x:
+        except socket.error as err:
             self.socket.close()
             self.socket = None
-            raise ServerConnectionError("Couldn't connect to socket: %s" % x)
-        self.connected = 1
+            raise ServerConnectionError("Couldn't connect to socket: %s" % err)
+        self.connected = True
         if self.irclibobj.fn_to_add_socket:
             self.irclibobj.fn_to_add_socket(self.socket)
 
         self.user(self.username, self.ircname)
         return self
 
+    def reconnect(self):
+        """
+        Reconnect with the last arguments passed to self.connect()
+        """
+        self.connect(*self._connect_saved.args, **self._connect_saved.kwargs)
+
     def close(self):
         """Close the connection.
 
+from __future__ import absolute_import, print_function, unicode_literals
+
+import functools
+import collections
+
+def save_method_args(method):
+    """
+    Wrap a method such that when it is called, we save the args and
+    kwargs with which it was called.
+
+    >>> class MyClass(object):
+    ...     @save_method_args
+    ...     def method(self, a, b):
+    ...         print(a, b)
+    >>> my_ob = MyClass()
+    >>> my_ob.method(1, 2)
+    1 2
+    >>> my_ob._saved_method.args
+    (1, 2)
+    >>> my_ob._saved_method.kwargs
+    {}
+    >>> my_ob.method(a=3, b='foo')
+    3 foo
+    >>> my_ob._saved_method.args
+    ()
+    >>> my_ob._saved_method.kwargs == dict(a=3, b='foo')
+    True
+    """
+    args_and_kwargs = collections.namedtuple('args_and_kwargs', 'args kwargs')
+    @functools.wraps(method)
+    def wrapper(self, *args, **kwargs):
+        attr_name = '_saved_' + method.func_name
+        attr = args_and_kwargs(args, kwargs)
+        setattr(self, attr_name, attr)
+        return method(self, *args, **kwargs)
+    return wrapper