def json_format(filename, indent=DEFAULT_INDENT_SIZE, **kwargs):
 console = kwargs.get('console', logging.getLogger('console'))
 encoding = kwargs.get('encoding', None)
 dry_run = kwargs.get('dry_run', False)
 if (indent is None):
  sort_keys = False
 else:
  sort_keys = True
 message = ('%s ...' % filename)
 contents = open(filename, 'r').read()
 data = json.loads(contents, encoding=encoding)
 contents2 = json.dumps(data, indent=indent, sort_keys=sort_keys)
 contents2 = contents2.strip()
 contents2 = ('%s\n' % contents2)
 if (contents == contents2):
  console.info('%s SKIP (already pretty)', message)
  return 2
 elif (not dry_run):
  outfile = open(filename, 'w')
  outfile.write(contents2)
  outfile.close()
  console.warn('%s OK', message)
  return 1

def json_format ( filename , indent = DEFAULT_INDENT_SIZE , ** kwargs ) : console = kwargs . get ( 'console' , logging . getLogger ( 'console' ) ) encoding = kwargs . get ( 'encoding' , None ) dry_run = kwargs . get ( 'dry_run' , False ) if ( indent is None ) : sort_keys = False else : sort_keys = True message = ( '%s ...' % filename ) contents = open ( filename , 'r' ) . read ( ) data = json . loads ( contents , encoding = encoding ) contents2 = json . dumps ( data , indent = indent , sort_keys = sort_keys ) contents2 = contents2 . strip ( ) contents2 = ( '%s\n' % contents2 ) if ( contents == contents2 ) : console . info ( '%s SKIP (already pretty)' , message ) return 2 elif ( not dry_run ) : outfile = open ( filename , 'w' ) outfile . write ( contents2 ) outfile . close ( ) console . warn ( '%s OK' , message ) return 1
format/beautify a json file .