eventparse.py :  » Mobile » Python-for-PalmOS » Python-1.5.2+reduced-1.0 » Tools » idle » 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 » Tools » idle » eventparse.py
#! /usr/bin/env python

"""Parse event definitions out of comments in source files."""

import re
import sys
import os
import string
import getopt
import glob
import fileinput
import pprint

def main():
    hits = []
    sublist = []
    args = sys.argv[1:]
    if not args:
        args = filter(lambda s: 'A' <= s[0] <= 'Z', glob.glob("*.py"))
        if not args:
            print "No arguments, no [A-Z]*.py files."
            return 1
    for line in fileinput.input(args):
        if line[:2] == '#$':
            if not sublist:
                sublist.append('file %s' % fileinput.filename())
                sublist.append('line %d' % fileinput.lineno())
            sublist.append(string.strip(line[2:-1]))
        else:
            if sublist:
                hits.append(sublist)
                sublist = []
    if sublist:
        hits.append(sublist)
        sublist = []
    dd = {}
    for sublist in hits:
        d = {}
        for line in sublist:
            words = string.split(line, None, 1)
            if len(words) != 2:
                continue
            tag = words[0]
            l = d.get(tag, [])
            l.append(words[1])
            d[tag] = l
        if d.has_key('event'):
            keys = d['event']
            if len(keys) != 1:
                print "Multiple event keys in", d
                print 'File "%s", line %d' % (d['file'], d['line'])
            key = keys[0]
            if dd.has_key(key):
                print "Duplicate event in", d
                print 'File "%s", line %d' % (d['file'], d['line'])
                return
            dd[key] = d
        else:
            print "No event key in", d
            print 'File "%s", line %d' % (d['file'], d['line'])
    winevents = getevents(dd, "win")
    unixevents = getevents(dd, "unix")
    save = sys.stdout
    f = open("keydefs.py", "w")
    try:
        sys.stdout = f
        print "windows_keydefs = \\"
        pprint.pprint(winevents)
        print
        print "unix_keydefs = \\"
        pprint.pprint(unixevents)
    finally:
        sys.stdout = save
    f.close()

def getevents(dd, key):
    res = {}
    events = dd.keys()
    events.sort()
    for e in events:
        d = dd[e]
        if d.has_key(key) or d.has_key("all"):
            list = []
            for x in d.get(key, []) + d.get("all", []):
                list.append(x)
                if key == "unix" and x[:5] == "<Alt-":
                    x = "<Meta-" + x[5:]
                    list.append(x)
            res[e] = list
    return res

if __name__ == '__main__':
    sys.exit(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.