refcounts.py :  » Mobile » Python-for-PalmOS » Python-1.5.2+reduced-1.0 » Doc » tools » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Mobile » Python for PalmOS 
Python for PalmOS » Python 1.5.2 reduced 1.0 » Doc » tools » refcounts.py
"""Support functions for loading the reference count data file."""
__version__ = '$Revision: 1.1.1.1 $'

import os
import string
import sys


# Determine the expected location of the reference count file:
try:
    p = os.path.dirname(__file__)
except NameError:
    p = sys.path[0]
p = os.path.normpath(os.path.join(os.getcwd(), p, os.pardir,
                                  "api", "refcounts.dat"))
DEFAULT_PATH = p
del p


def load(path=DEFAULT_PATH):
    return loadfile(open(path))


def loadfile(fp):
    d = {}
    while 1:
        line = fp.readline()
        if not line:
            break
        line = string.strip(line)
        if line[:1] in ("", "#"):
            # blank lines and comments
            continue
        parts = string.split(line, ":", 4)
        function, type, arg, refcount, comment = parts
        if refcount:
            refcount = int(refcount)
        else:
            refcount = None
        #
        # Get the entry, creating it if needed:
        #
        try:
            entry = d[function]
        except KeyError:
            entry = d[function] = Entry(function)
        #
        # Update the entry with the new parameter or the result information.
        #
        if arg:
            entry.args.append((arg, type, refcount))
        else:
            entry.result_type = type
            entry.result_refs = refcount
    return d


class Entry:
    def __init__(self, name):
        self.name = name
        self.args = []
        self.result_type = ''
        self.result_refs = None


def dump(d):
    """Dump the data in the 'canonical' format, with functions in
    sorted order."""
    items = d.items()
    items.sort()
    first = 1
    for k, entry in items:
        if first:
            first = 0
        else:
            print
        s = entry.name + ":%s:%s:%s:"
        if entry.result_refs is None:
            r = ""
        else:
            r = entry.result_refs
        print s % (entry.result_type, "", r)
        for t, n, r in entry.args:
            if r is None:
                r = ""
            print s % (t, n, r)


def main():
    d = load()
    dump(d)


if __name__ == "__main__":
    main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.