Parsing and Editing Source
PaulAlexWilson at gmail.com
Sat Aug 16 13:41:36 CEST 2008
On Aug 16, 3:51 am, Benjamin <musiccomposit... at gmail.com> wrote:
> 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
Thanks for the hint. I've looked at lib2to3 and there might be some
useful stuff in there!
More information about the Python-list