Fred Drake | d651280 | 1999-10-20 21:50:31 +0000 | [diff] [blame] | 1 | """Support functions for loading the reference count data file.""" |
| 2 | __version__ = '$Revision$' |
| 3 | |
| 4 | import os |
Fred Drake | d651280 | 1999-10-20 21:50:31 +0000 | [diff] [blame] | 5 | import sys |
| 6 | |
| 7 | |
| 8 | # Determine the expected location of the reference count file: |
| 9 | try: |
| 10 | p = os.path.dirname(__file__) |
| 11 | except NameError: |
Fred Drake | 071972e | 2002-10-16 15:30:17 +0000 | [diff] [blame] | 12 | p = os.path.dirname(sys.argv[0]) |
Fred Drake | d651280 | 1999-10-20 21:50:31 +0000 | [diff] [blame] | 13 | p = os.path.normpath(os.path.join(os.getcwd(), p, os.pardir, |
| 14 | "api", "refcounts.dat")) |
| 15 | DEFAULT_PATH = p |
| 16 | del p |
| 17 | |
| 18 | |
| 19 | def load(path=DEFAULT_PATH): |
| 20 | return loadfile(open(path)) |
| 21 | |
| 22 | |
| 23 | def loadfile(fp): |
| 24 | d = {} |
| 25 | while 1: |
| 26 | line = fp.readline() |
| 27 | if not line: |
| 28 | break |
Fred Drake | 071972e | 2002-10-16 15:30:17 +0000 | [diff] [blame] | 29 | line = line.strip() |
Fred Drake | d651280 | 1999-10-20 21:50:31 +0000 | [diff] [blame] | 30 | if line[:1] in ("", "#"): |
| 31 | # blank lines and comments |
| 32 | continue |
Fred Drake | 071972e | 2002-10-16 15:30:17 +0000 | [diff] [blame] | 33 | parts = line.split(":", 4) |
Fred Drake | 404ac97 | 2001-05-29 15:25:51 +0000 | [diff] [blame] | 34 | if len(parts) != 5: |
| 35 | raise ValueError("Not enough fields in " + `line`) |
Fred Drake | d651280 | 1999-10-20 21:50:31 +0000 | [diff] [blame] | 36 | function, type, arg, refcount, comment = parts |
Fred Drake | 5e872de | 2000-04-10 18:24:26 +0000 | [diff] [blame] | 37 | if refcount == "null": |
| 38 | refcount = None |
| 39 | elif refcount: |
Fred Drake | d651280 | 1999-10-20 21:50:31 +0000 | [diff] [blame] | 40 | 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 | |
| 61 | class Entry: |
| 62 | def __init__(self, name): |
| 63 | self.name = name |
| 64 | self.args = [] |
| 65 | self.result_type = '' |
| 66 | self.result_refs = None |
| 67 | |
| 68 | |
| 69 | def 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 | |
| 92 | def main(): |
| 93 | d = load() |
| 94 | dump(d) |
| 95 | |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | main() |