[svn-commits] russell: testsuite/asterisk/trunk r148 - /asterisk/trunk/lib/python/asterisk/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Mar 25 13:02:43 CDT 2010


Author: russell
Date: Thu Mar 25 13:02:40 2010
New Revision: 148

URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=148
Log:
Add support for multi-line comments.

Modified:
    asterisk/trunk/lib/python/asterisk/config.py

Modified: asterisk/trunk/lib/python/asterisk/config.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/lib/python/asterisk/config.py?view=diff&rev=148&r1=147&r2=148
==============================================================================
--- asterisk/trunk/lib/python/asterisk/config.py (original)
+++ asterisk/trunk/lib/python/asterisk/config.py Thu Mar 25 13:02:40 2010
@@ -9,7 +9,6 @@
 
 #
 # TODO
-# - Implement multi-line comment handling
 # - Implement config section template handling
 #
 
@@ -44,7 +43,7 @@
 
 
 class ConfigFile:
-    def __init__(self, fn, test_str=None):
+    def __init__(self, fn, config_str=None):
         self.categories = []
         self.category_re = re.compile("""
             \s*                       # Leading Whitespace
@@ -52,21 +51,25 @@
             \s*(?:;.*)?$              # trailing whitespace or a comment
             """, re.VERBOSE)
 
-        if test_str is not None:
-            for line in test_str.split("\n"):
-                self.parse_line(line)
-            return
+        if config_str is None:
+            try:
+                f = open(fn, "r")
+                config_str = f.read()
+                f.close()
+            except IOError:
+                print "Failed to open config file '%s'" % fn
+                return
+            except:
+                print "Unexpected error: %s" % sys.exc_info()[0]
+                return
 
-        try:
-            f = open(fn, "r")
-        except IOError:
-            print "Failed to open config file '%s'" % fn
-            return
+        config_str = self.strip_mline_comments(config_str)
 
-        for line in f:
+        for line in config_str.split("\n"):
             self.parse_line(line)
 
-        f.close()
+    def strip_mline_comments(self, text):
+        return re.compile(";--.*?--;", re.DOTALL).sub("", text)
 
     def parse_line(self, line):
         match = self.category_re.match(line)
@@ -85,6 +88,10 @@
             "; stuff\n" \
             "this line is invalid on purpose\n" \
             "[this is] also invalid]\n" \
+            ";-- comment --;\n" \
+            ";--   \n" \
+            "[this is commented out]\n" \
+            "         --;\n" \
             "[foo]\n" \
             "a = b\n" \
             "  b =   a  \n" \
@@ -96,7 +103,7 @@
             "xyz=x|y|z\n" \
             "1234 => 4242,Example Mailbox,root at localhost,,var=val\n"
 
-        conf = ConfigFile(fn=None, test_str=test)
+        conf = ConfigFile(fn=None, config_str=test)
 
         self.assertEqual(len(conf.categories), 2)
 




More information about the svn-commits mailing list