blob: d7c761b41e22c3ce8b3304741cdfa4c2201e3049 [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)
Fred Drake404ac972001-05-29 15:25:51 +000035 if len(parts) != 5:
36 raise ValueError("Not enough fields in " + `line`)
Fred Draked6512801999-10-20 21:50:31 +000037 function, type, arg, refcount, comment = parts
Fred Drake5e872de2000-04-10 18:24:26 +000038 if refcount == "null":
39 refcount = None
40 elif refcount:
Fred Draked6512801999-10-20 21:50:31 +000041 refcount = int(refcount)
42 else:
43 refcount = None
44 #
45 # Get the entry, creating it if needed:
46 #
47 try:
48 entry = d[function]
49 except KeyError:
50 entry = d[function] = Entry(function)
51 #
52 # Update the entry with the new parameter or the result information.
53 #
54 if arg:
55 entry.args.append((arg, type, refcount))
56 else:
57 entry.result_type = type
58 entry.result_refs = refcount
59 return d
60
61
62class Entry:
63 def __init__(self, name):
64 self.name = name
65 self.args = []
66 self.result_type = ''
67 self.result_refs = None
68
69
70def dump(d):
71 """Dump the data in the 'canonical' format, with functions in
72 sorted order."""
73 items = d.items()
74 items.sort()
75 first = 1
76 for k, entry in items:
77 if first:
78 first = 0
79 else:
80 print
81 s = entry.name + ":%s:%s:%s:"
82 if entry.result_refs is None:
83 r = ""
84 else:
85 r = entry.result_refs
86 print s % (entry.result_type, "", r)
87 for t, n, r in entry.args:
88 if r is None:
89 r = ""
90 print s % (t, n, r)
91
92
93def main():
94 d = load()
95 dump(d)
96
97
98if __name__ == "__main__":
99 main()