Parsing and Editing Source

Benjamin musiccomposition at gmail.com
Sat Aug 16 04:51:34 CEST 2008


On Aug 15, 9:21 am, "Paul Wilson" <paulalexwil... at gmail.com> wrote:
> Hi all,
>
> I'd like to be able to do the following to a python source file
> programmatically:
>  * Read in a source file
>  * Add/Remove/Edit Classes, methods, functions
>  * Add/Remove/Edit Decorators
>  * List the Classes
>  * List the imported modules
>  * List the functions
>  * List methods of classes
>
> And then save out the result back to the original file (or elsewhere).
>
> I've begun by using the tokenize module to generate a token-tuple list
> and am building datastructures around it that enable the above
> methods. I'm find that I'm getting a little caught up in the details
> and thought I'd step back and ask if there's a more elegant way to
> approach this, or if anyone knows a library that could assist.
>
> So far, I've got code that generates a line number to token-tuple list
> dictionary, and am working on a datastructure describing where the
> classes begin and end, indexed by their name, such that they can be
> later modified.


Look at the 2to3 tool which is good at this sort of thing. It lets you
define custom "fixers" that work on a fairly high-level representation
of the parse tree and then write the source back exactly unchanged.
>
> Any thoughts?
> Thanks,
> Paul




More information about the Python-list mailing list