blob: 6056328e84560595013c0ebba8acc83032e63019 [file] [log] [blame]
Fred Draked6512801999-10-20 21:50:31 +00001"""Support functions for loading the reference count data file."""
2__version__ = '$Revision$'
3
4import os
5import string
6import sys
7
8
9# Determine the expected location of the reference count file:
10try:
11 p = os.path.dirname(__file__)
12except NameError:
13 p = sys.path[0]
14p = os.path.normpath(os.path.join(os.getcwd(), p, os.pardir,
15 "api", "refcounts.dat"))
16DEFAULT_PATH = p
17del p
18
19
20def load(path=DEFAULT_PATH):
21 return loadfile(open(path))
22
23
24def loadfile(fp):
25 d = {}
26 while 1:
27 line = fp.readline()
28 if not line:
29 break
30 line = string.strip(line)
31 if line[:1] in ("", "#"):
32 # blank lines and comments
33 continue
34 parts = string.split(line, ":", 4)
35 function, type, arg, refcount, comment = parts
36 if refcount:
37 refcount = int(refcount)
38 else:
39 refcount = None
40 #
41 # Get the entry, creating it if needed:
42 #
43 try:
44 entry = d[function]
45 except KeyError:
46 entry = d[function] = Entry(function)
47 #
48 # Update the entry with the new parameter or the result information.
49 #
50 if arg:
51 entry.args.append((arg, type, refcount))
52 else:
53 entry.result_type = type
54 entry.result_refs = refcount
55 return d
56
57
58class Entry:
59 def __init__(self, name):
60 self.name = name
61 self.args = []
62 self.result_type = ''
63 self.result_refs = None
64
65
66def dump(d):
67 """Dump the data in the 'canonical' format, with functions in
68 sorted order."""
69 items = d.items()
70 items.sort()
71 first = 1
72 for k, entry in items:
73 if first:
74 first = 0
75 else:
76 print
77 s = entry.name + ":%s:%s:%s:"
78 if entry.result_refs is None:
79 r = ""
80 else:
81 r = entry.result_refs
82 print s % (entry.result_type, "", r)
83 for t, n, r in entry.args:
84 if r is None:
85 r = ""
86 print s % (t, n, r)
87
88
89def main():
90 d = load()
91 dump(d)
92
93
94if __name__ == "__main__":
95 main()