X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=code_ruby%2Flib%2Fmaasha%2Ffilesys.rb;h=6a6067fbc859bb3df62a7f4eda547c0f44d8070c;hb=3ce24a349c10a76d3e837e08b81259204ab870fe;hp=2ee775760fa36faabcabcbac99f193bdbc4524dc;hpb=c82f6849eedb996b79e44f3ee64679d21b8173b9;p=biopieces.git diff --git a/code_ruby/lib/maasha/filesys.rb b/code_ruby/lib/maasha/filesys.rb index 2ee7757..6a6067f 100644 --- a/code_ruby/lib/maasha/filesys.rb +++ b/code_ruby/lib/maasha/filesys.rb @@ -23,6 +23,7 @@ # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< require 'zlib' +require 'bzip2' # Error class for all exceptions to do with Filesys. class FilesysError < StandardError; end @@ -40,43 +41,61 @@ 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 = Zlib::GzipWriter.new File.open(file, mode, options) + when :bzip, :bzip2 + ios = Bzip2::Writer.new File.open(file, mode, options) + else + ios = File.open(file, mode, options) + end else - ios = self.zopen(*args) + if file == '-' + ios = STDIN + else + case `file #{file}` + when /gzip/ + ios = Zlib::GzipReader.new File.open(file, mode, options) + when /bzip/ + # ios = Bzip2::Reader.new File.open(file, mode, options) # TODO this method is buggy, investigate + ios = IO.popen("bzcat #{file}") + else + ios = File.open(file, mode, options) + 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 end - # Method to check if io is closed. - def closed? - @io.closed? + def eof? + @io.eof? end # Iterator method for parsing entries. @@ -85,26 +104,5 @@ class Filesys 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