Looking to do AST inspection/transform on all imports.
It's easy enough to arbitrarily transform the AST of, say, a user script before exec()ing it, but I'd like to intercept Python code as it's imported and transform it before it's executed. I know, vaguely, about the existence of "import hooks", but thought I'd ask here whether that's the right tree to bark up before investing any serious time in the effort.
Just to be clear, my idea is that the user writes
from foo import Foo
in their script, and I am given the text of foo.py to do what I wish to (which would be to compile it, and transform the AST). Am I in the ballpark?