2022-01-16 18:41:00 +01:00
|
|
|
from __future__ import print_function
|
2022-01-16 17:31:20 +01:00
|
|
|
|
|
|
|
def tsv( *fields ):
|
|
|
|
print( *fields, sep='\t' )
|
|
|
|
|
|
|
|
def csv( *fields ):
|
|
|
|
print( *[ '"' + f.replace('"','\\"') + '"' for f in fields ], sep=',' )
|
|
|
|
|
|
|
|
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 ):
|
|
|
|
jlog( list )
|
|
|
|
|
|
|
|
#def render_json( model, obj ):
|
2022-01-16 18:45:33 +01:00
|
|
|
def render( opts, model, list ): # +format=json,xml,csv +json +tsv
|
2022-01-16 18:50:32 +01:00
|
|
|
if ( hasattr(opts, 'json') and opts.json ) or ( opts.format and 'json' in opts.format ):
|
2022-01-16 17:31:20 +01:00
|
|
|
render_json( model, list )
|
2022-01-16 18:50:32 +01:00
|
|
|
if ( hasattr(opts, 'tsv') and opts.tsv ) or ( opts.format and 'tsv' in opts.format ):
|
2022-01-16 17:31:20 +01:00
|
|
|
render_tsv( model, list )
|
2022-01-16 18:50:32 +01:00
|
|
|
if ( hasattr(opts, 'csv') and opts.csv ) or ( opts.format and 'csv' in opts.format ):
|
2022-01-16 17:31:20 +01:00
|
|
|
render_csv( model, list )
|
2022-01-16 18:50:32 +01:00
|
|
|
# if ( hasattr(opts, 'xml') and opts.xml ) or ( opts.format and 'xml' in opts.format ):
|
2022-01-16 17:31:20 +01:00
|
|
|
# render_xml( model, list )
|