<s> from __future__ import print_function <EOL> import argparse <EOL> import sys <EOL> from collections import OrderedDict <EOL> import simplejson <EOL> def _get_pretty_format ( contents , indent , sort_keys = True ) : <EOL> return simplejson . dumps ( <EOL> simplejson . loads ( <EOL> contents , <EOL> object_pairs_hook = None if sort_keys else OrderedDict , <EOL> ) , <EOL> sort_keys = sort_keys , <EOL> indent = indent <EOL> ) + "<STR_LIT:\n>" <EOL> def _autofix ( filename , new_contents ) : <EOL> print ( "<STR_LIT>" . format ( filename ) ) <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( new_contents ) <EOL> def pretty_format_json ( argv = None ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> type = int , <EOL> default = <NUM_LIT:2> , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' , <EOL> ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:*>' , help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( argv ) <EOL> status = <NUM_LIT:0> <EOL> for json_file in args . filenames : <EOL> with open ( json_file ) as f : <EOL> contents = f . read ( ) <EOL> try : <EOL> pretty_contents = _get_pretty_format ( <EOL> contents , args . indent , sort_keys = not args . no_sort_keys , <EOL> ) <EOL> if contents != pretty_contents : <EOL> print ( "<STR_LIT>" . format ( json_file ) ) <EOL> if args . autofix : <EOL> _autofix ( json_file , pretty_contents ) <EOL> status = <NUM_LIT:1> <EOL> except simplejson . JSONDecodeError : <EOL> print ( <EOL> "<STR_LIT>" <EOL> . format ( json_file ) <EOL> ) <EOL> return <NUM_LIT:1> <EOL> return status <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( pretty_format_json ( ) ) </s>
