| 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 | """
|
|---|
| 7 |
|
|---|
| 8 | import sys
|
|---|
| 9 | import os
|
|---|
| 10 |
|
|---|
| 11 | __all__ = ["getline", "clearcache", "checkcache"]
|
|---|
| 12 |
|
|---|
| 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]
|
|---|
| 17 | else:
|
|---|
| 18 | return ''
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | # The cache
|
|---|
| 22 |
|
|---|
| 23 | cache = {} # The cache
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | def clearcache():
|
|---|
| 27 | """Clear the cache entirely."""
|
|---|
| 28 |
|
|---|
| 29 | global cache
|
|---|
| 30 | cache = {}
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 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."""
|
|---|
| 36 |
|
|---|
| 37 | if filename in cache:
|
|---|
|
|---|