Extract information from a Python code parse tree.
This module is based on the Demos/parser/example.py module
distributed with the Python source distribution.
Original comments
Simple code to extract class & function docstrings from a module.
This code is used as an example in the library reference manual in
the section on using the parser module. Refer to the manual for a
thorough discussion of the operation of this code.
Imported modules
|
|
import StructuredText
import os
import parser
import pprint
import re
import string
import symbol
import sys
import token
from types import ListType, TupleType
|
Functions
|
|
extractComments
getDocs
joinCodeSnippets
lenientMatch
match
parseTreeToString
|
|
extractComments
|
extractComments ( text, extractRe=re.compile( '^((?P<blankline>\s*$)|(?P<namedobj>\s*(?P<nametype>(class|def))\s+(?P<name>[0-9A-Za-z_]+))|(?P<commentline>\s*#+(?P<comment>.*)))' ) )
Given a block of Python source, extract the comments.
The comment text is associated with nearby named objects
(functions, methods, classes, etc.). This function returns
a dictionary of names and the associated comment text.
|
|
getDocs
|
getDocs ( fileName )
Retrieve information from the parse tree of a source file.
- fileName
Name of the file to read Python source code from.
|
|
joinCodeSnippets
|
joinCodeSnippets (
first,
second,
separator,
)
Join two code snippets into one string.
Use some general code content rules to try to make the
resulting snippet look nice.
|
|
lenientMatch
|
lenientMatch (
pattern,
data,
vars=None,
dbg=0,
)
Match `data' to `pattern', with variable extraction.
- pattern
Pattern to match against, possibly containing variables.
- data
Data to be checked and against which variables are extracted.
- vars
Dictionary of variables which have already been found. If not
provided, an empty dictionary is created.
The `pattern' value may contain variables of the form ['varname'] which
are allowed to match anything. The value that is matched is returned as
part of a dictionary which maps varname to the matched value. varname
is not required to be a string object, but using strings makes patterns
and the code which uses them more readable.
This function is based on the match() function, but is more lenient.
The pattern does not have to completely describe the tree. Instead,
it can be the top portion of the tree. Everything must match down
to the leaves of the pattern. At that point, the matching stops. If
a match was found at all, the return values indicate a match.
This function returns two values: a boolean indicating whether a match
was found and a dictionary mapping variable names to their associated
values.
|
|
match
|
match (
pattern,
data,
vars=None,
dbg=0,
)
Match `data' to `pattern', with variable extraction.
- pattern
Pattern to match against, possibly containing variables.
- data
Data to be checked and against which variables are extracted.
- vars
Dictionary of variables which have already been found. If not
provided, an empty dictionary is created.
The `pattern' value may contain variables of the form ['varname'] which
are allowed to match anything. The value that is matched is returned as
part of a dictionary which maps varname to the matched value. varname
is not required to be a string object, but using strings makes patterns
and the code which uses them more readable.
This function returns two values: a boolean indicating whether a match
was found and a dictionary mapping variable names to their associated
values.
|
|
parseTreeToString
|
parseTreeToString ( tree, separator=' ' )
Convert a parse tree to a string which would have parsed in that way.
Given a parse tree, walk it to determine the original string
which would have been parsed to produce that tree.
|
Classes
|
|
ClassInfo | Gather information about a Python class from its parse tree.
|
FunctionInfo | Gather information about a function or method definition.
|
ModuleInfo | Information gatherer for source code modules.
|
SuiteFuncInfo | Mixin class providing access to function names and info.
|
SuiteInfoBase | Base class for information gathering classes.
|
|
|