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
11 __all__
= ["getline", "clearcache", "checkcache"]
13 def getline(filename
, lineno
):
14 lines
= getlines(filename
)
15 if 1 <= lineno
<= len(lines
):
16 return lines
[lineno
-1]
23 cache
= {} # The cache
27 """Clear the cache entirely."""
33 def getlines(filename
):
34 """Get the lines for a file from the cache.
35 Update the cache if it doesn't contain an entry for this file already."""
38 return cache
[filename
][2]
40 return updatecache(filename
)
43 def checkcache(filename
=None):
44 """Discard cache entries that are out of date.
45 (This is not checked upon each call!)"""
48 filenames
= cache
.keys()
51 filenames
= [filename
]
55 for filename
in filenames
:
56 size
, mtime
, lines
, fullname
= cache
[filename
]
58 stat
= os
.stat(fullname
)
62 if size
!= stat
.st_size
or mtime
!= stat
.st_mtime
:
66 def updatecache(filename
):
67 """Update a cache entry and return its list of lines.
68 If something's wrong, print a message, discard the cache entry,
69 and return an empty list."""
73 if not filename
or filename
[0] + filename
[-1] == '<>':
77 stat
= os
.stat(fullname
)
79 # Try looking through the module search path.
80 basename
= os
.path
.split(filename
)[1]
81 for dirname
in sys
.path
:
82 # When using imputil, sys.path may contain things other than
83 # strings; ignore them when it happens.
85 fullname
= os
.path
.join(dirname
, basename
)
86 except (TypeError, AttributeError):
87 # Not sufficiently string-like to do anything useful with.
91 stat
= os
.stat(fullname
)
97 ## print '*** Cannot stat', filename, ':', msg
100 fp
= open(fullname
, 'rU')
101 lines
= fp
.readlines()
104 ## print '*** Cannot open', fullname, ':', msg
106 size
, mtime
= stat
.st_size
, stat
.st_mtime
107 cache
[filename
] = size
, mtime
, lines
, fullname