Class: Aspera::Cli::Formater
- Inherits:
-
Object
- Object
- Aspera::Cli::Formater
- Defined in:
- lib/aspera/cli/formater.rb
Overview
Take care of output
Instance Attribute Summary collapse
-
#option_flat_hash ⇒ Object
Returns the value of attribute option_flat_hash.
Class Method Summary collapse
-
.flatten_name_value_list(hash) ⇒ Object
special for Aspera on Cloud display node => [{“name”=>“foo”,“value”=>“bar”]} will be expanded to : “bar”.
- .flatten_object(source, keep_last) ⇒ Object
-
.flatten_sub_hash_rec(source, keep_last, prefix, dest) ⇒ Object
recursive function to modify a hash.
Instance Method Summary collapse
-
#display_message(message_level, message) ⇒ Object
main output method.
-
#display_results(results) ⇒ Object
this method displays the results, especially the table format.
- #display_status(status) ⇒ Object
-
#initialize(opt_mgr) ⇒ Formater
constructor
A new instance of Formater.
- #result_all_fields(results, table_rows_hash_val) ⇒ Object
- #result_default_fields(results, table_rows_hash_val) ⇒ Object
Constructor Details
#initialize(opt_mgr) ⇒ Formater
Returns a new instance of Formater.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/aspera/cli/formater.rb', line 24 def initialize(opt_mgr) @option_flat_hash=true @opt_mgr=opt_mgr @opt_mgr.set_obj_attr(:flat_hash,self,:option_flat_hash) @opt_mgr.add_opt_list(:format,DISPLAY_FORMATS,"output format") @opt_mgr.add_opt_list(:display,DISPLAY_LEVELS,"output only some information") @opt_mgr.add_opt_simple(:fields,"comma separated list of fields, or #{FIELDS_ALL}, or #{FIELDS_DEFAULT}") @opt_mgr.add_opt_simple(:select,"select only some items in lists, extended value: hash (column, value)") @opt_mgr.add_opt_simple(:table_style,"table display style") @opt_mgr.add_opt_boolean(:flat_hash,"display hash values as additional keys") @opt_mgr.set_option(:format,:table) @opt_mgr.set_option(:display,:info) @opt_mgr.set_option(:fields,FIELDS_DEFAULT) @opt_mgr.set_option(:table_style,':.:') end |
Instance Attribute Details
#option_flat_hash ⇒ Object
Returns the value of attribute option_flat_hash.
22 23 24 |
# File 'lib/aspera/cli/formater.rb', line 22 def option_flat_hash @option_flat_hash end |
Class Method Details
.flatten_name_value_list(hash) ⇒ Object
special for Aspera on Cloud display node => [{“name”=>“foo”,“value”=>“bar”]} will be expanded to : “bar”
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/aspera/cli/formater.rb', line 86 def self.flatten_name_value_list(hash) hash.keys.each do |k| v=hash[k] if v.is_a?(Array) and v.map{|i|i.class}.uniq.eql?([Hash]) and v.map{|i|i.keys}.flatten.sort.uniq.eql?(["name", "value"]) v.each do |pair| hash["#{k}.#{pair["name"]}"]=pair["value"] end hash.delete(k) end end end |
.flatten_object(source, keep_last) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/aspera/cli/formater.rb', line 57 def self.flatten_object(source,keep_last) newval={} flatten_sub_hash_rec(source,keep_last,'',newval) source.clear source.merge!(newval) end |
.flatten_sub_hash_rec(source, keep_last, prefix, dest) ⇒ Object
recursive function to modify a hash
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/aspera/cli/formater.rb', line 69 def self.flatten_sub_hash_rec(source,keep_last,prefix,dest) #is_simple_hash=source.is_a?(Hash) and source.values.inject(true){|m,v| xxx=!v.respond_to?(:each) and m;puts("->#{xxx}>#{v.respond_to?(:each)} #{v}-");xxx} is_simple_hash=false Log.log.debug("(#{keep_last})[#{is_simple_hash}] -#{source.values}- \n-#{source}-") return source if keep_last and is_simple_hash source.each do |k,v| if v.is_a?(Hash) and ( !keep_last or !is_simple_hash ) flatten_sub_hash_rec(v,keep_last,prefix+k.to_s+'.',dest) else dest[prefix+k.to_s]=v end end return nil end |
Instance Method Details
#display_message(message_level, message) ⇒ Object
main output method
41 42 43 44 45 46 47 48 49 |
# File 'lib/aspera/cli/formater.rb', line 41 def (,) display_level=@opt_mgr.get_option(:display,:mandatory) case when :info; STDOUT.puts() if display_level.eql?(:info) when :data; STDOUT.puts() unless display_level.eql?(:error) when :error; STDERR.puts() else raise "wrong message_level:#{message_level}" end end |
#display_results(results) ⇒ Object
this method displays the results, especially the table format
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/aspera/cli/formater.rb', line 117 def display_results(results) raise "INTERNAL ERROR, result must be Hash (got: #{results.class}: #{results})" unless results.is_a?(Hash) raise "INTERNAL ERROR, result must have type" unless results.has_key?(:type) raise "INTERNAL ERROR, result must have data" unless results.has_key?(:data) or [:empty,:nothing].include?(results[:type]) res_data=results[:data] # comma separated list in string format user_asked_fields_list_str=@opt_mgr.get_option(:fields,:mandatory) display_format=@opt_mgr.get_option(:format,:mandatory) case display_format when :nagios Nagios.process(res_data) when :ruby (:data,PP.pp(res_data,'')) when :json (:data,JSON.generate(res_data)) when :jsonpp (:data,JSON.pretty_generate(res_data)) when :yaml (:data,res_data.to_yaml) when :table,:csv case results[:type] when :object_list # goes to table display raise "internal error: unexpected type: #{res_data.class}, expecting Array" unless res_data.is_a?(Array) # :object_list is an array of hash tables, where key=colum name table_rows_hash_val = res_data final_table_columns=nil if @option_flat_hash table_rows_hash_val.each do |obj| self.class.flatten_object(obj,results[:option_expand_last]) end end final_table_columns=case user_asked_fields_list_str when FIELDS_DEFAULT; result_default_fields(results,table_rows_hash_val) when FIELDS_ALL; result_all_fields(results,table_rows_hash_val) else if user_asked_fields_list_str.start_with?('+') result_default_fields(results,table_rows_hash_val).push(*user_asked_fields_list_str.gsub(/^\+/,'').split(',')) else user_asked_fields_list_str.split(',') end end when :single_object # goes to table display # :single_object is a simple hash table (can be nested) raise "internal error: expecting Hash: got #{res_data.class}: #{res_data}" unless res_data.is_a?(Hash) final_table_columns = results[:columns] || ['key','value'] if @option_flat_hash self.class.flatten_object(res_data,results[:option_expand_last]) self.class.flatten_name_value_list(res_data) end asked_fields=case user_asked_fields_list_str when FIELDS_DEFAULT; results[:fields]||res_data.keys when FIELDS_ALL; res_data.keys else user_asked_fields_list_str.split(',') end table_rows_hash_val=asked_fields.map { |i| { final_table_columns.first => i, final_table_columns.last => res_data[i] } } when :value_list # goes to table display # :value_list is a simple array of values, name of column provided in the :name final_table_columns = [results[:name]] table_rows_hash_val=res_data.map { |i| { results[:name] => i } } when :empty # no table (:info,'empty') return when :nothing # no result expected Log.log.debug("no result expected") return when :status # no table # :status displays a simple message (:info,res_data) return when :text # no table # :status displays a simple message (:data,res_data) return when :other_struct # no table # :other_struct is any other type of structure (:data,PP.pp(res_data,'')) return else raise "unknown data type: #{results[:type]}" end # here we expect: table_rows_hash_val and final_table_columns raise "no field specified" if final_table_columns.nil? if table_rows_hash_val.empty? (:info,'empty'.gray) unless display_format.eql?(:csv) return end # convert to string with special function. here table_rows_hash_val is an array of hash table_rows_hash_val=results[:textify].call(table_rows_hash_val) if results.has_key?(:textify) filter=@opt_mgr.get_option(:select,:optional) unless filter.nil? raise CliBadArgument,"expecting hash for select" unless filter.is_a?(Hash) filter.each{|k,v|table_rows_hash_val.select!{|i|i[k].eql?(v)}} end # convert data to string, and keep only display fields final_table_rows=table_rows_hash_val.map { |r| final_table_columns.map { |c| r[c].to_s } } # here : final_table_columns : list of column names # here: final_table_rows : array of list of value case display_format when :table style=@opt_mgr.get_option(:table_style,:mandatory).split('') # display the table ! #display_message(:data,Text::Table.new( #:head => final_table_columns, #:rows => final_table_rows, #:horizontal_boundary => style[0], #:vertical_boundary => style[1], #:boundary_intersection => style[2])) (:data,Terminal::Table.new( :headings => final_table_columns, :rows => final_table_rows, :border_x => style[0], :border_y => style[1], :border_i => style[2])) when :csv (:data,final_table_rows.map{|t| t.join(CSV_FIELD_SEPARATOR)}.join(CSV_RECORD_SEPARATOR)) end end end |
#display_status(status) ⇒ Object
51 52 53 |
# File 'lib/aspera/cli/formater.rb', line 51 def display_status(status) (:info,status) end |
#result_all_fields(results, table_rows_hash_val) ⇒ Object
110 111 112 113 114 |
# File 'lib/aspera/cli/formater.rb', line 110 def result_all_fields(results,table_rows_hash_val) raise "internal error: must be array" unless table_rows_hash_val.is_a?(Array) # get the list of all column names used in all lines, not just frst one, as all lines may have different columns return table_rows_hash_val.inject({}){|m,v|v.keys.each{|c|m[c]=true};m}.keys end |
#result_default_fields(results, table_rows_hash_val) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/aspera/cli/formater.rb', line 98 def result_default_fields(results,table_rows_hash_val) if results.has_key?(:fields) and !results[:fields].nil? final_table_columns=results[:fields] else if !table_rows_hash_val.empty? final_table_columns=table_rows_hash_val.first.keys else final_table_columns=['empty'] end end end |