Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Cache lines from files. |
| 2 | |
| 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 |
| 5 | that name. |
| 6 | """ |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | c341c62 | 1992-03-27 15:12:43 +0000 | [diff] [blame] | 8 | import sys |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 9 | import os |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 10 | |
Skip Montanaro | 17ab123 | 2001-01-24 06:27:27 +0000 | [diff] [blame] | 11 | __all__ = ["getline","clearcache","checkcache"] |
| 12 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 13 | def getline(filename, lineno): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 14 | lines = getlines(filename) |
| 15 | if 1 <= lineno <= len(lines): |
| 16 | return lines[lineno-1] |
| 17 | else: |
| 18 | return '' |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | # The cache |
| 22 | |
| 23 | cache = {} # The cache |
| 24 | |
| 25 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 26 | def clearcache(): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 27 | """Clear the cache entirely.""" |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 29 | global cache |
| 30 | cache = {} |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 32 | |
| 33 | def getlines(filename): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 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.""" |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 36 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 37 | if filename in cache: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 38 | return cache[filename][2] |
| 39 | else: |
| 40 | return updatecache(filename) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 42 | |
| 43 | def checkcache(): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 44 | """Discard cache entries that are out of date. |
| 45 | (This is not checked upon each call!)""" |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 47 | 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 Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 54 | if size != stat.st_size or mtime != stat.st_mtime: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 55 | del cache[filename] |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 57 | |
| 58 | def updatecache(filename): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 59 | """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 Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 63 | if filename in cache: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 64 | 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 Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 71 | # Try looking through the module search path. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 72 | basename = os.path.split(filename)[1] |
| 73 | for dirname in sys.path: |
Tim Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 74 | # When using imputil, sys.path may contain things other than |
| 75 | # strings; ignore them when it happens. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 76 | try: |
Tim Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 77 | fullname = os.path.join(dirname, basename) |
| 78 | except (TypeError, AttributeError): |
| 79 | # Not sufficiently string-like to do anything useful with. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 80 | pass |
Tim Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 81 | else: |
| 82 | try: |
| 83 | stat = os.stat(fullname) |
| 84 | break |
| 85 | except os.error: |
| 86 | pass |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 87 | else: |
| 88 | # No luck |
| 89 | ## print '*** Cannot stat', filename, ':', msg |
| 90 | return [] |
| 91 | try: |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 92 | fp = open(fullname, 'rU') |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 93 | lines = fp.readlines() |
| 94 | fp.close() |
| 95 | except IOError, msg: |
| 96 | ## print '*** Cannot open', fullname, ':', msg |
| 97 | return [] |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 98 | size, mtime = stat.st_size, stat.st_mtime |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 99 | cache[filename] = size, mtime, lines, fullname |
| 100 | return lines |