Module: LineCache
- Defined in:
- lib/linecache.rb
Overview
module LineCache
A module to read and cache lines of a Ruby program.
Defined Under Namespace
Classes: LineCacheInfo
Constant Summary collapse
- VERSION =
'0.44'
- @@file_cache =
The file cache. The key is a name as would be given by Ruby for __FILE__. The value is a LineCacheInfo object.
{}
- @@script_cache =
{}
- @@file2file_remap =
Maps a string filename (a String) to a key in @@file_cache (a String).
One important use of @@file2file_remap is mapping the a full path of a file into the name stored in @@file_cache or given by Ruby’s __FILE__. Applications such as those that get input from users, may want canonicalize a file name before looking it up. This map gives a way to do that.
Another related use is when a template system is used. Here we’ll probably want to remap not only the file name but also line ranges. Will probably use this for that, but I’m not sure.
{}
- @@file2file_remap_lines =
{}
- @@script2file =
{}
Class Method Summary collapse
-
.cache(file_or_script, reload_on_change = false) ⇒ Object
Cache file name or script object if it’s not already cached.
-
.cache_file(filename, reload_on_change = false) ⇒ Object
Cache filename if it’s not already cached.
-
.cache_script(script, string = nil, sha1 = nil) ⇒ Object
Cache script if it’s not already cached.
-
.cached?(file_or_script) ⇒ Boolean
Return true if file_or_script is cached.
-
.cached_files ⇒ Object
Return an array of cached file names.
- .cached_script?(script) ⇒ Boolean
-
.checkcache(filename = nil, use_script_lines = false) ⇒ Object
Discard cache entries that are out of date.
-
.clear_file_cache ⇒ Object
Clear the file cache entirely.
- .empty?(filename) ⇒ Boolean
-
.getline(file_or_script, line_number, reload_on_change = true) ⇒ Object
Get line
line_number
from file namedfilename
. -
.getlines(filename, reload_on_change = false) ⇒ Object
Read lines of
filename
and cache the results. - .map_file(file) ⇒ Object
- .map_file_line(file, line) ⇒ Object
- .map_script(script) ⇒ Object
-
.path(filename) ⇒ Object
Return full filename path for filename.
- .remap_file(to_file, from_file) ⇒ Object
- .remap_file_lines(from_file, to_file, range, start) ⇒ Object
- .remove_script_temps ⇒ Object
-
.script_getlines(script) ⇒ Object
Read lines of
script
and cache the results. - .script_is_eval?(script) ⇒ Boolean
-
.sha1(filename) ⇒ Object
Return SHA1 of filename.
-
.size(file_or_script) ⇒ Object
Return the number of lines in filename.
-
.stat(filename) ⇒ Object
Return File.stat in the cache for filename.
-
.trace_line_numbers(filename, reload_on_change = false) ⇒ Object
Return an Array of breakpoints in filename.
-
.update_cache(filename, use_script_lines = false) ⇒ Object
Update a cache entry.
-
.update_script_cache(script, string = nil, sha1 = nil) ⇒ Object
Update a cache entry.
Instance Method Summary collapse
-
#clear_script_cache ⇒ Object
Clear the script cache entirely.
Class Method Details
.cache(file_or_script, reload_on_change = false) ⇒ Object
Cache file name or script object if it’s not already cached. Return the expanded filename for it in the cache if a filename, or the script, or nil if we can’t find the file.
172 173 174 175 176 177 178 |
# File 'lib/linecache.rb', line 172 def cache(file_or_script, reload_on_change=false) if file_or_script.kind_of?(String) cache_file(file_or_script, reload_on_change) else cache_script(file_or_script) end end |
.cache_file(filename, reload_on_change = false) ⇒ Object
Cache filename if it’s not already cached. Return the expanded filename for it in the cache or nil if we can’t find the file.
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/linecache.rb', line 184 def cache_file(filename, reload_on_change=false) if @@file_cache.member?(filename) checkcache(filename) if reload_on_change else update_cache(filename, true) end if @@file_cache.member?(filename) @@file_cache[filename].path else nil end end |
.cache_script(script, string = nil, sha1 = nil) ⇒ Object
Cache script if it’s not already cached.
161 162 163 164 165 166 |
# File 'lib/linecache.rb', line 161 def cache_script(script, string=nil, sha1=nil) if !@@script_cache.member?(script) update_script_cache(script, string, sha1) end script end |
.cached?(file_or_script) ⇒ Boolean
Return true if file_or_script is cached
199 200 201 202 203 204 205 |
# File 'lib/linecache.rb', line 199 def cached?(file_or_script) if file_or_script.kind_of?(String) @@file_cache.member?(map_file(file_or_script)) else cached_script?(file_or_script) end end |
.cached_files ⇒ Object
Return an array of cached file names
116 117 118 |
# File 'lib/linecache.rb', line 116 def cached_files() @@file_cache.keys end |
.cached_script?(script) ⇒ Boolean
208 209 210 |
# File 'lib/linecache.rb', line 208 def cached_script?(script) @@script_cache.member?(script) end |
.checkcache(filename = nil, use_script_lines = false) ⇒ Object
Discard cache entries that are out of date. If filename
is nil
all entries in the file cache @@file_cache are checked. If we don’t have stat information about a file, which can happen if the file was read from __SCRIPT_LINES but no corresponding file is found, it will be kept. Return a list of invalidated filenames. nil is returned if a filename was given but not found cached.
127 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 |
# File 'lib/linecache.rb', line 127 def checkcache(filename=nil, use_script_lines=false) if !filename filenames = @@file_cache.keys() elsif @@file_cache.member?(filename) filenames = [filename] else return nil end result = [] for filename in filenames next unless @@file_cache.member?(filename) path = @@file_cache[filename].path if File.exist?(path) cache_info = @@file_cache[filename].stat stat = File.stat(path) if cache_info if stat && (cache_info.size != stat.size or cache_info.mtime != stat.mtime) result << filename update_cache(filename, use_script_lines) end else result << filename update_cache(filename, use_script_lines) end end end return result end |
.clear_file_cache ⇒ Object
Clear the file cache entirely.
102 103 104 105 106 |
# File 'lib/linecache.rb', line 102 def clear_file_cache() @@file_cache = {} @@file2file_remap = {} @@file2file_remap_lines = {} end |
.empty?(filename) ⇒ Boolean
213 214 215 216 |
# File 'lib/linecache.rb', line 213 def empty?(filename) filename=map_file(filename) @@file_cache[filename].lines.empty? end |
.getline(file_or_script, line_number, reload_on_change = true) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/linecache.rb', line 230 def getline(file_or_script, line_number, reload_on_change=true) lines = if file_or_script.kind_of?(String) filename = map_file(file_or_script) filename, line_number = map_file_line(filename, line_number) getlines(filename, reload_on_change) else script_getlines(file_or_script) end if lines and (1..lines.size) === line_number return lines[line_number-1] else return nil end end |
.getlines(filename, reload_on_change = false) ⇒ Object
Read lines of filename
and cache the results. However filename
was previously cached use the results from the cache. Return nil if we can’t get lines
267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/linecache.rb', line 267 def getlines(filename, reload_on_change=false) filename = map_file(filename) checkcache(filename) if reload_on_change if @@file_cache.member?(filename) return @@file_cache[filename].lines else update_cache(filename, true) if @@file_cache.member?(filename) return @@file_cache[filename].lines else return nil end end end |
.map_file(file) ⇒ Object
363 364 365 |
# File 'lib/linecache.rb', line 363 def map_file(file) @@file2file_remap[file] ? @@file2file_remap[file] : file end |
.map_file_line(file, line) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/linecache.rb', line 387 def map_file_line(file, line) if @@file2file_remap_lines[file] @@file2file_remap_lines[file].each do |from_file, range, start| if range === line from_file = from_file || file return [from_file, start+line-range.begin] end end end return [map_file(file), line] end |
.map_script(script) ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/linecache.rb', line 368 def map_script(script) if @@script2file[script] @@script2file[script] else # Doc says there's new takes an optional string parameter # But it doesn't work for me sha1 = Digest::SHA1.new string = script.eval_source sha1 << script.eval_source tempfile = Tempfile.new(["eval-#{sha1.hexdigest[0...7]}-", '.rb']) tempfile.open.puts(string) tempfile.close # cache_script(script, string, sha1.hexdigest) @@script2file[script] = tempfile.path tempfile.path end end |
.path(filename) ⇒ Object
Return full filename path for filename
284 285 286 287 288 289 |
# File 'lib/linecache.rb', line 284 def path(filename) return unless filename.kind_of?(String) filename = map_file(filename) return nil unless @@file_cache.member?(filename) @@file_cache[filename].path end |
.remap_file(to_file, from_file) ⇒ Object
292 293 294 |
# File 'lib/linecache.rb', line 292 def remap_file(to_file, from_file) @@file2file_remap[to_file] = from_file end |
.remap_file_lines(from_file, to_file, range, start) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/linecache.rb', line 297 def remap_file_lines(from_file, to_file, range, start) range = (range..range) if range.kind_of?(Fixnum) to_file = from_file unless to_file if @@file2file_remap_lines[to_file] # FIXME: need to check for overwriting ranges: whether # they intersect or one encompasses another. @@file2file_remap_lines[to_file] << [from_file, range, start] else @@file2file_remap_lines[to_file] = [[from_file, range, start]] end end |
.remove_script_temps ⇒ Object
92 93 94 95 96 |
# File 'lib/linecache.rb', line 92 def remove_script_temps @@script2file.values.each do |filename| File.unlink(filename) end end |
.script_getlines(script) ⇒ Object
Read lines of script
and cache the results. However script
was previously cached use the results from the cache. Return nil if we can’t get lines
250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/linecache.rb', line 250 def script_getlines(script) if @@script_cache.member?(script) return @@script_cache[script].lines else update_script_cache(script) if @@script_cache.member?(script) return @@script_cache[script].lines else return nil end end end |
.script_is_eval?(script) ⇒ Boolean
400 401 402 |
# File 'lib/linecache.rb', line 400 def script_is_eval?(script) !!script.eval_source end |
.sha1(filename) ⇒ Object
Return SHA1 of filename.
311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/linecache.rb', line 311 def sha1(filename) filename = map_file(filename) return nil unless @@file_cache.member?(filename) return @@file_cache[filename].sha1.hexdigest if @@file_cache[filename].sha1 sha1 = Digest::SHA1.new @@file_cache[filename].lines.each do |line| sha1 << line + "\n" end @@file_cache[filename].sha1 = sha1 sha1.hexdigest end |
.size(file_or_script) ⇒ Object
Return the number of lines in filename
326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/linecache.rb', line 326 def size(file_or_script) cache(file_or_script) if file_or_script.kind_of?(String) file_or_script = map_file(file_or_script) return nil unless @@file_cache.member?(file_or_script) @@file_cache[file_or_script].lines.length else return nil unless @@script_cache.member?(file_or_script) @@script_cache[file_or_script].lines.length end end |
.stat(filename) ⇒ Object
Return File.stat in the cache for filename.
340 341 342 343 |
# File 'lib/linecache.rb', line 340 def stat(filename) return nil unless @@file_cache.member?(filename) @@file_cache[filename].stat end |
.trace_line_numbers(filename, reload_on_change = false) ⇒ Object
Return an Array of breakpoints in filename. The list will contain an entry for each distinct line event call so it is possible (and possibly useful) for a line number appear more than once.
350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/linecache.rb', line 350 def trace_line_numbers(filename, reload_on_change=false) fullname = cache(filename, reload_on_change) return nil unless fullname e = @@file_cache[filename] unless e.line_numbers e.line_numbers = TraceLineNumbers.lnums_for_str_array(e.lines) e.line_numbers = false unless e.line_numbers end e.line_numbers end |
.update_cache(filename, use_script_lines = false) ⇒ Object
Update a cache entry. If something’s wrong, return nil. Return true if the cache was updated and false if not. If use_script_lines is true, use that as the source for the lines of the file
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/linecache.rb', line 420 def update_cache(filename, use_script_lines=false) return nil unless filename @@file_cache.delete(filename) path = File.(filename) if File.exist?(path) stat = File.stat(path) elsif File.basename(filename) == filename # try looking through the search path. stat = nil for dirname in $: path = File.join(dirname, filename) if File.exist?(path) stat = File.stat(path) break end end return false unless stat end begin fp = File.open(path, 'r') lines = fp.readlines() fp.close() rescue ## print '*** cannot open', path, ':', msg return nil end @@file_cache[filename] = LineCacheInfo.new(File.stat(path), nil, lines, path, nil) @@file2file_remap[path] = filename return true end |
.update_script_cache(script, string = nil, sha1 = nil) ⇒ Object
Update a cache entry. If something is wrong, return nil. Return true if the cache was updated and false if not.
407 408 409 410 411 412 413 |
# File 'lib/linecache.rb', line 407 def update_script_cache(script, string=nil, sha1=nil) return false unless script_is_eval?(script) string = script.eval_source unless string @@script_cache[script] = LineCacheInfo.new(nil, nil, string.split(/\n/), nil, sha1) return true end |
Instance Method Details
#clear_script_cache ⇒ Object
Clear the script cache entirely.
110 111 112 |
# File 'lib/linecache.rb', line 110 def clear_script_cache() @@script_cache = {} end |