home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Lib / linecache.py < prev    next >
Text File  |  1993-12-29  |  2KB  |  91 lines

  1. # Cache lines from files.
  2. # This is intended to read lines from modules imported -- hence if a filename
  3. # is not found, it will look down the module search path for a file by
  4. # that name.
  5.  
  6. import sys
  7. import os
  8. from stat import *
  9.  
  10. def getline(filename, lineno):
  11.     lines = getlines(filename)
  12.     if 1 <= lineno <= len(lines):
  13.         return lines[lineno-1]
  14.     else:
  15.         return ''
  16.  
  17.  
  18. # The cache
  19.  
  20. cache = {} # The cache
  21.  
  22.  
  23. # Clear the cache entirely
  24.  
  25. def clearcache():
  26.     global cache
  27.     cache = {}
  28.  
  29.  
  30. # Get the lines for a file from the cache.
  31. # Update the cache if it doesn't contain an entry for this file already.
  32.  
  33. def getlines(filename):
  34.     if cache.has_key(filename):
  35.         return cache[filename][2]
  36.     else:
  37.         return updatecache(filename)
  38.  
  39.  
  40. # Discard cache entries that are out of date.
  41. # (This is not checked upon each call!)
  42.  
  43. def checkcache():
  44.     for filename in cache.keys():
  45.         size, mtime, lines, fullname = cache[filename]
  46.         try:
  47.             stat = os.stat(fullname)
  48.         except os.error:
  49.             del cache[filename]
  50.             continue
  51.         if size <> stat[ST_SIZE] or mtime <> stat[ST_MTIME]:
  52.             del cache[filename]
  53.  
  54.  
  55. # Update a cache entry and return its list of lines.
  56. # If something's wrong, print a message, discard the cache entry,
  57. # and return an empty list.
  58.  
  59. def updatecache(filename):
  60.     if cache.has_key(filename):
  61.         del cache[filename]
  62.     if filename[0] + filename[-1] == '<>':
  63.         return []
  64.     fullname = filename
  65.     try:
  66.         stat = os.stat(fullname)
  67.     except os.error, msg:
  68.         # Try looking through the module search path
  69.         basename = os.path.split(filename)[1]
  70.         for dirname in sys.path:
  71.             fullname = os.path.join(dirname, basename)
  72.             try:
  73.                 stat = os.stat(fullname)
  74.                 break
  75.             except os.error:
  76.                 pass
  77.         else:
  78.             # No luck
  79.             print '*** Cannot stat', filename, ':', msg
  80.             return []
  81.     try:
  82.         fp = open(fullname, 'r')
  83.         lines = fp.readlines()
  84.         fp.close()
  85.     except IOError, msg:
  86.         print '*** Cannot open', fullname, ':', msg
  87.         return []
  88.     size, mtime = stat[ST_SIZE], stat[ST_MTIME]
  89.     cache[filename] = size, mtime, lines, fullname
  90.     return lines
  91.