summaryrefslogtreecommitdiffstats
path: root/Demo/ibrowse/icache.py
blob: 0629bf9f6122a9df631338a1b679aae2e4ee6470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Cache management for info file processing.
# The function get_node() is the standard interface;
# its signature is the same as ifile.get_node() but it uses
# the cache and supports indirect tag tables.


import string
import ifile
from ifile import NoSuchNode, NoSuchFile
import itags


# Special hack to save the cache when using reload().
# This can just be "cache = {}" in a production version.
#
try:
	dummy = cache
	del dummy
except NameError:
	cache = {}


# Clear the entire cache.
#
def resetcache():
	for key in cache.keys():
		del cache[key]


# Clear the node info from the cache (the most voluminous data).
#
def resetnodecache():
	for key in cache.keys():
		tags, nodes = cache[key]
		cache[key] = tags, {}


# Get a node.
#
def get_node(curfile, ref):
	file, node = ifile.parse_ref(curfile, ref)
	file = string.lower(file)
	node = string.lower(node)
	if node == '*':
		# Don't cache whole file references;
		# reading the data is faster than displaying it anyway.
		return ifile.get_whole_file(file) # May raise NoSuchFile
	if not cache.has_key(file):
		cache[file] = get_tags(file), {} # May raise NoSuchFile
	tags, nodes = cache[file]
	if not nodes.has_key(node):
		if not tags.has_key(node):
			raise NoSuchNode, ref
		file1, offset, line = tags[node]
		if not file1:
			file1 = file
		file1, node1, header, menu, footnotes, text = \
			ifile.get_file_node(file1, offset, node)
		nodes[node] = file, node1, header, menu, footnotes, text
	return nodes[node]


# Get the tag table for a file.
# Either construct one or get the one found in the file.
# Raise NoSuchFile if the file isn't found.
#
def get_tags(file):
	f = ifile.try_open(file) # May raise NoSuchFile
	tags = itags.get_tags(f)
	if not tags:
		###print 'Scanning file...'
		f.seek(0)
		tags = ifile.make_tags(f)
	return tags