Module: Path

Defined in:
lib/rbbt/resource/path.rb,
lib/rbbt/resource/util.rb

Constant Summary collapse

SEARCH_PATHS =
IndiferentHash.setup({
  :current => File.join("{PWD}", "{TOPLEVEL}", "{SUBPATH}"),
  :user    => File.join(ENV['HOME'], ".{PKGDIR}", "{TOPLEVEL}", "{SUBPATH}"),
  :global  => File.join('/', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"),
  :local   => File.join('/usr/local', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"),
  :fast   => File.join('/fast', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"),
  :cache   => File.join('/cache', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"),
  :bulk   => File.join('/bulk', "{TOPLEVEL}", "{PKGDIR}", "{SUBPATH}"),
  :lib     => File.join('{LIBDIR}', "{TOPLEVEL}", "{SUBPATH}"),
  :base   => File.join(caller_lib_dir(__FILE__), "{TOPLEVEL}", "{SUBPATH}"),
  :default => :user
})
STANDARD_SEARCH =
%w(current workflow user local global lib fast cache bulk)
SLASH =
DOT =

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, prev = nil, *args, &block) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rbbt/resource/path.rb', line 121

def method_missing(name, prev = nil, *args, &block)
  if block_given?
    super name, prev, *args, &block
  else
    # Fix problem with ruby 1.9 calling methods by its own initiative. ARG
    super(name, prev, *args) if name.to_s =~ /^to_/
    if prev.nil?
      join name
    else
      join(prev).join(name)
    end
  end
end

Instance Attribute Details

#libdirObject

Returns the value of attribute libdir.



6
7
8
# File 'lib/rbbt/resource/path.rb', line 6

def libdir
  @libdir
end

#originalObject

Returns the value of attribute original.



6
7
8
# File 'lib/rbbt/resource/path.rb', line 6

def original
  @original
end

#pkgdirObject

Returns the value of attribute pkgdir.



6
7
8
# File 'lib/rbbt/resource/path.rb', line 6

def pkgdir
  @pkgdir
end

#resourceObject

Returns the value of attribute resource.



6
7
8
# File 'lib/rbbt/resource/path.rb', line 6

def resource
  @resource
end

#search_orderObject

Returns the value of attribute search_order.



6
7
8
# File 'lib/rbbt/resource/path.rb', line 6

def search_order
  @search_order
end

#search_pathsObject

Returns the value of attribute search_paths.



6
7
8
# File 'lib/rbbt/resource/path.rb', line 6

def search_paths
  @search_paths
end

#whereObject

Returns the value of attribute where.



6
7
8
# File 'lib/rbbt/resource/path.rb', line 6

def where
  @where
end

Class Method Details

.caller_lib_dir(file = nil, relative_to = ['lib', 'bin']) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rbbt/resource/util.rb', line 3

def self.caller_lib_dir(file = nil, relative_to = ['lib', 'bin'])
  #file = caller.reject{|l| 
  #  l =~ /rbbt\/(?:resource\.rb|workflow\.rb)/ or
  #  l =~ /rbbt\/resource\/path\.rb/ or
  #  l =~ /rbbt\/persist.rb/ or
  #  l =~ /rbbt\/util\/misc\.rb/ or
  #  l =~ /progress-monitor\.rb/ 
  #}.first.sub(/\.rb[^\w].*/,'.rb') if file.nil?
  
  
  if file.nil?
    caller_dup = caller.dup
    while file = caller_dup.shift
      break unless file =~ /rbbt\/(?:resource\.rb|workflow\.rb)/ or
        file =~ /rbbt\/resource\/path\.rb/ or
        file =~ /rbbt\/persist.rb/ or
        file =~ /rbbt\/util\/misc\.rb/ or
        file =~ /progress-monitor\.rb/ 
    end
    file = file.sub(/\.rb[^\w].*/,'.rb')
  end

  relative_to = [relative_to] unless Array === relative_to
  file = File.expand_path(file)
  return Path.setup(file) if relative_to.select{|d| File.exist? File.join(file, d)}.any?

  while file != '/'
    dir = File.dirname file

    return dir if relative_to.select{|d| File.exist? File.join(dir, d)}.any?

    file = File.dirname file
  end

  return nil
end

.get_extension(path) ⇒ Object



427
428
429
# File 'lib/rbbt/resource/path.rb', line 427

def self.get_extension(path)
  path.match(/\.([^\.\/]{1,5})$/)[1]
end

.setup(string, pkgdir = nil, resource = nil, search_paths = nil, search_order = nil, libdir = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rbbt/resource/path.rb', line 8

def self.setup(string, pkgdir = nil, resource = nil, search_paths = nil, search_order = nil, libdir = nil)
  return string if string.nil?
  string = string.dup if string.frozen?
  string.extend Path
  string.pkgdir = pkgdir || 'rbbt'
  string.resource = resource
  string.search_paths = search_paths
  string.search_order = search_order
  string.libdir = libdir || Path.caller_lib_dir 
  string
end

Instance Method Details

#[](name, orig = false) ⇒ Object



112
113
114
115
# File 'lib/rbbt/resource/path.rb', line 112

def [](name, orig = false)
  return super(name) if orig
  join name
end

#_exists?Boolean

Returns:

  • (Boolean)


289
290
291
# File 'lib/rbbt/resource/path.rb', line 289

def _exists?
  Open.exists? self.find.to_s
end

#add_search_path(name, dir) ⇒ Object



28
29
30
# File 'lib/rbbt/resource/path.rb', line 28

def add_search_path(name, dir)
  search_paths[name.to_sym] = dir
end

#all_fieldsObject



395
396
397
398
399
# File 'lib/rbbt/resource/path.rb', line 395

def all_fields
  self.open do |stream|
    TSV.parse_header(stream).all_fields
  end
end

#annotate(name) ⇒ Object



46
47
48
49
50
# File 'lib/rbbt/resource/path.rb', line 46

def annotate(name)
  name = name.to_s
  name = Path.setup name, @pkgdir, @resource, @search_paths, @search_order, @libdir
  name
end

#append_search_path(name, dir) ⇒ Object



37
38
39
40
# File 'lib/rbbt/resource/path.rb', line 37

def append_search_path(name, dir)
  add_search_path(name, dir)
  search_order.push(name.to_sym)
end

#basenameObject



330
331
332
# File 'lib/rbbt/resource/path.rb', line 330

def basename
  Path.setup(File.basename(self), self.resource, self.pkgdir)
end

#byte(pos) ⇒ Object



117
118
119
# File 'lib/rbbt/resource/path.rb', line 117

def byte(pos)
  send(:[], pos, true)
end

#clean_annotationsObject



475
476
477
# File 'lib/rbbt/resource/path.rb', line 475

def clean_annotations
  "" << self.to_s
end

#directory?Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/rbbt/resource/path.rb', line 67

def directory?
  return nil unless self.exists?
  File.directory? self.find 
end

#dirnameObject



63
64
65
# File 'lib/rbbt/resource/path.rb', line 63

def dirname
  Path.setup File.dirname(self), @pkgdir, @resource
end

#doc_file(relative_to = 'lib') ⇒ Object



443
444
445
446
447
448
449
450
451
# File 'lib/rbbt/resource/path.rb', line 443

def doc_file(relative_to = 'lib')
  if located?
    lib_dir = Path.caller_lib_dir(self, relative_to)
    relative_file = File.join( 'doc', self.sub(lib_dir,''))
    Path.setup File.join(lib_dir, relative_file) , @pkgdir, @resource
  else
    Path.setup File.join('doc', self) , @pkgdir, @resource
  end
end

#exists?Boolean

Returns:

  • (Boolean)


293
294
295
296
297
298
299
300
# File 'lib/rbbt/resource/path.rb', line 293

def exists?
  begin
    self.produce
    _exists?
  rescue Exception
    false
  end
end

#fieldsObject



391
392
393
# File 'lib/rbbt/resource/path.rb', line 391

def fields
  TSV.parse_header(self.open).fields
end

#filenameObject



285
286
287
# File 'lib/rbbt/resource/path.rb', line 285

def filename
  self.find
end

#find(where = nil, caller_lib = nil, paths = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/rbbt/resource/path.rb', line 161

def find(where = nil, caller_lib = nil, paths = nil)

  if located?
    self.original ||= self
    return self
  end

  @path ||= {}
  rsearch_paths = (resource and resource.respond_to?(:search_paths)) ? resource.search_paths : nil 
  key = [where, caller_lib, rsearch_paths, paths].inspect
  self.sub!('~/', Etc.getpwuid.dir + '/') if self.include? "~"

  return @path[key] if @path[key]

  @path[key] ||= begin
                   paths = [paths, rsearch_paths, self.search_paths, SEARCH_PATHS].reverse.compact.inject({}){|acc,h| acc.merge! h; acc }
                   where = paths[:default] if where == :default
                   if self.match(/(.*?)\/(.*)/)
                     toplevel, subpath = self.match(/(.*?)\/(.*)/).values_at 1, 2
                   else
                     toplevel, subpath = "{REMOVE}", self
                   end

                   path = nil
                   search_order = self.search_order || []
                   res = nil
                   if where.nil?

                     (STANDARD_SEARCH - search_order).each do |w| 
                       w = w.to_sym
                       break if res
                       next unless paths.include? w
                       path = find(w, caller_lib, paths)
                       res = path if File.exist? path
                     end

                     search_order.each do |w| 
                       w = w.to_sym
                       next if res
                       next unless paths.include? w
                       path = find(w, caller_lib, paths)
                       res = path if File.exist?(path)
                     end if res.nil?

                     (paths.keys - STANDARD_SEARCH - search_order).each do |w|
                       w = w.to_sym
                       next if res
                       next unless paths.include? w
                       path = find(w, caller_lib, paths)
                       res = path if File.exist? path
                     end if res.nil?

                     if paths.include? :default
                       res = find((paths[:default] || :user), caller_lib, paths)
                     else
                       raise "Path '#{ path }' not found, and no default specified in search paths: #{paths.inspect}"
                     end if res.nil?

                   else
                     where = where.to_sym

                     if paths.include? where
                       path = paths[where]
                     elsif where.to_s.include?("/")
                       path = where.to_s
                     else
                       raise "Did not recognize the 'where' tag: #{where}. Options: #{paths.keys}" unless paths.include? where
                     end

                     if where == :lib 
                       libdir = @libdir || Path.caller_lib_dir(caller_lib) || "NOLIBDIR" 
                     else
                       libdir = "NOLIBDIR"
                     end

                     pwd = FileUtils.pwd
                     path = File.join(path, "{PATH}") unless path.include? "PATH}" or path.include? "{BASENAME}"
                     path = path.
                       sub('{PKGDIR}', pkgdir).
                       sub('{PWD}', pwd).
                       sub('{TOPLEVEL}', toplevel).
                       sub('{SUBPATH}', subpath).
                       sub('{BASENAME}', File.basename(self)).
                       sub('{PATH}', self).
                       sub('{LIBDIR}', libdir).
                       sub('{RESOURCE}', resource.to_s).
                       sub('{REMOVE}/', '').
                       sub('{REMOVE}', '')

                     path = path + '.gz' if File.exist?(path + '.gz')
                     path = path + '.bgz' if File.exist?(path + '.bgz')

                     self.annotate path

                     res = path
                   end

                   res.original = self.original || self
                   res.where = where

                   res
                 end
  @path[key]
end

#find_all(caller_lib = nil, search_paths = nil) ⇒ Object



266
267
268
269
270
271
272
273
# File 'lib/rbbt/resource/path.rb', line 266

def find_all(caller_lib = nil, search_paths = nil)
  search_paths ||= @search_paths || SEARCH_PATHS
  search_paths = search_paths.dup

  search_paths.keys.
    collect{|where| find(where, Path.caller_lib_dir, search_paths) }.
    compact.select{|file| file.exists? }.uniq
end

#glob(pattern = '*') ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rbbt/resource/path.rb', line 72

def glob(pattern = '*')
  if self.include? "*"
    self.glob_all
  else
    return [] unless self.exists? 
    found = self.find
    exp = File.join(found, pattern)
    paths = Dir.glob(exp).collect{|f| self.annotate(f) }

    paths.each do |p|
      p.original = File.join(found.original, p.sub(/^#{found}/, ''))
    end if found.original

    paths
  end
end

#glob_all(pattern = nil, caller_lib = nil, search_paths = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rbbt/resource/path.rb', line 89

def glob_all(pattern = nil, caller_lib = nil, search_paths = nil)
  search_paths ||= @search_paths || SEARCH_PATHS
  search_paths = search_paths.dup

  location_paths = {}
  search_paths.keys.collect do |where| 
    found = find(where, Path.caller_lib_dir, search_paths)
    paths = pattern ? Dir.glob(File.join(found, pattern)) : Dir.glob(found) 

    paths = paths.collect{|p| self.annotate p }

    paths = paths.each do |p|
      p.original = File.join(found.original, p.sub(/^#{found}/, ''))
      p.where = where
    end if found.original and pattern

    location_paths[where] = paths
  end

  #location_paths.values.compact.flatten.collect{|file| File.expand_path(file) }.uniq.collect{|path| Path.setup(path, self.resource, self.pkgdir)}
  location_paths.values.compact.flatten.uniq
end

#identifier_file_pathObject



401
402
403
404
405
406
407
# File 'lib/rbbt/resource/path.rb', line 401

def identifier_file_path
  if self.dirname.identifiers.exists?
    self.dirname.identifiers
  else
    nil
  end
end

#identifier_filesObject



409
410
411
412
413
414
415
# File 'lib/rbbt/resource/path.rb', line 409

def identifier_files
  if identifier_file_path.nil?
    []
  else
    [identifier_file_path]
  end
end

#in_dir?(dir) ⇒ Boolean

{{{ Methods

Returns:

  • (Boolean)


277
278
279
# File 'lib/rbbt/resource/path.rb', line 277

def in_dir?(dir)
  ! ! File.expand_path(self).match(/^#{Regexp.quote dir}/)
end

#index(options = {}) ⇒ Object



375
376
377
# File 'lib/rbbt/resource/path.rb', line 375

def index(options = {})
  TSV.index(self.produce, options)
end

#join(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/rbbt/resource/path.rb', line 52

def join(name)
  raise "Invalid path: #{ self }" if self.nil?
  new = if self.empty?
          self.annotate name.to_s.dup.chomp
        else
          self.annotate File.join(self, name.to_s.chomp)
        end
  new.original = File.join(self.original, name.to_s.chomp) if self.original
  new
end

#keys(field = 0, sep = "\t") ⇒ Object



361
362
363
# File 'lib/rbbt/resource/path.rb', line 361

def keys(field = 0, sep = "\t")
  Open.read(self.produce.find).split("\n").collect{|l| next if l =~ /^#/; l.split(sep, -1)[field]}.compact
end

#listObject



357
358
359
# File 'lib/rbbt/resource/path.rb', line 357

def list
  Open.read(self.produce.find).split "\n"
end

#located?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/rbbt/resource/util.rb', line 42

def located?
  self.byte(0) == SLASH || (self.byte(0) == DOT && self.byte(1) == SLASH) || (resource != Rbbt && (Open.remote?(self) || Open.ssh?(self)))
end

#open(options = {}, &block) ⇒ Object



321
322
323
324
# File 'lib/rbbt/resource/path.rb', line 321

def open(options = {}, &block)
  file = Open.remote?(self) || Open.ssh?(self) ? self : self.produce.find
  Open.open(file, options, &block)
end

#pipe_to(cmd, options = {}) ⇒ Object



371
372
373
# File 'lib/rbbt/resource/path.rb', line 371

def pipe_to(cmd, options = {})
  CMD.cmd(cmd, {:in => self.open, :pipe => true}.merge(options))
end

#pos_index(pos, options = {}) ⇒ Object



383
384
385
# File 'lib/rbbt/resource/path.rb', line 383

def pos_index(pos, options = {})
  TSV.pos_index(self.produce, pos, options)
end

#prepend_search_path(name, dir) ⇒ Object



32
33
34
35
# File 'lib/rbbt/resource/path.rb', line 32

def prepend_search_path(name, dir)
  add_search_path(name, dir)
  search_order.unshift(name.to_sym)
end

#produce(force = false) ⇒ Object



302
303
304
305
306
307
308
309
310
# File 'lib/rbbt/resource/path.rb', line 302

def produce(force = false)
  return self if _exists? and not force

  raise "No resource defined to produce file: #{ self }" if resource.nil?

  resource.produce self, force if Resource === resource

  self
end

#range_index(start, eend, options = {}) ⇒ Object



379
380
381
# File 'lib/rbbt/resource/path.rb', line 379

def range_index(start, eend, options = {})
  TSV.range_index(self.produce, start, eend, options)
end

#read(&block) ⇒ Object



312
313
314
# File 'lib/rbbt/resource/path.rb', line 312

def read(&block)
  Open.read(self.produce.find, &block)
end

#remove_extension(new_extension = nil) ⇒ Object



422
423
424
# File 'lib/rbbt/resource/path.rb', line 422

def remove_extension(new_extension = nil)
  self.sub(/\.[^\.\/]{1,5}$/,'')
end

#replace_extension(new_extension = nil, multiple = false) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
# File 'lib/rbbt/resource/path.rb', line 431

def replace_extension(new_extension = nil, multiple = false)
  if String === multiple
    new_path = self.sub(/(\.[^\.\/]{1,5})(.#{multiple})?$/,'')
  elsif multiple
    new_path = self.sub(/(\.[^\.\/]{1,5})+$/,'')
  else
    new_path = self.sub(/\.[^\.\/]{1,5}$/,'')
  end
  new_path = new_path + "." + new_extension.to_s
  self.annotate(new_path)
end

#set_extension(new_extension = nil) ⇒ Object



417
418
419
420
# File 'lib/rbbt/resource/path.rb', line 417

def set_extension(new_extension = nil)
  new_path = self + "." + new_extension.to_s
  self.annotate(new_path)
end

#source_for_doc_file(relative_to = 'lib') ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/rbbt/resource/path.rb', line 453

def source_for_doc_file(relative_to = 'lib')
  if located?
    lib_dir = Path.caller_lib_dir(Path.caller_lib_dir(self, 'doc'), relative_to)
    relative_file = self.sub(/(.*\/)doc\//, '\1').sub(lib_dir + "/",'')
    file = File.join(lib_dir, relative_file)

    if not File.exist?(file)
      file= Dir.glob(file.sub(/\.[^\.\/]+$/, '.*')).first
    end

    Path.setup file, @pkgdir, @resource
  else
    relative_file = self.sub(/^doc\//, '\1')

    if not File.exist?(relative_file)
      relative_file = Dir.glob(relative_file.sub(/\.[^\.\/]+$/, '.*')).first
    end

    Path.setup relative_file , @pkgdir, @resource
  end
end

#sub(*args) ⇒ Object



42
43
44
# File 'lib/rbbt/resource/path.rb', line 42

def sub(*args)
  self.annotate super(*args)
end

#to_sObject



281
282
283
# File 'lib/rbbt/resource/path.rb', line 281

def to_s
  self.find
end

#to_yaml(*args) ⇒ Object



387
388
389
# File 'lib/rbbt/resource/path.rb', line 387

def to_yaml(*args)
  self.to_s.to_yaml(*args)
end

#traverse(options = {}, &block) ⇒ Object



353
354
355
# File 'lib/rbbt/resource/path.rb', line 353

def traverse(options = {}, &block)
  TSV::Parser.traverse(self.open, options, &block)
end

#tsv(*args) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/rbbt/resource/path.rb', line 334

def tsv(*args)
  begin
    path = self.produce
  rescue Resource::ResourceNotFound => e
    begin
      path = self.set_extension('tsv').produce
    rescue Resource::ResourceNotFound 
      raise e
    end
  end
  TSV.open(path, *args)
end

#tsv_options(options = {}) ⇒ Object



347
348
349
350
351
# File 'lib/rbbt/resource/path.rb', line 347

def tsv_options(options = {})
  self.open do |stream|
    TSV::Parser.new(stream, options).options
  end
end

#write(*args, &block) ⇒ Object



316
317
318
# File 'lib/rbbt/resource/path.rb', line 316

def write(*args, &block)
  Open.write(self.produce.find, *args, &block)
end

#yamlObject



365
366
367
368
369
# File 'lib/rbbt/resource/path.rb', line 365

def yaml
  self.open do |f|
    YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(f) : YAML.load(f)
  end
end