from __future__ import print_function import json as JSON # TABLE VIEW COLUMNS cols = { '': [ 'id', 'name', 'create_uid' ], # default 'ir.model': [ 'id', 'model' ], 'ir.model.fields': [ 'id', 'name', 'ttype', 'create_uid', 'relation', 'display_name', 'complete_name' ], } def tsv( *fields ): # fields = [ '"' + str(f).replace('"','\\"') + '"' for f in fields ] fields = [ str(s).encode('utf-8') for s in fields ] print( *fields, sep='\t' ) def csv( *fields ): fields = [ '"' + str(f).replace('"','\\"') + '"' for f in fields ] fields = [ str(s).encode('utf-8') for s in fields ] print( *fields, sep=',' ) def json( obj ): print( JSON.dumps(obj, indent=4) ) #"\t") ) def render_tsv( model, list ): if model in cols: _cols = cols[model] else: _cols = cols[''] tsv( *_cols ) for obj in list: tsv( *[ obj[col] for col in _cols] ) def render_csv( model, list ): if model in cols: _cols = cols[model] else: _cols = cols[''] csv( *_cols ) for obj in list: csv( *[ obj[col] for col in _cols] ) def render_json( model, list ): json( list.read() ) #def render_json( model, obj ): def render( opts, model, list ): # +format=json,xml,csv +json +tsv if ( 'json' in opts and opts['json'] ) or ( 'format' in opts and 'json' in opts['format'] ): render_json( model, list ) elif ( 'tsv' in opts and opts['tsv'] ) or ( 'format' in opts and 'tsv' in opts['format'] ): render_tsv( model, list ) elif ( 'csv' in opts and opts['csv'] ) or ( 'format' in opts and 'csv' in opts['format'] ): render_csv( model, list ) # elif ( 'xml' in opts and opts['xml'] ) or ( 'format' in opts and 'xml' in opts['format'] ): # render_xml( model, list ) else: render_tsv( model, list )