X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=code_ruby%2Flib%2Fmaasha%2Ffilesys.rb;h=02a8a842f1157a9824052c589795c1630580ff90;hb=5de6112b70b59420b245ce636a8b2e3c90acbe00;hp=00530d655720eaa7e82f51f6ece33353527ae8d8;hpb=c460a7a4917df591f7e422776ee388763f49742a;p=biopieces.git diff --git a/code_ruby/lib/maasha/filesys.rb b/code_ruby/lib/maasha/filesys.rb index 00530d6..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,68 +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 - # TODO figure out what type is for. - def initialize(io, type=nil) - @io = io - @type = type + def initialize(ios) + @io = ios + end + + def puts(*args) + @io.puts(*args) end - # Method to close ios. def close @io.close end + def eof? + @io.eof? + 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