view scripts/ast_generator.py @ 794:ada9d53ea751

Merged AST branch back into trunk. Most of this code was written by Marcin Kurczych for his Google Summer of Code 2008 project. The merge of this branch means that Genshi now uses the native `_ast` module on Python >= 2.5, and an emulation thereof on Python 2.4. This replaces the usage of the `compiler` package, which was deprecated in Python 2.6 and removed in Python 3.0. Another effect is that Genshi now runs on Google AppEngine (although performance is bad due to the lack of template caching).
author cmlenz
date Tue, 16 Dec 2008 23:02:36 +0000
parents
children 4376010bb97e 09cc3627654c
line wrap: on
line source
#!/usr/bin/env python2.5

"""Script to that automatically generates genshi/templates/_astpy24.py.
Be sure to run this with a Python 2.5 interpreter.
"""

import _ast

done = set()

IGNORE_ATTRS = ('__module__', '__dict__', '__weakref__', '__setattr__',
                '__new__', '__getattribute__', '__reduce__', '__delattr__',
                '__init__')

def print_class(cls):
    bnames = []
    for base in cls.__bases__:
        if base.__module__ == '_ast':
            if base not in done:
                print_class(base)
            bnames.append(base.__name__)
        elif base.__module__ == '__builtin__':
            bnames.append("%s"%base.__name__)
        else:
            bnames.append("%s.%s"%(base.__module__,base.__name__))
    print "class %s(%s):"%(cls.__name__, ", ".join(bnames))
    written = False
    for attr in cls.__dict__:
        if attr not in IGNORE_ATTRS:
            written = True
            print "\t%s = %s"%(attr, repr(cls.__dict__[attr]),)
    if not written:
        print "\tpass"
    done.add(cls)

print "# Generated automatically, please do not edit"
print "# Generator can be found in Genshi SVN, scripts/ast-generator.py"
print
print "__version__ = %s" % _ast.__version__
print

for name in dir(_ast):
    cls = getattr(_ast, name)
    if cls.__class__ is type:
        print_class(cls)
        print
Copyright (C) 2012-2017 Edgewall Software