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
25 if type(json) in [str, unicode]:
26 print("error: %s" % json)
29 ovs.json.to_stream(json, sys.stdout)
30 sys.stdout.write("\n")
34 def parse_multiple(stream):
35 buf = stream.read(4096)
39 if parser is None and buf[0] in " \t\r\n":
43 parser = ovs.json.Parser()
47 if not print_json(parser.finish()):
51 buf = stream.read(4096)
52 if parser and not print_json(parser.finish()):
60 # Make stdout and stderr UTF-8, even if they are redirected to a file.
61 sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
62 sys.stderr = codecs.getwriter("utf-8")(sys.stderr)
65 options, args = getopt.gnu_getopt(argv[1:], '', ['multiple'])
66 except getopt.GetoptError as geo:
67 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
71 for key, value in options:
72 if key == '--multiple':
75 sys.stderr.write("%s: unhandled option %s\n" % (argv0, key))
79 sys.stderr.write("usage: %s [--multiple] INPUT.json\n" % argv0)
86 stream = open(input_file, "r")
89 ok = parse_multiple(stream)
91 ok = print_json(ovs.json.from_stream(stream))
97 if __name__ == '__main__':