Convert autoserv extra arguments in the form of key=val or key:val to a 
dictionary.

Signed-off-by: Jongki Suwandi <[email protected]>

--- autotest/client/common_lib/utils.py 2010-06-08 17:04:36.000000000 -0700
+++ autotest/client/common_lib/utils.py 2010-06-08 17:04:36.000000000 -0700
@@ -1195,3 +1195,25 @@
         if c != 0:
             return c
     return cmp(len(ax), len(ay))
+
+
+def args_to_dict(args):
+    """Convert autoserv extra arguments in the form of key=val or key:val to a
+    dictionary.  Each argument key is converted to lowercase dictionary key.
+
+    Args:
+        args - list of autoserv extra arguments.
+
+    Returns:
+        dictionary
+    """
+    arg_re = re.compile(r'(\w+)[:=](.*)$')
+    dict = {}
+    for arg in args:
+        match = arg_re.match(arg)
+        if match:
+            dict[match.group(1).lower()] = match.group(2)
+        else:
+            logging.warning("args_to_dict: argument '%s' doesn't match "
+                            "'%s' pattern. Ignored." % (arg, arg_re.pattern))
+    return dict
--- autotest/client/common_lib/utils_unittest.py        2010-06-08 
17:04:36.000000000 -0700
+++ autotest/client/common_lib/utils_unittest.py        2010-06-08 
17:04:36.000000000 -0700
@@ -1,6 +1,6 @@
 #!/usr/bin/python
 
-import os, unittest, StringIO, socket, urllib2, shutil, subprocess
+import os, unittest, StringIO, socket, urllib2, shutil, subprocess, logging
 
 import common
 from autotest_lib.client.common_lib import utils, autotemp
@@ -744,5 +744,34 @@
         self.assertEqual(utils.compare_versions('k.231-1', 'k.231-1'), 0)
 
 
+class test_args_to_dict(unittest.TestCase):
+    def test_no_args(self):
+        result = utils.args_to_dict([])
+        self.assertEqual({}, result)
+
+
+    def test_matches(self):
+        result = utils.args_to_dict(['aBc:DeF', 'SyS=DEf', 'XY_Z:',
+                                     'F__o0O=', 'B8r:=:=', '_bAZ_=:=:'])
+        self.assertEqual(result, {'abc':'DeF', 'sys':'DEf', 'xy_z':'',
+                                  'f__o0o':'', 'b8r':'=:=', '_baz_':':=:'})
+
+
+    def test_unmatches(self):
+        # Temporarily shut warning messages from args_to_dict() when an 
argument
+        # doesn't match its pattern.
+        logger = logging.getLogger()
+        saved_level = logger.level
+        logger.setLevel(logging.ERROR)
+
+        try:
+            result = utils.args_to_dict(['ab-c:DeF', '--SyS=DEf', 'a*=b', 
'a*b',
+                                         ':VAL', '=VVV', 'WORD'])
+            self.assertEqual({}, result)
+        finally:
+            # Restore level.
+            logger.setLevel(saved_level)
+
+
 if __name__ == "__main__":
     unittest.main()
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to