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}"), :default => :user })
- STANDARD_SEARCH =
%w(current workflow user local global lib fast cache bulk)
- SLASH =
"/"[0]
- DOT =
"."[0]
Instance Attribute Summary collapse
-
#libdir ⇒ Object
Returns the value of attribute libdir.
-
#original ⇒ Object
Returns the value of attribute original.
-
#pkgdir ⇒ Object
Returns the value of attribute pkgdir.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#search_order ⇒ Object
Returns the value of attribute search_order.
-
#search_paths ⇒ Object
Returns the value of attribute search_paths.
Class Method Summary collapse
- .caller_lib_dir(file = nil, relative_to = 'lib') ⇒ Object
- .setup(string, pkgdir = nil, resource = nil, search_paths = nil, search_order = nil, libdir = nil) ⇒ Object
Instance Method Summary collapse
- #[](name, orig = false) ⇒ Object
- #_exists? ⇒ Boolean
- #add_search_path(name, dir) ⇒ Object
- #all_fields ⇒ Object
- #annotate(name) ⇒ Object
- #append_search_path(name, dir) ⇒ Object
- #basename ⇒ Object
- #byte(pos) ⇒ Object
- #directory? ⇒ Boolean
- #dirname ⇒ Object
- #doc_file(relative_to = 'lib') ⇒ Object
- #exists? ⇒ Boolean
- #fields ⇒ Object
- #filename ⇒ Object
- #find(where = nil, caller_lib = nil, paths = nil) ⇒ Object
- #find_all(caller_lib = nil, search_paths = nil) ⇒ Object
- #glob(pattern = '*') ⇒ Object
- #glob_all(pattern = nil, caller_lib = nil, search_paths = nil) ⇒ Object
- #identifier_file_path ⇒ Object
- #identifier_files ⇒ Object
-
#in_dir?(dir) ⇒ Boolean
{{{ Methods.
- #index(options = {}) ⇒ Object
- #join(name) ⇒ Object
- #keys(field = 0, sep = "\t") ⇒ Object
- #list ⇒ Object
- #located? ⇒ Boolean
- #method_missing(name, prev = nil, *args, &block) ⇒ Object
- #open(options = {}, &block) ⇒ Object
- #pipe_to(cmd, options = {}) ⇒ Object
- #pos_index(pos, options = {}) ⇒ Object
- #prepend_search_path(name, dir) ⇒ Object
- #produce(force = false) ⇒ Object
- #range_index(start, eend, options = {}) ⇒ Object
- #read(&block) ⇒ Object
- #replace_extension(new_extension = nil) ⇒ Object
- #set_extension(new_extension = nil) ⇒ Object
- #source_for_doc_file(relative_to = 'lib') ⇒ Object
- #sub(*args) ⇒ Object
- #to_s ⇒ Object
- #to_yaml(*args) ⇒ Object
- #traverse(options = {}, &block) ⇒ Object
- #tsv(*args) ⇒ Object
- #tsv_options(options = {}) ⇒ Object
- #write(*args, &block) ⇒ Object
- #yaml ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, prev = nil, *args, &block) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rbbt/resource/path.rb', line 89 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
#libdir ⇒ Object
Returns the value of attribute libdir.
6 7 8 |
# File 'lib/rbbt/resource/path.rb', line 6 def libdir @libdir end |
#original ⇒ Object
Returns the value of attribute original.
6 7 8 |
# File 'lib/rbbt/resource/path.rb', line 6 def original @original end |
#pkgdir ⇒ Object
Returns the value of attribute pkgdir.
6 7 8 |
# File 'lib/rbbt/resource/path.rb', line 6 def pkgdir @pkgdir end |
#resource ⇒ Object
Returns the value of attribute resource.
6 7 8 |
# File 'lib/rbbt/resource/path.rb', line 6 def resource @resource end |
#search_order ⇒ Object
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_paths ⇒ Object
Returns the value of attribute search_paths.
6 7 8 |
# File 'lib/rbbt/resource/path.rb', line 6 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 23 |
# 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? file = File.(file) return Path.setup(file) if File.exist? File.join(file, relative_to) while file != '/' dir = File.dirname file return Path.setup(dir) if File.exist? File.join(dir, relative_to) file = File.dirname file end return nil 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 string end |
Instance Method Details
#[](name, orig = false) ⇒ Object
80 81 82 83 |
# File 'lib/rbbt/resource/path.rb', line 80 def [](name, orig = false) return super(name) if orig join name end |
#_exists? ⇒ Boolean
256 257 258 |
# File 'lib/rbbt/resource/path.rb', line 256 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_fields ⇒ Object
352 353 354 355 356 |
# File 'lib/rbbt/resource/path.rb', line 352 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 |
#basename ⇒ Object
296 297 298 |
# File 'lib/rbbt/resource/path.rb', line 296 def basename Path.setup(File.basename(self), self.resource, self.pkgdir) end |
#byte(pos) ⇒ Object
85 86 87 |
# File 'lib/rbbt/resource/path.rb', line 85 def byte(pos) send(:[], pos, true) end |
#directory? ⇒ Boolean
65 66 67 68 |
# File 'lib/rbbt/resource/path.rb', line 65 def directory? return nil unless self.exists? File.directory? self.find end |
#dirname ⇒ Object
61 62 63 |
# File 'lib/rbbt/resource/path.rb', line 61 def dirname Path.setup File.dirname(self), @pkgdir, @resource end |
#doc_file(relative_to = 'lib') ⇒ Object
385 386 387 388 389 390 391 392 393 |
# File 'lib/rbbt/resource/path.rb', line 385 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
260 261 262 263 264 265 266 267 |
# File 'lib/rbbt/resource/path.rb', line 260 def exists? begin self.produce _exists? rescue Exception false end end |
#fields ⇒ Object
348 349 350 |
# File 'lib/rbbt/resource/path.rb', line 348 def fields TSV.parse_header(self.open).fields end |
#filename ⇒ Object
252 253 254 |
# File 'lib/rbbt/resource/path.rb', line 252 def filename self.find end |
#find(where = nil, caller_lib = nil, paths = nil) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 |
# File 'lib/rbbt/resource/path.rb', line 128 def find(where = nil, caller_lib = nil, paths = nil) @path ||= {} rsearch_paths = (resource and resource.respond_to?(:search_paths)) ? resource.search_paths : nil key_elems = [where, caller_lib, rsearch_paths, paths] key = Misc.obj2digest(key_elems.inspect) self.sub!('~/', Etc.getpwuid.dir + '/') if self.include? "~" return @path[key] if @path[key] if located? @path[key] = self return self end @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 raise "Did not recognize the 'where' tag: #{where}. Options: #{paths.keys}" unless paths.include? where if where == :lib libdir = @libdir || Path.caller_lib_dir(caller_lib) || "NOLIBDIR" else libdir = "NOLIBDIR" end pwd = FileUtils.pwd path = paths[where] 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('{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 res end @path[key] end |
#find_all(caller_lib = nil, search_paths = nil) ⇒ Object
225 226 227 228 229 230 231 232 |
# File 'lib/rbbt/resource/path.rb', line 225 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
70 71 72 73 74 75 76 77 78 |
# File 'lib/rbbt/resource/path.rb', line 70 def glob(pattern = '*') if self.include? "*" self.glob_all else return [] unless self.exists? exp = File.join(self.find, pattern) Dir.glob(exp).collect{|f| Path.setup(f, self.resource, self.pkgdir)} end end |
#glob_all(pattern = nil, caller_lib = nil, search_paths = nil) ⇒ Object
234 235 236 237 238 239 240 241 |
# File 'lib/rbbt/resource/path.rb', line 234 def glob_all(pattern = nil, caller_lib = nil, search_paths = nil) search_paths ||= @search_paths || SEARCH_PATHS search_paths = search_paths.dup search_paths.keys. collect{|where| pattern ? Dir.glob(File.join(find(where, Path.caller_lib_dir, search_paths), pattern)) : Dir.glob(find(where, Path.caller_lib_dir, search_paths)) }. compact.flatten.collect{|file| File.(file)}.uniq.collect{|path| Path.setup(path, self.resource, self.pkgdir)} end |
#identifier_file_path ⇒ Object
358 359 360 361 362 363 364 |
# File 'lib/rbbt/resource/path.rb', line 358 def identifier_file_path if self.dirname.identifiers.exists? self.dirname.identifiers else nil end end |
#identifier_files ⇒ Object
366 367 368 369 370 371 372 |
# File 'lib/rbbt/resource/path.rb', line 366 def identifier_files if identifier_file_path.nil? [] else [identifier_file_path] end end |
#in_dir?(dir) ⇒ Boolean
{{{ Methods
244 245 246 |
# File 'lib/rbbt/resource/path.rb', line 244 def in_dir?(dir) ! ! File.(self).match(/^#{Regexp.quote dir}/) end |
#index(options = {}) ⇒ Object
332 333 334 |
# File 'lib/rbbt/resource/path.rb', line 332 def index( = {}) TSV.index(self.produce, ) end |
#join(name) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/rbbt/resource/path.rb', line 52 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
318 319 320 |
# File 'lib/rbbt/resource/path.rb', line 318 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 |
#list ⇒ Object
314 315 316 |
# File 'lib/rbbt/resource/path.rb', line 314 def list Open.read(self.produce.find).split "\n" end |
#located? ⇒ Boolean
27 28 29 |
# File 'lib/rbbt/resource/util.rb', line 27 def located? self.byte(0) == SLASH or (self.byte(0) == DOT and self.byte(1) == SLASH) end |
#open(options = {}, &block) ⇒ Object
288 289 290 |
# File 'lib/rbbt/resource/path.rb', line 288 def open( = {}, &block) Open.open(self.produce.find, , &block) end |
#pipe_to(cmd, options = {}) ⇒ Object
328 329 330 |
# File 'lib/rbbt/resource/path.rb', line 328 def pipe_to(cmd, = {}) CMD.cmd(cmd, {:in => self.open, :pipe => true}.merge()) end |
#pos_index(pos, options = {}) ⇒ Object
340 341 342 |
# File 'lib/rbbt/resource/path.rb', line 340 def pos_index(pos, = {}) TSV.pos_index(self.produce, pos, ) 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
269 270 271 272 273 274 275 276 277 |
# File 'lib/rbbt/resource/path.rb', line 269 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 self end |
#range_index(start, eend, options = {}) ⇒ Object
336 337 338 |
# File 'lib/rbbt/resource/path.rb', line 336 def range_index(start, eend, = {}) TSV.range_index(self.produce, start, eend, ) end |
#read(&block) ⇒ Object
279 280 281 |
# File 'lib/rbbt/resource/path.rb', line 279 def read(&block) Open.read(self.produce.find, &block) end |
#replace_extension(new_extension = nil) ⇒ Object
379 380 381 382 383 |
# File 'lib/rbbt/resource/path.rb', line 379 def replace_extension(new_extension = nil) new_path = self.sub(/\.[^\.\/]{2,4}$/,'') new_path = new_path + "." + new_extension.to_s self.annotate(new_path) end |
#set_extension(new_extension = nil) ⇒ Object
374 375 376 377 |
# File 'lib/rbbt/resource/path.rb', line 374 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
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/rbbt/resource/path.rb', line 395 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_s ⇒ Object
248 249 250 |
# File 'lib/rbbt/resource/path.rb', line 248 def to_s self.find end |
#to_yaml(*args) ⇒ Object
344 345 346 |
# File 'lib/rbbt/resource/path.rb', line 344 def to_yaml(*args) self.to_s.to_yaml(*args) end |
#traverse(options = {}, &block) ⇒ Object
310 311 312 |
# File 'lib/rbbt/resource/path.rb', line 310 def traverse( = {}, &block) TSV::Parser.traverse(self.open, , &block) end |
#tsv(*args) ⇒ Object
300 301 302 |
# File 'lib/rbbt/resource/path.rb', line 300 def tsv(*args) TSV.open(self.produce, *args) end |
#tsv_options(options = {}) ⇒ Object
304 305 306 307 308 |
# File 'lib/rbbt/resource/path.rb', line 304 def ( = {}) self.open do |stream| TSV::Parser.new(stream, ). end end |
#write(*args, &block) ⇒ Object
283 284 285 |
# File 'lib/rbbt/resource/path.rb', line 283 def write(*args, &block) Open.write(self.produce.find, *args, &block) end |
#yaml ⇒ Object
322 323 324 325 326 |
# File 'lib/rbbt/resource/path.rb', line 322 def yaml self.open do |f| YAML.load f end end |