]>
git.proxmox.com Git - ovs.git/blob - tests/test-json.py
1 # Copyright (c) 2009, 2010 Nicira, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from __future__
import print_function
27 if isinstance(json
, six
.string_types
):
28 print("error: %s" % json
)
31 ovs
.json
.to_stream(json
, sys
.stdout
)
32 sys
.stdout
.write("\n")
36 def parse_multiple(stream
):
37 buf
= stream
.read(4096)
41 if parser
is None and buf
[0] in " \t\r\n":
45 parser
= ovs
.json
.Parser()
49 if not print_json(parser
.finish()):
53 buf
= stream
.read(4096)
54 if parser
and not print_json(parser
.finish()):
62 # When this is used with Python 3, the program produces no output.
64 # Make stdout and stderr UTF-8, even if they are redirected to a file.
65 sys
.stdout
= codecs
.getwriter("utf-8")(sys
.stdout
)
66 sys
.stderr
= codecs
.getwriter("utf-8")(sys
.stderr
)
69 options
, args
= getopt
.gnu_getopt(argv
[1:], '', ['multiple'])
70 except getopt
.GetoptError
as geo
:
71 sys
.stderr
.write("%s: %s\n" % (argv0
, geo
.msg
))
75 for key
, value
in options
:
76 if key
== '--multiple':
79 sys
.stderr
.write("%s: unhandled option %s\n" % (argv0
, key
))
83 sys
.stderr
.write("usage: %s [--multiple] INPUT.json\n" % argv0
)
90 stream
= open(input_file
, "r")
93 ok
= parse_multiple(stream
)
95 ok
= print_json(ovs
.json
.from_stream(stream
))
101 if __name__
== '__main__':