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 |
| 5 | import string |
| 6 | import sys |
| 7 | |
| 8 | |
| 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 |
| 18 | |
| 19 | |
| 20 | def load(path=DEFAULT_PATH): |
| 21 | return loadfile(open(path)) |
| 22 | |
| 23 | |
| 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 |
Fred Drake | 5e872de | 2000-04-10 18:24:26 +0000 | [diff] [blame] | 36 | if refcount == "null": |
| 37 | refcount = None |
| 38 | elif refcount: |
Fred Drake | d651280 | 1999-10-20 21:50:31 +0000 | [diff] [blame] | 39 | 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 | |
| 60 | class Entry: |
| 61 | def __init__(self, name): |
| 62 | self.name = name |
| 63 | self.args = [] |
| 64 | self.result_type = '' |
| 65 | self.result_refs = None |
| 66 | |
| 67 | |
| 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) |
| 89 | |
| 90 | |
| 91 | def main(): |
| 92 | d = load() |
| 93 | dump(d) |
| 94 | |
| 95 | |
| 96 | if __name__ == "__main__": |
| 97 | main() |