MRG: refactor API generation for sharing

I used the API generation stuff in nibabel, and did some changes.  I tried to
make them general enough so it would be easier to share the code between
projects, with fewer modifications.