> On Fri, 23 Aug 2013 17:59:21 +0900 (JST) > yamam...@valinux.co.jp (YAMAMOTO Takashi) wrote: > >>> On Fri, 23 Aug 2013 13:42:54 +0900 (JST) >>> yamam...@valinux.co.jp (YAMAMOTO Takashi) wrote: >>> >>>>> On Fri, 23 Aug 2013 12:18:31 +0900 (JST) >>>>> yamam...@valinux.co.jp (YAMAMOTO Takashi) wrote: >>>>> >>>>>>> On Fri, 16 Aug 2013 15:29:22 +0900 >>>>>>> YAMAMOTO Takashi <yamam...@valinux.co.jp> wrote: >>>>>>> >>>>>>>> Signed-off-by: YAMAMOTO Takashi <yamam...@valinux.co.jp> >>>>>>>> --- >>>>>>>> ryu/ofproto/ofproto_v1_0_parser.py | 6 +++++- >>>>>>>> 1 file changed, 5 insertions(+), 1 deletion(-) >>>>>>>> >>>>>>>> diff --git a/ryu/ofproto/ofproto_v1_0_parser.py >>>>>>>> b/ryu/ofproto/ofproto_v1_0_parser.py >>>>>>>> index 3fae86d..e1951c8 100644 >>>>>>>> --- a/ryu/ofproto/ofproto_v1_0_parser.py >>>>>>>> +++ b/ryu/ofproto/ofproto_v1_0_parser.py >>>>>>>> @@ -18,6 +18,7 @@ import struct >>>>>>>> import binascii >>>>>>>> >>>>>>>> from ofproto_parser import StringifyMixin, MsgBase, msg_pack_into, >>>>>>>> msg_str_attr >>>>>>>> +from ryu.lib import addrconv >>>>>>>> from ryu.lib import mac >>>>>>>> from . import ofproto_parser >>>>>>>> from . import ofproto_v1_0 >>>>>>>> @@ -86,6 +87,9 @@ class >>>>>>>> OFPPhyPort(ofproto_parser.namedtuple('OFPPhyPort', ( >>>>>>>> def parser(cls, buf, offset): >>>>>>>> port = struct.unpack_from(ofproto_v1_0.OFP_PHY_PORT_PACK_STR, >>>>>>>> buf, offset) >>>>>>>> + i = cls._fields.index('hw_addr') >>>>>>>> + port = list(port) >>>>>>>> + port[i] = addrconv.mac.bin_to_text(port[i]) >>>>>>>> return cls(*port) >>>>>>> >>>>>>> Can we use unicode type here? Then _encode_value() in stringfy.py just >>>>>> >>>>>> are you talking about "name", not "hw_addr"? >>>>> >>>>> hw_addr. >>>> >>>> then i'm not sure if i understand what you mean. >>>> do you mean to use different python types (str vs unicode) to >>>> tell the encoder do base64 or not? it can work for encoding >>>> but not for decoding. >>> >>> _JSON_FORMATER thing in this patchset describes the type of members in >>> a class. Then you can know how to decode. >>> >>> I thought that we could use unicode for 'string' type however looks >>> like it needs more changes than I want. Scratch that. >> >> IMO, if how to decode is specified _JSON_FORMATTER or _STRING, >> it's better to use the same mechanism to specify how to encode. >> >>> >>> I like to simply use 'type' attribute rather than allowing own >>> encoder/decoder. We use JSON so it's just about string or binary. I >>> like a simpler like the following. I don't like to use class attribute >>> though. I prefer to use the attribute of each member. >> >> do you like something like the following? >> >> python: >> >> class Binary(str): >> pass >> >> class Hoge(StringifyMixin): >> def __init__(self): >> self.field1 = Binary('foo') >> self.field2 = 'bar' >> >> json: >> >> { "Hoge": { >> "field1": { "Binary": "xxx base64 encoded 'foo' xxx" }, >> "field2": "bar" >> }} > > Not sure. For me, the above JSON looks unfamiliar. If we do the above, > I think that we should do all the field in the same way for > consistency (that is, adding 'u32', 'u64', etc description). And > surely I don't like such.
ok. neither do i. > > I'm not sure we would add other types except for 'string' but I'm > inclined to something like the following. it looks like same as what i posted last week to me. are you happier with the following? or is tagging with a string ('string') essential? YAMAMOTO Takashi diff --git a/ryu/lib/stringify.py b/ryu/lib/stringify.py index b71da75..ea3b3b8 100644 --- a/ryu/lib/stringify.py +++ b/ryu/lib/stringify.py @@ -21,6 +21,9 @@ import collections import inspect +from ryu.lib import addrconv + + # Some arguments to __init__ is mungled in order to avoid name conflicts # with builtin names. # The standard mangling is to append '_' in order to avoid name clashes @@ -46,6 +49,9 @@ _mapdict_key = lambda f, d: dict([(f(k), v) for k, v in d.items()]) _mapdict_kv = lambda f, d: dict([(k, f(k, v)) for k, v in d.items()]) +StringType = addrconv.plain_text + + class StringifyMixin(object): _class_prefixes = [] @@ -80,7 +86,10 @@ class StringifyMixin(object): @classmethod def _get_converter(cls, k): - return cls._JSON_FORMATTER[k] + for t, attrs in cls._TYPE.iteritems(): + if k in attrs: + return t + raise KeyError @classmethod def _get_encoder(cls, k, encode_string): diff --git a/ryu/ofproto/ofproto_v1_0_parser.py b/ryu/ofproto/ofproto_v1_0_parser.py index d8e42bb..03c11ff 100644 --- a/ryu/ofproto/ofproto_v1_0_parser.py +++ b/ryu/ofproto/ofproto_v1_0_parser.py @@ -24,6 +24,7 @@ from . import ofproto_parser from . import ofproto_v1_0 from . import nx_match from ryu import utils +from ryu.lib.stringify import StringType import logging LOG = logging.getLogger('ryu.ofproto.ofproto_v1_0_parser') @@ -83,11 +84,13 @@ class OFPPhyPort(ofproto_parser.namedtuple('OFPPhyPort', ( 'port_no', 'hw_addr', 'name', 'config', 'state', 'curr', 'advertised', 'supported', 'peer'))): - _JSON_FORMATTER = { - 'hw_addr': addrconv.plain_text, - # XXX OF spec is unclear about the encoding of name. - # OVS seems to use UTF-8. - # 'name': addrconv.plain_text, + _TYPE = { + StringType: [ + 'hw_addr', + # XXX OF spec is unclear about the encoding of name. + # OVS seems to use UTF-8. + # 'name' + ] } @classmethod @@ -2126,7 +2129,11 @@ class OFPFlowMod(MsgBase): @_set_msg_type(ofproto_v1_0.OFPT_PORT_MOD) class OFPPortMod(MsgBase): - _JSON_FORMATTER = {'hw_addr': addrconv.plain_text} + _TYPE = { + StringType: [ + 'hw_addr', + ] + } def __init__(self, datapath, port_no, hw_addr, config, mask, advertise): super(OFPPortMod, self).__init__(datapath) > > = >>From b5ddaabfbb4ad5500d70fdd350f616ae357058f4 Mon Sep 17 00:00:00 2001 > From: FUJITA Tomonori <fujita.tomon...@lab.ntt.co.jp> > Date: Sat, 24 Aug 2013 08:54:16 +0900 > Subject: [PATCH 3/6] stop be64 encode/decode for strings in JSON > representation > > Our JSON handles binary and string in the same way (use be64 > always). Had better to handle string in human readable way (string is > supposed to be utf-8 encoded/decoded by default in JSON). > > Signed-off-by: FUJITA Tomonori <fujita.tomon...@lab.ntt.co.jp> > --- > ryu/lib/stringify.py | 15 +++++++++++++-- > 1 file changed, 13 insertions(+), 2 deletions(-) > > diff --git a/ryu/lib/stringify.py b/ryu/lib/stringify.py > index 8b755c5..050aa0a 100644 > --- a/ryu/lib/stringify.py > +++ b/ryu/lib/stringify.py > @@ -79,9 +79,18 @@ class StringifyMixin(object): > return False > > @classmethod > + def _is_string_type(cls, k): > + if hasattr(cls, '_TYPE'): > + if 'string' in cls._TYPE and k in cls._TYPE['string']: > + return True > + return False > + > + @classmethod > def _encode_value(cls, k, v, encode_string=base64.b64encode): > encode = lambda x: cls._encode_value(k, x, encode_string) > - if isinstance(v, (bytes, unicode)): > + if cls._is_string_type(k): > + json_value = v.encode('utf') > + elif isinstance(v, (bytes, unicode)): > json_value = encode_string(v) > elif isinstance(v, list): > json_value = map(encode, v) > @@ -124,7 +133,9 @@ class StringifyMixin(object): > @classmethod > def _decode_value(cls, k, json_value, decode_string=base64.b64decode): > decode = lambda x: cls._decode_value(k, x, decode_string) > - if isinstance(json_value, (bytes, unicode)): > + if cls._is_string_type(k): > + v = json_value > + elif isinstance(json_value, (bytes, unicode)): > v = decode_string(json_value) > elif isinstance(json_value, list): > v = map(decode, json_value) > -- > 1.7.12.4 (Apple Git-37) > > > > > > ------------------------------------------------------------------------------ > Introducing Performance Central, a new site from SourceForge and > AppDynamics. Performance Central is your source for news, insights, > analysis and resources for efficient Application Performance Management. > Visit us today! > http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk > _______________________________________________ > Ryu-devel mailing list > Ryu-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/ryu-devel ------------------------------------------------------------------------------ Introducing Performance Central, a new site from SourceForge and AppDynamics. Performance Central is your source for news, insights, analysis and resources for efficient Application Performance Management. Visit us today! http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk _______________________________________________ Ryu-devel mailing list Ryu-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/ryu-devel