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, module_globals=None):
14 lines = getlines(filename, module_globals)
15 if 1 <= lineno <= len(lines):
16 return lines[lineno-1]
23 cache = {} # The cache
27 """Clear the cache entirely."""
33 def getlines(filename, module_globals=None):
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, module_globals)
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 continue # no-op for files loaded via a __loader__
60 stat = os.stat(fullname)
64 if size != stat.st_size or mtime != stat.st_mtime:
68 def updatecache(filename, module_globals=None):
69 """Update a cache entry and return its list of lines.
70 If something's wrong, print a message, discard the cache entry,
71 and return an empty list."""
75 if not filename or filename[0] + filename[-1] == '<>':
80 stat = os.stat(fullname)
82 basename = os.path.split(filename)[1]
84 # Try for a __loader__, if available
85 if module_globals and '__loader__' in module_globals:
86 name = module_globals.get('__name__')
87 loader = module_globals['__loader__']
88 get_source = getattr(loader, 'get_source', None)
90 if name and get_source:
91 if basename.startswith(name.split('.')[-1]+'.'):
93 data = get_source(name)
94 except (ImportError, IOError):
98 # No luck, the PEP302 loader cannot find the source
103 [line+'\n' for line in data.splitlines()], fullname
105 return cache[filename][2]
107 # Try looking through the module search path.
109 for dirname in sys.path:
110 # When using imputil, sys.path may contain things other than
111 # strings; ignore them when it happens.
113 fullname = os.path.join(dirname, basename)
114 except (TypeError, AttributeError):
115 # Not sufficiently string-like to do anything useful with.
119 stat = os.stat(fullname)
125 ## print '*** Cannot stat', filename, ':', msg
128 fp = open(fullname, 'rU')
129 lines = fp.readlines()
132 ## print '*** Cannot open', fullname, ':', msg
134 size, mtime = stat.st_size, stat.st_mtime
135 cache[filename] = size, mtime, lines, fullname