[ovs-dev] [python idl 14/16] python: Accept multiple forms of strings and lists when parsing JSON.

Ethan Jackson ethan at nicira.com
Tue Sep 20 01:19:39 UTC 2011


Looks good,

Ethan

On Mon, Sep 19, 2011 at 11:18, Ben Pfaff <blp at nicira.com> wrote:
> The JSON parser in OVS always yields unicode strings and lists, never
> non-unicode strings or tuples, but it's easy to create them when building
> JSON elsewhere, so accept both forms.
> ---
>  python/ovs/db/data.py   |    3 ++-
>  python/ovs/db/parser.py |    9 ++++-----
>  python/ovs/db/schema.py |   10 +++++-----
>  python/ovs/db/types.py  |    6 +++---
>  python/ovs/ovsuuid.py   |    5 +++--
>  5 files changed, 17 insertions(+), 16 deletions(-)
>
> diff --git a/python/ovs/db/data.py b/python/ovs/db/data.py
> index 334c261..f71def9 100644
> --- a/python/ovs/db/data.py
> +++ b/python/ovs/db/data.py
> @@ -288,7 +288,8 @@ class Datum(object):
>             else:
>                 class_ = "set"
>
> -            inner = ovs.db.parser.unwrap_json(json, class_, list)
> +            inner = ovs.db.parser.unwrap_json(json, class_, [list, tuple],
> +                                              "array")
>             n = len(inner)
>             if n < type_.n_min or n > type_.n_max:
>                 raise error.Error("%s must have %d to %d members but %d are "
> diff --git a/python/ovs/db/parser.py b/python/ovs/db/parser.py
> index 173922b..7f0a6f0 100644
> --- a/python/ovs/db/parser.py
> +++ b/python/ovs/db/parser.py
> @@ -89,11 +89,10 @@ def json_type_to_string(type_):
>     else:
>         return "<invalid>"
>
> -def unwrap_json(json, name, need_type):
> -    if (type(json) != list or len(json) != 2 or json[0] != name or
> -        type(json[1]) != need_type):
> -        raise error.Error('expected ["%s", <%s>]'
> -                          % (name, json_type_to_string(need_type)), json)
> +def unwrap_json(json, name, types, desc):
> +    if (type(json) not in (list, tuple) or len(json) != 2 or json[0] != name or
> +        type(json[1]) not in types):
> +        raise error.Error('expected ["%s", <%s>]' % (name, desc), json)
>     return json[1]
>
>  def parse_json_pair(json):
> diff --git a/python/ovs/db/schema.py b/python/ovs/db/schema.py
> index 65ddca6..1c474a8 100644
> --- a/python/ovs/db/schema.py
> +++ b/python/ovs/db/schema.py
> @@ -64,8 +64,8 @@ class DbSchema(object):
>     def from_json(json):
>         parser = ovs.db.parser.Parser(json, "database schema")
>         name = parser.get("name", ['id'])
> -        version = parser.get_optional("version", [unicode])
> -        parser.get_optional("cksum", [unicode])
> +        version = parser.get_optional("version", [str, unicode])
> +        parser.get_optional("cksum", [str, unicode])
>         tablesJson = parser.get("tables", [dict])
>         parser.finish()
>
> @@ -125,8 +125,8 @@ class IdlSchema(DbSchema):
>     @staticmethod
>     def from_json(json):
>         parser = ovs.db.parser.Parser(json, "IDL schema")
> -        idlPrefix = parser.get("idlPrefix", [unicode])
> -        idlHeader = parser.get("idlHeader", [unicode])
> +        idlPrefix = parser.get("idlPrefix", [str, unicode])
> +        idlHeader = parser.get("idlHeader", [str, unicode])
>
>         subjson = dict(json)
>         del subjson["idlPrefix"]
> @@ -249,7 +249,7 @@ class ColumnSchema(object):
>         parser = ovs.db.parser.Parser(json, "schema for column %s" % name)
>         mutable = parser.get_optional("mutable", [bool], True)
>         ephemeral = parser.get_optional("ephemeral", [bool], False)
> -        type_ = types.Type.from_json(parser.get("type", [dict, unicode]))
> +        type_ = types.Type.from_json(parser.get("type", [dict, str, unicode]))
>         parser.finish()
>
>         return ColumnSchema(name, mutable, not ephemeral, type_)
> diff --git a/python/ovs/db/types.py b/python/ovs/db/types.py
> index 08dfa0a..72ab410 100644
> --- a/python/ovs/db/types.py
> +++ b/python/ovs/db/types.py
> @@ -150,7 +150,7 @@ class BaseType(object):
>
>     @staticmethod
>     def from_json(json):
> -        if type(json) == unicode:
> +        if type(json) in [str, unicode]:
>             return BaseType(AtomicType.from_json(json))
>
>         parser = ovs.db.parser.Parser(json, "ovsdb type")
> @@ -437,8 +437,8 @@ class Type(object):
>             return Type(BaseType.from_json(json))
>
>         parser = ovs.db.parser.Parser(json, "ovsdb type")
> -        key_json = parser.get("key", [dict, unicode])
> -        value_json = parser.get_optional("value", [dict, unicode])
> +        key_json = parser.get("key", [dict, str, unicode])
> +        value_json = parser.get_optional("value", [dict, str, unicode])
>         min_json = parser.get_optional("min", [int])
>         max_json = parser.get_optional("max", [int, str, unicode])
>         parser.finish()
> diff --git a/python/ovs/ovsuuid.py b/python/ovs/ovsuuid.py
> index 0776dd5..9bd1392 100644
> --- a/python/ovs/ovsuuid.py
> +++ b/python/ovs/ovsuuid.py
> @@ -37,7 +37,7 @@ def from_string(s):
>
>  def from_json(json, symtab=None):
>     try:
> -        s = ovs.db.parser.unwrap_json(json, "uuid", unicode)
> +        s = ovs.db.parser.unwrap_json(json, "uuid", [str, unicode], "string")
>         if not uuidRE.match(s):
>             raise error.Error("\"%s\" is not a valid UUID" % s, json)
>         return uuid.UUID(s)
> @@ -45,7 +45,8 @@ def from_json(json, symtab=None):
>         if not symtab:
>             raise e
>         try:
> -            name = ovs.db.parser.unwrap_json(json, "named-uuid", unicode)
> +            name = ovs.db.parser.unwrap_json(json, "named-uuid",
> +                                             [str, unicode], "string")
>         except error.Error:
>             raise e
>
> --
> 1.7.4.4
>
> _______________________________________________
> dev mailing list
> dev at openvswitch.org
> http://openvswitch.org/mailman/listinfo/dev
>



More information about the dev mailing list