home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / linecache.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2004-02-22  |  3KB  |  111 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  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. __all__ = [
  13.     'getline',
  14.     'clearcache',
  15.     'checkcache']
  16.  
  17. def getline(filename, lineno):
  18.     lines = getlines(filename)
  19.     if lineno <= lineno:
  20.         pass
  21.     elif lineno <= len(lines):
  22.         return lines[lineno - 1]
  23.     else:
  24.         return ''
  25.  
  26. cache = { }
  27.  
  28. def clearcache():
  29.     '''Clear the cache entirely.'''
  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.     if filename in cache:
  38.         return cache[filename][2]
  39.     else:
  40.         return updatecache(filename)
  41.  
  42.  
  43. def checkcache():
  44.     '''Discard cache entries that are out of date.
  45.     (This is not checked upon each call!)'''
  46.     for filename in cache.keys():
  47.         (size, mtime, lines, fullname) = cache[filename]
  48.         
  49.         try:
  50.             stat = os.stat(fullname)
  51.         except os.error:
  52.             del cache[filename]
  53.             continue
  54.  
  55.         if size != stat.st_size or mtime != stat.st_mtime:
  56.             del cache[filename]
  57.             continue
  58.     
  59.  
  60.  
  61. def updatecache(filename):
  62.     """Update a cache entry and return its list of lines.
  63.     If something's wrong, print a message, discard the cache entry,
  64.     and return an empty list."""
  65.     if filename in cache:
  66.         del cache[filename]
  67.     
  68.     if not filename or filename[0] + filename[-1] == '<>':
  69.         return []
  70.     
  71.     fullname = filename
  72.     
  73.     try:
  74.         stat = os.stat(fullname)
  75.     except os.error:
  76.         msg = None
  77.         basename = os.path.split(filename)[1]
  78.         for dirname in sys.path:
  79.             
  80.             try:
  81.                 fullname = os.path.join(dirname, basename)
  82.             except (TypeError, AttributeError):
  83.                 continue
  84.  
  85.             
  86.             try:
  87.                 stat = os.stat(fullname)
  88.             continue
  89.             except os.error:
  90.                 continue
  91.             
  92.  
  93.         else:
  94.             return []
  95.     except:
  96.         None<EXCEPTION MATCH>os.error
  97.  
  98.     
  99.     try:
  100.         fp = open(fullname, 'rU')
  101.         lines = fp.readlines()
  102.         fp.close()
  103.     except IOError:
  104.         msg = None
  105.         return []
  106.  
  107.     (size, mtime) = (stat.st_size, stat.st_mtime)
  108.     cache[filename] = (size, mtime, lines, fullname)
  109.     return lines
  110.  
  111.