Signed-off-by: WATANABE Fumitaka <[email protected]>
---
 ryu/lib/packet/packet.py      |    4 ++++
 ryu/lib/packet/packet_base.py |   38 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/ryu/lib/packet/packet.py b/ryu/lib/packet/packet.py
index f80948a..785a28b 100644
--- a/ryu/lib/packet/packet.py
+++ b/ryu/lib/packet/packet.py
@@ -127,3 +127,7 @@ class Packet(object):
                 issubclass(protocol, packet_base.PacketBase)):
             return protocol in [p.__class__ for p in self.protocols]
         return protocol in self.protocols
+
+    def __str__(self):
+        return ', '.join(repr(protocol) for protocol in self.protocols)
+    __repr__ = __str__  # note: str(list) uses __repr__ for elements
diff --git a/ryu/lib/packet/packet_base.py b/ryu/lib/packet/packet_base.py
index e61e7ee..19b33ba 100644
--- a/ryu/lib/packet/packet_base.py
+++ b/ryu/lib/packet/packet_base.py
@@ -14,9 +14,45 @@
 # limitations under the License.

 import abc
+from ryu.lib import stringify


-class PacketBase(object):
+class StringifyMixin(stringify.StringifyMixin):
+
+    _STR_CONVERT_RULE = {}
+
+    @classmethod
+    def register_str_conversion_rule(cls, reg_cls):
+        rule = cls._get_conversion_rules(reg_cls, {})
+        setattr(reg_cls, '_STR_CONVERT_RULES', rule)
+        return reg_cls
+
+    @classmethod
+    def _get_conversion_rules(cls, reg_cls, rule):
+        for key, func in reg_cls._STR_CONVERT_RULE.items():
+            rule.setdefault(key, func)
+
+        for base_cls in reg_cls.__bases__:
+            if (issubclass(base_cls, StringifyMixin)
+                    and base_cls != StringifyMixin):
+                rule = cls._get_conversion_rules(base_cls, rule)
+
+        return rule
+
+    def __init__(self):
+        super(StringifyMixin, self).__init__()
+
+    def stringify_attrs(self):
+        attrs = super(StringifyMixin, self).stringify_attrs()
+
+        conversion_rule = getattr(self, '_STR_CONVERT_RULES', {})
+        for k, v in attrs:
+            if k in conversion_rule:
+                v = conversion_rule[k](v)
+            yield(k, v)
+
+
+class PacketBase(StringifyMixin):
     """A base class for a protocol (ethernet, ipv4, ...) header."""
     __metaclass__ = abc.ABCMeta
     _TYPES = {}
-- 1.7.10.4


------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to