1 # Copyright (c) 2009, 2010 Nicira Networks.
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.
22 if type(json) in [str, unicode]:
23 print "error: %s" % json
26 ovs.json.to_stream(json, sys.stdout)
27 sys.stdout.write("\n")
30 def parse_multiple(stream):
31 buf = stream.read(4096)
35 if parser is None and buf[0] in " \t\r\n":
39 parser = ovs.json.Parser()
43 if not print_json(parser.finish()):
47 buf = stream.read(4096)
48 if parser and not print_json(parser.finish()):
55 # Make stdout and stderr UTF-8, even if they are redirected to a file.
56 sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
57 sys.stderr = codecs.getwriter("utf-8")(sys.stderr)
60 options, args = getopt.gnu_getopt(argv[1:], '', ['multiple'])
61 except getopt.GetoptError, geo:
62 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
66 for key, value in options:
67 if key == '--multiple':
70 sys.stderr.write("%s: unhandled option %s\n" % (argv0, key))
74 sys.stderr.write("usage: %s [--multiple] INPUT.json\n" % argv0)
81 stream = open(input_file, "r")
84 ok = parse_multiple(stream)
86 ok = print_json(ovs.json.from_stream(stream))
91 if __name__ == '__main__':