Module: Path

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

Constant Summary collapse

SEARCH_PATHS =
{
  :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}"),
  :lib     => File.join('{LIBDIR}', "{TOPLEVEL}", "{SUBPATH}"),
  :default => :user
}
SLASH =
"/"[0]
DOT =
"."[0]

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rbbt/resource/path.rb', line 56

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

#pkgdirObject

Returns the value of attribute pkgdir.



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

def pkgdir
  @pkgdir
end

#resourceObject

Returns the value of attribute resource.



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

def resource
  @resource
end

#search_pathsObject

Returns the value of attribute search_paths.



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

def search_paths
  @search_paths
end

Class Method Details

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



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rbbt/resource/util.rb', line 3

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

  return Path.setup(file) if File.exists? File.join(file, relative_to)

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

    return Path.setup(dir) if File.exists? File.join(dir, relative_to)

    file = File.dirname file
  end

  return nil
end

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



7
8
9
10
11
12
13
# File 'lib/rbbt/resource/path.rb', line 7

def self.setup(string, pkgdir = nil, resource = nil, search_paths = nil)
  string.extend Path
  string.pkgdir = pkgdir || 'rbbt'
  string.resource = resource
  string.search_paths = search_paths
  string
end

Instance Method Details

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



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

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

#all_fieldsObject



247
248
249
250
251
# File 'lib/rbbt/resource/path.rb', line 247

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

#annotate(name) ⇒ Object



19
20
21
# File 'lib/rbbt/resource/path.rb', line 19

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

#basenameObject



191
192
193
# File 'lib/rbbt/resource/path.rb', line 191

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

#byte(pos) ⇒ Object



52
53
54
# File 'lib/rbbt/resource/path.rb', line 52

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

#directory?Boolean

Returns:

  • (Boolean)


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

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

#dirnameObject



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

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

#doc_file(relative_to = 'lib') ⇒ Object



274
275
276
277
278
279
280
281
282
# File 'lib/rbbt/resource/path.rb', line 274

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)


153
154
155
156
157
158
159
160
# File 'lib/rbbt/resource/path.rb', line 153

def exists?
  begin
    self.produce
    File.exists? self.find
  rescue
    false
  end
end

#fieldsObject



243
244
245
# File 'lib/rbbt/resource/path.rb', line 243

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

#filenameObject



149
150
151
# File 'lib/rbbt/resource/path.rb', line 149

def filename
  self.find
end

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rbbt/resource/path.rb', line 86

def find(where = nil, caller_lib = nil, paths = nil)
  paths = (self.search_paths || SEARCH_PATHS).merge(paths || {})
  where = paths[:default] if where == :default
  return self if located?
  if self.match(/(.*?)\/(.*)/)
    toplevel, subpath = self.match(/(.*?)\/(.*)/).values_at 1, 2
  else
    toplevel, subpath = self, ""
  end

  path = nil
  if where.nil?
    %w(current user local global lib).each do |w| 
      w = w.to_sym
      next unless paths.include? w
      path = find(w, caller_lib, paths)
      return path if File.exists? path
    end
    if paths.include? :default
      find((paths[:default] || :user), caller_lib, paths)
    else
      raise "Path '#{ path }' not found, and no default specified in search paths: #{paths.inspect}"
    end
  else
    where = where.to_sym
    raise "Did not recognize the 'where' tag: #{where}. Options: #{paths.keys}" unless paths.include? where

    if where == :lib
      libdir = Path.caller_lib_dir(caller_lib) || "NOLIBDIR"
    else
      libdir = "NOLIBDIR"
    end
    pwd = FileUtils.pwd
    path = paths[where].
      sub('{PKGDIR}', pkgdir).
      sub('{PWD}', pwd).
      sub('{TOPLEVEL}', toplevel).
      sub('{SUBPATH}', subpath).
      sub('{PATH}', self).
      sub('{LIBDIR}', libdir) #, @pkgdir, @resource, @search_paths
    self.annotate path
  end
end

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



130
131
132
133
134
135
136
137
# File 'lib/rbbt/resource/path.rb', line 130

def find_all(caller_lib = nil, search_paths = nil)
  search_paths ||= self.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



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

def glob(pattern = '*')
  return [] unless self.exists?
  exp = File.join(self.find, pattern)
  Dir.glob(exp).collect{|f| Path.setup(f, self.resource, self.pkgdir)}
end

#identifier_file_pathObject



253
254
255
256
257
258
259
# File 'lib/rbbt/resource/path.rb', line 253

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

#identifier_filesObject



261
262
263
264
265
266
267
# File 'lib/rbbt/resource/path.rb', line 261

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

#in_dir?(dir) ⇒ Boolean

{{{ Methods

Returns:

  • (Boolean)


141
142
143
# File 'lib/rbbt/resource/path.rb', line 141

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

#index(options = {}) ⇒ Object



227
228
229
# File 'lib/rbbt/resource/path.rb', line 227

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

#join(name) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/rbbt/resource/path.rb', line 23

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

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



213
214
215
# File 'lib/rbbt/resource/path.rb', line 213

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



209
210
211
# File 'lib/rbbt/resource/path.rb', line 209

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

#located?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rbbt/resource/util.rb', line 26

def located?
  self.byte(0) == SLASH or (self.byte(0) == DOT and self.byte(1) == SLASH)
end

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



183
184
185
# File 'lib/rbbt/resource/path.rb', line 183

def open(options = {}, &block)
  Open.open(self.produce.find, options, &block)
end

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



223
224
225
# File 'lib/rbbt/resource/path.rb', line 223

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

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



235
236
237
# File 'lib/rbbt/resource/path.rb', line 235

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

#produce(force = false) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rbbt/resource/path.rb', line 162

def produce(force = false)
  path = self.find

  return self if Open.exists?(path.to_s) and not force

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

  resource.produce self, force

  self
end

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



231
232
233
# File 'lib/rbbt/resource/path.rb', line 231

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

#read(&block) ⇒ Object



174
175
176
# File 'lib/rbbt/resource/path.rb', line 174

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

#set_extension(new_extension = nil) ⇒ Object



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

def set_extension(new_extension = nil)
  new_path = self.sub(/\.[^\.\/]+$/, "." << new_extension.to_s)
  Path.setup new_path, @pkgdir, @resource
end

#source_for_doc_file(relative_to = 'lib') ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/rbbt/resource/path.rb', line 284

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.exists?(file)
      file= Dir.glob(file.sub(/\.[^\.\/]+$/, '.*')).first
    end

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

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

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

#sub(*args) ⇒ Object



15
16
17
# File 'lib/rbbt/resource/path.rb', line 15

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

#to_sObject



145
146
147
# File 'lib/rbbt/resource/path.rb', line 145

def to_s
  self.find
end

#to_yaml(*args) ⇒ Object



239
240
241
# File 'lib/rbbt/resource/path.rb', line 239

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

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



205
206
207
# File 'lib/rbbt/resource/path.rb', line 205

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

#tsv(*args) ⇒ Object



195
196
197
# File 'lib/rbbt/resource/path.rb', line 195

def tsv(*args)
  TSV.open(self.produce, *args)
end

#tsv_options(options = {}) ⇒ Object



199
200
201
202
203
# File 'lib/rbbt/resource/path.rb', line 199

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

#write(*args, &block) ⇒ Object



178
179
180
# File 'lib/rbbt/resource/path.rb', line 178

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

#yamlObject



217
218
219
220
221
# File 'lib/rbbt/resource/path.rb', line 217

def yaml
  self.open do |f|
    YAML.load f
  end
end