This patch unifies str representation of MsgBase into comma-separated
array of 'key=value' style.

before:
  version: 0x1 msg_type 0x0 xid 0x660a4ade OFPHello()

after:
  version=0x1,msg_type=0x0,msg_len=0x8,xid=0x660a4ade,OFPHello()

Signed-off-by: IWASE Yusuke <[email protected]>
---
 ryu/ofproto/ofproto_parser.py                 |  8 ++++----
 ryu/tests/unit/ofproto/test_ofproto_parser.py | 24 +++++++++++-------------
 2 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/ryu/ofproto/ofproto_parser.py b/ryu/ofproto/ofproto_parser.py
index 4049503..360e938 100644
--- a/ryu/ofproto/ofproto_parser.py
+++ b/ryu/ofproto/ofproto_parser.py
@@ -171,10 +171,10 @@ class MsgBase(StringifyMixin):
 
     def __str__(self):
         def hexify(x):
-            return str(None) if x is None else '0x%x' % x
-        buf = 'version: %s msg_type %s xid %s ' % (hexify(self.version),
-                                                   hexify(self.msg_type),
-                                                   hexify(self.xid))
+            return hex(x) if isinstance(x, int) else x
+        buf = 'version=%s,msg_type=%s,msg_len=%s,xid=%s,' %\
+              (hexify(self.version), hexify(self.msg_type),
+               hexify(self.msg_len), hexify(self.xid))
         return buf + StringifyMixin.__str__(self)
 
     @classmethod
diff --git a/ryu/tests/unit/ofproto/test_ofproto_parser.py 
b/ryu/tests/unit/ofproto/test_ofproto_parser.py
index 7f2d604..bc3c1fd 100644
--- a/ryu/tests/unit/ofproto/test_ofproto_parser.py
+++ b/ryu/tests/unit/ofproto/test_ofproto_parser.py
@@ -185,20 +185,18 @@ class TestMsgBase(unittest.TestCase):
         eq_(buffer(buf), res.buf)
 
         # test __str__()
-        list_ = ('version:', 'msg_type', 'xid')
+        list_ = ('version', 'msg_type', 'msg_len', 'xid')
         check = {}
-        str_ = str(res)
-        str_ = str_.rsplit()
-
-        i = 0
-        for s in str_:
-            if s in list_:
-                check[str_[i]] = str_[i + 1]
-            i += 1
-
-        eq_(hex(ofproto_v1_0.OFP_VERSION).find(check['version:']), 0)
-        eq_(hex(ofproto_v1_0.OFPT_HELLO).find(check['msg_type']), 0)
-        eq_(hex(xid).find(check['xid']), 0)
+        for s in str(res).rsplit(','):
+            if '=' in s:
+                (k, v,) = s.rsplit('=')
+                if k in list_:
+                    check[k] = v
+
+        eq_(hex(ofproto_v1_0.OFP_VERSION), check['version'])
+        eq_(hex(ofproto_v1_0.OFPT_HELLO), check['msg_type'])
+        eq_(hex(msg_len), check['msg_len'])
+        eq_(hex(xid), check['xid'])
 
         return True
 
-- 
1.9.1



------------------------------------------------------------------------------
Monitor Your Dynamic Infrastructure at Any Scale With Datadog!
Get real-time metrics from all of your servers, apps and tools
in one place.
SourceForge users - Click here to start your Free Trial of Datadog now!
http://pubads.g.doubleclick.net/gampad/clk?id=241902991&iu=/4140
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to