home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / linecache.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-22  |  3.2 KB  |  108 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Cache lines from files.
  5.  
  6. This is intended to read lines from modules imported -- hence if a filename
  7. is not found, it will look down the module search path for a file by
  8. that name.
  9. '''
  10. import sys
  11. import os
  12. from stat import *
  13. __all__ = [
  14.     'getline',
  15.     'clearcache',
  16.     'checkcache']
  17.  
  18. def getline(filename, lineno):
  19.     lines = getlines(filename)
  20.     if lineno <= lineno:
  21.         pass
  22.     elif lineno <= len(lines):
  23.         return lines[lineno - 1]
  24.     else:
  25.         return ''
  26.  
  27. cache = { }
  28.  
  29. def clearcache():
  30.     '''Clear the cache entirely.'''
  31.     global cache
  32.     cache = { }
  33.  
  34.  
  35. def getlines(filename):
  36.     """Get the lines for a file from the cache.
  37.     Update the cache if it doesn't contain an entry for this file already."""
  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.     for filename in cache.keys():
  48.         (size, mtime, lines, fullname) = cache[filename]
  49.         
  50.         try:
  51.             stat = os.stat(fullname)
  52.         except os.error:
  53.             del cache[filename]
  54.             continue
  55.  
  56.         if size != stat[ST_SIZE] or mtime != stat[ST_MTIME]:
  57.             del cache[filename]
  58.         
  59.     
  60.  
  61.  
  62. def updatecache(filename):
  63.     """Update a cache entry and return its list of lines.
  64.     If something's wrong, print a message, discard the cache entry,
  65.     and return an empty list."""
  66.     if cache.has_key(filename):
  67.         del cache[filename]
  68.     
  69.     if not filename or filename[0] + filename[-1] == '<>':
  70.         return []
  71.     
  72.     fullname = filename
  73.     
  74.     try:
  75.         stat = os.stat(fullname)
  76.     except os.error:
  77.         msg = None
  78.         basename = os.path.split(filename)[1]
  79.         for dirname in sys.path:
  80.             
  81.             try:
  82.                 fullname = os.path.join(dirname, basename)
  83.             except (TypeError, AttributeError):
  84.                 pass
  85.  
  86.             
  87.             try:
  88.                 stat = os.stat(fullname)
  89.             except os.error:
  90.                 pass
  91.  
  92.         else:
  93.             return []
  94.  
  95.     
  96.     try:
  97.         fp = open(fullname, 'r')
  98.         lines = fp.readlines()
  99.         fp.close()
  100.     except IOError:
  101.         msg = None
  102.         return []
  103.  
  104.     (size, mtime) = (stat[ST_SIZE], stat[ST_MTIME])
  105.     cache[filename] = (size, mtime, lines, fullname)
  106.     return lines
  107.  
  108.