Port kernel-gen to Python

Make kernel-gen maintainable, easier to debug, easier to read, and more
error-proof.