UsingPyParse - robmcmullen/peppy GitHub Wiki
The problem of computing the correct indentation level of a given line of Python code does not have a trivial solution. There are many factors to consider, including whether the line is within a multi-line string or a continuation line.
Thanks to a tip from Tal Einat on the pyxides mailing list, he pointed me to the reindent code from the IDLE editor that is shipped with the python source code.
IDLE has been around for a long time and has been well tested and debugged for many years. If a standalone block of code can be found that 1) does what you want and 2) is well tested, it's almost always worth the time investment required to figure out how a piece of code can be integrated into your code. So, that's what I did.