Source

astdump /

Filename Size Date modified Message
270 B
2.0 KB
5.4 KB

Get information from Python module without executing it.

When used standalone can generate setup.py information from the module. Can become handy for distutils. Uses AST (abstract syntax tree) magic.

$ ./astdump.py
Usage: astdump.py [options] <filename.py>

Read top level variables from the module without importing it. Additional keys
allow to generate setup.py automatically or view tree structure of an Abstract
Syntax Tree.

Options:
  -h, --help  show this help message and exit
  --dump      dump abstract syntax tree
  --generate  generate setup.py for a given filename

Examples:

$ ./astdump.py astdump.py
__author__ = 'anatoly techtonik <techtonik@gmail.com>'
__description__ = 'Extract information from Python module without importing it.'
__license__ = 'Public Domain'
__version__ = '1.0'
$ ./astdump.py astdump.py --generate
#!/usr/bin/env python
from distutils.core import setup

setup(
  name = 'astdump',
  version = '0.5',
  author = 'anatoly techtonik',
  author_email = 'techtonik@gmail.com',
  description = 'Extract information from Python module without importing it.',
  license = 'Public Domain',

  py_modules=['astdump'],
)

API

top_level_vars(filename)
Return name/value pairs for top level variables for the script specified as filename. Only string and int values are supported.
>>> import astdump
>>> astdump.top_level_vars("astdump.py")
{'__version__': '1.0', '__description__': 'Extract information from Python module without importing it.', '__license__': 'Public Domain', '__author__': 'anatoly techtonik <techtonik@gmail.com>'}

Other interesting tools

  • pss - filetype aware grep for sources, with colors, public domain
  • redhawk - AST aware grep, BSD license

Changes

1.0
  • API change: get_top_vars(node) is replaced with top_level_vars(filename)