1 """Cache lines from files.
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
12 __all__ = [
"getline",
"clearcache",
"checkcache"]
16 if 1 <= lineno <= len(lines):
17 return lines[lineno-1]
28 """Clear the cache entirely."""
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."""
38 if cache.has_key(filename):
39 return cache[filename][2]
45 """Discard cache entries that are out of date.
46 (This is not checked upon each call!)"""
48 for filename
in cache.keys():
49 size, mtime, lines, fullname = cache[filename]
51 stat = os.stat(fullname)
55 if size != stat[ST_SIZE]
or mtime != stat[ST_MTIME]:
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."""
64 if cache.has_key(filename):
66 if not filename
or filename[0] + filename[-1] ==
'<>':
70 stat = os.stat(fullname)
73 basename = os.path.split(filename)[1]
74 for dirname
in sys.path:
78 fullname = os.path.join(dirname, basename)
79 except (TypeError, AttributeError):
84 stat = os.stat(fullname)
94 lines = fp.readlines()
99 size, mtime = stat[ST_SIZE], stat[ST_MTIME]
100 cache[filename] = size, mtime, lines, fullname