@@ -20,54 +20,66 @@ def __iter__(self):
2020 return iter ([self .TABLE , self .CSV , self .JSON , self .RAW ])
2121
2222
23- def get_output_format_func (value ):
24- if value is not None :
25- value = value .upper ()
26- if value == OutputFormat .CSV :
27- return to_csv
28- if value == OutputFormat .RAW :
29- return to_json
30- if value == OutputFormat .TABLE :
31- return to_table
32- if value == OutputFormat .JSON :
33- return to_formatted_json
34- # default option
35- return to_table
36-
37-
38- def to_csv (output , header ):
23+ class OutputFormatter :
24+ def __init__ (self , output_format , header = None ):
25+ output_format = output_format .upper () if output_format else OutputFormat .TABLE
26+ self .output_format = output_format
27+ self ._format_func = to_table
28+ self .header = header
29+
30+ if output_format == OutputFormat .CSV :
31+ self ._format_func = to_csv
32+ elif output_format == OutputFormat .RAW :
33+ self ._format_func = to_json
34+ elif output_format == OutputFormat .TABLE :
35+ self ._format_func = self ._to_table
36+ elif output_format == OutputFormat .JSON :
37+ self ._format_func = to_formatted_json
38+
39+ def _format_output (self , output ):
40+ return self ._format_func (output )
41+
42+ def _to_table (self , output ):
43+ return to_table (output , self .header )
44+
45+ def get_formatted_output (self , output ):
46+ if self ._requires_list_output :
47+ yield self ._format_output (output )
48+ else :
49+ for item in output :
50+ yield self ._format_output (item )
51+
52+ @property
53+ def _requires_list_output (self ):
54+ return self .output_format in (OutputFormat .TABLE , OutputFormat .CSV )
55+
56+
57+ def to_csv (output ):
58+ """Output is a list of records"""
3959 if not output :
4060 return
4161 string_io = io .StringIO ()
42- writer = csv .DictWriter (string_io , fieldnames = output [0 ].keys ())
62+ fieldnames = list ({k for d in output for k in d .keys ()})
63+ writer = csv .DictWriter (string_io , fieldnames = fieldnames )
4364 writer .writeheader ()
4465 writer .writerows (output )
4566 return string_io .getvalue ()
4667
4768
4869def to_table (output , header ):
70+ """Output is a list of records"""
4971 if not output :
5072 return
51- header = header or get_dynamic_header (output [0 ])
5273 rows , column_size = find_format_width (output , header )
5374 return format_to_table (rows , column_size )
5475
5576
56- def _filter (output , header ):
57- return [{header [key ]: row [key ] for key in header .keys ()} for row in output ]
77+ def to_json (output ):
78+ """Output is a single record"""
79+ return "{}\n " .format (json .dumps (output ))
5880
5981
60- def to_json (output , header = None ):
61- return json .dumps (output )
62-
63-
64- def to_formatted_json (output , header = None ):
65- return json .dumps (_filter (output , header ), indent = 4 )
66-
67-
68- def get_dynamic_header (header_items ):
69- return {
70- key : key .capitalize ()
71- for key in header_items .keys ()
72- if type (header_items [key ]) == str
73- }
82+ def to_formatted_json (output ):
83+ """Output is a single record"""
84+ json_str = "{}\n " .format (json .dumps (output , indent = 4 ))
85+ return json_str
0 commit comments