"""Cache lines from files.
This is intended to read lines from modules imported -- hence if a filename
is not found, it will look down the module search path for a file by
__all__
= ["getline", "clearcache", "checkcache"]
def getline(filename
, lineno
):
lines
= getlines(filename
)
if 1 <= lineno
<= len(lines
):
"""Clear the cache entirely."""
"""Get the lines for a file from the cache.
Update the cache if it doesn't contain an entry for this file already."""
return cache
[filename
][2]
return updatecache(filename
)
def checkcache(filename
=None):
"""Discard cache entries that are out of date.
(This is not checked upon each call!)"""
for filename
in filenames
:
size
, mtime
, lines
, fullname
= cache
[filename
]
if size
!= stat
.st_size
or mtime
!= stat
.st_mtime
:
def updatecache(filename
):
"""Update a cache entry and return its list of lines.
If something's wrong, print a message, discard the cache entry,
and return an empty list."""
if not filename
or filename
[0] + filename
[-1] == '<>':
# Try looking through the module search path.
basename
= os
.path
.split(filename
)[1]
# When using imputil, sys.path may contain things other than
# strings; ignore them when it happens.
fullname
= os
.path
.join(dirname
, basename
)
except (TypeError, AttributeError):
# Not sufficiently string-like to do anything useful with.
## print '*** Cannot stat', filename, ':', msg
fp
= open(fullname
, 'rU')
## print '*** Cannot open', fullname, ':', msg
size
, mtime
= stat
.st_size
, stat
.st_mtime
cache
[filename
] = size
, mtime
, lines
, fullname