blob: 0629bf9f6122a9df631338a1b679aae2e4ee6470 [file] [log] [blame]
Guido van Rossum9cf8f331992-03-30 10:54:51 +00001# 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
7import string
8import ifile
9from ifile import NoSuchNode, NoSuchFile
10import itags
11
12
13# Special hack to save the cache when using reload().
14# This can just be "cache = {}" in a production version.
15#
16try:
17 dummy = cache
18 del dummy
19except NameError:
20 cache = {}
21
22
23# Clear the entire cache.
24#
25def 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#
32def resetnodecache():
33 for key in cache.keys():
34 tags, nodes = cache[key]
35 cache[key] = tags, {}
36
37
38# Get a node.
39#
40def 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#
67def 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