Looks Good.

I wonder if the str_recursive function should be incorporated into the
ovs.json module somehow.  I don't know enough about how it's used to
say.

Also, historically speaking, why do we have our own json
implementation?  Does the python one have some sort of deficiency?

Ethan

On Tue, May 3, 2011 at 10:17, Ben Pfaff <[email protected]> wrote:
> I can't figure out where JsonReader and JsonWriter come from.  I know that
> they must exist, because I (and others) have used ovsdbmonitor before, but
> I can't find them now.
>
> Switch to using ovs.json, which is part of Open vSwitch so we know that
> it exists.  At the same time, we have to start translating the Unicode
> strings that ovs.json outputs into standard Python strings; otherwise
> the "twisted conch" ssh implementation craps out because it tries to
> concatenate this Unicode string with a standard string that contains
> non-ASCII characters.
> ---
>  ovsdb/ovsdbmonitor/OVEConfig.py   |   26 +++++++++++++++++++++-----
>  ovsdb/ovsdbmonitor/OVEFetch.py    |    4 +++-
>  ovsdb/ovsdbmonitor/OVEStandard.py |    5 +++--
>  3 files changed, 27 insertions(+), 8 deletions(-)
>
> diff --git a/ovsdb/ovsdbmonitor/OVEConfig.py b/ovsdb/ovsdbmonitor/OVEConfig.py
> index 7cc18eb..4754c67 100644
> --- a/ovsdb/ovsdbmonitor/OVEConfig.py
> +++ b/ovsdb/ovsdbmonitor/OVEConfig.py
> @@ -1,3 +1,4 @@
> +# Copyright (c) 2011 Nicira Networks.
>  # Copyright (c) 2010 Citrix Systems, Inc.
>  #
>  # Licensed under the Apache License, Version 2.0 (the "License");
> @@ -14,6 +15,21 @@
>
>  from OVEStandard import *
>  from OVELogger import *
> +import ovs.json
> +
> +def str_recursive(x):
> +    t = type(x)
> +    if t == unicode:
> +        return str(x)
> +    elif t == list:
> +        return [str_recursive(_) for _ in x]
> +    elif t == dict:
> +        out = {}
> +        for k,v in x.iteritems():
> +            out[str_recursive(k)] = str_recursive(v)
> +        return out
> +    else:
> +        return x
>
>  class OVEConfig(QtCore.QObject):
>     instance = None
> @@ -40,21 +56,21 @@ class OVEConfig(QtCore.QObject):
>
>     def saveConfig(self):
>         settings = QtCore.QSettings()
> -        settings.setValue('config/hosts', 
> QVariant(json.JsonWriter().write(self.hosts)))
> +        settings.setValue('config/hosts', 
> QVariant(ovs.json.to_string((self.hosts))))
>         settings.setValue('config/logTraffic', QVariant(self.logTraffic))
>         settings.setValue('config/truncateUuids', 
> QVariant(self.truncateUuids))
> -        settings.setValue('config/ssgList', 
> QVariant(json.JsonWriter().write(self.ssgList)))
> +        settings.setValue('config/ssgList', 
> QVariant(ovs.json.to_string(self.ssgList)))
>         settings.sync()
>         self.emitUpdated()
> -
> +
>     def loadConfig(self):
>         settings = QtCore.QSettings()
>         jsonText = unicode(settings.value('config/hosts', 
> QVariant('[]')).toString())
> -        self.hosts = json.JsonReader().read(str(jsonText))
> +        self.hosts = str_recursive(ovs.json.from_string(str(jsonText)))
>         self.logTraffic = settings.value('config/logTraffic', 
> QVariant(False)).toBool()
>         self.truncateUuids = settings.value('config/truncateUuids', 
> QVariant(False)).toBool()
>         jsonText = unicode(settings.value('config/ssgList', 
> QVariant('[]')).toString())
> -        self.ssgList = json.JsonReader().read(str(jsonText))
> +        self.ssgList = ovs.json.from_string(str(jsonText))
>         if len(self.ssgList) == 0:
>             self.ssgList = [
>                 r'in_port0000',
> diff --git a/ovsdb/ovsdbmonitor/OVEFetch.py b/ovsdb/ovsdbmonitor/OVEFetch.py
> index 9dd1118..8bc5757 100644
> --- a/ovsdb/ovsdbmonitor/OVEFetch.py
> +++ b/ovsdb/ovsdbmonitor/OVEFetch.py
> @@ -1,3 +1,4 @@
> +# Copyright (c) 2011 Nicira Networks.
>  # Copyright (c) 2010 Citrix Systems, Inc.
>  #
>  # Licensed under the Apache License, Version 2.0 (the "License");
> @@ -15,6 +16,7 @@
>  from OVEStandard import *
>  from OVEConfig import *
>  from OVELogger import *
> +import ovs.json
>
>  # This sequence installs the qt4reactor before twisted gets a chance to 
> install its reactor
>  import qt4reactor
> @@ -166,7 +168,7 @@ class OVECommandChannel(channel.SSHChannel, 
> QtCore.QObject):
>                 if self.commandType == 'JSON':
>                     try:
>                         # Decode the JSON data, to confirm that we have all 
> of the data
> -                        self._jsonValues = json.read(str(self._data)) # 
> FIXME: Should handle unicode
> +                        self._jsonValues = 
> ovs.json.from_string(str(self._data)) # FIXME: Should handle unicode
>                         self.sendResult()
>                     except:
>                         pass # Wait for more data
> diff --git a/ovsdb/ovsdbmonitor/OVEStandard.py 
> b/ovsdb/ovsdbmonitor/OVEStandard.py
> index b9bc419..23b3e66 100644
> --- a/ovsdb/ovsdbmonitor/OVEStandard.py
> +++ b/ovsdb/ovsdbmonitor/OVEStandard.py
> @@ -1,3 +1,4 @@
> +# Copyright (c) 2011 Nicira Networks.
>  # Copyright (c) 2010 Citrix Systems, Inc.
>  #
>  # Licensed under the Apache License, Version 2.0 (the "License");
> @@ -20,9 +21,9 @@ from pprint import pprint
>  globalForcePySide = False
>
>  try:
> -    import json
> +    import ovs.json
>  except Exception, e:
> -    print('+++ Python JSON module is required\n')
> +    print('+++ OVS JSON module is required\n')
>     raise
>
>  try:
> --
> 1.7.4.4
>
> _______________________________________________
> dev mailing list
> [email protected]
> http://openvswitch.org/mailman/listinfo/dev
>
_______________________________________________
dev mailing list
[email protected]
http://openvswitch.org/mailman/listinfo/dev

Reply via email to