[tor-commits] [stem/master] Implement Controller.load_conf

atagar at torproject.org atagar at torproject.org
Mon Jul 9 18:41:26 UTC 2012


commit 6cc4e195452bc573e34f0b2de295b1028a20eb4a
Author: Ravi Chandra Padmala <neenaoffline at gmail.com>
Date:   Fri Jun 29 00:25:17 2012 +0530

    Implement Controller.load_conf
---
 stem/control.py                  |   20 ++++++++++++++++++++
 test/integ/control/controller.py |   28 ++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index 2c12d43..f68183e 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -756,6 +756,26 @@ class Controller(BaseController):
         raise stem.socket.InvalidRequest(response.code, response.message)
       else:
         raise stem.socket.ProtocolError("%s returned unexpected status code" % command)
+  
+  def load_conf(self, configtext):
+    """
+    Sends the configuration text to Tor and loads it as if it has been read from
+    disk.
+    
+    :param str configtext: the configuration text
+    
+    :raises: :class:`stem.socket.ControllerError` if the call fails
+    """
+    
+    response = self.msg("LOADCONF\n%s" % configtext)
+    stem.response.convert("SINGLELINE", response)
+    
+    if response.code in ("552", "553"):
+      if response.code == "552" and response.message.startswith("Invalid config file: Failed to parse/validate config: Unknown option"):
+        raise stem.socket.InvalidArguments(response.code, response.message, [response.message[70:response.message.find('.', 70) - 1]])
+      raise stem.socket.InvalidRequest(response.code, response.message)
+    elif not response.is_ok():
+      raise stem.socket.ProtocolError("+LOADCONF Received unexpected response\n%s" % str(response))
 
 def _case_insensitive_lookup(entries, key, default = UNDEFINED):
   """
diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 65dfe18..bcd76fb 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -267,4 +267,32 @@ class TestController(unittest.TestCase):
         ), reset = True)
         
         shutil.rmtree(tmpdir)
+  
+  def test_loadconf(self):
+    """
+    Exercises Controller.load_conf with valid and invalid requests.
+    """
+    
+    if test.runner.require_control(self): return
+    
+    runner = test.runner.get_runner()
+    
+    with runner.get_tor_controller() as controller:
+      oldconf = runner.get_torrc_contents()
+      
+      # invalid requests
+      self.assertRaises(stem.socket.InvalidRequest, controller.load_conf, "ContactInfo confloaded")
+      try:
+        controller.load_conf("Blahblah blah")
+      except stem.socket.InvalidArguments, exc:
+        self.assertEqual(["Blahblah"], exc.arguments)
+      else:
+        self.fail()
+      
+      # valid config
+      controller.load_conf(runner.get_torrc_contents() + "\nContactInfo confloaded\n")
+      self.assertEqual("confloaded", controller.get_conf("ContactInfo"))
+      
+      # reload original valid config
+      controller.load_conf(oldconf)
 





More information about the tor-commits mailing list