I solved this kind of problem years ago for Sage 9. This might help get you moving in the right direction at least?