X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=code_ruby%2Flib%2Fmaasha%2Ffilesys.rb;h=02a8a842f1157a9824052c589795c1630580ff90;hb=HEAD;hp=e842178853a6f79d9aab4df1239c7a5908c7a6fa;hpb=db292cc5d7f63299f8f2234a6150d3f16ac324ae;p=biopieces.git diff --git a/code_ruby/lib/maasha/filesys.rb b/code_ruby/lib/maasha/filesys.rb index e842178..02a8a84 100644 --- a/code_ruby/lib/maasha/filesys.rb +++ b/code_ruby/lib/maasha/filesys.rb @@ -22,7 +22,7 @@ # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< -require 'zlib' +require 'open3' # Error class for all exceptions to do with Filesys. class FilesysError < StandardError; end @@ -40,75 +40,69 @@ class Filesys path end - # Class method allowing open to be used on (zipped) files. - # See File.open. def self.open(*args) - args = *args - file = args.first - - if file == "-" - ios = self.new(STDIN) - elsif File.pipe? file - ios = self.new(File.open(*args)) + file = args.shift + mode = args.shift + options = args.shift || {} + + if mode == 'w' + case options[:compress] + when :gzip + ios, = Open3.pipeline_w("gzip -f", out: file) + when :bzip, :bzip2 + ios, = Open3.pipeline_w("bzip2 -c", out: file) + else + ios = File.open(file, mode, options) + end else - ios = self.zopen(*args) + if file == '-' + ios = STDIN + else + case `file -Lk #{file}` + when /gzip/ + ios = IO.popen("gzip -cd #{file}", :external_encoding=>"EUC-JP") + when /bzip/ + ios = IO.popen("bzcat #{file}") + when /ASCII/ + ios = File.open(file, mode, options) + else + raise "Unknown file type: #{`file -L #{file}`}" + end + end end if block_given? begin - yield ios + yield self.new(ios) ensure ios.close end else - return ios + return self.new(ios) end end - def initialize(io) - @io = io + def initialize(ios) + @io = ios + end + + def puts(*args) + @io.puts(*args) end - # Method to close ios. def close - @io.close unless @io.is_a? Zlib::GzipReader + @io.close end def eof? @io.eof? end - # Method to check if io is closed. - def closed? - @io.closed? - end - # Iterator method for parsing entries. def each while entry = get_entry do yield entry end end - - # Method to puts directoy on Filesys objects. - def puts(*args) - @io.puts(*args) - end - - private - - # Helper method to return an ios to a file that may be zipped in which case - # the ios is unzipped on the fly. See File.open. - def self.zopen(*args) - ios = File.open(*args) - - begin - ios = Zlib::GzipReader.new(ios) - rescue - ios.rewind - end - - self.new(ios) - end end