blob: 1b70c7c230546bf54b19cf70a369f73d9fde8b13 [file] [log] [blame]
Guido van Rossum54f22ed2000-02-04 15:10:34 +00001"""Cache lines from files.
2
3This is intended to read lines from modules imported -- hence if a filename
4is not found, it will look down the module search path for a file by
5that name.
6"""
Guido van Rossum921c8241992-01-10 14:54:42 +00007
Guido van Rossumc341c621992-03-27 15:12:43 +00008import sys
Guido van Rossum921c8241992-01-10 14:54:42 +00009import os
Guido van Rossum921c8241992-01-10 14:54:42 +000010
Jeremy Hylton97b2e842003-06-29 16:59:43 +000011__all__ = ["getline", "clearcache", "checkcache"]
Skip Montanaro17ab1232001-01-24 06:27:27 +000012
Guido van Rossum921c8241992-01-10 14:54:42 +000013def getline(filename, lineno):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000014 lines = getlines(filename)
15 if 1 <= lineno <= len(lines):
16 return lines[lineno-1]
17 else:
18 return ''
Guido van Rossum921c8241992-01-10 14:54:42 +000019
20
21# The cache
22
23cache = {} # The cache
24
25
Guido van Rossum921c8241992-01-10 14:54:42 +000026def clearcache():
Guido van Rossum54f22ed2000-02-04 15:10:34 +000027 """Clear the cache entirely."""
Guido van Rossum921c8241992-01-10 14:54:42 +000028
Guido van Rossum54f22ed2000-02-04 15:10:34 +000029 global cache
30 cache = {}
Guido van Rossum921c8241992-01-10 14:54:42 +000031
Guido van Rossum921c8241992-01-10 14:54:42 +000032
33def getlines(filename):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000034 """Get the lines for a file from the cache.
35 Update the cache if it doesn't contain an entry for this file already."""
Guido van Rossum921c8241992-01-10 14:54:42 +000036
Raymond Hettinger54f02222002-06-01 14:18:47 +000037 if filename in cache:
Guido van Rossum54f22ed2000-02-04 15:10:34 +000038 return cache[filename][2]
39 else:
40 return updatecache(filename)
Guido van Rossum921c8241992-01-10 14:54:42 +000041
Guido van Rossum921c8241992-01-10 14:54:42 +000042
43def checkcache():
Guido van Rossum54f22ed2000-02-04 15:10:34 +000044 """Discard cache entries that are out of date.
45 (This is not checked upon each call!)"""
Guido van Rossum921c8241992-01-10 14:54:42 +000046
Guido van Rossum54f22ed2000-02-04 15:10:34 +000047 for filename in cache.keys():
48 size, mtime, lines, fullname = cache[filename]
49 try:
50 stat = os.stat(fullname)
51 except os.error:
52 del cache[filename]
53 continue
Raymond Hettinger32200ae2002-06-01 19:51:15 +000054 if size != stat.st_size or mtime != stat.st_mtime:
Guido van Rossum54f22ed2000-02-04 15:10:34 +000055 del cache[filename]
Guido van Rossum921c8241992-01-10 14:54:42 +000056
Guido van Rossum921c8241992-01-10 14:54:42 +000057
58def updatecache(filename):
Guido van Rossum54f22ed2000-02-04 15:10:34 +000059 """Update a cache entry and return its list of lines.
60 If something's wrong, print a message, discard the cache entry,
61 and return an empty list."""
62
Raymond Hettinger54f02222002-06-01 14:18:47 +000063 if filename in cache:
Guido van Rossum54f22ed2000-02-04 15:10:34 +000064 del cache[filename]
65 if not filename or filename[0] + filename[-1] == '<>':
66 return []
67 fullname = filename
68 try:
69 stat = os.stat(fullname)
70 except os.error, msg:
Tim Peters12f21ae2001-05-29 04:27:01 +000071 # Try looking through the module search path.
Guido van Rossum54f22ed2000-02-04 15:10:34 +000072 basename = os.path.split(filename)[1]
73 for dirname in sys.path:
Tim Peters12f21ae2001-05-29 04:27:01 +000074 # When using imputil, sys.path may contain things other than
75 # strings; ignore them when it happens.
Guido van Rossum54f22ed2000-02-04 15:10:34 +000076 try:
Tim Peters12f21ae2001-05-29 04:27:01 +000077 fullname = os.path.join(dirname, basename)
78 except (TypeError, AttributeError):
79 # Not sufficiently string-like to do anything useful with.
Guido van Rossum54f22ed2000-02-04 15:10:34 +000080 pass
Tim Peters12f21ae2001-05-29 04:27:01 +000081 else:
82 try:
83 stat = os.stat(fullname)
84 break
85 except os.error:
86 pass
Guido van Rossum54f22ed2000-02-04 15:10:34 +000087 else:
88 # No luck
89## print '*** Cannot stat', filename, ':', msg
90 return []
91 try:
Jack Jansen7b8c7542002-04-14 20:12:41 +000092 fp = open(fullname, 'rU')
Guido van Rossum54f22ed2000-02-04 15:10:34 +000093 lines = fp.readlines()
94 fp.close()
95 except IOError, msg:
96## print '*** Cannot open', fullname, ':', msg
97 return []
Raymond Hettinger32200ae2002-06-01 19:51:15 +000098 size, mtime = stat.st_size, stat.st_mtime
Guido van Rossum54f22ed2000-02-04 15:10:34 +000099 cache[filename] = size, mtime, lines, fullname
100 return lines