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