[tor-commits] [stem/master] Documentation fixes & some code refactoring

atagar at torproject.org atagar at torproject.org
Wed Jul 4 21:34:20 UTC 2012


commit a035e89b522950a0bef54840ace887d2af31095c
Author: Ravi Chandra Padmala <neenaoffline at gmail.com>
Date:   Thu Jun 21 03:39:34 2012 +0530

    Documentation fixes & some code refactoring
---
 stem/control.py                  |   30 +++++++-----------------------
 stem/response/__init__.py        |    4 ++--
 stem/response/getconf.py         |    3 +--
 stem/socket.py                   |    5 ++---
 test/integ/control/controller.py |    5 -----
 5 files changed, 12 insertions(+), 35 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index 45e4f10..d4c9db5 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -565,28 +565,12 @@ class Controller(BaseController):
       :class:`stem.socket.InvalidArguments` if the configuration option requested was invalid
     """
     
-    try:
-      if param == "":  raise stem.socket.InvalidRequest("Received empty parameter")
-      
-      # automagically change the requested parameter if it's context sensitive
-      # and cannot be returned on it's own.
-      if param.lower() in self._mapped_config_keys.keys():
-        return self.get_conf_map(self._mapped_config_keys[param], default, multiple)[param]
-      
-      response = self.msg("GETCONF %s" % param)
-      stem.response.convert("GETCONF", response)
-      
-      # error if we got back different parameters than we requested
-      if response.entries.keys()[0].lower() != param.lower():
-        raise stem.socket.ProtocolError("GETCONF reply doesn't match the parameters that we requested. Queried '%s' but got '%s'." % (param, response.entries.keys()[0]))
-      
-      if not multiple:
-        return response.entries[param][0]
-      return response.entries[param]
-      
-    except stem.socket.ControllerError, exc:
-      if default is UNDEFINED: raise exc
-      else: return default
+    # automagically change the requested parameter if it's context sensitive
+    # and cannot be returned on it's own.
+    if param.lower() in self._mapped_config_keys.keys():
+      return self.get_conf_map(self._mapped_config_keys[param], default, multiple)[param]
+    else:
+      return self.get_conf_map(param, default, multiple)[param]
   
   def get_conf_map(self, param, default = UNDEFINED, multiple = False):
     """
@@ -625,7 +609,7 @@ class Controller(BaseController):
       
       requested_params = set(map(lambda x: x.lower(), param))
       reply_params = set(map(lambda x: x.lower(), response.entries.keys()))
-
+      
       # if none of the requested parameters are context sensitive and if the
       # parameters received don't match the parameters requested
       if not set(self._mapped_config_keys.values()) & requested_params and requested_params != reply_params:
diff --git a/stem/response/__init__.py b/stem/response/__init__.py
index 590e4c2..192e313 100644
--- a/stem/response/__init__.py
+++ b/stem/response/__init__.py
@@ -61,8 +61,8 @@ def convert(response_type, message, **kwargs):
   
   :raises:
     * :class:`stem.socket.ProtocolError` the message isn't a proper response of that type
-    * :class:`stem.socket.InvalidArguments` raised if the arguments given as
-        input are invalid. Raised whenj converting GETINFO or GETCONF requests
+    * :class:`stem.socket.InvalidArguments` the arguments given as input are
+        invalid. Raised when converting GETINFO or GETCONF requests
     * TypeError if argument isn't a :class:`stem.response.ControlMessage` or response_type isn't supported
   """
   
diff --git a/stem/response/getconf.py b/stem/response/getconf.py
index 1527e39..f2dc6b2 100644
--- a/stem/response/getconf.py
+++ b/stem/response/getconf.py
@@ -27,9 +27,8 @@ class GetConfResponse(stem.response.ControlMessage):
           unrecognized_keywords.append(line[32:-1])
       
       if unrecognized_keywords:
-        exc = stem.socket.InvalidArguments("552", "GETCONF request contained unrecognized keywords: %s" \
+        raise stem.socket.InvalidArguments("552", "GETCONF request contained unrecognized keywords: %s" \
             % ', '.join(unrecognized_keywords), unrecognized_keywords)
-        raise exc
       else:
         raise stem.socket.ProtocolError("GETCONF response contained a non-OK status code:\n%s" % self)
     
diff --git a/stem/socket.py b/stem/socket.py
index 6972929..db2e451 100644
--- a/stem/socket.py
+++ b/stem/socket.py
@@ -563,9 +563,8 @@ class InvalidRequest(ControllerError):
     """
     Initializes an InvalidRequest object.
     
-    :param str code: The error code returned by Tor (if applicable)
-    :param str message: The error message returned by Tor (if applicable) or a
-      human readable error message
+    :param str code: The error code returned by Tor
+    :param str message: The error message returned by Tor
     
     :returns: object of InvalidRequest class
     """
diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index f9db10e..329481b 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -144,7 +144,6 @@ class TestController(unittest.TestCase):
     
     with runner.get_tor_controller() as controller:
       # successful single query
-      
       socket = runner.get_tor_socket()
       if isinstance(socket, stem.socket.ControlPort):
         connection_value = str(socket.get_port())
@@ -157,7 +156,6 @@ class TestController(unittest.TestCase):
       self.assertEqual(connection_value, controller.get_conf(config_key, "la-di-dah"))
       
       # succeessful batch query
-      
       expected = {config_key: connection_value}
       self.assertEqual(expected, controller.get_conf_map([config_key]))
       self.assertEqual(expected, controller.get_conf_map([config_key], "la-di-dah"))
@@ -167,7 +165,6 @@ class TestController(unittest.TestCase):
         "DirPort", "DataDirectory"])))
       
       # non-existant option(s)
-      
       self.assertRaises(stem.socket.InvalidArguments, controller.get_conf, "blarg")
       self.assertEqual("la-di-dah", controller.get_conf("blarg", "la-di-dah"))
       self.assertRaises(stem.socket.InvalidArguments, controller.get_conf_map, "blarg")
@@ -178,13 +175,11 @@ class TestController(unittest.TestCase):
           controller.get_conf_map(["erfusdj", "afiafj"], "la-di-dah", multiple = True))
       
       # multivalue configuration keys
-      
       nodefamilies = [node_family[11:].strip() for node_family in
           runner.get_torrc_contents().split("\n") if node_family.startswith("NodeFamily ")]
       self.assertEqual(nodefamilies, controller.get_conf("NodeFamily", multiple = True))
       
       # empty input
-      
       self.assertRaises(stem.socket.InvalidRequest, controller.get_conf, "")
       self.assertRaises(stem.socket.InvalidRequest, controller.get_conf_map, [])
       self.assertRaises(stem.socket.InvalidRequest, controller.get_conf_map, "")





More information about the tor-commits mailing list