Commits

Jason R. Coombs committed 4950fe1

Removed Connection._get_socket (and replaced it with a simple attribute)

Comments (0)

Files changed (2)

 ===
 
 Another backward-incompatible change. In irc 5.0, many of the unnecessary
-getter functions have been removed and replaced with simple attributes. In
-particular:
+getter functions have been removed and replaced with simple attributes. This
+change addresses issue #2. In particular:
 
+ - Connection._get_socket() -> Connection.socket (including subclasses)
  - Event.eventtype() -> Event.type
  - Event.source() -> Event.source
  - Event.target() -> Event.target
 import logging
 import itertools
 import threading
+import abc
 
 try:
     import pkg_resources
         with self.mutex:
             log.log(logging.DEBUG-2, "process_data()")
             for s, c in itertools.product(sockets, self.connections):
-                if s == c._get_socket():
+                if s == c.socket:
                     c.process_data()
 
     def process_timeout(self):
         """
         with self.mutex:
             log.log(logging.DEBUG-2, "process_once()")
-            sockets = [x._get_socket() for x in self.connections if x is not None]
+            sockets = [x.socket for x in self.connections if x is not None]
             sockets = [x for x in sockets if x is not None]
             if sockets:
                 (i, o, e) = select.select(sockets, [], [], timeout)
         with self.mutex:
             self.connections.remove(connection)
             if self.fn_to_remove_socket:
-                self.fn_to_remove_socket(connection._get_socket())
+                self.fn_to_remove_socket(connection.socket)
 
 class DelayedCommand(datetime.datetime):
     """
 _rfc_1459_command_regexp = re.compile("^(:(?P<prefix>[^ ]+) +)?(?P<command>[^ ]+)( *(?P<argument> .+))?")
 
 class Connection(object):
-    """Base class for IRC connections.
+    """
+    Base class for IRC connections.
+    """
+    __metaclass__ = abc.ABCMeta
 
-    Must be overridden.
-    """
+    @abc.abstractproperty
+    def socket(self):
+        "The socket for this connection"
+
     def __init__(self, irclibobj):
         self.irclibobj = irclibobj
 
-    def _get_socket():
-        raise IRCError("Not overridden")
-
     ##############################
     ### Convenience wrappers.
 
     """
 
     buffer_class = DecodingLineBuffer
+    socket = None
 
     def __init__(self, irclibobj):
         super(ServerConnection, self).__init__(irclibobj)
         self.connected = False
-        self.socket = None
 
     # save the method args to allow for easier reconnection.
     @irc_functools.save_method_args
         try:
             self.socket = self.connect_factory(self.server_address)
         except socket.error as err:
-            self.socket = None
             raise ServerConnectionError("Couldn't connect to socket: %s" % err)
         self.connected = True
         if self.irclibobj.fn_to_add_socket:
             self.disconnect("Closing object")
             self.irclibobj._remove_connection(self)
 
-    def _get_socket(self):
-        """[Internal]"""
-        return self.socket
-
     def get_server_name(self):
         """Get the (real) server name.
 
             self.socket.close()
         except socket.error:
             pass
-        self.socket = None
+        del self.socket
         self._handle_event(Event("disconnect", self.server, "", [message]))
 
     def globops(self, text):
     DCCConnection objects are instantiated by calling the dcc
     method on an IRC object.
     """
+    socket = None
+
     def __init__(self, irclibobj, dcctype):
         super(DCCConnection, self).__init__(irclibobj)
         self.connected = 0
         """
         self.peeraddress = socket.gethostbyname(address)
         self.peerport = port
-        self.socket = None
         self.buffer = LineBuffer()
         self.handlers = {}
         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
             self.socket.close()
         except socket.error:
             pass
-        self.socket = None
+        del self.socket
         self.irclibobj._handle_event(
             self,
             Event("dcc_disconnect", self.peeraddress, "", [message]))
                 self,
                 Event(command, prefix, target, arguments))
 
-    def _get_socket(self):
-        """[Internal]"""
-        return self.socket
-
     def privmsg(self, string):
         """Send data to DCC peer.