X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=code_ruby%2Flib%2Fmaasha%2Ffilesys.rb;h=4bb7b8f7d17fd4b88b90da6d64a0aa33be527d8d;hb=2f0fd91b461033529a4a72e161bd133252a22eb6;hp=b3b450a9aefd653a53f8d80a9647a2e231f372c5;hpb=14e51cd98e6f898e4b37d9d102922fee25c40ac5;p=biopieces.git diff --git a/code_ruby/lib/maasha/filesys.rb b/code_ruby/lib/maasha/filesys.rb index b3b450a..4bb7b8f 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,68 +41,67 @@ 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) + 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 - # 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