py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Doc / tools / refcounts.py
blobd7c761b41e22c3ce8b3304741cdfa4c2201e3049
1 """Support functions for loading the reference count data file."""
2 __version__ = '$Revision$'
4 import os
5 import string
6 import sys
9 # Determine the expected location of the reference count file:
10 try:
11 p = os.path.dirname(__file__)
12 except NameError:
13 p = sys.path[0]
14 p = os.path.normpath(os.path.join(os.getcwd(), p, os.pardir,
15 "api", "refcounts.dat"))
16 DEFAULT_PATH = p
17 del p
20 def load(path=DEFAULT_PATH):
21 return loadfile(open(path))
24 def 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 if len(parts) != 5:
36 raise ValueError("Not enough fields in " + `line`)
37 function, type, arg, refcount, comment = parts
38 if refcount == "null":
39 refcount = None
40 elif refcount:
41 refcount = int(refcount)
42 else:
43 refcount = None
45 # Get the entry, creating it if needed:
47 try:
48 entry = d[function]
49 except KeyError:
50 entry = d[function] = Entry(function)
52 # Update the entry with the new parameter or the result information.
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
62 class Entry:
63 def __init__(self, name):
64 self.name = name
65 self.args = []
66 self.result_type = ''
67 self.result_refs = None
70 def 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)
93 def main():
94 d = load()
95 dump(d)
98 if __name__ == "__main__":
99 main()