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

[tor-commits] [stem/master] Allowing ControlMessage.from_str() to include conversion



commit 0efbdf80f94574805b0fa080965bb509d535a4db
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun May 26 21:27:43 2013 -0700

    Allowing ControlMessage.from_str() to include conversion
    
    For txtorcon or anything else using our ControlMessage.from_str() function the
    following is very, very common...
    
    msg = stem.response.ControlMessage.from_str(getinfo_content)
    stem.response.convert("GETINFO", msg)
    
    Adding a 'msg_type' argument to from_str() so the two calls can be combined.
    This addresses...
    
    https://trac.torproject.org/8605
---
 stem/response/__init__.py             |   11 +++++++++--
 test/output.py                        |    2 +-
 test/unit/response/control_message.py |   12 ++++++++++++
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/stem/response/__init__.py b/stem/response/__init__.py
index 01af23e..5e0d077 100644
--- a/stem/response/__init__.py
+++ b/stem/response/__init__.py
@@ -136,16 +136,23 @@ class ControlMessage(object):
   """
 
   @staticmethod
-  def from_str(content):
+  def from_str(content, msg_type = None, **kwargs):
     """
     Provides a ControlMessage for the given content.
 
     :param str content: message to construct the message from
+    :param str msg_type: type of tor reply to parse the content as
+    :param kwargs: optional keyword arguments to be passed to the parser method
 
     :returns: stem.response.ControlMessage instance
     """
 
-    return stem.socket.recv_message(StringIO.StringIO(content))
+    msg = stem.socket.recv_message(StringIO.StringIO(content))
+
+    if msg_type is not None:
+      convert(msg_type, msg, **kwargs)
+
+    return msg
 
   def __init__(self, parsed_content, raw_content):
     if not parsed_content:
diff --git a/test/output.py b/test/output.py
index 7cd00b7..3f5efee 100644
--- a/test/output.py
+++ b/test/output.py
@@ -133,7 +133,7 @@ def strip_module(line_type, line_content):
   repetitive, and redundant with the headers.
   """
 
-  m = re.match(".*( \(.*?\)).*", line_content)
+  m = re.match(".*( \(test\..*?\)).*", line_content)
 
   if m:
     line_content = line_content.replace(m.groups()[0], "", 1)
diff --git a/test/unit/response/control_message.py b/test/unit/response/control_message.py
index 75a5f6e..33b2008 100644
--- a/test/unit/response/control_message.py
+++ b/test/unit/response/control_message.py
@@ -7,6 +7,8 @@ import StringIO
 import unittest
 
 import stem.socket
+import stem.response
+import stem.response.getinfo
 
 OK_REPLY = "250 OK\r\n"
 
@@ -32,6 +34,16 @@ version -- The current version of Tor.
 
 
 class TestControlMessage(unittest.TestCase):
+  def test_from_str(self):
+    msg = stem.response.ControlMessage.from_str(GETINFO_VERSION)
+
+    self.assertTrue(isinstance(msg, stem.response.ControlMessage))
+    self.assertEqual('version=0.2.2.23-alpha (git-b85eb949b528f4d7)\nOK', str(msg))
+
+    msg = stem.response.ControlMessage.from_str(GETINFO_VERSION, "GETINFO")
+    self.assertTrue(isinstance(msg, stem.response.getinfo.GetInfoResponse))
+    self.assertEqual({'version': '0.2.2.23-alpha (git-b85eb949b528f4d7)'}, msg.entries)
+
   def test_ok_response(self):
     """
     Checks the basic 'OK' response that we get for most commands.

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