home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_Lib_linecache.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  2.4 KB  |  95 lines

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