/usr/lib/python2.7/dist-packages/os_doc_tools/jsoncheck.py is in python-openstack-doc-tools 0.34.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | #!/usr/bin/env python
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
"""
Usage:
jsoncheck.py [-f] FILES
Checks JSON syntax and optionally reformats (pretty-prints) the valid files.
Optional:
- demjson Python library (better diagnostics for invalid JSON synax)
"""
from __future__ import print_function
import argparse
import collections
import json
import sys
import textwrap
try:
import demjson
except ImportError:
demjson = None
sys.stderr.write("Cannot import the demjson Python module. Diagnostics "
"for invalid JSON files\nwill be limited.\n")
# -----------------------------------------------------------------------------
# Public interface
# -----------------------------------------------------------------------------
def check_syntax(path):
"""Check syntax of one JSON file."""
_process_file(path)
def check_formatting(path):
"""Check formatting of one JSON file."""
_process_file(path, formatting='check')
def fix_formatting(path, verbose=False):
"""Fix formatting of one JSON file."""
_process_file(path, formatting='fix', verbose=verbose)
# -----------------------------------------------------------------------------
# Implementation details
# -----------------------------------------------------------------------------
def _indent_note(note):
"""Indents and wraps a string."""
indented_note = []
# Split into single lines in case the argument is pre-formatted.
for line in note.splitlines():
indented_note.append(textwrap.fill(line, initial_indent=4 * ' ',
subsequent_indent=12 * ' ',
width=80))
return "\n".join(indented_note)
def _get_demjson_diagnostics(raw):
"""Get diagnostics string for invalid JSON files from demjson."""
errstr = None
try:
demjson.decode(raw, strict=True)
except demjson.JSONError as err:
errstr = err.pretty_description()
return errstr
class ParserException(Exception):
pass
def _parse_json(raw):
"""Parse raw JSON file."""
try:
parsed = json.loads(raw, object_pairs_hook=collections.OrderedDict)
except ValueError as err:
note = str(err)
# if demjson is available, print its diagnostic string as well
if demjson:
demerr = _get_demjson_diagnostics(raw)
if demerr:
note += "\n" + demerr
raise ParserException(note)
else:
return parsed
def _format_parsed_json(parsed):
"""Pretty-print JSON file content while retaining key order."""
return json.dumps(parsed, sort_keys=False, separators=(',', ': '),
indent=4) + "\n"
def _process_file(path, formatting=None, verbose=False):
"""Check syntax/formatting and fix formatting of a JSON file.
:param formatting: one of 'check' or 'fix' (default: None)
Raises ValueError if JSON syntax is invalid or reformatting needed.
"""
with open(path, 'r') as infile:
raw = infile.read()
try:
parsed = _parse_json(raw)
except ParserException as err:
raise ValueError(err)
else:
if formatting in ('check', 'fix'):
formatted = _format_parsed_json(parsed)
if formatted != raw:
if formatting == "fix":
with open(path, 'w') as outfile:
outfile.write(formatted)
if verbose:
print("%s\n%s" % (path,
_indent_note("Reformatted")))
else:
raise ValueError("Reformatting needed")
elif formatting is not None:
# for the benefit of external callers
raise ValueError("Called with invalid formatting value.")
def main():
parser = argparse.ArgumentParser(description="Validate and reformat JSON"
"files.")
parser.add_argument('files', metavar='FILES', nargs='+')
parser.add_argument('-f', '--formatting', choices=['check', 'fix'],
help='check or fix formatting of JSON files')
args = parser.parse_args()
exit_status = 0
for path in args.files:
try:
_process_file(path, args.formatting, verbose=True)
except ValueError as err:
print("%s\n%s" % (path, _indent_note(str(err))))
exit_status = 1
return exit_status
if __name__ == "__main__":
sys.exit(main())
|