Signed-off-by: YAMAMOTO Takashi <[email protected]>
---
 ryu/lib/stringify.py          | 71 +++++++++++++++++++++++++++++++++++++++++--
 ryu/ofproto/ofproto_parser.py | 41 +++++++++++++++++++++++++
 2 files changed, 110 insertions(+), 2 deletions(-)

diff --git a/ryu/lib/stringify.py b/ryu/lib/stringify.py
index a575e8e..d3e0ee2 100644
--- a/ryu/lib/stringify.py
+++ b/ryu/lib/stringify.py
@@ -66,6 +66,33 @@ _types = {
 
 
 class StringifyMixin(object):
+
+    _TYPE = {}
+    """_TYPE class attribute is used to annotate types of attributes.
+
+    This type information is used to find an appropriate conversion for
+    a JSON style dictionary.
+
+    Currently the following types are implemented.
+
+    ===== ==========
+    Type  Descrption
+    ===== ==========
+    ascii US-ASCII
+    utf-8 UTF-8
+    ===== ==========
+
+    Example::
+        _TYPE = {
+            'ascii': [
+                'hw_addr',
+            ],
+            'utf-8': [
+                'name',
+            ]
+        }
+    """
+
     _class_prefixes = []
 
     def stringify_attrs(self):
@@ -138,7 +165,31 @@ class StringifyMixin(object):
         return _encode
 
     def to_jsondict(self, encode_string=base64.b64encode):
-        """returns an object to feed json.dumps()
+        """
+        This method returns a JSON style dict to describe this object.
+
+        The returned dict is compatible with json.dumps() and json.loads().
+
+        Suppose ClassName object inherits StringifyMixin.
+        For an object like the following::
+
+            ClassName(Param1=100, Param2=200)
+
+        this method would produce::
+
+            { "ClassName": {"Param1": 100, "Param2": 200} }
+
+        This method takes the following arguments.
+
+        =============  =====================================================
+        Argument       Description
+        =============  =====================================================
+        encode_string  (Optional) specify how to encode attributes which has
+                       python 'str' type.
+                       The default is base64.
+                       This argument is used only for attributes which don't
+                       have explicit type annotations in _TYPE class attribute.
+        =============  =====================================================
         """
         dict_ = {}
         encode = lambda k, x: self._encode_value(k, x, encode_string)
@@ -206,7 +257,23 @@ class StringifyMixin(object):
     @classmethod
     def from_jsondict(cls, dict_, decode_string=base64.b64decode,
                       **additional_args):
-        """create an instance from a result of json.loads()
+        """Create an instance from a JSON style dict.
+
+        Instantiate this class with parameters specified by the dict.
+
+        This method takes the following arguments.
+
+        =============== =====================================================
+        Argument        Descrpition
+        =============== =====================================================
+        dict\_          A dictionary which describes the parameters.
+                        For example, {"Param1": 100, "Param2": 200}
+        decode_string   (Optional) specify how to decode strings.
+                        The default is base64.
+                        This argument is used only for attributes which don't
+                        have explicit type annotations in _TYPE class 
attribute.
+        additional_args (Optional) Additional kwargs for constructor.
+        =============== =====================================================
         """
         decode = lambda k, x: cls._decode_value(k, x, decode_string)
         kwargs = cls._restore_args(_mapdict_kv(decode, dict_))
diff --git a/ryu/ofproto/ofproto_parser.py b/ryu/ofproto/ofproto_parser.py
index bfb9866..97b83e7 100644
--- a/ryu/ofproto/ofproto_parser.py
+++ b/ryu/ofproto/ofproto_parser.py
@@ -68,6 +68,30 @@ def create_list_of_base_attributes(f):
 
 
 def ofp_msg_from_jsondict(dp, jsondict):
+    """
+    This function instanticates an appropriate OpenFlow message class
+    from the given JSON style dictionary.
+    The objects created by following two code fragments are equivalent.
+
+    Code A::
+
+        jsonstr = '{ "OFPSetConfig": { "flags": 0, "miss_send_len": 128 } }'
+        jsondict = json.loads(jsonstr)
+        o = ofp_msg_from_jsondict(dp, jsondict)
+
+    Code B::
+
+        o = dp.ofproto_parser.OFPSetConfig(flags=0, miss_send_len=128)
+
+    This function takes the following arguments.
+
+    ======== =======================================
+    Argument Description
+    ======== =======================================
+    dp       An instance of ryu.controller.Datapath.
+    jsondict A JSON style dict.
+    ======== =======================================
+    """
     parser = dp.ofproto_parser
     assert len(jsondict) == 1
     for k, v in jsondict.iteritems():
@@ -87,6 +111,23 @@ class StringifyMixin(stringify.StringifyMixin):
 
 
 class MsgBase(StringifyMixin):
+    """
+    This is a base class for OpenFlow message classes.
+
+    An instance of this class has at least the following attributes.
+
+    ========= ==============================
+    Attribute Description
+    ========= ==============================
+    datapath  A ryu.controller.controller.Datapath instance for this message
+    version   OpenFlow protocol version
+    msg_type  Type of OpenFlow message
+    msg_len   Length of the message
+    xid       Transaction id
+    buf       Raw data
+    ========= ==============================
+    """
+
     @create_list_of_base_attributes
     def __init__(self, datapath):
         super(MsgBase, self).__init__()
-- 
1.8.3.1


------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58041391&iu=/4140/ostg.clktrk
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to