bitbake has lib/codegen.py to generate Python source from an AST object. However, as of Python 3.9 there is ast.unparse() which looks like it does the same thing: https://docs.python.org/3/library/ast.html#ast.unparse.
https://git.openembedded.org/bitbake/commit/?id=76dd6d69c59c1686be2dfb3fc5b72c2a9df34871