[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Configuration parse_enum() function



commit 2b43313d72c4a73ff5055e2bf5c5b162d8f1f4b6
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Jan 1 13:04:56 2013 -0800

    Configuration parse_enum() function
    
    Convenience function to do the same thing as parse_enum_csv() but for a single
    enum value.
---
 stem/util/conf.py      |   16 ++++++++++++++++
 test/unit/util/conf.py |   12 +++++++++++-
 2 files changed, 27 insertions(+), 1 deletions(-)

diff --git a/stem/util/conf.py b/stem/util/conf.py
index 408b165..21106bf 100644
--- a/stem/util/conf.py
+++ b/stem/util/conf.py
@@ -194,6 +194,22 @@ def get_config(handle):
   if not handle in CONFS: CONFS[handle] = Config()
   return CONFS[handle]
 
+def parse_enum(key, value, enumeration):
+  """
+  Provides the enumeration value for a given key. This is a case insensitive
+  lookup and raises an exception if the enum key doesn't exist.
+  
+  :param str key: configuration key being looked up
+  :param str value: value to be parsed
+  :param stem.util.enum.Enum enumeration: enumeration the values should be in
+  
+  :returns: enumeration value
+  
+  :raises: **ValueError** if the **value** isn't among the enumeration keys
+  """
+  
+  return parse_enum_csv(key, value, enumeration, 1)[0]
+
 def parse_enum_csv(key, value, enumeration, count = None):
   """
   Parses a given value as being a comma separated listing of enumeration keys,
diff --git a/test/unit/util/conf.py b/test/unit/util/conf.py
index 25d312e..36afe10 100644
--- a/test/unit/util/conf.py
+++ b/test/unit/util/conf.py
@@ -7,7 +7,7 @@ import unittest
 import stem.util.conf
 import stem.util.enum
 
-from stem.util.conf import parse_enum_csv
+from stem.util.conf import parse_enum, parse_enum_csv
 
 class TestConf(unittest.TestCase):
   def tearDown(self):
@@ -53,6 +53,16 @@ class TestConf(unittest.TestCase):
     test_config.set("list_value", "c", False)
     self.assertEquals(["a", "b", "c"], my_config["list_value"])
   
+  def test_parse_enum(self):
+    """
+    Tests the parse_enum function.
+    """
+    
+    Insects = stem.util.enum.Enum("BUTTERFLY", "LADYBUG", "CRICKET")
+    self.assertEqual(Insects.LADYBUG, parse_enum("my_option", "ladybug", Insects))
+    self.assertRaises(ValueError, parse_enum, "my_option", "ugabuga", Insects)
+    self.assertRaises(ValueError, parse_enum, "my_option", "ladybug, cricket", Insects)
+  
   def test_parse_enum_csv(self):
     """
     Tests the parse_enum_csv function.



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits