lib/rbbt/util/tsv.rb in rbbt-util-2.1.0 vs lib/rbbt/util/tsv.rb in rbbt-util-3.0.2
- old
+ new
@@ -1,8 +1,8 @@
+require 'rbbt/util/resource'
require 'rbbt/util/misc'
require 'rbbt/util/open'
-require 'rbbt/util/path'
require 'rbbt/util/tc_hash'
require 'rbbt/util/tmpfile'
require 'rbbt/util/log'
require 'rbbt/util/persistence'
require 'digest'
@@ -11,10 +11,11 @@
require 'rbbt/util/tsv/parse'
require 'rbbt/util/tsv/accessor'
require 'rbbt/util/tsv/manipulate'
require 'rbbt/util/tsv/index'
require 'rbbt/util/tsv/attach'
+require 'rbbt/util/tsv/resource'
class TSV
def self.headers(file, options = {})
## Remove options from filename
@@ -63,11 +64,11 @@
file, extra = file if Array === file and file.length == 2 and Hash === file.last
@filename = Misc.process_options options, :filename
@filename ||= case
- when Path === file
+ when Resource::Path === file
file
when (String === file and File.exists? file)
File.expand_path file
when String === file
file
@@ -87,17 +88,22 @@
# Filename or File: Parsed
# Hash: Encapsulated, empty info
# TSV: Duplicate
case
when block_given?
- @data, extra = Persistence.persist(@filename, :TSV, :tsv_extra, options.merge(:force_array => true)) do |filename, options| yield filename, options end
+ @data, extra = Persistence.persist(file, :TSV, :tsv_extra, options.merge(:force_array => true)) do |file, options, filename| yield file, options, filename end
extra.each do |key, values|
self.send("#{ key }=".to_sym, values) if self.respond_to? "#{ key }=".to_sym
end if not extra.nil?
else
+
case
+ when Array === file
+ @data = Hash[file.collect{|v|
+ [v,[]]
+ }]
when Hash === file
@data = file
when TSV === file
@data = file.data
when Persistence::TSV === file
@@ -106,32 +112,31 @@
if @data.respond_to?(key.to_sym) and self.respond_to?("#{key}=".to_sym)
self.send "#{key}=".to_sym, @data.send(key.to_sym)
end
end
else
- @data, extra = Persistence.persist(@filename, :TSV, :tsv_extra, options) do |file, options, filename|
+ @data, extra = Persistence.persist(file, :TSV, :tsv_extra, options) do |file, options, filename|
data, extra = nil
case
## Parse source
- when (String === file and file.respond_to? :open)
+ when Resource::Path === file #(String === file and file.respond_to? :open)
data, extra = TSV.parse(file.open(:grep => options[:grep]) , options)
extra[:namespace] ||= file.namespace
extra[:datadir] ||= file.datadir
when StringIO === file
data, extra = TSV.parse(file, options)
when Open.can_open?(file)
Open.open(file, :grep => options[:grep]) do |f|
data, extra = TSV.parse(f, options)
end
- #extra[:namespace] = File.basename(File.dirname(filename))
- #extra.delete :namespace if extra[:namespace].empty? or extra[:namespace] == "."
when File === file
+ path = file.path
file = Open.grep(file, options[:grep]) if options[:grep]
data, extra = TSV.parse(file, options)
- extra[:namespace] = File.basename(File.dirname(file.filename))
- extra.delete :namespace if extra[:namespace].empty? or extra[:namespace] == "."
- ## Encapsulate Hash or TSV
+ when IO === file
+ file = Open.grep(file, options[:grep]) if options[:grep]
+ data, extra = TSV.parse(file, options)
when block_given?
data
else
raise "Unknown input in TSV.new #{file.inspect}"
end