Class: SsExport
- Inherits:
-
Object
- Object
- SsExport
- Defined in:
- lib/spreadsheet_export_refi.rb
Constant Summary collapse
- @@special_col_labels =
['ACTIVE','INSTRUCTION','FILE','SHEET','EXPORT','PARAMS','IGNORE']
- @@dont_duplicate_from_super_map =
['INSTRUCTION','FILE','SHEET','IGNORE','PARAMS']
- @@replace_functions =
#placeholder_name_example#“ -a_function_name(a_minival_string)”
{ "nothing" => proc{|context,minival| next "" }, # file can only open files in subdirectories # TODO security stuff "read_file" => proc{|context,minival| if(minival[0].is_a?(String)) text = context.text_file_cache_manager.get_text(minival[0]) next text else dparn('W1001',"read_file - could not open file because minival was not a string") next nil end }, "current_spreadsheet_file_pathname" => proc{|context,minival| next context.ss_pn }, #"current_template_file_pathname" => proc{|context,minival| # next context.current_file_info[:pn] #}, "catch" => proc{|context,minival| catch_name = minival[0] if(catch_name.is_a?(String)) res = context.var_map[catch_name] if res==nil dput dparn 1012,"catch could not find '#{catch_name}' in var_map" res = '' end next res else dparn(1002,"catch - could not open file because minival was not a string") next nil end }, "minilang_eval" => proc{|context,minival| next context.minilang_env.run_str(minival[0]) }, }
Instance Attribute Summary collapse
-
#ss_pn ⇒ Object
readonly
Returns the value of attribute ss_pn.
-
#var_map ⇒ Object
readonly
Returns the value of attribute var_map.
Class Method Summary collapse
Instance Method Summary collapse
- #consume_after_last_row ⇒ Object
- #create_loop(l_var_name, ary) ⇒ Object
- #delegate_instruction(row_i) ⇒ Object
- #do_instruction(row_i) ⇒ Object
- #eval_mvar(col_label) ⇒ Object
- #eval_var(var) ⇒ Object
- #get_cell(row_i, col_i) ⇒ Object
- #get_instruction(row_i) ⇒ Object
- #get_params(row_i) ⇒ Object
- #get_row(row_i) ⇒ Object
-
#get_sheet_dims ⇒ Object
dpun.
-
#initialize(p = {}) ⇒ SsExport
constructor
A new instance of SsExport.
- #insert_instruction(row_i) ⇒ Object
- #is_row_active(row_i) ⇒ Object
- #process ⇒ Object
- #process_instruction(instruction_type, row_i) ⇒ Object
- #process_instructions ⇒ Object
- #process_row(row_i) ⇒ Object
- #process_rows_from(from) ⇒ Object
- #read_file(pn) ⇒ Object
- #read_headers ⇒ Object
- #select_instruction(row_i) ⇒ Object
-
#write(text) ⇒ Object
dpun.
Constructor Details
#initialize(p = {}) ⇒ SsExport
Returns a new instance of SsExport.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/spreadsheet_export_refi.rb', line 77 def initialize(p={}) @instructions = { "insert" => self.method(:insert_instruction), "delegate" => self.method(:delegate_instruction), "do" => self.method(:do_instruction), "select" => self.method(:select_instruction), } @ss_pn = p[:ss_pn] || nil @sheet_name = p[:sheet_name] || dperr(1008,'a sheetname must be specified') @output_file = p[:output_file] || dperr(1009,'no output file specified') @transpose = p[:transpose] || false @ss_db_cache_manager = p[:ss_db_cache_manager] || SsDbCacheManager.new({default_db_pa: './cache/'}) @template_pa_prefix = p[:template_pa_prefix] @text_file_cache_manager = p[:text_file_cache_manager] || TextFileCacheManager.new() #@export_type = p[:export_type] || dperr(1010,'no export type specified') @export_type = p[:export_type] || $Log.tlog({id: 'err1010'}) @super_var_map = p[:super_var_map] || {} @subs = p[:subs] || dperr(1011,'no subsitutions passed to ss_export') @var_map = {} @ss_db_cache = @ss_db_cache_manager.get_ss_db_cache(@ss_pn) @sheet = @ss_db_cache.get_sheet(@sheet_name) @minilang_env = MinilangEnv.new(self, {export_type: @export_type} ) @loops = [] if(@sheet == nil) # TODO make check for sheet_names dperr('E1003',"no sheet: #{$st[:sheet]}#{@sheet_name}#{$st[:error]} in #{$st[:ss_file]}#{@ss_pn}\nsheets available are:#{@spreadsheet_cache.get_sheet_names.inspect}") end end |
Instance Attribute Details
#ss_pn ⇒ Object (readonly)
Returns the value of attribute ss_pn.
74 75 76 |
# File 'lib/spreadsheet_export_refi.rb', line 74 def ss_pn @ss_pn end |
#var_map ⇒ Object (readonly)
Returns the value of attribute var_map.
75 76 77 |
# File 'lib/spreadsheet_export_refi.rb', line 75 def var_map @var_map end |
Class Method Details
.call_replace_function(context, name, minival_str) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/spreadsheet_export_refi.rb', line 66 def self.call_replace_function(context,name,minival_str) fun = @@replace_functions[name] return nil unless fun minival = Minival.new(minival_str) return nil unless minival.valid return fun.call(context,minival) end |
Instance Method Details
#consume_after_last_row ⇒ Object
148 149 150 151 |
# File 'lib/spreadsheet_export_refi.rb', line 148 def consume_after_last_row write @after_last_row @after_last_row = '' end |
#create_loop(l_var_name, ary) ⇒ Object
251 252 253 254 255 256 257 258 259 260 |
# File 'lib/spreadsheet_export_refi.rb', line 251 def create_loop(l_var_name,ary) # TODO # set l_var to ary.shift @minilang_env.set_l(l_var_name,ary.shift) @loops.push({from:(@cur_row+1),ary:ary,l_var_name:l_var_name}) #@creator.create_loop(l_var_name,ary) #@loops. end |
#delegate_instruction(row_i) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/spreadsheet_export_refi.rb', line 296 def delegate_instruction(row_i) @debug_row_i = row_i params = get_params(row_i) p2_ss_pn = eval_mvar('FILE') p2_sheet_name = eval_mvar('SHEET') transpose_str = params[:transpose] transpose = false $Log.tlog({id: 'info1124_delegate_instruction', ss_pn: @ss_pn, sheet_name: @sheet_name, row_i: row_i, p2_ss_pn: p2_ss_pn, p2_sheet_name: p2_sheet_name}) $Log.indent #dputi "delegate from #{$st[:ss_file]}#{@ss_pn}#{$st[:std]}" << reset << ":#{$st[:sheet]}#{@sheet_name}:#{$st[:num]}#{row_i}" << reset << " to #{$st[:ss_file]}#{p2_ss_pn}#{$st[:std]}" << reset << ":#{$st[:sheet]}#{p2_sheet_name}" if(transpose_str) if(transpose_str == 'true') transpose = true elsif(transpose_str == 'false') # ok else dparn(401,"delegate_instruction - value for transpose is neither 'true' nor 'false' (defaulting to false) - value is: #{transpose_str}") end end se = SsExport.new({ ss_pn: p2_ss_pn, sheet_name: p2_sheet_name, output_file: @output_file, transpose: transpose, ss_db_cache_manager: @ss_db_cache_manager, template_pa_prefix: @template_pa_prefix, text_file_cache_manager: @text_file_cache_manager, export_type: @export_type, super_var_map: @var_map, subs: @subs, }) se.process() #dpun $Log.undent #dput "done with: #{$st[:ss_file]}#{p2_ss_pn}#{$st[:std]}" << reset << ":#{$st[:sheet]}#{p2_sheet_name}" $Log.tlog({id:'info1060',p2_ss_pn:p2_ss_pn,p2_sheet_name:p2_sheet_name}) end |
#do_instruction(row_i) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/spreadsheet_export_refi.rb', line 234 def do_instruction(row_i) $Log.tlog({id: 'info1125_do_instruction'}) params = get_params(row_i) @minilang_env.set_var_map @var_map @minilang_env.run_str(params[:do]) text = @minilang_env.consume_text if params[:on_exit] @on_sheet_exit_add << text else write text end end |
#eval_mvar(col_label) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/spreadsheet_export_refi.rb', line 335 def eval_mvar(col_label) unless @var_map.has_key?(col_label) dperr(1007,"no column named:'#{col_label}'") end str = @var_map[col_label] if(str.is_a? String) str = str.gsub(/\\_/,'') return eval_var(str) else dparn(406,"") return 'XXX - WARN 406' end end |
#eval_var(var) ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/spreadsheet_export_refi.rb', line 348 def eval_var(var) if(var.class == Fixnum) return var.to_s elsif(var.class == Bignum) return var.to_s end if(var.class != String) dparn('W601',"var from db is of class:'#{var.class.to_s}'") return "XXX WARN 601 - var from db is of class:'#{var.class.to_s}'" end if(var == '') dperr(1372,"empty cell #{$st[:ss_file]}#{@ss_pn}:#{$st[:sheet]}#{@sheet_name}:#{$st[:num]}#{@debug_row_i}") end var = var.gsub(/\\_/,'') unless var.index("\\-") return var end t = Template.new(var) parts = t.get_parts ret_var = '' parts.each{|part| if(part.is_a? FunctionCall) r = @@replace_functions[part.name].call(self,part.args) ret_var << r elsif(part.is_a? String) ret_var << part end } return ret_var.strip end |
#get_cell(row_i, col_i) ⇒ Object
173 174 175 |
# File 'lib/spreadsheet_export_refi.rb', line 173 def get_cell(row_i,col_i) return @sheet.get_cell(row_i,col_i,@transpose) end |
#get_instruction(row_i) ⇒ Object
176 177 178 |
# File 'lib/spreadsheet_export_refi.rb', line 176 def get_instruction(row_i) return get_cell(row_i,@header_cols['INSTRUCTION']).value end |
#get_params(row_i) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/spreadsheet_export_refi.rb', line 379 def get_params(row_i) params_str = @var_map['PARAMS'] params = {} if(params_str == nil) elsif(params_str.is_a? String) pr = Minival.new(params_str) if(pr.valid) params = pr.get_value else pr.report_errors dperr(311,"invalid 'PARAMS' ? - value: '#{params_str}' (row:#{row_i})") end else dperr 310,"params_str not nil and not string (params_str.class:#{params_str.class.to_s})" end if(params.class != {}.class) dparn(303,"failed to parse minival (row:#{row_i} column:'PARAMS')") params = {'WARN'=>'303'} end return params end |
#get_row(row_i) ⇒ Object
230 231 232 |
# File 'lib/spreadsheet_export_refi.rb', line 230 def get_row(row_i) return @sheet.get_row(row_i,@transpose) end |
#get_sheet_dims ⇒ Object
dpun
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/spreadsheet_export_refi.rb', line 131 def get_sheet_dims sheet_dims = @sheet.get_dims if(@transpose) sheet_dims = { col: sheet_dims[:row], row: sheet_dims[:col], } end @sheet_dims = sheet_dims end |
#insert_instruction(row_i) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/spreadsheet_export_refi.rb', line 267 def insert_instruction(row_i) print 'i' #if(row_i <= 12 || row_i % 20 == 0) # print "r#{row_i} " #end params = get_params(row_i) do_expression = params[:do] template_text = '' filename = @var_map['FILE'] #template_text = @text_file_cache_man.get_text("#{@template_pa_prefix}#{filename}") template_text = read_file(filename) text = template_text.gsub(/(\\)#(.*?)#/){ v = @var_map[$2] if(v.is_a? String) next v else dparn(304,"placeholder not in @var_map ('#{$2}')") next "XXX WARN 304 @var_map[#{$2}].is_a? was not a string" # replace placeholder with error message end } if params[:on_exit] @on_sheet_exit_add << text elsif params[:after_last_row] @after_last_row << text else write text end end |
#is_row_active(row_i) ⇒ Object
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 |
# File 'lib/spreadsheet_export_refi.rb', line 179 def is_row_active(row_i) active_col_i = @header_cols['ACTIVE'] if(active_col_i == nil) $Log.tlog( {id: 'err1402'} ) #dperr(402,"no column named 'ACTIVE' - this column is mandatory") end is_active_str = get_cell(row_i,active_col_i).value if(is_active_str.is_a?(String)) if(is_active_str == "NO") $Log.tlog( {id: 'info1403'} ) return false elsif(is_active_str == "YES") ex_i = @header_cols['EXPORT'] ex_range = nil if ex_i == nil return true # missing EXPORT column defaults to active else ex_range = get_cell(row_i,ex_i).value end if (("*" == ex_range) or (@export_type == ex_range)) # EXPORT type matches return true else # 'EXPORT' must be something we are not interested in return false end else dperr(404,"'ACTIVE' has invalid value: #{is_active_str}\n(valid values are the strings 'YES' and 'NO')") end else # not ''.class dperr(403,"'ACTIVE' has invalid value/type (row:#{row_i} col:#{active_col_i}) value:'#{is_active_str}' value.class:'#{is_active_str.class}' last row:#{@sheet.get_last_row.to_s} (valid values are the strings 'YES' and 'NO')") end return false end |
#process ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/spreadsheet_export_refi.rb', line 105 def process #dputi "to #{$st[:ss_file]}#{@ss_pn}#{$st[:std]}" << reset << ":#{$st[:sheet]}#{@sheet_nam}" #dputi #dpit '' get_sheet_dims read_headers() @on_sheet_exit_add = '' process_instructions() write @on_sheet_exit_add #dput $Log.tlog({id: 'newline'}) #dpun end |
#process_instruction(instruction_type, row_i) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/spreadsheet_export_refi.rb', line 211 def process_instruction(instruction_type,row_i) # TODO add warnings @debug_row_i = row_i @var_map = @super_var_map.dup() @@dont_duplicate_from_super_map.each{|col_label| @var_map.delete(col_label) } row = get_row(row_i) @header_cols.each_pair{|col_label,col_i| @var_map[col_label] = eval_var(row[col_i].value) } inst_p = @instructions[instruction_type] if(inst_p) inst_p.call(row_i) else $Log.tlog( {id: 'err1302', ss_pn: @ss_pn, sheet_name:@sheet_name, row_i: row_i} ) #dperr('E302',"invalid instruction - spreadsheet:'#{$st[:ss_file]}#{@ss_pn}#{$st[:error]} sheet:#{$st[:sheet]}#{@sheet_name}#{$st[:error]} row:#{$st[:num]}#{row_i}") end end |
#process_instructions ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/spreadsheet_export_refi.rb', line 159 def process_instructions() @after_last_row = '' process_rows_from(2) @loops.each{|l| #pp l from = l[:from] ary = l[:ary] l_var_name = l[:l_var_name] while !ary.empty? @minilang_env.set_l(l_var_name,ary.shift) process_rows_from(from) end } end |
#process_row(row_i) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/spreadsheet_export_refi.rb', line 141 def process_row(row_i) @cur_row = row_i if(is_row_active(row_i)) instruction_str = get_instruction(row_i) process_instruction(instruction_str,row_i) end end |
#process_rows_from(from) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/spreadsheet_export_refi.rb', line 152 def process_rows_from from last_row = @sheet_dims[:row][:max] from.upto(last_row) do |row_i| process_row(row_i) end consume_after_last_row end |
#read_file(pn) ⇒ Object
261 262 263 264 265 266 |
# File 'lib/spreadsheet_export_refi.rb', line 261 def read_file pn if pn == '' return '' end return @text_file_cache_manager.get_text("#{@template_pa_prefix}#{pn}") end |
#read_headers ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/spreadsheet_export_refi.rb', line 124 def read_headers() #dputi "#{$st[:whisper]}K - read column labels" first = @sheet_dims[:col][:min] last = @sheet_dims[:col][:max] @header_cols = get_row(1).inject({}){|memo,(k,v)| memo[v.value.to_s] = k ; memo } #dpun end |
#select_instruction(row_i) ⇒ Object
247 248 249 |
# File 'lib/spreadsheet_export_refi.rb', line 247 def select_instruction(row_i) $Log.tlog( {id: 'info1123_select_instruction'} ) end |
#write(text) ⇒ Object
dpun
118 119 120 121 122 123 |
# File 'lib/spreadsheet_export_refi.rb', line 118 def write text @subs[:write].each{|r,s| text = text.gsub(r,s) } @output_file.write text end |