Updated for 2.1a3
[python/dscho.git] / Doc / tools / refcounts.py
blob90f47d061db4dd70071b58211db3fd3ffaa24481
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 function, type, arg, refcount, comment = parts
36 if refcount == "null":
37 refcount = None
38 elif refcount:
39 refcount = int(refcount)
40 else:
41 refcount = None
43 # Get the entry, creating it if needed:
45 try:
46 entry = d[function]
47 except KeyError:
48 entry = d[function] = Entry(function)
50 # Update the entry with the new parameter or the result information.
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
60 class Entry:
61 def __init__(self, name):
62 self.name = name
63 self.args = []
64 self.result_type = ''
65 self.result_refs = None
68 def 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)
91 def main():
92 d = load()
93 dump(d)
96 if __name__ == "__main__":
97 main()