Python code cleanup

This cleans up the python code. The first commit makes it pyflakes clean and does fix some bugs with undefined names.

The second commit makes it PEP8 clean for which I used autopep8 and did some very small number of manual fixes. I ignored "E501 line too long" for now because in a lot of cases automatically fixing those doesn't make the code more readable. I also think that a max line length of 80 is a bit too small, I would rather set this to 120 or 140.

The last commit changes the DOS line endings of one file to UNIX line endings.