[Python-projects] pylint: Rewriting a little bit of the ast ?

Monty Taylor mtaylor at mysql.com
Tue Feb 5 13:56:27 CET 2008


Hi!

I'm trying to write a plugin to handle the bzr.lazy_import function.
Specifically, it's a function that causes modules to be imported. In
use, it looks like this:

from bzrlib.lazy_import import lazy_import

lazy_import(globals(), """
import collapse
import parent
import hooks
""")


So obviously, since they aren't part of the tree, pylint isn't going to
know anything about collapse, parent or hooks.

I got as far as the following :

   def visit_callfunc(self, node):
        """called when a CallFunc node is encountered. See compiler.ast
        documentation for a description of available nodes:
        http://www.python.org/doc/current/lib/module-compiler.ast.html
        )
        """
        if self.already_done:
            return
        if isinstance(node.node, astng.Name):
            if node.node.name == "lazy_import":
                theImports = node.args[1].value

                newList=list()
                for f in theImports.split('\n'):

                    foo=compiler.parse(f,"exec")
                    newList.extend(foo.node.nodes)



                in_class = node.frame()
                theNodes =  in_class.node.nodes

                newList.extend(theNodes)
                in_class.node.nodes = newList
                self.already_done = True

                self.walk(in_class.node)

So I can detect that we're doing a lazy_import, and I can parse that
string out... but I can't figure out how to inject it back into the tree
that we're walking at the moment...

Anything I'm missing here? (other than the fact that I'm on crack...)

Monty


More information about the Python-Projects mailing list