exportoo/renderers.py

46 lines
1.1 KiB
Python
Raw Normal View History

2022-01-16 18:41:00 +01:00
from __future__ import print_function
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 ):
def render( opts, model, list ): # +format=json,xml,csv +json +tsv
if ( hasattr(opts, 'json') and opts.json ) or ( opts.format and 'json' in opts.format ):
render_json( model, list )
if ( hasattr(opts, 'tsv') and opts.tsv ) or ( opts.format and 'tsv' in opts.format ):
render_tsv( model, list )
if ( hasattr(opts, 'csv') and opts.csv ) or ( opts.format and 'csv' in opts.format ):
render_csv( model, list )
# if ( hasattr(opts, 'xml') and opts.xml ) or ( opts.format and 'xml' in opts.format ):
# render_xml( model, list )