module Etti::FileReader
Constants
- ERROR
- NO
- NO_ALLWAYAS
- OK
message codes from the callback
- WARNING
- YES
return values to the callback
- YES_ALLWAYS
Public Class Methods
data_read(path, separator, max_slices, &callback)
click to toggle source
path = path to the data file separator = the separator for the split function to each line max_slices = the maximum number of slices each line split to callback {} = block to pass warnings and errors to
# File lib/etti/file/data-reader.rb, line 23 def data_read path, separator, max_slices, &callback unless File.readable? path callback.call ERROR, _("%s\n is not readable") % path return end fm = FileMagic.mime mime = fm.file path unless mime.start_with? 'text/plain;' answer = callback.call(WARNING, _("Files other then plain text are not supported.\n" + "Should the file be loaded anyway?")) return if answer != YES end file = File.new path, 'r' # warn the user if the file is bigger then 1 MB size = file.size if size > 1_000_000 mb = sprintf "%.3f", size.to_f / 1_000_000 answer= callback.call(WARNING, _("The selected file is very big (%s MB)\n" + "Do you really want to open it?") % mb) if answer != YES file.close return else if remind end end end lines = [] cols = 0 file.each do |line| toc = line.strip.split(separator, max_slices) lines << toc cols = cols.max toc.length end [lines, cols] end
Private Instance Methods
data_read(path, separator, max_slices, &callback)
click to toggle source
path = path to the data file separator = the separator for the split function to each line max_slices = the maximum number of slices each line split to callback {} = block to pass warnings and errors to
# File lib/etti/file/data-reader.rb, line 23 def data_read path, separator, max_slices, &callback unless File.readable? path callback.call ERROR, _("%s\n is not readable") % path return end fm = FileMagic.mime mime = fm.file path unless mime.start_with? 'text/plain;' answer = callback.call(WARNING, _("Files other then plain text are not supported.\n" + "Should the file be loaded anyway?")) return if answer != YES end file = File.new path, 'r' # warn the user if the file is bigger then 1 MB size = file.size if size > 1_000_000 mb = sprintf "%.3f", size.to_f / 1_000_000 answer= callback.call(WARNING, _("The selected file is very big (%s MB)\n" + "Do you really want to open it?") % mb) if answer != YES file.close return else if remind end end end lines = [] cols = 0 file.each do |line| toc = line.strip.split(separator, max_slices) lines << toc cols = cols.max toc.length end [lines, cols] end