Guido van Rossum | 9cf8f33 | 1992-03-30 10:54:51 +0000 | [diff] [blame] | 1 | # Cache management for info file processing. |
| 2 | # The function get_node() is the standard interface; |
| 3 | # its signature is the same as ifile.get_node() but it uses |
| 4 | # the cache and supports indirect tag tables. |
| 5 | |
| 6 | |
| 7 | import string |
| 8 | import ifile |
| 9 | from ifile import NoSuchNode, NoSuchFile |
| 10 | import itags |
| 11 | |
| 12 | |
| 13 | # Special hack to save the cache when using reload(). |
| 14 | # This can just be "cache = {}" in a production version. |
| 15 | # |
| 16 | try: |
| 17 | dummy = cache |
| 18 | del dummy |
| 19 | except NameError: |
| 20 | cache = {} |
| 21 | |
| 22 | |
| 23 | # Clear the entire cache. |
| 24 | # |
| 25 | def resetcache(): |
| 26 | for key in cache.keys(): |
| 27 | del cache[key] |
| 28 | |
| 29 | |
| 30 | # Clear the node info from the cache (the most voluminous data). |
| 31 | # |
| 32 | def resetnodecache(): |
| 33 | for key in cache.keys(): |
| 34 | tags, nodes = cache[key] |
| 35 | cache[key] = tags, {} |
| 36 | |
| 37 | |
| 38 | # Get a node. |
| 39 | # |
| 40 | def get_node(curfile, ref): |
| 41 | file, node = ifile.parse_ref(curfile, ref) |
| 42 | file = string.lower(file) |
| 43 | node = string.lower(node) |
| 44 | if node == '*': |
| 45 | # Don't cache whole file references; |
| 46 | # reading the data is faster than displaying it anyway. |
| 47 | return ifile.get_whole_file(file) # May raise NoSuchFile |
| 48 | if not cache.has_key(file): |
| 49 | cache[file] = get_tags(file), {} # May raise NoSuchFile |
| 50 | tags, nodes = cache[file] |
| 51 | if not nodes.has_key(node): |
| 52 | if not tags.has_key(node): |
| 53 | raise NoSuchNode, ref |
| 54 | file1, offset, line = tags[node] |
| 55 | if not file1: |
| 56 | file1 = file |
| 57 | file1, node1, header, menu, footnotes, text = \ |
| 58 | ifile.get_file_node(file1, offset, node) |
| 59 | nodes[node] = file, node1, header, menu, footnotes, text |
| 60 | return nodes[node] |
| 61 | |
| 62 | |
| 63 | # Get the tag table for a file. |
| 64 | # Either construct one or get the one found in the file. |
| 65 | # Raise NoSuchFile if the file isn't found. |
| 66 | # |
| 67 | def get_tags(file): |
| 68 | f = ifile.try_open(file) # May raise NoSuchFile |
| 69 | tags = itags.get_tags(f) |
| 70 | if not tags: |
| 71 | ###print 'Scanning file...' |
| 72 | f.seek(0) |
| 73 | tags = ifile.make_tags(f) |
| 74 | return tags |