<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import argparse <EOL> import collections <EOL> import json <EOL> import sys <EOL> import textwrap <EOL> try : <EOL> import demjson <EOL> except ImportError : <EOL> demjson = None <EOL> sys . stderr . write ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def check_syntax ( path ) : <EOL> """<STR_LIT>""" <EOL> _process_file ( path ) <EOL> def check_formatting ( path ) : <EOL> """<STR_LIT>""" <EOL> _process_file ( path , formatting = '<STR_LIT>' ) <EOL> def fix_formatting ( path , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> _process_file ( path , formatting = '<STR_LIT>' , verbose = verbose ) <EOL> def _indent_note ( note ) : <EOL> """<STR_LIT>""" <EOL> indented_note = [ ] <EOL> for line in note . splitlines ( ) : <EOL> indented_note . append ( textwrap . fill ( line , initial_indent = <NUM_LIT:4> * '<STR_LIT:U+0020>' , <EOL> subsequent_indent = <NUM_LIT:12> * '<STR_LIT:U+0020>' , <EOL> width = <NUM_LIT> ) ) <EOL> return "<STR_LIT:\n>" . join ( indented_note ) <EOL> def _get_demjson_diagnostics ( raw ) : <EOL> """<STR_LIT>""" <EOL> errstr = None <EOL> try : <EOL> demjson . decode ( raw , strict = True ) <EOL> except demjson . JSONError as err : <EOL> errstr = err . pretty_description ( ) <EOL> return errstr <EOL> class ParserException ( Exception ) : <EOL> pass <EOL> def _parse_json ( raw ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> parsed = json . loads ( raw , object_pairs_hook = collections . OrderedDict ) <EOL> except ValueError as err : <EOL> note = str ( err ) <EOL> if demjson : <EOL> demerr = _get_demjson_diagnostics ( raw ) <EOL> if demerr : <EOL> note += "<STR_LIT:\n>" + demerr <EOL> raise ParserException ( note ) <EOL> else : <EOL> return parsed <EOL> def _format_parsed_json ( parsed ) : <EOL> """<STR_LIT>""" <EOL> return json . dumps ( parsed , sort_keys = False , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) , <EOL> indent = <NUM_LIT:4> ) + "<STR_LIT:\n>" <EOL> def _process_file ( path , formatting = None , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> with open ( path , '<STR_LIT:r>' ) as infile : <EOL> raw = infile . read ( ) <EOL> try : <EOL> parsed = _parse_json ( raw ) <EOL> except ParserException as err : <EOL> raise ValueError ( err ) <EOL> else : <EOL> if formatting in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> formatted = _format_parsed_json ( parsed ) <EOL> if formatted != raw : <EOL> if formatting == "<STR_LIT>" : <EOL> with open ( path , '<STR_LIT:w>' ) as outfile : <EOL> outfile . write ( formatted ) <EOL> if verbose : <EOL> print ( "<STR_LIT>" % ( path , <EOL> _indent_note ( "<STR_LIT>" ) ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> elif formatting is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , nargs = '<STR_LIT:+>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , choices = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> exit_status = <NUM_LIT:0> <EOL> for path in args . files : <EOL> try : <EOL> _process_file ( path , args . formatting , verbose = True ) <EOL> except ValueError as err : <EOL> print ( "<STR_LIT>" % ( path , _indent_note ( str ( err ) ) ) ) <EOL> exit_status = <NUM_LIT:1> <EOL> return exit_status <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( ) ) </s>
