2012-05-02 15:21:36 -07:00
|
|
|
# Copyright (c) 2009, 2010 Nicira, Inc.
|
2011-09-23 23:43:12 -07:00
|
|
|
#
|
2010-08-25 10:26:40 -07:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at:
|
2011-09-23 23:43:12 -07:00
|
|
|
#
|
2010-08-25 10:26:40 -07:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2011-09-23 23:43:12 -07:00
|
|
|
#
|
2010-08-25 10:26:40 -07:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2015-12-14 10:21:53 -05:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
import codecs
|
|
|
|
import getopt
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import ovs.json
|
2015-12-14 17:01:11 -05:00
|
|
|
import six
|
2010-08-25 10:26:40 -07:00
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def print_json(json):
|
2015-12-14 17:01:11 -05:00
|
|
|
if isinstance(json, six.string_types):
|
2015-12-14 10:21:53 -05:00
|
|
|
print("error: %s" % json)
|
2010-08-25 10:26:40 -07:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
ovs.json.to_stream(json, sys.stdout)
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
return True
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def parse_multiple(stream):
|
|
|
|
buf = stream.read(4096)
|
|
|
|
ok = True
|
|
|
|
parser = None
|
|
|
|
while len(buf):
|
|
|
|
if parser is None and buf[0] in " \t\r\n":
|
|
|
|
buf = buf[1:]
|
|
|
|
else:
|
|
|
|
if parser is None:
|
|
|
|
parser = ovs.json.Parser()
|
|
|
|
n = parser.feed(buf)
|
|
|
|
buf = buf[n:]
|
|
|
|
if len(buf):
|
|
|
|
if not print_json(parser.finish()):
|
|
|
|
ok = False
|
|
|
|
parser = None
|
|
|
|
if len(buf) == 0:
|
|
|
|
buf = stream.read(4096)
|
|
|
|
if parser and not print_json(parser.finish()):
|
|
|
|
ok = False
|
|
|
|
return ok
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def main(argv):
|
|
|
|
argv0 = argv[0]
|
|
|
|
|
2015-12-17 12:22:31 -05:00
|
|
|
# When this is used with Python 3, the program produces no output.
|
2016-04-14 09:23:27 -04:00
|
|
|
if six.PY2:
|
2015-12-17 12:22:31 -05:00
|
|
|
# Make stdout and stderr UTF-8, even if they are redirected to a file.
|
|
|
|
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
|
|
|
|
sys.stderr = codecs.getwriter("utf-8")(sys.stderr)
|
2010-08-25 10:26:40 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
options, args = getopt.gnu_getopt(argv[1:], '', ['multiple'])
|
2016-01-06 13:48:16 -05:00
|
|
|
except getopt.GetoptError as geo:
|
2010-08-25 10:26:40 -07:00
|
|
|
sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
multiple = False
|
|
|
|
for key, value in options:
|
|
|
|
if key == '--multiple':
|
|
|
|
multiple = True
|
|
|
|
else:
|
|
|
|
sys.stderr.write("%s: unhandled option %s\n" % (argv0, key))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if len(args) != 1:
|
|
|
|
sys.stderr.write("usage: %s [--multiple] INPUT.json\n" % argv0)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
input_file = args[0]
|
|
|
|
if input_file == "-":
|
|
|
|
stream = sys.stdin
|
|
|
|
else:
|
|
|
|
stream = open(input_file, "r")
|
|
|
|
|
|
|
|
if multiple:
|
|
|
|
ok = parse_multiple(stream)
|
|
|
|
else:
|
|
|
|
ok = print_json(ovs.json.from_stream(stream))
|
|
|
|
|
|
|
|
if not ok:
|
|
|
|
sys.exit(1)
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv)
|