WIP python port, for use in Bikeshed

[Bikeshed](https://github.com/tabatkins/bikeshed).

**Not ready to merge yet.** Opening a PR in case someone wants to pick this up, as I will not be working on this for a while.

This is still buggy, and based on an older version of the JS implementation: fe2ff118ff5244a855ba31904271aebf5ca73896