<s> from __future__ import print_function <EOL> import argparse <EOL> import functools <EOL> import itertools <EOL> import json <EOL> import os <EOL> import subprocess <EOL> import sys <EOL> import re <EOL> ST_NONE = <NUM_LIT:1> <EOL> ST_BOOL = <NUM_LIT:2> <EOL> ST_INT = <NUM_LIT:3> <EOL> ST_STR = <NUM_LIT:4> <EOL> ST_FLOAT = <NUM_LIT:5> <EOL> ST_URL = <NUM_LIT:6> <EOL> ST_ANYOBJECT = <NUM_LIT> <EOL> def mergeIntoSingleObject ( guessedObjectArray ) : <EOL> keys = reduce ( lambda a , e : a . union ( set ( e . keys ( ) ) ) , guessedObjectArray , set ( ) ) <EOL> merged = { } <EOL> for obj in guessedObjectArray : <EOL> for key in keys : <EOL> value = obj . get ( key , set ( [ ST_NONE ] ) ) <EOL> if type ( value ) == dict : <EOL> merged [ key ] = value <EOL> elif type ( value ) == list : <EOL> merged [ key ] = mergeIntoSingleObject ( value ) <EOL> elif key in merged : <EOL> merged [ key ] = merged [ key ] . union ( value ) <EOL> else : <EOL> merged [ key ] = value <EOL> return [ merged ] <EOL> def guessTypenameForArray ( json ) : <EOL> arr = [ ] <EOL> for value in json : <EOL> if type ( value ) == dict : <EOL> arr . append ( guessTypenameForDict ( value ) ) <EOL> if all ( [ type ( i ) == dict for i in arr ] ) : <EOL> return mergeIntoSingleObject ( arr ) <EOL> assert ( False ) <EOL> return arr <EOL> def guessTypenameForDict ( json ) : <EOL> info = { } <EOL> for key , value in json . iteritems ( ) : <EOL> if type ( value ) == dict : <EOL> info [ key ] = guessTypenameForDict ( value ) <EOL> elif type ( value ) == list : <EOL> info [ key ] = guessTypenameForArray ( value ) <EOL> else : <EOL> info [ key ] = set ( [ guessTypename ( value ) ] ) <EOL> return info <EOL> def toCamelCase ( name , firstLetterUpper = True ) : <EOL> ts = re . split ( '<STR_LIT>' , name ) <EOL> if len ( ts ) > <NUM_LIT:1> : <EOL> ret = '<STR_LIT>' . join ( [ e . capitalize ( ) for e in ts ] ) <EOL> else : <EOL> ret = name <EOL> return ( unicode . upper if firstLetterUpper else unicode . lower ) ( ret [ <NUM_LIT:0> ] ) + ret [ <NUM_LIT:1> : ] <EOL> def guessTypename ( v ) : <EOL> if type ( v ) == type ( None ) : return ST_NONE <EOL> typemap = { <EOL> bool : ST_BOOL , <EOL> int : ST_INT , <EOL> str : ST_STR , <EOL> unicode : ST_STR , <EOL> float : ST_FLOAT <EOL> } <EOL> typename = typemap . get ( type ( v ) , ST_NONE ) <EOL> if typename == ST_STR : <EOL> if v . find ( '<STR_LIT>' ) == <NUM_LIT:0> or v . find ( '<STR_LIT>' ) == <NUM_LIT:0> : <EOL> if v . find ( '<STR_LIT:{>' ) == - <NUM_LIT:1> and v . find ( '<STR_LIT:{>' ) == - <NUM_LIT:1> : <EOL> return ST_URL <EOL> return typename <EOL> def guessTypenameFromSet ( ts ) : <EOL> assert ( len ( ts ) > <NUM_LIT:0> ) <EOL> isOptional = ST_NONE in ts <EOL> if isOptional : <EOL> ts . remove ( ST_NONE ) <EOL> if len ( ts ) == <NUM_LIT:0> : return '<STR_LIT>' <EOL> typemap = { <EOL> ST_BOOL : '<STR_LIT>' , <EOL> ST_INT : '<STR_LIT>' , <EOL> ST_STR : '<STR_LIT>' , <EOL> ST_FLOAT : '<STR_LIT>' , <EOL> ST_URL : '<STR_LIT>' , <EOL> ST_ANYOBJECT : '<STR_LIT>' <EOL> } <EOL> if len ( ts ) == <NUM_LIT:1> : <EOL> s = list ( ts ) [ <NUM_LIT:0> ] <EOL> else : <EOL> if ST_STR in ts : <EOL> s = ST_STR <EOL> else : <EOL> s = ST_ANYOBJECT <EOL> ret = typemap . get ( s , None ) <EOL> if ret == None : return '<STR_LIT>' <EOL> return ret + ( '<STR_LIT:?>' if isOptional else '<STR_LIT>' ) <EOL> def printClass ( info , name , level = <NUM_LIT:0> ) : <EOL> subdicts = [ ] <EOL> print ( '<STR_LIT:\t>' * level + '<STR_LIT>' % name ) <EOL> for key , value in info . iteritems ( ) : <EOL> fieldName = toCamelCase ( key , False ) <EOL> comment = '<STR_LIT>' if fieldName == key else '<STR_LIT>' % key <EOL> typename = toCamelCase ( key ) <EOL> if type ( value ) == dict : <EOL> subdicts . append ( ( value , typename ) ) <EOL> elif type ( value ) == list : <EOL> assert ( len ( value ) == <NUM_LIT:1> ) <EOL> subdicts . append ( ( value [ <NUM_LIT:0> ] , typename ) ) <EOL> typename = '<STR_LIT:[>' + typename + '<STR_LIT:]>' <EOL> else : <EOL> typename = guessTypenameFromSet ( value ) <EOL> print ( '<STR_LIT:\t>' * ( level + <NUM_LIT:1> ) + '<STR_LIT>' % ( fieldName , typename , comment ) ) <EOL> if len ( subdicts ) > <NUM_LIT:0> : <EOL> print ( ) <EOL> map ( lambda e : printClass ( e [ <NUM_LIT:0> ] , e [ <NUM_LIT:1> ] , level + <NUM_LIT:1> ) , subdicts ) <EOL> print ( '<STR_LIT:\t>' * level + '<STR_LIT:}>' ) <EOL> def parseArgs ( ) : <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , type = argparse . FileType ( '<STR_LIT:r>' ) , nargs = '<STR_LIT:?>' , help = '<STR_LIT>' , default = sys . stdin ) <EOL> parser . add_argument ( '<STR_LIT:-c>' , '<STR_LIT>' , type = str , default = None , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , default = None , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , help = '<STR_LIT>' ) <EOL> return parser . parse_args ( ) <EOL> def resolveStructName ( args ) : <EOL> cname = args . classname <EOL> if cname : <EOL> return toCamelCase ( cname ) <EOL> name = args . jsonfile . name <EOL> if name == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> name , ext = os . path . splitext ( os . path . basename ( name ) ) <EOL> return toCamelCase ( name ) <EOL> def execute ( ) : <EOL> args = parseArgs ( ) <EOL> obj = json . loads ( args . jsonfile . read ( ) ) <EOL> args . jsonfile . close ( ) <EOL> info = guessTypenameForDict ( obj ) <EOL> typename = resolveStructName ( args ) <EOL> if args . apikit : <EOL> param = args . parameter <EOL> if not param : <EOL> param = '<STR_LIT:U+002C>' + ( args . classname or typename ) <EOL> print ( '<STR_LIT>' % ( typename , param ) ) <EOL> print ( '<STR_LIT>' % ( typename , ) ) <EOL> print ( '<STR_LIT>' ) <EOL> printClass ( info , typename + '<STR_LIT>' , <NUM_LIT:1> ) <EOL> print ( '<STR_LIT:}>' ) <EOL> else : <EOL> printClass ( info , typename ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> execute ( ) </s>
