[Zconfig] SVN: ZConfig/trunk/ add inet-binding-address and inet-connection-address, with more reasonable

Fred L. Drake, Jr. fdrake at gmail.com
Wed May 18 17:25:03 EDT 2005


Log message for revision 30393:
  add inet-binding-address and inet-connection-address, with more reasonable
  default IP address values
  (see http://www.zope.org/Collectors/Zope/1507 for more information)
  

Changed:
  U   ZConfig/trunk/datatypes.py
  U   ZConfig/trunk/doc/zconfig.pdf
  U   ZConfig/trunk/doc/zconfig.tex
  U   ZConfig/trunk/tests/test_datatypes.py

-=-
Modified: ZConfig/trunk/datatypes.py
===================================================================
--- ZConfig/trunk/datatypes.py	2005-05-18 19:51:02 UTC (rev 30392)
+++ ZConfig/trunk/datatypes.py	2005-05-18 21:25:03 UTC (rev 30393)
@@ -168,43 +168,65 @@
 port_number = RangeCheckedConversion(integer, min=1, max=0xffff).__call__
 
 
+class InetAddress:
+
+    def __init__(self, default_host):
+        self.DEFAULT_HOST = default_host
+
+    def __call__(self, s):
+        # returns (host, port) tuple
+        host = ''
+        port = None
+        if ":" in s:
+            host, s = s.split(":", 1)
+            if s:
+                port = port_number(s)
+            host = host.lower()
+        else:
+            try:
+                port = port_number(s)
+            except ValueError:
+                if len(s.split()) != 1:
+                    raise ValueError("not a valid host name: " + repr(s))
+                host = s.lower()
+        if not host:
+            host = self.DEFAULT_HOST
+        return host, port
+
+
 if sys.platform[:3] == "win":
     DEFAULT_HOST = "localhost"
 else:
     DEFAULT_HOST = ""
 
-def inet_address(s):
-    # returns (host, port) tuple
-    host = ''
-    port = None
-    if ":" in s:
-        host, s = s.split(":", 1)
-        if s:
-            port = port_number(s)
-        host = host.lower()
-    else:
-        try:
-            port = port_number(s)
-        except ValueError:
-            if len(s.split()) != 1:
-                raise ValueError("not a valid host name: " + repr(s))
-            host = s.lower()
-    if not host:
-        host = DEFAULT_HOST
-    return host, port
+inet_address = InetAddress(DEFAULT_HOST)
+inet_connection_address = InetAddress("127.0.0.1")
+inet_binding_address = InetAddress("")
 
-
 class SocketAddress:
     def __init__(self, s):
-        # returns (family, address) tuple
         import socket
         if "/" in s or s.find(os.sep) >= 0:
             self.family = getattr(socket, "AF_UNIX", None)
             self.address = s
         else:
             self.family = socket.AF_INET
-            self.address = inet_address(s)
+            self.address = self._parse_address(s)
 
+    def _parse_address(self, s):
+        return inet_address(s)
+
+class SocketBindingAddress(SocketAddress):
+
+    def _parse_address(self, s):
+        return inet_binding_address(s)
+
+class SocketConnectionAddress(SocketAddress):
+
+    def _parse_address(self, s):
+        return inet_connection_address(s)
+
+
 def float_conversion(v):
     if isinstance(v, basestring):
         if v.lower() in ["inf", "-inf", "nan"]:
@@ -330,7 +352,11 @@
     "port-number":       port_number,
     "basic-key":         BasicKeyConversion(),
     "inet-address":      inet_address,
+    "inet-binding-address":    inet_binding_address,
+    "inet-connection-address": inet_connection_address,
     "socket-address":    SocketAddress,
+    "socket-binding-address":    SocketBindingAddress,
+    "socket-connection-address": SocketConnectionAddress,
     "ipaddr-or-hostname":IpaddrOrHostname(),
     "existing-directory":existing_directory,
     "existing-path":     existing_path,

Modified: ZConfig/trunk/doc/zconfig.pdf
===================================================================
(Binary files differ)

Modified: ZConfig/trunk/doc/zconfig.tex
===================================================================
--- ZConfig/trunk/doc/zconfig.tex	2005-05-18 19:51:02 UTC (rev 30392)
+++ ZConfig/trunk/doc/zconfig.tex	2005-05-18 21:25:03 UTC (rev 30393)
@@ -882,6 +882,21 @@
   platforms.  If the port is omitted, \code{None} will be returned for
   \var{port}.
 
+\term{\datatype{inet-binding-address}}
+  An Internet address expressed as a \code{(\var{hostname},
+  \var{port})} pair.  The address is suitable for binding a socket.
+  If only the port is specified, the default host will be returned for
+  \var{hostname}.  The default host is the empty string on all
+  platforms.  If the port is omitted, \code{None} will be returned for
+  \var{port}.
+
+\term{\datatype{inet-connection-address}}
+  An Internet address expressed as a \code{(\var{hostname},
+  \var{port})} pair.  The address is suitable for connecting a socket
+  to a server.  If only the port is specified, \code{'127.0.0.1'} will
+  be returned for \var{hostname}.  If the port is omitted, \code{None}
+  will be returned for \var{port}.
+
 \term{\datatype{integer}}
   Convert a value to an integer.  This will be a Python \class{int} if
   the value is in the range allowed by \class{int}, otherwise a Python

Modified: ZConfig/trunk/tests/test_datatypes.py
===================================================================
--- ZConfig/trunk/tests/test_datatypes.py	2005-05-18 19:51:02 UTC (rev 30392)
+++ ZConfig/trunk/tests/test_datatypes.py	2005-05-18 21:25:03 UTC (rev 30393)
@@ -188,6 +188,26 @@
         eq(convert("host.EXAMPLE.com"),    ("host.example.com", None))
         self.assertRaises(ValueError, convert, "40 # foo")
 
+    def test_datatype_inet_binding_address(self):
+        convert = self.types.get("inet-binding-address")
+        eq = self.assertEqual
+        defhost = ""
+        eq(convert("Host.Example.Com:80"), ("host.example.com", 80))
+        eq(convert(":80"),                 (defhost, 80))
+        eq(convert("80"),                  (defhost, 80))
+        eq(convert("host.EXAMPLE.com"),    ("host.example.com", None))
+        self.assertRaises(ValueError, convert, "40 # foo")
+
+    def test_datatype_inet_connection_address(self):
+        convert = self.types.get("inet-connection-address")
+        eq = self.assertEqual
+        defhost = "127.0.0.1"
+        eq(convert("Host.Example.Com:80"), ("host.example.com", 80))
+        eq(convert(":80"),                 (defhost, 80))
+        eq(convert("80"),                  (defhost, 80))
+        eq(convert("host.EXAMPLE.com"),    ("host.example.com", None))
+        self.assertRaises(ValueError, convert, "40 # foo")
+
     def test_datatype_integer(self):
         convert = self.types.get("integer")
         eq = self.assertEqual



More information about the ZConfig mailing list