Ditched '_find_SET()', since it was a no-value-added wrapper around
[python/dscho.git] / Demo / stdwin / ibrowse / icache.py
blob0629bf9f6122a9df631338a1b679aae2e4ee6470
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.
7 import string
8 import ifile
9 from ifile import NoSuchNode, NoSuchFile
10 import itags
13 # Special hack to save the cache when using reload().
14 # This can just be "cache = {}" in a production version.
16 try:
17 dummy = cache
18 del dummy
19 except NameError:
20 cache = {}
23 # Clear the entire cache.
25 def resetcache():
26 for key in cache.keys():
27 del cache[key]
30 # Clear the node info from the cache (the most voluminous data).
32 def resetnodecache():
33 for key in cache.keys():
34 tags, nodes = cache[key]
35 cache[key] = tags, {}
38 # Get a node.
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]
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.
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