On Wed, Jul 24, 2013 at 05:53:27PM +0900, watanabe.fumitaka wrote:
> 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
> +

Should this decorator be called for all subclasses?
If so, meta class fits the purpose. Something like this.

class StringifyMeta(type):
    def __init__(self, name, bases, cls_dict):
        rule = {}
        for base in reversed(bases):
            rule.update(getattr(base, '_STR_CONVERT_RULE', {}))
        rule.update(cls_dict.get('_STR_CONVERT_RULE', {}))
        cls_dict['_STR_CONVERT_RULE'] = rule
        return super(Stringify, self).__init__(name, bases, cls_dict)


class StringifyMixin(stringify.StringifyMixin):
    __metaclass__ = StringifyMeta
    _STR_CONVERT_RULE = {}


class A(M):
    _CONVERT_RULE = {'a': int}


class B(A):
    _CONVERT_RULE = {'b': str}


thanks,

> +    @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
> 

-- 
yamahata

------------------------------------------------------------------------------
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