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 |
| 10 | from stat import * |
| 11 | |
Skip Montanaro | 17ab123 | 2001-01-24 06:27:27 +0000 | [diff] [blame] | 12 | __all__ = ["getline","clearcache","checkcache"] |
| 13 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 14 | def getline(filename, lineno): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 15 | lines = getlines(filename) |
| 16 | if 1 <= lineno <= len(lines): |
| 17 | return lines[lineno-1] |
| 18 | else: |
| 19 | return '' |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | # The cache |
| 23 | |
| 24 | cache = {} # The cache |
| 25 | |
| 26 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 27 | def clearcache(): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 28 | """Clear the cache entirely.""" |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 30 | global cache |
| 31 | cache = {} |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 33 | |
| 34 | def getlines(filename): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 35 | """Get the lines for a file from the cache. |
| 36 | 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] | 37 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 38 | if cache.has_key(filename): |
| 39 | return cache[filename][2] |
| 40 | else: |
| 41 | return updatecache(filename) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 43 | |
| 44 | def checkcache(): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 45 | """Discard cache entries that are out of date. |
| 46 | (This is not checked upon each call!)""" |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 48 | for filename in cache.keys(): |
| 49 | size, mtime, lines, fullname = cache[filename] |
| 50 | try: |
| 51 | stat = os.stat(fullname) |
| 52 | except os.error: |
| 53 | del cache[filename] |
| 54 | continue |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 55 | if size != stat[ST_SIZE] or mtime != stat[ST_MTIME]: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 56 | del cache[filename] |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 58 | |
| 59 | def updatecache(filename): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 60 | """Update a cache entry and return its list of lines. |
| 61 | If something's wrong, print a message, discard the cache entry, |
| 62 | and return an empty list.""" |
| 63 | |
| 64 | if cache.has_key(filename): |
| 65 | del cache[filename] |
| 66 | if not filename or filename[0] + filename[-1] == '<>': |
| 67 | return [] |
| 68 | fullname = filename |
| 69 | try: |
| 70 | stat = os.stat(fullname) |
| 71 | except os.error, msg: |
Tim Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 72 | # Try looking through the module search path. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 73 | basename = os.path.split(filename)[1] |
| 74 | for dirname in sys.path: |
Tim Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 75 | # When using imputil, sys.path may contain things other than |
| 76 | # strings; ignore them when it happens. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 77 | try: |
Tim Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 78 | fullname = os.path.join(dirname, basename) |
| 79 | except (TypeError, AttributeError): |
| 80 | # Not sufficiently string-like to do anything useful with. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 81 | pass |
Tim Peters | 12f21ae | 2001-05-29 04:27:01 +0000 | [diff] [blame] | 82 | else: |
| 83 | try: |
| 84 | stat = os.stat(fullname) |
| 85 | break |
| 86 | except os.error: |
| 87 | pass |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 88 | else: |
| 89 | # No luck |
| 90 | ## print '*** Cannot stat', filename, ':', msg |
| 91 | return [] |
| 92 | try: |
| 93 | fp = open(fullname, 'r') |
| 94 | lines = fp.readlines() |
| 95 | fp.close() |
| 96 | except IOError, msg: |
| 97 | ## print '*** Cannot open', fullname, ':', msg |
| 98 | return [] |
| 99 | size, mtime = stat[ST_SIZE], stat[ST_MTIME] |
| 100 | cache[filename] = size, mtime, lines, fullname |
| 101 | return lines |