wingo - in flumotion/trunk: . flumotion/common flumotion/test

flumotion-commit at lists.fluendo.com flumotion-commit at lists.fluendo.com
Wed Feb 21 18:02:54 CET 2007


Author: wingo
Date: Wed Feb 21 18:02:51 2007
New Revision: 4508

Modified:
   flumotion/trunk/ChangeLog
   flumotion/trunk/flumotion/common/netutils.py
   flumotion/trunk/flumotion/test/test_common_netutils.py
Log:
2007-02-21  Andy Wingo  <wingo at pobox.com>

	* flumotion/common/netutils.py (RoutingTable.fromFile): New way of
	instantiating a routing table, parsing from a file.

	* flumotion/test/test_common_netutils.py
	(TestRoutingTable.testParseFromFile): Test case.



Modified: flumotion/trunk/ChangeLog
==============================================================================
--- flumotion/trunk/ChangeLog	(original)
+++ flumotion/trunk/ChangeLog	Wed Feb 21 18:02:51 2007
@@ -1,3 +1,11 @@
+2007-02-21  Andy Wingo  <wingo at pobox.com>
+
+	* flumotion/common/netutils.py (RoutingTable.fromFile): New way of
+	instantiating a routing table, parsing from a file.
+
+	* flumotion/test/test_common_netutils.py
+	(TestRoutingTable.testParseFromFile): Test case.
+
 2007-02-21  Michael Smith  <msmith at fluendo.com>
 
 	* flumotion/worker/worker.py:

Modified: flumotion/trunk/flumotion/common/netutils.py
==============================================================================
--- flumotion/trunk/flumotion/common/netutils.py	(original)
+++ flumotion/trunk/flumotion/common/netutils.py	Wed Feb 21 18:02:51 2007
@@ -27,7 +27,9 @@
 import fcntl
 import struct
 import array
-import avltree
+import re
+
+from flumotion.common import avltree
 
 # Thanks to Paul Cannon, see 
 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/439093
@@ -115,6 +117,40 @@
     return tz
 
 class RoutingTable(object):
+    def fromFile(klass, f):
+        """Make a new routing table, populated from entries in an open
+        file object.
+
+        The entries are expected to have the form:
+          IP-ADDRESS/MASK-BITS ROUTE-NAME
+
+        The `#' character denotes a comment. Empty lines are allowed.
+
+        @param f: file from whence to read a routing table
+        @type  f: open file object
+        """
+        comment = re.compile(r'^\s*#')
+        empty = re.compile(r'^\s*$')
+        entry = re.compile(r'^\s*'
+                           r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
+                           r'/'
+                           r'(\d{1,2})'
+                           r'\s+(.*)\s*$')
+        ret = klass()
+        n = 0
+        for line in f:
+            n += 1
+            if comment.match(line) or empty.match(line):
+                continue
+            m = entry.match(line)
+            if not m:
+                raise ValueError('While loading routing table from file'
+                                 ' %s: line %d: invalid syntax: %r'
+                                 % (f, n, line))
+            ret.addSubnet(m.group(3), m.group(1), int(m.group(2)))
+        return ret
+    fromFile = classmethod(fromFile)
+
     def __init__(self):
         self.avltree = avltree.AVLTree()
 

Modified: flumotion/trunk/flumotion/test/test_common_netutils.py
==============================================================================
--- flumotion/trunk/flumotion/test/test_common_netutils.py	(original)
+++ flumotion/trunk/flumotion/test/test_common_netutils.py	Wed Feb 21 18:02:51 2007
@@ -20,6 +20,7 @@
 # Headers in this file shall remain intact.
 
 import common
+import StringIO
 
 from twisted.trial import unittest
 
@@ -108,3 +109,49 @@
         ar('192.168.1.0', 'foo')
         ar('192.168.1.1', 'bar')
         ar('192.168.2.1', 'baz')
+
+    def assertParseFailure(self, string):
+        f = StringIO.StringIO(string)
+        self.assertRaises(ValueError, RoutingTable.fromFile, f)
+        f.close()
+        
+    def assertParseEquals(self, string, routes):
+        f = StringIO.StringIO(string)
+        net = RoutingTable.fromFile(f)
+        f.close()
+
+        expectednet = RoutingTable()
+        for route in routes:
+            expectednet.addSubnet(*route)
+        self.assertEquals(list(iter(net)),
+                          list(iter(expectednet)))
+        
+    def testParseFromFile(self):
+        self.assertParseFailure('bad line')
+        self.assertParseFailure('# comment\n'
+                                'bad line')
+        self.assertParseFailure('bad line\n'
+                                '# comment')
+        self.assertParseFailure('192.168.1.1/10')
+        self.assertParseFailure('192.168.1.1/10  ')
+        self.assertParseFailure('192.168.1.1/100 foo')
+        self.assertParseFailure('192.168.1.1000/32 foo')
+        self.assertParseFailure('192.168.1.0/32 good\n'
+                                '192.168.2.0/32')
+
+        self.assertParseEquals('',
+                               [])
+        self.assertParseEquals('#comment\n'
+                               '  ',
+                               [])
+        self.assertParseEquals('#comment\n'
+                               '  \n'
+                               '192.168.1.1/32 foo',
+                               [('foo', '192.168.1.1', 32)])
+        self.assertParseEquals('#comment\n'
+                               '  \n'
+                               '192.168.1.1/32 foo\n'
+                               '#general\n'
+                               '0.0.0.0/0 general',
+                               [('foo', '192.168.1.1', 32),
+                                ('general', '0.0.0.0', 0)])


More information about the flumotion-commit mailing list