<s> import logging <EOL> import simplejson as json <EOL> from optparse import OptionParser , TitledHelpFormatter <EOL> from fnmatch import fnmatch <EOL> from turbulenz_tools . utils . disassembler import Json2htmlRenderer , Json2txtRenderer , Json2txtColourRenderer , Disassembler <EOL> from turbulenz_tools . tools . stdtool import simple_options <EOL> __version__ = '<STR_LIT>' <EOL> __dependencies__ = [ '<STR_LIT>' ] <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def _parser ( ) : <EOL> parser = OptionParser ( description = '<STR_LIT>' , <EOL> usage = '<STR_LIT>' , <EOL> formatter = TitledHelpFormatter ( ) ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT:input>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT:3> , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT:-c>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT:3> , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT:path>" , type = "<STR_LIT:str>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , default = <NUM_LIT:2> , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT:html>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> return parser <EOL> def main ( ) : <EOL> ( options , args_ , parser_ ) = simple_options ( _parser , __version__ , __dependencies__ ) <EOL> source_file = options . input <EOL> LOG . info ( '<STR_LIT>' , __file__ , source_file ) <EOL> LOG . info ( '<STR_LIT>' , source_file ) <EOL> try : <EOL> with open ( source_file , '<STR_LIT:r>' ) as source : <EOL> json_asset = json . load ( source ) <EOL> def find_node ( nodes , sub_asset ) : <EOL> for ( k , v ) in sub_asset . iteritems ( ) : <EOL> if fnmatch ( k , nodes [ <NUM_LIT:0> ] ) : <EOL> if len ( nodes ) == <NUM_LIT:1> : <EOL> yield ( k , sub_asset [ k ] ) <EOL> elif isinstance ( v , dict ) : <EOL> for n in find_node ( nodes [ <NUM_LIT:1> : ] , sub_asset [ k ] ) : <EOL> yield n <EOL> for n in find_node ( nodes , sub_asset [ k ] ) : <EOL> yield n <EOL> if options . path : <EOL> node_list = options . path . split ( '<STR_LIT:/>' ) <EOL> if options . html : <EOL> renderer = Json2htmlRenderer ( ) <EOL> elif options . color : <EOL> renderer = Json2txtColourRenderer ( ) <EOL> else : <EOL> renderer = Json2txtRenderer ( ) <EOL> disassembler = Disassembler ( renderer , options . listcull , options . dictcull , options . depth ) <EOL> expand = True <EOL> if options . output : <EOL> with open ( options . output , '<STR_LIT:w>' ) as target : <EOL> if options . path : <EOL> for name , node in find_node ( node_list , json_asset ) : <EOL> target . write ( name + '<STR_LIT>' + disassembler . mark_up_asset ( { name : node } , expand ) ) <EOL> target . write ( '<STR_LIT:\n>' ) <EOL> else : <EOL> target . write ( disassembler . mark_up_asset ( { '<STR_LIT:root>' : json_asset } , expand ) ) <EOL> target . write ( '<STR_LIT:\n>' ) <EOL> elif options . color : <EOL> if options . path : <EOL> for name , node in find_node ( node_list , json_asset ) : <EOL> print '<STR_LIT>' + name + '<STR_LIT>' + '<STR_LIT>' + disassembler . mark_up_asset ( { name : node } , expand ) , <EOL> else : <EOL> print disassembler . mark_up_asset ( { '<STR_LIT:root>' : json_asset } , expand ) <EOL> else : <EOL> if options . path : <EOL> for name , node in find_node ( node_list , json_asset ) : <EOL> print name + '<STR_LIT>' + disassembler . mark_up_asset ( { name : node } , expand ) , <EOL> else : <EOL> print disassembler . mark_up_asset ( { '<STR_LIT:root>' : json_asset } , expand ) <EOL> except IOError as e : <EOL> LOG . error ( e ) <EOL> return e . errno <EOL> except Exception as e : <EOL> LOG . critical ( '<STR_LIT>' , e ) <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> exit ( main ( ) ) </s>
