blob: d82def7a7d84722017e7733f8f877f4c72180f7b [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
Fred Draked6512801999-10-20 21:50:31 +00005import sys
6
7
8# Determine the expected location of the reference count file:
9try:
10 p = os.path.dirname(__file__)
11except NameError:
Fred Drake071972e2002-10-16 15:30:17 +000012 p = os.path.dirname(sys.argv[0])
Fred Draked6512801999-10-20 21:50:31 +000013p = os.path.normpath(os.path.join(os.getcwd(), p, os.pardir,
14 "api", "refcounts.dat"))
15DEFAULT_PATH = p
16del p
17
18
19def load(path=DEFAULT_PATH):
20 return loadfile(open(path))
21
22
23def loadfile(fp):
24 d = {}
25 while 1:
26 line = fp.readline()
27 if not line:
28 break
Fred Drake071972e2002-10-16 15:30:17 +000029 line = line.strip()
Fred Draked6512801999-10-20 21:50:31 +000030 if line[:1] in ("", "#"):
31 # blank lines and comments
32 continue
Fred Drake071972e2002-10-16 15:30:17 +000033 parts = line.split(":", 4)
Fred Drake404ac972001-05-29 15:25:51 +000034 if len(parts) != 5:
35 raise ValueError("Not enough fields in " + `line`)
Fred Draked6512801999-10-20 21:50:31 +000036 function, type, arg, refcount, comment = parts
Fred Drake5e872de2000-04-10 18:24:26 +000037 if refcount == "null":
38 refcount = None
39 elif refcount:
Fred Draked6512801999-10-20 21:50:31 +000040 refcount = int(refcount)
41 else:
42 refcount = None
43 #
44 # Get the entry, creating it if needed:
45 #
46 try:
47 entry = d[function]
48 except KeyError:
49 entry = d[function] = Entry(function)
50 #
51 # Update the entry with the new parameter or the result information.
52 #
53 if arg:
54 entry.args.append((arg, type, refcount))
55 else:
56 entry.result_type = type
57 entry.result_refs = refcount
58 return d
59
60
61class Entry:
62 def __init__(self, name):
63 self.name = name
64 self.args = []
65 self.result_type = ''
66 self.result_refs = None
67
68
69def dump(d):
70 """Dump the data in the 'canonical' format, with functions in
71 sorted order."""
72 items = d.items()
73 items.sort()
74 first = 1
75 for k, entry in items:
76 if first:
77 first = 0
78 else:
79 print
80 s = entry.name + ":%s:%s:%s:"
81 if entry.result_refs is None:
82 r = ""
83 else:
84 r = entry.result_refs
85 print s % (entry.result_type, "", r)
86 for t, n, r in entry.args:
87 if r is None:
88 r = ""
89 print s % (t, n, r)
90
91
92def main():
93 d = load()
94 dump(d)
95
96
97if __name__ == "__main__":
98 main()